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