]> git.phdru.name Git - ppu.git/blob - tests/test_remove_old_files.py
Fix tests: use more meaningful old time instead of epoch
[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
10
11 tmp_dir = None
12 old_time = time() - 1000 * 24 * 3600  # 1000 days ago
13
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):
18         break
19 else:
20     sys.exit("Cannot find remove-old-files.py in %s" % os.environ["PATH"])
21
22
23 def setup():
24     global tmp_dir
25     tmp_dir = mkdtemp()
26     os.chdir(tmp_dir)
27
28
29 def teardown():
30     os.chdir(os.sep)  # To the root of the FS
31     shutil.rmtree(tmp_dir)
32
33
34 def create_files(files, subdirectory=None):
35     if subdirectory:
36         os.makedirs(subdirectory)
37     else:
38         subdirectory = ''
39     for fname in files:
40         with open(os.path.join(subdirectory, fname), 'w'):
41             pass
42
43
44 def assert_files_exist(files):
45     if isinstance(files, str):
46         files = [files]
47     for fname in files:
48         assert os.path.exists(fname)
49
50
51 def assert_files_not_exist(files):
52     if isinstance(files, str):
53         files = [files]
54     for fname in files:
55         assert not os.path.exists(fname)
56
57
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')
66
67
68 def test_recursive():
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)
78
79
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')