]> git.phdru.name Git - ppu.git/blob - tests/test_remove_old_files.py
Split test_remove_old_files.py into find_in_path.py
[ppu.git] / tests / test_remove_old_files.py
1 #! /usr/bin/env python
2
3 from time import time
4 import os
5 import shutil
6 import subprocess
7 import sys
8 from tempfile import mkdtemp
9 from find_in_path import find_in_path
10
11
12 tmp_dir = None
13 old_time = time() - 1000 * 24 * 3600  # 1000 days ago
14
15 test_prog_path = find_in_path('remove-old-files.py')
16 if not test_prog_path:
17     sys.exit("Cannot find remove-old-files.py in %s" % os.environ["PATH"])
18
19
20 def setup():
21     global tmp_dir
22     tmp_dir = mkdtemp()
23     os.chdir(tmp_dir)
24
25
26 def teardown():
27     os.chdir(os.sep)  # To the root of the FS
28     shutil.rmtree(tmp_dir)
29
30
31 def create_files(files, subdirectory=None):
32     if subdirectory:
33         os.makedirs(subdirectory)
34     else:
35         subdirectory = ''
36     for fname in files:
37         with open(os.path.join(subdirectory, fname), 'w'):
38             pass
39
40
41 def assert_files_exist(files):
42     if isinstance(files, str):
43         files = [files]
44     for fname in files:
45         assert os.path.exists(fname)
46
47
48 def assert_files_not_exist(files):
49     if isinstance(files, str):
50         files = [files]
51     for fname in files:
52         assert not os.path.exists(fname)
53
54
55 def test_remove_old_files():
56     create_files(['test1', 'test2'])
57     assert_files_exist(['test1', 'test2'])
58     os.utime('test2', (old_time, old_time))
59     assert subprocess.call(
60         [sys.executable, test_prog_path, "--older", "100", "."]) == 0
61     assert_files_exist('test1')
62     assert_files_not_exist('test2')
63
64
65 def test_recursive():
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, (old_time, old_time))
71     assert subprocess.call(
72         [sys.executable, test_prog_path, "--older", "100", "."]) == 0
73     assert_files_exist(test3)
74     assert_files_not_exist(test4)
75
76
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, (old_time, old_time))
83     os.utime(test4, (old_time, old_time))
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')