@@ -1299,19 +1299,25 @@ def __exit__(self, *exc_info):
1299
1299
class TestTemporaryDirectory (BaseTestCase ):
1300
1300
"""Test TemporaryDirectory()."""
1301
1301
1302
- def do_create (self , dir = None , pre = "" , suf = "" , recurse = 1 ):
1302
+ def do_create (self , dir = None , pre = "" , suf = "" , recurse = 1 , dirs = 1 , files = 1 ):
1303
1303
if dir is None :
1304
1304
dir = tempfile .gettempdir ()
1305
1305
tmp = tempfile .TemporaryDirectory (dir = dir , prefix = pre , suffix = suf )
1306
1306
self .nameCheck (tmp .name , dir , pre , suf )
1307
- # Create a subdirectory and some files
1308
- if recurse :
1309
- d1 = self .do_create (tmp .name , pre , suf , recurse - 1 )
1310
- d1 .name = None
1311
- with open (os .path .join (tmp .name , "test.txt" ), "wb" ) as f :
1312
- f .write (b"Hello world!" )
1307
+ self .do_create2 (tmp .name , recurse , dirs , files )
1313
1308
return tmp
1314
1309
1310
+ def do_create2 (self , path , recurse = 1 , dirs = 1 , files = 1 ):
1311
+ # Create subdirectories and some files
1312
+ if recurse :
1313
+ for i in range (dirs ):
1314
+ name = os .path .join (path , "dir%d" % i )
1315
+ os .mkdir (name )
1316
+ self .do_create2 (name , recurse - 1 , dirs , files )
1317
+ for i in range (files ):
1318
+ with open (os .path .join (path , "test%d.txt" % i ), "wb" ) as f :
1319
+ f .write (b"Hello world!" )
1320
+
1315
1321
def test_mkdtemp_failure (self ):
1316
1322
# Check no additional exception if mkdtemp fails
1317
1323
# Previously would raise AttributeError instead
@@ -1351,7 +1357,7 @@ def test_cleanup_with_symlink_to_a_directory(self):
1351
1357
"TemporaryDirectory %s exists after cleanup" % d1 .name )
1352
1358
self .assertTrue (os .path .exists (d2 .name ),
1353
1359
"Directory pointed to by a symlink was deleted" )
1354
- self .assertEqual (os .listdir (d2 .name ), ['test .txt' ],
1360
+ self .assertEqual (os .listdir (d2 .name ), ['test0 .txt' ],
1355
1361
"Contents of the directory pointed to by a symlink "
1356
1362
"were deleted" )
1357
1363
d2 .cleanup ()
@@ -1483,7 +1489,7 @@ def test_del_on_shutdown(self):
1483
1489
1484
1490
tmp2 = os.path.join(tmp.name, 'test_dir')
1485
1491
os.mkdir(tmp2)
1486
- with open(os.path.join(tmp2, "test .txt"), "w") as f:
1492
+ with open(os.path.join(tmp2, "test0 .txt"), "w") as f:
1487
1493
f.write("Hello world!")
1488
1494
1489
1495
{mod}.tmp = tmp
@@ -1551,6 +1557,51 @@ def test_context_manager(self):
1551
1557
self .assertEqual (name , d .name )
1552
1558
self .assertFalse (os .path .exists (name ))
1553
1559
1560
+ def test_modes (self ):
1561
+ for mode in range (8 ):
1562
+ mode <<= 6
1563
+ with self .subTest (mode = format (mode , '03o' )):
1564
+ d = self .do_create (recurse = 3 , dirs = 2 , files = 2 )
1565
+ with d :
1566
+ # Change files and directories mode recursively.
1567
+ for root , dirs , files in os .walk (d .name , topdown = False ):
1568
+ for name in files :
1569
+ os .chmod (os .path .join (root , name ), mode )
1570
+ os .chmod (root , mode )
1571
+ d .cleanup ()
1572
+ self .assertFalse (os .path .exists (d .name ))
1573
+
1574
+ def check_flags (self , flags ):
1575
+ # skip the test if these flags are not supported (ex: FreeBSD 13)
1576
+ filename = support .TESTFN
1577
+ try :
1578
+ open (filename , "w" ).close ()
1579
+ try :
1580
+ os .chflags (filename , flags )
1581
+ except OSError as exc :
1582
+ # "OSError: [Errno 45] Operation not supported"
1583
+ self .skipTest (f"chflags() doesn't support flags "
1584
+ f"{ flags :#b} : { exc } " )
1585
+ else :
1586
+ os .chflags (filename , 0 )
1587
+ finally :
1588
+ support .unlink (filename )
1589
+
1590
+ @unittest .skipUnless (hasattr (os , 'chflags' ), 'requires os.lchflags' )
1591
+ def test_flags (self ):
1592
+ flags = stat .UF_IMMUTABLE | stat .UF_NOUNLINK
1593
+ self .check_flags (flags )
1594
+
1595
+ d = self .do_create (recurse = 3 , dirs = 2 , files = 2 )
1596
+ with d :
1597
+ # Change files and directories flags recursively.
1598
+ for root , dirs , files in os .walk (d .name , topdown = False ):
1599
+ for name in files :
1600
+ os .chflags (os .path .join (root , name ), flags )
1601
+ os .chflags (root , flags )
1602
+ d .cleanup ()
1603
+ self .assertFalse (os .path .exists (d .name ))
1604
+
1554
1605
1555
1606
if __name__ == "__main__" :
1556
1607
unittest .main ()
0 commit comments