-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
137 lines (98 loc) · 5.14 KB
/
test.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
import ban
import io
import unittest
def get_entry_equality_func(obj):
"""The comparator function is used to compare the ban.Entry objects"""
def comparator(leftEntry, rightEntry, msg=None):
if leftEntry.get_sha() != rightEntry.get_sha():
raise obj.failureException(f'SHA "{leftEntry.get_sha()}" != "{rightEntry.get_sha()}"')
if leftEntry.get_path() != rightEntry.get_path():
raise obj.failureException(f'path "{leftEntry.getPath()}" != "{rightEntry.getPath()}"')
return comparator
class TestEquality(unittest.TestCase):
"""Test the comparator function from this test suite"""
def setUp(self):
self.addTypeEqualityFunc(ban.Entry, get_entry_equality_func(self))
def testEqual(self):
left = ban.Entry('123', '/path')
right = ban.Entry('123', '/path')
self.assertEqual(left, right)
def testUnEqual(self):
left = ban.Entry('12', '/pa')
right = ban.Entry('123', '/path')
self.assertNotEqual(left, right)
class TestFiltering(unittest.TestCase):
def testAllAllowed(self):
pathToExclude = ['./some/path']
entries = [ban.Entry('123', './path1'),
ban.Entry('234', './path2')]
result = ban.filter_entries(entries, pathToExclude)
self.assertEqual(len(result), 2)
self.assertEqual(result[0].get_sha(), '123')
self.assertEqual(result[0].get_path(), './path1')
self.assertEqual(result[1].get_sha(), '234')
self.assertEqual(result[1].get_path(), './path2')
def testFilterOutSome(self):
pathToExclude = ['./some/path']
entries = [ban.Entry('123', './path1'), # stays
ban.Entry('234', './some/path'), # removed
ban.Entry('567', './path3'), # stays
ban.Entry('890', './some/path/1'), # removed
ban.Entry('901', './some/path/'), # removed
ban.Entry('251', './some/path/file'), # removed
ban.Entry('982', './some/other')] # stays
result = ban.filter_entries(entries, pathToExclude)
self.assertEqual(len(result), 3)
self.assertEqual(result[0].get_sha(), '123')
self.assertEqual(result[0].get_path(), './path1')
self.assertEqual(result[1].get_sha(), '567')
self.assertEqual(result[1].get_path(), './path3')
self.assertEqual(result[2].get_sha(), '982')
self.assertEqual(result[2].get_path(), './some/other')
class TestAppleDoubleeFilesFiltering(unittest.TestCase):
def teastAllAllowed(self):
entries = [ban.Entry('123', './path1'),
ban.Entry('234', './p[ath2')]
result = ban.filter_out_apple_doubles(entries)
self.assertEqual(len(result), 2)
self.assertEqual(result[0].get_sha(), '123')
self.assertEqual(result[0].get_path(), './path1')
self.assertEqual(result[1].get_sha(), '234')
self.assertEqual(result[1].get_path(), './path2')
def testFilterOutSome(self):
entries = [ban.Entry('123', './path1'),
ban.Entry('143', './._path1'),
ban.Entry('345', './some/path.jpg'),
ban.Entry('945', './some/._path.jpg'),
ban.Entry('903', './other/._file.dng')]
result = ban.filter_out_apple_doubles(entries)
self.assertEqual(len(result), 2)
self.assertEqual(result[0].get_sha(), '123')
self.assertEqual(result[0].get_path(), './path1')
self.assertEqual(result[1].get_sha(), '345')
self.assertEqual(result[1].get_path(), './some/path.jpg')
class TestRead(unittest.TestCase):
def setUp(self):
self.addTypeEqualityFunc(ban.Entry, get_entry_equality_func(self))
def testReadNormal(self):
"""Test that a normal line is read just fine"""
buf = io.StringIO('0123456789012345678901234567890123456789012345678901234567890123 /path/to/file')
entries = ban.read_entries(buf)
expected = ban.Entry('0123456789012345678901234567890123456789012345678901234567890123', '/path/to/file')
self.assertEqual(entries[0], expected)
def testReadWithSpaces(self):
"""Test that paths with space are read properly"""
buf = io.StringIO('0123456789012345678901234567890123456789012345678901234567890123 /path/to/file with spaces')
entries = ban.read_entries(buf)
expected = ban.Entry('0123456789012345678901234567890123456789012345678901234567890123', '/path/to/file with spaces')
self.assertEqual(entries[0], expected)
def testReadTwoSpaces(self):
'''
Two spaces are the separator between the hash and the file path.
Emited by sha256sum, two spaces mean that the file was read in a text mode,
as opposed to the * separator used for binary reading mode.
'''
buf = io.StringIO('0123456789012345678901234567890123456789012345678901234567890123 /path/to/file with spaces')
entries = ban.read_entries(buf)
expected = ban.Entry('0123456789012345678901234567890123456789012345678901234567890123', '/path/to/file with spaces')
self.assertEqual(entries[0], expected)