@@ -1297,19 +1297,25 @@ def __exit__(self, *exc_info):
1297
1297
class TestTemporaryDirectory (BaseTestCase ):
1298
1298
"""Test TemporaryDirectory()."""
1299
1299
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 ):
1301
1301
if dir is None :
1302
1302
dir = tempfile .gettempdir ()
1303
1303
tmp = tempfile .TemporaryDirectory (dir = dir , prefix = pre , suffix = suf )
1304
1304
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 )
1311
1306
return tmp
1312
1307
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
+
1313
1319
def test_mkdtemp_failure (self ):
1314
1320
# Check no additional exception if mkdtemp fails
1315
1321
# Previously would raise AttributeError instead
@@ -1349,7 +1355,7 @@ def test_cleanup_with_symlink_to_a_directory(self):
1349
1355
"TemporaryDirectory %s exists after cleanup" % d1 .name )
1350
1356
self .assertTrue (os .path .exists (d2 .name ),
1351
1357
"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' ],
1353
1359
"Contents of the directory pointed to by a symlink "
1354
1360
"were deleted" )
1355
1361
d2 .cleanup ()
@@ -1384,7 +1390,7 @@ def test_del_on_shutdown(self):
1384
1390
1385
1391
tmp2 = os.path.join(tmp.name, 'test_dir')
1386
1392
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:
1388
1394
f.write("Hello world!")
1389
1395
1390
1396
{mod}.tmp = tmp
@@ -1452,6 +1458,33 @@ def test_context_manager(self):
1452
1458
self .assertEqual (name , d .name )
1453
1459
self .assertFalse (os .path .exists (name ))
1454
1460
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
+
1455
1488
1456
1489
if __name__ == "__main__" :
1457
1490
unittest .main ()
0 commit comments