-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_dff.py
executable file
·179 lines (146 loc) · 7.54 KB
/
test_dff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python3
import os
import shutil
import stat
import unittest
from stat import S_IREAD, S_IRGRP, S_IROTH
from dff import (
clear_globals_for_unittests,
dff,
set_delete_shorter,
set_output_immediately,
set_trial_delete,
set_verbose_output,
)
version = "0.10.0"
# self.assertTrue(exp)
# self.assertEqual(a,b)
# self.assertContains(response, 'must supply test_name')
# self.assertRegex(response.content.decode('utf-8'), 'Test Passed: True')
# self.assertNotRegex(response.content.decode('utf-8'), 'delete id test')
def one_time_setup():
set_verbose_output(True)
set_output_immediately(False)
set_trial_delete(True)
set_delete_shorter(False)
class Testdff(unittest.TestCase):
def setUp(self):
clear_globals_for_unittests()
pass
def tearDown(self):
pass
def test_verbose_output_enabled(self):
response = dff("test/one_file")
self.assertRegex(response, "Checking size of file")
def test_find_small_file_duplicate(self):
response = dff("test/one_small_duplicate")
self.assertRegex(response, "calculating hash snippet")
self.assertRegex(response, "bbb.txt\n is dupe of test.one_small_duplicate.aaa.txt")
def test_find_two_small_file_duplicates(self):
response = dff("test/two_small_duplicates")
self.assertRegex(response, "bbb.txt\n is dupe of test.two_small_duplicates.aaa.txt")
self.assertRegex(response, "ccc.txt\n is dupe of test.two_small_duplicates.aaa.txt")
self.assertNotRegex(response, "aaa.txt\n is dupe of")
def test_find_large_file_duplicate(self):
response = dff("test/one_large_duplicate")
self.assertRegex(response, "big bbb.txt\n is dupe of test.one_large_duplicate.big aaa.txt")
self.assertRegex(response, "calculating full hash")
self.assertNotRegex(response, "big aaa.txt\n is dupe")
def test_find_two_large_file_duplicates(self):
response = dff("test/two_large_duplicates")
self.assertRegex(response, "big bbb.txt\n is dupe of test.two_large_duplicates.big aaa.txt")
self.assertRegex(response, "big ccc.txt\n is dupe of test.two_large_duplicates.big aaa.txt")
self.assertNotRegex(response, "big aaa.txt\n is dupe")
def test_do_not_find_large_file_almost_duplicate(self):
response = dff("test/one_large_almost_duplicate")
self.assertNotRegex(response, "is dupe of")
self.assertRegex(response, "but files are different")
def test_do_not_find_many_large_files_almost_duplicate(self):
response = dff("test/many_large_almost_duplicate")
self.assertNotRegex(response, "is dupe of")
self.assertRegex(response, "but files are different")
def test_search_sub_folders(self):
response = dff("test")
self.assertRegex(response, "is dupe of")
self.assertRegex(response, "but files are different")
def test_duplicate_across_folders(self):
response = dff("test/duplicate_across_folders")
self.assertRegex(response, "sub1.dupe.txt\n is dupe of test.duplicate_across_folders.master.txt")
self.assertRegex(response, "sub1.supersub.also_dupe.txt\n is dupe of test.duplicate_across_folders.master.txt")
self.assertRegex(response, "sub2.dupe.txt\n is dupe of test.duplicate_across_folders.master.txt")
def test_show_count_of_duplicates(self):
response = dff("test/duplicate_across_folders")
self.assertRegex(response, "3 duplicate files found")
def test_show_megabytes_scanned(self):
response = dff("test/duplicate_across_folders")
self.assertRegex(response, "0.046875 megabytes scanned")
def test_delete_duplicates_trial(self):
response = dff("test/duplicate_across_folders", True)
self.assertRegex(response, "deleted ... test.duplicate_across_folders.sub1.dupe.txt")
self.assertRegex(response, "deleted ... test.duplicate_across_folders.sub1.supersub.also_dupe.txt")
self.assertRegex(response, "deleted ... test.duplicate_across_folders.sub2.dupe.txt")
def test_count_of_examined_files(self):
response = dff("test/duplicate_across_folders")
self.assertRegex(response, "4 files and")
def test_delete_read_only_file(self):
try:
os.chmod("test/delete_unit_test/bbb.txt", stat.S_IWRITE)
except FileNotFoundError:
pass
# Why is this so unreliable ???
shutil.rmtree("test/delete_unit_test", ignore_errors=True)
shutil.rmtree("test/delete_unit_test", ignore_errors=True)
shutil.rmtree("test/delete_unit_test", ignore_errors=True)
try:
shutil.copytree("test/one_small_duplicate", "test/delete_unit_test")
except PermissionError:
print("Got some really wierd permission error - try again")
shutil.copytree("test/one_small_duplicate", "test/delete_unit_test")
os.chmod("test/delete_unit_test/bbb.txt", S_IREAD | S_IRGRP | S_IROTH)
set_trial_delete(False)
response = dff("test/delete_unit_test", True)
set_trial_delete(True)
self.assertRegex(response, "deleted ... test.delete_unit_test.bbb.txt")
def test_show_start_and_end_times(self):
response = dff("test/duplicate_across_folders")
self.assertRegex(response, r"\d{2}:\d{2}:\d{2}")
def test_show_run_time_in_seconds(self):
response = dff("test/duplicate_across_folders")
self.assertRegex(response, r"in \d+\.\d+ seconds")
def test_delete_file_with_longest_name(self):
shutil.rmtree("test/delete_filename_length", ignore_errors=True)
shutil.rmtree("test/delete_filename_length", ignore_errors=True)
shutil.rmtree("test/delete_filename_length", ignore_errors=True)
shutil.copytree("test/filename_length", "test/delete_filename_length")
set_delete_shorter(True)
set_trial_delete(False)
response = dff("test/delete_filename_length", True)
set_delete_shorter(False)
set_trial_delete(True)
self.assertRegex(response, "deleted ... test.delete_filename_length.bbbb.txt")
self.assertRegex(response, "deleted ... test.delete_filename_length.cc.txt")
self.assertRegex(response, "aaaaaa.txt ... deleted")
self.assertRegex(response, "deleted ... test.delete_filename_length.ee.txt")
self.assertRegex(response, "aaaaaa.txt ... already deleted")
self.assertRegex(response, "failed to delete 1 duplicates - rerun script")
def test_do_not_process_file_of_unique_byte_size(self):
response = dff("test/different_file_sizes")
self.assertNotRegex(response, "Processing file")
def test_files_with_non_unique_file_size_added_to_process_list(self):
response = dff("test/one_large_almost_duplicate")
self.assertRegex(response, "big bbb.txt added to process list")
self.assertRegex(response, "big aaa.txt added to process list")
self.assertRegex(response, "has non unique")
def test_known_duplicates_not_added_to_list_multiple_times(self):
response = dff("test/two_small_duplicates")
self.assertRegex(response, "aaa.txt is a known size duplicate")
self.assertRegex(response, "aaa.txt added to process list")
self.assertRegex(response, "bbb.txt added to process list")
self.assertRegex(response, "ccc.txt added to process list")
def test_ignore_zero_byte_files(self):
response = dff("test/two_zero_byte_files")
self.assertNotRegex(response, "Processing file")
self.assertNotRegex(response, "is dupe of")
if __name__ == "__main__":
one_time_setup()
unittest.main()