Skip to content

Commit 4a9eb37

Browse files
committed
Test that cleanup() does not chmod the parent directory
1 parent d4e5045 commit 4a9eb37

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎Lib/test/test_tempfile.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,26 @@ def test_modes(self):
20792079
d.cleanup()
20802080
self.assertFalse(os.path.exists(d.name))
20812081

2082+
def test_parent_mode_preserved(self):
2083+
# Test that cleanup does not touch the parent directory,
2084+
# even if that prevents removal.
2085+
for mode in range(8):
2086+
mode <<= 6
2087+
with self.subTest(mode=format(mode, '03o')):
2088+
outer = self.do_create()
2089+
with outer:
2090+
d = self.do_create(dir=outer.name, dirs=2, files=2)
2091+
with d:
2092+
os.chmod(outer.name, mode)
2093+
orig_mode = os.stat(outer.name).st_mode
2094+
try:
2095+
d.cleanup()
2096+
except PermissionError:
2097+
pass
2098+
self.assertEqual(os.stat(outer.name).st_mode, orig_mode)
2099+
outer.cleanup()
2100+
self.assertFalse(os.path.exists(outer.name))
2101+
20822102
def check_flags(self, flags):
20832103
# skip the test if these flags are not supported (ex: FreeBSD 13)
20842104
filename = os_helper.TESTFN

0 commit comments

Comments
 (0)