8 from tempfile import mkdtemp
12 old_time = time() - 1000 * 24 * 3600 # 1000 days ago
14 for path in os.environ["PATH"].split(os.pathsep):
15 path = path.strip('"')
16 test_prog_path = os.path.join(path, 'remove-old-files.py')
17 if os.path.exists(test_prog_path):
20 sys.exit("Cannot find remove-old-files.py in %s" % os.environ["PATH"])
30 os.chdir(os.sep) # To the root of the FS
31 shutil.rmtree(tmp_dir)
34 def create_files(files, subdirectory=None):
36 os.makedirs(subdirectory)
40 with open(os.path.join(subdirectory, fname), 'w'):
44 def assert_files_exist(files):
45 if isinstance(files, str):
48 assert os.path.exists(fname)
51 def assert_files_not_exist(files):
52 if isinstance(files, str):
55 assert not os.path.exists(fname)
58 def test_remove_old_files():
59 create_files(['test1', 'test2'])
60 assert_files_exist(['test1', 'test2'])
61 os.utime('test2', (old_time, old_time))
62 assert subprocess.call(
63 [sys.executable, test_prog_path, "--older", "100", "."]) == 0
64 assert_files_exist('test1')
65 assert_files_not_exist('test2')
69 create_files(['test3', 'test4'], 'subdir')
70 test3 = os.path.join('subdir', 'test3')
71 test4 = os.path.join('subdir', 'test4')
72 assert_files_exist([test3, test4])
73 os.utime(test4, (old_time, old_time))
74 assert subprocess.call(
75 [sys.executable, test_prog_path, "--older", "100", "."]) == 0
76 assert_files_exist(test3)
77 assert_files_not_exist(test4)
80 def test_remove_empty_directory():
81 create_files(['test3', 'test4'], 'subdir')
82 test3 = os.path.join('subdir', 'test3')
83 test4 = os.path.join('subdir', 'test4')
84 assert_files_exist([test3, test4])
85 os.utime(test3, (old_time, old_time))
86 os.utime(test4, (old_time, old_time))
87 assert subprocess.call(
88 [sys.executable, test_prog_path, "--older", "100", "."]) == 0
89 assert_files_exist('subdir')
90 assert_files_not_exist([test3, test4])
91 assert subprocess.call(
92 [sys.executable, test_prog_path, "-e", "--older", "100", "."]) == 0
93 assert_files_not_exist('subdir')