]> git.phdru.name Git - ppu.git/blob - tests/test_remove_old_files.py
67da601952463f0c5379af8614b0b371aa7332f7
[ppu.git] / tests / test_remove_old_files.py
1 #! /usr/bin/env python
2
3 import shutil
4 import os
5 from tempfile import mkdtemp
6
7
8 tmp_dir = None
9
10
11 def setup():
12     global tmp_dir
13     tmp_dir = mkdtemp()
14     os.chdir(tmp_dir)
15
16
17 def teardown():
18     os.chdir(os.sep)  # To the root of the FS
19     shutil.rmtree(tmp_dir)
20
21
22 def create_files(files, subdirectory=None):
23     if subdirectory:
24         os.makedirs(subdirectory)
25     else:
26         subdirectory = ''
27     for fname in files:
28         with open(os.path.join(subdirectory, fname), 'w'):
29             pass
30
31
32 def assert_files_exist(files):
33     if isinstance(files, str):
34         files = [files]
35     for fname in files:
36         assert os.path.exists(fname)
37
38
39 def assert_files_not_exist(files):
40     if isinstance(files, str):
41         files = [files]
42     for fname in files:
43         assert not os.path.exists(fname)
44
45
46 def test_remove_old_files():
47     create_files(['test1', 'test2'])
48     assert_files_exist(['test1', 'test2'])
49     os.utime('test2', (0, 0))
50     assert os.system("remove-old-files.py --older 100 .") == 0
51     assert_files_exist('test1')
52     assert_files_not_exist('test2')
53
54
55 def test_recursive():
56     create_files(['test3', 'test4'], 'subdir')
57     test3 = os.path.join('subdir', 'test3')
58     test4 = os.path.join('subdir', 'test4')
59     assert_files_exist([test3, test4])
60     os.utime(test4, (0, 0))
61     assert os.system("remove-old-files.py --older 100 .") == 0
62     assert_files_exist(test3)
63     assert_files_not_exist(test4)
64
65
66 def test_remove_empty_directory():
67     create_files(['test3', 'test4'], 'subdir')
68     test3 = os.path.join('subdir', 'test3')
69     test4 = os.path.join('subdir', 'test4')
70     assert_files_exist([test3, test4])
71     os.utime(test3, (0, 0))
72     os.utime(test4, (0, 0))
73     assert os.system("remove-old-files.py --older 100 .") == 0
74     assert_files_exist('subdir')
75     assert_files_not_exist([test3, test4])
76     assert os.system("remove-old-files.py -e --older 100 .") == 0
77     assert_files_not_exist('subdir')