5 from tempfile import mkdtemp
18 os.chdir(os.sep) # To the root of the FS
19 shutil.rmtree(tmp_dir)
22 def create_files(files, subdirectory=None):
24 os.makedirs(subdirectory)
28 with open(os.path.join(subdirectory, fname), 'w'):
32 def assert_files_exist(files):
33 if isinstance(files, str):
36 assert os.path.exists(fname)
39 def assert_files_not_exist(files):
40 if isinstance(files, str):
43 assert not os.path.exists(fname)
46 def test_remove_old_files():
47 create_files(['test1', 'test2'])
48 assert_files_exist(['test1', 'test2'])
49 os.utime('test2', (0, 0))
50 assert os.system("remove-old-files.py --older 100 .") == 0
51 assert_files_exist('test1')
52 assert_files_not_exist('test2')
56 create_files(['test3', 'test4'], 'subdir')
57 test3 = os.path.join('subdir', 'test3')
58 test4 = os.path.join('subdir', 'test4')
59 assert_files_exist([test3, test4])
60 os.utime(test4, (0, 0))
61 assert os.system("remove-old-files.py --older 100 .") == 0
62 assert_files_exist(test3)
63 assert_files_not_exist(test4)
66 def test_remove_empty_directory():
67 create_files(['test3', 'test4'], 'subdir')
68 test3 = os.path.join('subdir', 'test3')
69 test4 = os.path.join('subdir', 'test4')
70 assert_files_exist([test3, test4])
71 os.utime(test3, (0, 0))
72 os.utime(test4, (0, 0))
73 assert os.system("remove-old-files.py --older 100 .") == 0
74 assert_files_exist('subdir')
75 assert_files_not_exist([test3, test4])
76 assert os.system("remove-old-files.py -e --older 100 .") == 0
77 assert_files_not_exist('subdir')