Skip to content

Commit 3008d05

Browse files
committed
Fix some bytes warnings
- make bytes warning an error in test run - see #651
1 parent d828745 commit 3008d05

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
- name: Run unit tests without extra packages as non-root user
8484
run: |
8585
export TEST_REAL_FS=1
86-
python -m pyfakefs.tests.all_tests_without_extra_packages
86+
python -bb -m pyfakefs.tests.all_tests_without_extra_packages
8787
shell: bash
8888
- name: Run setup.py test (uses pytest)
8989
run: |

pyfakefs/fake_filesystem.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,9 @@ def _auto_mount_drive_if_needed(self, path: AnyStr,
11181118

11191119
def _mount_point_for_path(self, path: AnyStr) -> Dict:
11201120
path = self.absnormpath(self._original_path(path))
1121-
if path in self.mount_points:
1122-
return self.mount_points[path]
1121+
for mount_path in self.mount_points:
1122+
if path == matching_string(path, mount_path):
1123+
return self.mount_points[mount_path]
11231124
mount_path = matching_string(path, '')
11241125
drive = self.splitdrive(path)[0]
11251126
for root_path in self.mount_points:
@@ -2048,9 +2049,9 @@ def get_object_from_normpath(self,
20482049
OSError: if the object is not found.
20492050
"""
20502051
path = make_string_path(file_path)
2051-
if path == self.root.name:
2052+
if path == matching_string(path, self.root.name):
20522053
return self.root
2053-
if path == self.dev_null.name:
2054+
if path == matching_string(path, self.dev_null.name):
20542055
return self.dev_null
20552056

20562057
path = self._original_path(path)
@@ -2143,7 +2144,7 @@ def lresolve(self, path: AnyPath) -> FakeFile:
21432144
path_str = make_string_path(path)
21442145
if not path_str:
21452146
raise OSError(errno.ENOENT, path_str)
2146-
if path_str == self.root.name:
2147+
if path_str == matching_string(path_str, self.root.name):
21472148
# The root directory will never be a link
21482149
return self.root
21492150

@@ -3201,7 +3202,7 @@ def rmdir(self, target_directory: AnyStr,
32013202
OSError: if removal failed per FakeFilesystem.RemoveObject.
32023203
Cannot remove '.'.
32033204
"""
3204-
if target_directory in (b'.', u'.'):
3205+
if target_directory == matching_string(target_directory, '.'):
32053206
error_nr = errno.EACCES if self.is_windows_fs else errno.EINVAL
32063207
self.raise_os_error(error_nr, target_directory)
32073208
ends_with_sep = self.ends_with_path_separator(target_directory)
@@ -4652,7 +4653,7 @@ def mknod(self, path: AnyStr, mode: Optional[int] = None,
46524653
if self.filesystem.exists(head, check_link=True):
46534654
self.filesystem.raise_os_error(errno.EEXIST, path)
46544655
self.filesystem.raise_os_error(errno.ENOENT, path)
4655-
if tail in (b'.', u'.', b'..', u'..'):
4656+
if tail in (matching_string(tail, '.'), matching_string(tail, '..')):
46564657
self.filesystem.raise_os_error(errno.ENOENT, path)
46574658
if self.filesystem.exists(path, check_link=True):
46584659
self.filesystem.raise_os_error(errno.EEXIST, path)

pyfakefs/tests/fake_filesystem_vs_real_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ def _error_class(exc):
182182
real_err, real_value = self._get_real_value(method_name, path, real)
183183
fake_err, fake_value = self._get_fake_value(method_name, path, fake)
184184

185-
method_call = '%s' % method_name
186-
method_call += '()' if path == () else '(%s)' % path
185+
method_call = f'{method_name}'
186+
method_call += '()' if path == () else '({path})'
187187
# We only compare on the error class because the acutal error contents
188188
# is almost always different because of the file paths.
189189
if _error_class(real_err) != _error_class(fake_err):
@@ -223,7 +223,11 @@ def _get_fake_value(method_name, path, fake):
223223
if not callable(fake):
224224
fake_method = getattr(fake, method_name)
225225
args = [] if path == () else [path]
226-
fake_value = str(fake_method(*args))
226+
result = fake_method(*args)
227+
if isinstance(result, bytes):
228+
fake_value = result.decode()
229+
else:
230+
fake_value = str(result)
227231
except Exception as e: # pylint: disable-msg=W0703
228232
fake_err = e
229233
return fake_err, fake_value
@@ -238,7 +242,11 @@ def _get_real_value(method_name, path, real):
238242
real_method = real
239243
if not callable(real):
240244
real_method = getattr(real, method_name)
241-
real_value = str(real_method(*args))
245+
result = real_method(*args)
246+
if isinstance(result, bytes):
247+
real_value = result.decode()
248+
else:
249+
real_value = str(result)
242250
except Exception as e: # pylint: disable-msg=W0703
243251
real_err = e
244252
return real_err, real_value

0 commit comments

Comments
 (0)