@@ -99,9 +99,9 @@ def test_makedirs_and_rmdirs_success(self):
9999 assert not os .path .exists (path )
100100 assert not self .operations .path_exists (path )
101101
102- def test_makedirs_and_rmdirs_failure (self ):
102+ def test_makedirs_failure (self ):
103103 """
104- Test makedirs and rmdirs for directory creation and removal failure.
104+ Test makedirs for failure.
105105 """
106106 # Try to create a directory in a read-only location
107107 path = "/root/test_dir"
@@ -110,16 +110,84 @@ def test_makedirs_and_rmdirs_failure(self):
110110 with pytest .raises (Exception ):
111111 self .operations .makedirs (path )
112112
113- # Test rmdirs
114- while True :
115- try :
116- self .operations .rmdirs (path , verbose = True )
117- except ExecUtilException as e :
118- assert e .message == "Utility exited with non-zero code (1). Error: `rm: cannot remove '/root/test_dir': Permission denied`"
119- assert type (e .error ) == bytes # noqa: E721
120- assert e .error .strip () == b"rm: cannot remove '/root/test_dir': Permission denied"
121- break
122- raise Exception ("We wait an exception!" )
113+ def test_rmdirs (self ):
114+ path = self .operations .mkdtemp ()
115+ assert os .path .exists (path )
116+
117+ assert self .operations .rmdirs (path , ignore_errors = False ) is True
118+ assert not os .path .exists (path )
119+
120+ def test_rmdirs__01_with_subfolder (self ):
121+ # folder with subfolder
122+ path = self .operations .mkdtemp ()
123+ assert os .path .exists (path )
124+
125+ dir1 = os .path .join (path , "dir1" )
126+ assert not os .path .exists (dir1 )
127+
128+ self .operations .makedirs (dir1 )
129+ assert os .path .exists (dir1 )
130+
131+ assert self .operations .rmdirs (path , ignore_errors = False ) is True
132+ assert not os .path .exists (path )
133+ assert not os .path .exists (dir1 )
134+
135+ def test_rmdirs__02_with_file (self ):
136+ # folder with file
137+ path = self .operations .mkdtemp ()
138+ assert os .path .exists (path )
139+
140+ file1 = os .path .join (path , "file1.txt" )
141+ assert not os .path .exists (file1 )
142+
143+ self .operations .touch (file1 )
144+ assert os .path .exists (file1 )
145+
146+ assert self .operations .rmdirs (path , ignore_errors = False ) is True
147+ assert not os .path .exists (path )
148+ assert not os .path .exists (file1 )
149+
150+ def test_rmdirs__03_with_subfolder_and_file (self ):
151+ # folder with subfolder and file
152+ path = self .operations .mkdtemp ()
153+ assert os .path .exists (path )
154+
155+ dir1 = os .path .join (path , "dir1" )
156+ assert not os .path .exists (dir1 )
157+
158+ self .operations .makedirs (dir1 )
159+ assert os .path .exists (dir1 )
160+
161+ file1 = os .path .join (dir1 , "file1.txt" )
162+ assert not os .path .exists (file1 )
163+
164+ self .operations .touch (file1 )
165+ assert os .path .exists (file1 )
166+
167+ assert self .operations .rmdirs (path , ignore_errors = False ) is True
168+ assert not os .path .exists (path )
169+ assert not os .path .exists (dir1 )
170+ assert not os .path .exists (file1 )
171+
172+ def test_rmdirs__try_to_delete_nonexist_path (self ):
173+ path = "/root/test_dir"
174+
175+ assert self .operations .rmdirs (path , ignore_errors = False ) is True
176+
177+ def test_rmdirs__try_to_delete_file (self ):
178+ path = self .operations .mkstemp ()
179+ assert os .path .exists (path )
180+
181+ with pytest .raises (ExecUtilException ) as x :
182+ self .operations .rmdirs (path , ignore_errors = False )
183+
184+ assert os .path .exists (path )
185+ assert type (x .value ) == ExecUtilException # noqa: E721
186+ assert x .value .message == "Utility exited with non-zero code (20). Error: `cannot remove '" + path + "': it is not a directory`"
187+ assert type (x .value .error ) == str # noqa: E721
188+ assert x .value .error .strip () == "cannot remove '" + path + "': it is not a directory"
189+ assert type (x .value .exit_code ) == int # noqa: E721
190+ assert x .value .exit_code == 20
123191
124192 def test_listdir (self ):
125193 """
0 commit comments