@@ -905,6 +905,50 @@ def tearDown(self):
905905
906906 os .removedirs (path )
907907
908+
909+ class RemoveDirsTests (unittest .TestCase ):
910+ def setUp (self ):
911+ os .makedirs (support .TESTFN )
912+
913+ def tearDown (self ):
914+ support .rmtree (support .TESTFN )
915+
916+ def test_remove_all (self ):
917+ dira = os .path .join (support .TESTFN , 'dira' )
918+ os .mkdir (dira )
919+ dirb = os .path .join (dira , 'dirb' )
920+ os .mkdir (dirb )
921+ os .removedirs (dirb )
922+ self .assertFalse (os .path .exists (dirb ))
923+ self .assertFalse (os .path .exists (dira ))
924+ self .assertFalse (os .path .exists (support .TESTFN ))
925+
926+ def test_remove_partial (self ):
927+ dira = os .path .join (support .TESTFN , 'dira' )
928+ os .mkdir (dira )
929+ dirb = os .path .join (dira , 'dirb' )
930+ os .mkdir (dirb )
931+ with open (os .path .join (dira , 'file.txt' ), 'w' ) as f :
932+ f .write ('text' )
933+ os .removedirs (dirb )
934+ self .assertFalse (os .path .exists (dirb ))
935+ self .assertTrue (os .path .exists (dira ))
936+ self .assertTrue (os .path .exists (support .TESTFN ))
937+
938+ def test_remove_nothing (self ):
939+ dira = os .path .join (support .TESTFN , 'dira' )
940+ os .mkdir (dira )
941+ dirb = os .path .join (dira , 'dirb' )
942+ os .mkdir (dirb )
943+ with open (os .path .join (dirb , 'file.txt' ), 'w' ) as f :
944+ f .write ('text' )
945+ with self .assertRaises (OSError ):
946+ os .removedirs (dirb )
947+ self .assertTrue (os .path .exists (dirb ))
948+ self .assertTrue (os .path .exists (dira ))
949+ self .assertTrue (os .path .exists (support .TESTFN ))
950+
951+
908952class DevNullTests (unittest .TestCase ):
909953 def test_devnull (self ):
910954 with open (os .devnull , 'wb' ) as f :
@@ -913,6 +957,7 @@ def test_devnull(self):
913957 with open (os .devnull , 'rb' ) as f :
914958 self .assertEqual (f .read (), b'' )
915959
960+
916961class URandomTests (unittest .TestCase ):
917962 def test_urandom_length (self ):
918963 self .assertEqual (len (os .urandom (0 )), 0 )
@@ -2176,6 +2221,7 @@ def test_main():
21762221 Win32DeprecatedBytesAPI ,
21772222 TermsizeTests ,
21782223 OSErrorTests ,
2224+ RemoveDirsTests ,
21792225 )
21802226
21812227if __name__ == "__main__" :
0 commit comments