Skip to content

Commit

Permalink
Fix handling of byte strings in some os.path functions
Browse files Browse the repository at this point in the history
- includes exists(), isfile(), islink(), isdir() and ismount()
- fixes pytest-dev#595
- comment docker tests (handled in another issue)
  • Loading branch information
mrbean-bremen committed May 13, 2021
1 parent 05925ab commit 98a30ae
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
27 changes: 14 additions & 13 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ jobs:
fi
shell: bash

dockertests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
docker-image: [centos, debian, fedora, ubuntu]
steps:
- uses: actions/checkout@v2
- name: Setup docker container
run: |
docker build -t pyfakefs -f $GITHUB_WORKSPACE/.github/workflows/dockerfiles/Dockerfile_${{ matrix.docker-image }} . --build-arg github_repo=$GITHUB_REPOSITORY --build-arg github_branch=$(basename $GITHUB_REF)
- name: Run tests
run: docker run -t pyfakefs
# setting up up docker containers currently fails
# dockertests:
# runs-on: ubuntu-latest
# strategy:
# fail-fast: false
# matrix:
# docker-image: [centos, debian, fedora, ubuntu]
# steps:
# - uses: actions/checkout@v2
# - name: Setup docker container
# run: |
# docker build -t pyfakefs -f $GITHUB_WORKSPACE/.github/workflows/dockerfiles/Dockerfile_${{ matrix.docker-image }} . --build-arg github_repo=$GITHUB_REPOSITORY --build-arg github_branch=$(basename $GITHUB_REF)
# - name: Run tests
# run: docker run -t pyfakefs
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The released versions correspond to PyPi releases.
functionality as `pathlib` and is no longer tested separately;
the previous behavior broke newer `pathlib` features if `pathlib2`
was installed (see [#592](../../issues/592))

### Fixes
* correctly handle byte paths in `os.path.exists`
(see [#595](../../issues/595))

## [Version 4.4.0](https://pypi.python.org/pypi/pyfakefs/4.4.0) (2021-02-24)
Adds better support for Python 3.8 / 3.9.
Expand Down
6 changes: 3 additions & 3 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ def exists(self, file_path, check_link=False):
"""
if check_link and self.islink(file_path):
return True
file_path = make_string_path(file_path)
file_path = to_string(make_string_path(file_path))
if file_path is None:
raise TypeError
if not file_path:
Expand Down Expand Up @@ -2928,7 +2928,7 @@ def _is_of_type(self, path, st_flag, follow_symlinks=True,
Raises:
TypeError: if path is None
"""
path = make_string_path(path)
path = to_string(make_string_path(path))
if path is None:
raise TypeError
try:
Expand Down Expand Up @@ -3495,7 +3495,7 @@ def ismount(self, path):
Under Windows also returns True for drive and UNC roots
(independent of their existence).
"""
path = make_string_path(path)
path = to_string(make_string_path(path))
if not path:
return False
normed_path = self.filesystem.absnormpath(path)
Expand Down
44 changes: 32 additions & 12 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,8 @@ def tearDown(self):
def check_abspath(self, is_windows):
# the implementation differs in Windows and Posix, so test both
self.filesystem.is_windows_fs = is_windows
filename = u'foo'
abspath = u'!%s' % filename
filename = 'foo'
abspath = '!%s' % filename
self.filesystem.create_file(abspath)
self.assertEqual(abspath, self.path.abspath(abspath))
self.assertEqual(abspath, self.path.abspath(filename))
Expand Down Expand Up @@ -914,10 +914,14 @@ def test_abs_path_with_drive_component(self):
def test_isabs_with_drive_component(self):
self.filesystem.is_windows_fs = False
self.assertFalse(self.path.isabs('C:!foo'))
self.assertFalse(self.path.isabs(b'C:!foo'))
self.assertTrue(self.path.isabs('!'))
self.assertTrue(self.path.isabs(b'!'))
self.filesystem.is_windows_fs = True
self.assertTrue(self.path.isabs('C:!foo'))
self.assertTrue(self.path.isabs(b'C:!foo'))
self.assertTrue(self.path.isabs('!'))
self.assertTrue(self.path.isabs(b'!'))

def test_relpath(self):
path_foo = '!path!to!foo'
Expand Down Expand Up @@ -957,46 +961,53 @@ def test_samefile(self):
self.assertFalse(self.path.samefile(file_path1, file_path2))
self.assertTrue(
self.path.samefile(file_path1, '!foo!..!foo!bar!..!bar!baz'))
self.assertTrue(
self.path.samefile(file_path1, b'!foo!..!foo!bar!..!bar!baz'))

def test_exists(self):
file_path = 'foo!bar!baz'
file_path_bytes = b'foo!bar!baz'
self.filesystem.create_file(file_path)
self.assertTrue(self.path.exists(file_path))
self.assertTrue(self.path.exists(file_path_bytes))
self.assertFalse(self.path.exists('!some!other!bogus!path'))

def test_lexists(self):
file_path = 'foo!bar!baz'
file_path_bytes = b'foo!bar!baz'
self.filesystem.create_dir('foo!bar')
self.filesystem.create_symlink(file_path, 'bogus')
self.assertTrue(self.path.lexists(file_path))
self.assertTrue(self.path.lexists(file_path_bytes))
self.assertFalse(self.path.exists(file_path))
self.assertFalse(self.path.exists(file_path_bytes))
self.filesystem.create_file('foo!bar!bogus')
self.assertTrue(self.path.exists(file_path))

def test_dirname_with_drive(self):
self.filesystem.is_windows_fs = True
self.assertEqual(u'c:!foo',
self.path.dirname(u'c:!foo!bar'))
self.assertEqual('c:!foo',
self.path.dirname('c:!foo!bar'))
self.assertEqual(b'c:!',
self.path.dirname(b'c:!foo'))
self.assertEqual(u'!foo',
self.path.dirname(u'!foo!bar'))
self.assertEqual('!foo',
self.path.dirname('!foo!bar'))
self.assertEqual(b'!',
self.path.dirname(b'!foo'))
self.assertEqual(u'c:foo',
self.path.dirname(u'c:foo!bar'))
self.assertEqual('c:foo',
self.path.dirname('c:foo!bar'))
self.assertEqual(b'c:',
self.path.dirname(b'c:foo'))
self.assertEqual(u'foo',
self.path.dirname(u'foo!bar'))
self.assertEqual('foo',
self.path.dirname('foo!bar'))

def test_dirname(self):
dirname = 'foo!bar'
self.assertEqual(dirname, self.path.dirname('%s!baz' % dirname))

def test_join_strings(self):
components = [u'foo', u'bar', u'baz']
self.assertEqual(u'foo!bar!baz', self.path.join(*components))
components = ['foo', 'bar', 'baz']
self.assertEqual('foo!bar!baz', self.path.join(*components))

def test_join_bytes(self):
components = [b'foo', b'bar', b'baz']
Expand Down Expand Up @@ -1031,8 +1042,10 @@ def test_getsize_file_empty(self):

def test_getsize_file_non_zero_size(self):
file_path = 'foo!bar!baz'
file_path_bytes = b'foo!bar!baz'
self.filesystem.create_file(file_path, contents='1234567')
self.assertEqual(7, self.path.getsize(file_path))
self.assertEqual(7, self.path.getsize(file_path_bytes))

def test_getsize_dir_empty(self):
# For directories, only require that the size is non-negative.
Expand All @@ -1053,6 +1066,7 @@ def test_getsize_dir_non_zero_size(self):
def test_isdir(self):
self.filesystem.create_file('foo!bar')
self.assertTrue(self.path.isdir('foo'))
self.assertTrue(self.path.isdir(b'foo'))
self.assertFalse(self.path.isdir('foo!bar'))
self.assertFalse(self.path.isdir('it_dont_exist'))

Expand All @@ -1071,6 +1085,7 @@ def test_isfile(self):
self.filesystem.create_file('foo!bar')
self.assertFalse(self.path.isfile('foo'))
self.assertTrue(self.path.isfile('foo!bar'))
self.assertTrue(self.path.isfile(b'foo!bar'))
self.assertFalse(self.path.isfile('it_dont_exist'))

def test_get_mtime(self):
Expand All @@ -1079,6 +1094,7 @@ def test_get_mtime(self):
self.assertEqual(10, test_file.st_mtime)
test_file.st_mtime = 24
self.assertEqual(24, self.path.getmtime('foo!bar1.txt'))
self.assertEqual(24, self.path.getmtime(b'foo!bar1.txt'))

def test_get_mtime_raises_os_error(self):
self.assertFalse(self.path.exists('it_dont_exist'))
Expand All @@ -1095,6 +1111,8 @@ def test_islink(self):
# comments in Python/Lib/posixpath.py.
self.assertTrue(self.path.islink('foo!link_to_file'))
self.assertTrue(self.path.isfile('foo!link_to_file'))
self.assertTrue(self.path.islink(b'foo!link_to_file'))
self.assertTrue(self.path.isfile(b'foo!link_to_file'))

self.assertTrue(self.path.isfile('foo!regular_file'))
self.assertFalse(self.path.islink('foo!regular_file'))
Expand All @@ -1111,9 +1129,11 @@ def test_is_link_case_sensitive(self):
def test_ismount(self):
self.assertFalse(self.path.ismount(''))
self.assertTrue(self.path.ismount('!'))
self.assertTrue(self.path.ismount(b'!'))
self.assertFalse(self.path.ismount('!mount!'))
self.filesystem.add_mount_point('!mount')
self.assertTrue(self.path.ismount('!mount'))
self.assertTrue(self.path.ismount(b'!mount'))
self.assertTrue(self.path.ismount('!mount!'))

def test_ismount_with_drive_letters(self):
Expand Down

0 comments on commit 98a30ae

Please sign in to comment.