@@ -1297,19 +1297,25 @@ def __exit__(self, *exc_info):
12971297class TestTemporaryDirectory (BaseTestCase ):
12981298 """Test TemporaryDirectory()."""
12991299
1300- def do_create (self , dir = None , pre = "" , suf = "" , recurse = 1 ):
1300+ def do_create (self , dir = None , pre = "" , suf = "" , recurse = 1 , dirs = 1 , files = 1 ):
13011301 if dir is None :
13021302 dir = tempfile .gettempdir ()
13031303 tmp = tempfile .TemporaryDirectory (dir = dir , prefix = pre , suffix = suf )
13041304 self .nameCheck (tmp .name , dir , pre , suf )
1305- # Create a subdirectory and some files
1306- if recurse :
1307- d1 = self .do_create (tmp .name , pre , suf , recurse - 1 )
1308- d1 .name = None
1309- with open (os .path .join (tmp .name , "test.txt" ), "wb" ) as f :
1310- f .write (b"Hello world!" )
1305+ self .do_create2 (tmp .name , recurse , dirs , files )
13111306 return tmp
13121307
1308+ def do_create2 (self , path , recurse = 1 , dirs = 1 , files = 1 ):
1309+ # Create subdirectories and some files
1310+ if recurse :
1311+ for i in range (dirs ):
1312+ name = os .path .join (path , "dir%d" % i )
1313+ os .mkdir (name )
1314+ self .do_create2 (name , recurse - 1 , dirs , files )
1315+ for i in range (files ):
1316+ with open (os .path .join (path , "test%d.txt" % i ), "wb" ) as f :
1317+ f .write (b"Hello world!" )
1318+
13131319 def test_mkdtemp_failure (self ):
13141320 # Check no additional exception if mkdtemp fails
13151321 # Previously would raise AttributeError instead
@@ -1349,7 +1355,7 @@ def test_cleanup_with_symlink_to_a_directory(self):
13491355 "TemporaryDirectory %s exists after cleanup" % d1 .name )
13501356 self .assertTrue (os .path .exists (d2 .name ),
13511357 "Directory pointed to by a symlink was deleted" )
1352- self .assertEqual (os .listdir (d2 .name ), ['test .txt' ],
1358+ self .assertEqual (os .listdir (d2 .name ), ['test0 .txt' ],
13531359 "Contents of the directory pointed to by a symlink "
13541360 "were deleted" )
13551361 d2 .cleanup ()
@@ -1384,7 +1390,7 @@ def test_del_on_shutdown(self):
13841390
13851391 tmp2 = os.path.join(tmp.name, 'test_dir')
13861392 os.mkdir(tmp2)
1387- with open(os.path.join(tmp2, "test .txt"), "w") as f:
1393+ with open(os.path.join(tmp2, "test0 .txt"), "w") as f:
13881394 f.write("Hello world!")
13891395
13901396 {mod}.tmp = tmp
@@ -1452,6 +1458,33 @@ def test_context_manager(self):
14521458 self .assertEqual (name , d .name )
14531459 self .assertFalse (os .path .exists (name ))
14541460
1461+ def test_modes (self ):
1462+ for mode in range (8 ):
1463+ mode <<= 6
1464+ with self .subTest (mode = format (mode , '03o' )):
1465+ d = self .do_create (recurse = 3 , dirs = 2 , files = 2 )
1466+ with d :
1467+ # Change files and directories mode recursively.
1468+ for root , dirs , files in os .walk (d .name , topdown = False ):
1469+ for name in files :
1470+ os .chmod (os .path .join (root , name ), mode )
1471+ os .chmod (root , mode )
1472+ d .cleanup ()
1473+ self .assertFalse (os .path .exists (d .name ))
1474+
1475+ @unittest .skipUnless (hasattr (os , 'chflags' ), 'requires os.lchflags' )
1476+ def test_flags (self ):
1477+ flags = stat .UF_IMMUTABLE | stat .UF_NOUNLINK
1478+ d = self .do_create (recurse = 3 , dirs = 2 , files = 2 )
1479+ with d :
1480+ # Change files and directories flags recursively.
1481+ for root , dirs , files in os .walk (d .name , topdown = False ):
1482+ for name in files :
1483+ os .chflags (os .path .join (root , name ), flags )
1484+ os .chflags (root , flags )
1485+ d .cleanup ()
1486+ self .assertFalse (os .path .exists (d .name ))
1487+
14551488
14561489if __name__ == "__main__" :
14571490 unittest .main ()
0 commit comments