@@ -84,6 +84,12 @@ def setUpClass(cls):
8484 def create_directory (cls , dir_name ):
8585 return tempfile .mkdtemp (dir = cls .temp_dir , prefix = dir_name )
8686
87+ @staticmethod
88+ def mkdirs (* dirs ):
89+ for d in dirs :
90+ os .makedirs (d )
91+ return dirs [:]
92+
8793 def assert_path_does_not_exist (self , * trailing_segments ):
8894 full_path = os .path .join (* trailing_segments )
8995 if os .path .exists (full_path ):
@@ -184,6 +190,7 @@ def test_that_directories_are_not_removed_in_dry_run_mode(self):
184190
185191
186192class VirtualEnvironmentCleanupTests (DirectoryCleanupMixin , unittest .TestCase ):
193+
187194 def setUp (self ):
188195 super (VirtualEnvironmentCleanupTests , self ).setUp ()
189196 self ._saved_venv = os .environ .get ('VIRTUAL_ENV' , None )
@@ -216,3 +223,83 @@ def test_that_janitor_does_not_fail_when_envdir_is_missing(self):
216223 def test_that_janitor_does_not_fail_when_no_dir_specified (self ):
217224 os .environ .pop ('VIRTUAL_ENV' , None )
218225 run_setup ('clean' , '--environment' )
226+
227+
228+ class PycacheCleanupTests (DirectoryCleanupMixin , unittest .TestCase ):
229+
230+ def setUp (self ):
231+ super (PycacheCleanupTests , self ).setUp ()
232+ self .test_root = self .create_directory ('test-root' )
233+ starting_dir = os .curdir
234+ self .addCleanup (os .chdir , starting_dir )
235+ os .chdir (self .test_root )
236+
237+ def test_that_pycache_directories_are_removed (self ):
238+ all_dirs = self .mkdirs (
239+ os .path .join (self .test_root , '__pycache__' ),
240+ os .path .join (self .test_root , 'a' , '__pycache__' ),
241+ os .path .join (self .test_root , 'a' , 'b' , '__pycache__' ),
242+ os .path .join (self .test_root , 'b' , '__pycache__' ),
243+ )
244+
245+ run_setup ('clean' , '--pycache' )
246+ for cache_dir in all_dirs :
247+ self .assert_path_does_not_exist (cache_dir )
248+
249+ def test_that_janitor_does_not_fail_when_cache_parent_is_removed (self ):
250+ all_dirs = self .mkdirs (
251+ os .path .join (self .test_root , 'dist' ),
252+ os .path .join (self .test_root , 'dist' , '__pycache__' , '__pycache__' ),
253+ os .path .join (self .test_root , 'dist' , 'foo' , '__pycache__' ),
254+ )
255+
256+ run_setup ('clean' , '--dist' , '--pycache' )
257+ for cache_dir in all_dirs :
258+ self .assert_path_does_not_exist (cache_dir )
259+
260+
261+ class RemoveAllTests (DirectoryCleanupMixin , unittest .TestCase ):
262+
263+ @classmethod
264+ def setUpClass (cls ):
265+ super (RemoveAllTests , cls ).setUpClass ()
266+ test_root = cls .create_directory ('test-root' )
267+ starting_dir = os .curdir
268+ saved_env_dir = os .environ .get ('VIRTUAL_ENV' , None )
269+
270+ def restore ():
271+ os .chdir (starting_dir )
272+ os .environ .pop ('VIRTUAL_ENV' , None )
273+ if saved_env_dir is not None :
274+ os .environ ['VIRTUAL_ENV' ] = saved_env_dir
275+
276+ os .chdir (test_root )
277+ try :
278+ all_dirs = cls .mkdirs (
279+ '__pycache__' ,
280+ 'dist' ,
281+ 'foo.egg-info' ,
282+ 'env' ,
283+ )
284+ cls .pycache_dir = all_dirs [0 ]
285+ cls .dist_dir = all_dirs [1 ]
286+ cls .egg_dir = all_dirs [2 ]
287+ cls .env_dir = all_dirs [3 ]
288+ os .environ ['VIRTUAL_ENV' ] = cls .env_dir
289+ run_setup ('clean' , '--all' )
290+ restore ()
291+ except :
292+ restore ()
293+ raise
294+
295+ def test_that_pycache_is_removed (self ):
296+ self .assert_path_does_not_exist (self .pycache_dir )
297+
298+ def test_that_distdir_is_removed (self ):
299+ self .assert_path_does_not_exist (self .dist_dir )
300+
301+ def test_that_eggdir_is_removed (self ):
302+ self .assert_path_does_not_exist (self .egg_dir )
303+
304+ def test_that_envdir_is_removed (self ):
305+ self .assert_path_does_not_exist (self .env_dir )
0 commit comments