#! /usr/bin/env python
-import shutil
+from time import time
import os
-from tempfile import mkdtemp
+import subprocess
+import sys
+from ppu_tu import setup, teardown, find_in_path # noqa
+from ppu_tu import create_files, assert_files_exist, assert_files_not_exist
-tmp_dir = None
+test_prog_path = find_in_path('remove-old-files.py')
-def setup():
- global tmp_dir
- tmp_dir = mkdtemp()
- os.chdir(tmp_dir)
-
-
-def teardown():
- os.chdir(os.sep) # To the root of the FS
- shutil.rmtree(tmp_dir)
-
-
-def create_files(files, subdirectory=None):
- if subdirectory:
- os.makedirs(subdirectory)
- else:
- subdirectory = ''
- for fname in files:
- with open(os.path.join(subdirectory, fname), 'w'):
- pass
-
-
-def assert_files_exist(files):
- if isinstance(files, str):
- files = [files]
- for fname in files:
- assert os.path.exists(fname)
-
-
-def assert_files_not_exist(files):
- if isinstance(files, str):
- files = [files]
- for fname in files:
- assert not os.path.exists(fname)
+old_time = time() - 1000 * 24 * 3600 # 1000 days ago
def test_remove_old_files():
create_files(['test1', 'test2'])
assert_files_exist(['test1', 'test2'])
- os.utime('test2', (0, 0))
- assert os.system("remove-old-files.py --older 100 .") == 0
+ os.utime('test2', (old_time, old_time))
+ assert subprocess.call(
+ [sys.executable, test_prog_path, "--older", "100", "."]) == 0
assert_files_exist('test1')
assert_files_not_exist('test2')
test3 = os.path.join('subdir', 'test3')
test4 = os.path.join('subdir', 'test4')
assert_files_exist([test3, test4])
- os.utime(test4, (0, 0))
- assert os.system("remove-old-files.py --older 100 .") == 0
+ os.utime(test4, (old_time, old_time))
+ assert subprocess.call(
+ [sys.executable, test_prog_path, "--older", "100", "."]) == 0
assert_files_exist(test3)
assert_files_not_exist(test4)
+
+
+def test_remove_empty_directory():
+ create_files(['test3', 'test4'], 'subdir')
+ test3 = os.path.join('subdir', 'test3')
+ test4 = os.path.join('subdir', 'test4')
+ assert_files_exist([test3, test4])
+ os.utime(test3, (old_time, old_time))
+ os.utime(test4, (old_time, old_time))
+ assert subprocess.call(
+ [sys.executable, test_prog_path, "--older", "100", "."]) == 0
+ assert_files_exist('subdir')
+ assert_files_not_exist([test3, test4])
+ assert subprocess.call(
+ [sys.executable, test_prog_path, "-e", "--older", "100", "."]) == 0
+ assert_files_not_exist('subdir')