6 from tempfile import mkdtemp
10 for path in os.environ["PATH"].split(os.pathsep):
11 path = path.strip('"')
12 test_prog_path = os.path.join(path, 'remove-old-files.py')
13 if os.path.exists(test_prog_path):
16 sys.exit("Cannot find remove-old-files.py in %s" % os.environ["PATH"])
26 os.chdir(os.sep) # To the root of the FS
27 shutil.rmtree(tmp_dir)
30 def create_files(files, subdirectory=None):
32 os.makedirs(subdirectory)
36 with open(os.path.join(subdirectory, fname), 'w'):
40 def assert_files_exist(files):
41 if isinstance(files, str):
44 assert os.path.exists(fname)
47 def assert_files_not_exist(files):
48 if isinstance(files, str):
51 assert not os.path.exists(fname)
54 def test_remove_old_files():
55 create_files(['test1', 'test2'])
56 assert_files_exist(['test1', 'test2'])
57 os.utime('test2', (0, 0))
59 "%s %s --older 100 ." % (sys.executable, test_prog_path)) == 0
60 assert_files_exist('test1')
61 assert_files_not_exist('test2')
65 create_files(['test3', 'test4'], 'subdir')
66 test3 = os.path.join('subdir', 'test3')
67 test4 = os.path.join('subdir', 'test4')
68 assert_files_exist([test3, test4])
69 os.utime(test4, (0, 0))
71 "%s %s --older 100 ." % (sys.executable, test_prog_path)) == 0
72 assert_files_exist(test3)
73 assert_files_not_exist(test4)
76 def test_remove_empty_directory():
77 create_files(['test3', 'test4'], 'subdir')
78 test3 = os.path.join('subdir', 'test3')
79 test4 = os.path.join('subdir', 'test4')
80 assert_files_exist([test3, test4])
81 os.utime(test3, (0, 0))
82 os.utime(test4, (0, 0))
84 "%s %s --older 100 ." % (sys.executable, test_prog_path)) == 0
85 assert_files_exist('subdir')
86 assert_files_not_exist([test3, test4])
88 "%s %s -e --older 100 ." % (sys.executable, test_prog_path)) == 0
89 assert_files_not_exist('subdir')