@@ -1496,7 +1496,7 @@ def iterdir(self):
1496
1496
if path in self ._files :
1497
1497
raise NotADirectoryError (errno .ENOTDIR , "Not a directory" , path )
1498
1498
elif path in self ._directories :
1499
- return ( self / name for name in self ._directories [path ])
1499
+ return iter ([ self / name for name in self ._directories [path ] ])
1500
1500
else :
1501
1501
raise FileNotFoundError (errno .ENOENT , "File not found" , path )
1502
1502
@@ -1517,6 +1517,37 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
1517
1517
self .parent .mkdir (parents = True , exist_ok = True )
1518
1518
self .mkdir (mode , parents = False , exist_ok = exist_ok )
1519
1519
1520
+ def unlink (self , missing_ok = False ):
1521
+ path_obj = self .parent .resolve (strict = True ) / self .name
1522
+ path = str (path_obj )
1523
+ name = path_obj .name
1524
+ parent = str (path_obj .parent )
1525
+ if path in self ._directories :
1526
+ raise IsADirectoryError (errno .EISDIR , "Is a directory" , path )
1527
+ elif path in self ._files :
1528
+ self ._directories [parent ].remove (name )
1529
+ del self ._files [path ]
1530
+ elif path in self ._symlinks :
1531
+ self ._directories [parent ].remove (name )
1532
+ del self ._symlinks [path ]
1533
+ elif not missing_ok :
1534
+ raise FileNotFoundError (errno .ENOENT , "File not found" , path )
1535
+
1536
+ def rmdir (self ):
1537
+ path_obj = self .parent .resolve (strict = True ) / self .name
1538
+ path = str (path_obj )
1539
+ if path in self ._files or path in self ._symlinks :
1540
+ raise NotADirectoryError (errno .ENOTDIR , "Not a directory" , path )
1541
+ elif path not in self ._directories :
1542
+ raise FileNotFoundError (errno .ENOENT , "File not found" , path )
1543
+ elif self ._directories [path ]:
1544
+ raise OSError (errno .ENOTEMPTY , "Directory not empty" , path )
1545
+ else :
1546
+ name = path_obj .name
1547
+ parent = str (path_obj .parent )
1548
+ self ._directories [parent ].remove (name )
1549
+ del self ._directories [path ]
1550
+
1520
1551
1521
1552
class DummyPathTest (DummyPurePathTest ):
1522
1553
"""Tests for PathBase methods that use stat(), open() and iterdir()."""
@@ -2400,6 +2431,25 @@ def test_complex_symlinks_relative(self):
2400
2431
def test_complex_symlinks_relative_dot_dot (self ):
2401
2432
self ._check_complex_symlinks (self .parser .join ('dirA' , '..' ))
2402
2433
2434
+ def test_unlink (self ):
2435
+ p = self .cls (self .base ) / 'fileA'
2436
+ p .unlink ()
2437
+ self .assertFileNotFound (p .stat )
2438
+ self .assertFileNotFound (p .unlink )
2439
+
2440
+ def test_unlink_missing_ok (self ):
2441
+ p = self .cls (self .base ) / 'fileAAA'
2442
+ self .assertFileNotFound (p .unlink )
2443
+ p .unlink (missing_ok = True )
2444
+
2445
+ def test_rmdir (self ):
2446
+ p = self .cls (self .base ) / 'dirA'
2447
+ for q in p .iterdir ():
2448
+ q .unlink ()
2449
+ p .rmdir ()
2450
+ self .assertFileNotFound (p .stat )
2451
+ self .assertFileNotFound (p .unlink )
2452
+
2403
2453
def setUpWalk (self ):
2404
2454
# Build:
2405
2455
# TESTFN/
0 commit comments