@@ -634,6 +634,50 @@ def tearDown(self):
634634
635635 os .removedirs (path )
636636
637+
638+ class RemoveDirsTests (unittest .TestCase ):
639+ def setUp (self ):
640+ os .makedirs (support .TESTFN )
641+
642+ def tearDown (self ):
643+ support .rmtree (support .TESTFN )
644+
645+ def test_remove_all (self ):
646+ dira = os .path .join (support .TESTFN , 'dira' )
647+ os .mkdir (dira )
648+ dirb = os .path .join (dira , 'dirb' )
649+ os .mkdir (dirb )
650+ os .removedirs (dirb )
651+ self .assertFalse (os .path .exists (dirb ))
652+ self .assertFalse (os .path .exists (dira ))
653+ self .assertFalse (os .path .exists (support .TESTFN ))
654+
655+ def test_remove_partial (self ):
656+ dira = os .path .join (support .TESTFN , 'dira' )
657+ os .mkdir (dira )
658+ dirb = os .path .join (dira , 'dirb' )
659+ os .mkdir (dirb )
660+ with open (os .path .join (dira , 'file.txt' ), 'w' ) as f :
661+ f .write ('text' )
662+ os .removedirs (dirb )
663+ self .assertFalse (os .path .exists (dirb ))
664+ self .assertTrue (os .path .exists (dira ))
665+ self .assertTrue (os .path .exists (support .TESTFN ))
666+
667+ def test_remove_nothing (self ):
668+ dira = os .path .join (support .TESTFN , 'dira' )
669+ os .mkdir (dira )
670+ dirb = os .path .join (dira , 'dirb' )
671+ os .mkdir (dirb )
672+ with open (os .path .join (dirb , 'file.txt' ), 'w' ) as f :
673+ f .write ('text' )
674+ with self .assertRaises (OSError ):
675+ os .removedirs (dirb )
676+ self .assertTrue (os .path .exists (dirb ))
677+ self .assertTrue (os .path .exists (dira ))
678+ self .assertTrue (os .path .exists (support .TESTFN ))
679+
680+
637681class DevNullTests (unittest .TestCase ):
638682 def test_devnull (self ):
639683 with open (os .devnull , 'wb' ) as f :
@@ -642,6 +686,7 @@ def test_devnull(self):
642686 with open (os .devnull , 'rb' ) as f :
643687 self .assertEqual (f .read (), b'' )
644688
689+
645690class URandomTests (unittest .TestCase ):
646691 def test_urandom_length (self ):
647692 self .assertEqual (len (os .urandom (0 )), 0 )
@@ -1310,6 +1355,7 @@ def test_main():
13101355 PidTests ,
13111356 LoginTests ,
13121357 LinkTests ,
1358+ RemoveDirsTests ,
13131359 )
13141360
13151361if __name__ == "__main__" :
0 commit comments