7 from tempfile import mkdtemp
11 for path in os.environ["PATH"].split(os.pathsep):
12 path = path.strip('"')
13 test_prog_path = os.path.join(path, 'remove-old-files.py')
14 if os.path.exists(test_prog_path):
17 sys.exit("Cannot find remove-old-files.py in %s" % os.environ["PATH"])
27 os.chdir(os.sep) # To the root of the FS
28 shutil.rmtree(tmp_dir)
31 def create_files(files, subdirectory=None):
33 os.makedirs(subdirectory)
37 with open(os.path.join(subdirectory, fname), 'w'):
41 def assert_files_exist(files):
42 if isinstance(files, str):
45 assert os.path.exists(fname)
48 def assert_files_not_exist(files):
49 if isinstance(files, str):
52 assert not os.path.exists(fname)
55 def test_remove_old_files():
56 create_files(['test1', 'test2'])
57 assert_files_exist(['test1', 'test2'])
58 os.utime('test2', (0, 0))
59 assert subprocess.call(
60 [sys.executable, test_prog_path, "--older", "100", "."]) == 0
61 assert_files_exist('test1')
62 assert_files_not_exist('test2')
66 create_files(['test3', 'test4'], 'subdir')
67 test3 = os.path.join('subdir', 'test3')
68 test4 = os.path.join('subdir', 'test4')
69 assert_files_exist([test3, test4])
70 os.utime(test4, (0, 0))
71 assert subprocess.call(
72 [sys.executable, test_prog_path, "--older", "100", "."]) == 0
73 assert_files_exist(test3)
74 assert_files_not_exist(test4)
77 def test_remove_empty_directory():
78 create_files(['test3', 'test4'], 'subdir')
79 test3 = os.path.join('subdir', 'test3')
80 test4 = os.path.join('subdir', 'test4')
81 assert_files_exist([test3, test4])
82 os.utime(test3, (0, 0))
83 os.utime(test4, (0, 0))
84 assert subprocess.call(
85 [sys.executable, test_prog_path, "--older", "100", "."]) == 0
86 assert_files_exist('subdir')
87 assert_files_not_exist([test3, test4])
88 assert subprocess.call(
89 [sys.executable, test_prog_path, "-e", "--older", "100", "."]) == 0
90 assert_files_not_exist('subdir')