You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I'm trying to modify a file in a unittest so that it is in read-only mode and therefore non-deletable unless the user has root privileges.
How To Reproduce
Run the following unittest class:
#!/usr/bin/env python3importosimportstatfrompyfakefs.fake_filesystem_unittestimportTestCaseclassTestCaseExample(TestCase):
defsetUp(self):
self.setUpPyfakefs()
deftest_delete_file_insufficient_permissions(self):
file_path="/tmp/file.txt"file_content="Doesn't really matter but hey :)"self.fs.create_file(file_path, contents=file_content)
self.assertTrue(os.path.exists(file_path))
# Make sure we're not adminself.assertFalse(os.getuid() ==0)
# Make sure that the default mask is -rw-rw-rwdefault_mask=0o666initial_mask=stat.S_IMODE(os.lstat(file_path).st_mode)
self.assertEqual(default_mask, initial_mask)
# Make sure that the new mask is -r--r--r--read_only_mask=0o444os.chmod(file_path, read_only_mask)
new_mask=stat.S_IMODE(os.lstat(file_path).st_mode)
self.assertEqual(read_only_mask, new_mask)
# Deleting the file should not be possiblewithself.assertRaises(PermissionError):
os.remove(file_path)
Expected result: The test runs without errors, because the file is in read-only mode and the user is not privileged and therefore must not delete the file.
Actual result: The test fails
Ran 1 test in 0.051s
FAILED (failures=1)
Failure
Traceback (most recent call last):
File "/usr/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
yield
File "/usr/lib/python3.8/unittest/case.py", line 676, in run
self._callTestMethod(testMethod)
File "/usr/lib/python3.8/unittest/case.py", line 633, in _callTestMethod
method()
File "/tmp/pyfakefs-permissions/main.py", line 36, in test_delete_file_insufficient_permissions
os.remove(file_path)
File "/usr/lib/python3.8/unittest/case.py", line 227, in __exit__
self._raiseFailure("{} not raised".format(exc_name))
File "/usr/lib/python3.8/unittest/case.py", line 164, in _raiseFailure
raise self.test_case.failureException(msg)
AssertionError: PermissionError not raised
This is intentionally. Under Unix (not Windows), you can remove such a file, as long as the file directory has write permissions. Here ist the respective test in pyfakefs (in fake_os_test.py):
Describe the bug
I'm trying to modify a file in a unittest so that it is in read-only mode and therefore non-deletable unless the user has root privileges.
How To Reproduce
Run the following unittest class:
Expected result: The test runs without errors, because the file is in read-only mode and the user is not privileged and therefore must not delete the file.
Actual result: The test fails
Your enviroment
Linux-5.7.12-arch1-1-x86_64-with-glibc2.2.5
Python 3.8.5 (default, Jul 27 2020, 08:42:51)
[GCC 10.1.0]
pyfakefs 4.1.0
Notes
Even if I explicitly initialize the environment as non-privileged
self.setUpPyfakefs(allow_root_user=False)
the problem still existsPossibly related issues
The text was updated successfully, but these errors were encountered: