/
test_Manifest_walk.py
324 lines (276 loc) · 9.96 KB
/
test_Manifest_walk.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import unittest
from manifest import Manifest
from manifest_file import ManifestFileParser
from manifest_dir import ManifestDirWalker
from test_utils import unpacked_tar, Manifest_from_walking_unpacked_tar
class Test_Manifest_walk_visits_all_paths(unittest.TestCase):
def check_visited_paths(self, tar_path, expect):
m = Manifest_from_walking_unpacked_tar(tar_path)
self.assertEqual(["/".join(path) for path, entries, attrs in m.walk()],
expect)
def test_empty(self):
self.check_visited_paths("empty.tar", [""])
def test_single_file(self):
self.check_visited_paths("single_file.tar", ["", "foo"])
def test_two_files(self):
self.check_visited_paths("two_files.tar", ["", "bar", "foo"])
def test_file_and_empty_subdir(self):
self.check_visited_paths("file_and_empty_subdir.tar",
["", "file", "subdir"])
def test_file_and_subdir(self):
self.check_visited_paths("file_and_subdir.tar",
["", "file", "subdir", "subdir/foo"])
def test_file_and_subdir_trailing_slash(self):
with unpacked_tar("file_and_subdir.tar") as d:
m = ManifestDirWalker().build(d + "/")
self.assertEqual(["/".join(path) for path, entries, attrs in m.walk()],
["", "file", "subdir", "subdir/foo"])
def test_files_at_many_levels(self):
self.check_visited_paths("files_at_many_levels.tar", [
"",
"bar",
"baz",
"baz/bar",
"baz/baz",
"baz/baz/bar",
"baz/baz/baz",
"baz/baz/foo",
"baz/foo",
"foo",
])
def test_nonrecursive(self):
m = ManifestFileParser().build("""\
foo
child1
child2
bar
baz
child3
""".split("\n"))
result = []
for path, names, attrs in m.walk():
result.append("/".join(path))
del names[:]
self.assertEqual(result, [""])
result = []
for path, names, attrs in m.walk():
result.append("/".join(path))
if path:
del names[:]
self.assertEqual(result, ["", "bar", "baz", "foo"])
class Test_Manifest_walk_selective_recurse(unittest.TestCase):
def setUp(self):
self.m = ManifestFileParser().build("""\
a
a
a
b
c
b
a
b
c
c
a
b
c
b
a
a
b
c
b
a
b
c
c
a
b
c
c
a
a
b
c
b
a
b
c
c
a
b
c
""".split("\n"))
self.all = ["",
"a", "a/a", "a/a/a", "a/a/b", "a/a/c",
"a/b", "a/b/a", "a/b/b", "a/b/c",
"a/c", "a/c/a", "a/c/b", "a/c/c",
"b", "b/a", "b/a/a", "b/a/b", "b/a/c",
"b/b", "b/b/a", "b/b/b", "b/b/c",
"b/c", "b/c/a", "b/c/b", "b/c/c",
"c", "c/a", "c/a/a", "c/a/b", "c/a/c",
"c/b", "c/b/a", "c/b/b", "c/b/c",
"c/c", "c/c/a", "c/c/b", "c/c/c"]
def check_paths(self, pred, expect):
actual = []
for path, names, attrs in self.m.walk():
if pred(path, names):
actual.append("/".join(path))
self.assertEqual(actual, expect)
def test_all(self):
def modifier(path, names):
return True
self.check_paths(modifier, self.all)
def test_none(self):
def modifier(path, names):
return False
self.check_paths(modifier, [])
def test_first(self):
def modifier(path, names):
del names[:]
return True
self.check_paths(modifier, [""])
def modifier(path, names):
return not path
self.check_paths(modifier, [""])
def test_not_first(self):
def modifier(path, names):
return path
self.check_paths(modifier, self.all[1:])
def test_no_a(self):
expect = [
"", "b", "b/b", "b/b/b", "b/b/c", "b/c", "b/c/b", "b/c/c",
"c", "c/b", "c/b/b", "c/b/c", "c/c", "c/c/b", "c/c/c"]
def modifier(path, names):
try:
del names[names.index("a")]
except:
pass
return True
self.check_paths(modifier, expect)
def modifier(path, names):
return "a" not in path
self.check_paths(modifier, expect)
def test_only_a(self):
expect = ["", "a", "a/a", "a/a/a"]
def modifier(path, names):
names[:] = ["a"] if "a" in names else []
return True
self.check_paths(modifier, expect)
def modifier(path, names):
return not any(True for p in path if p != "a")
self.check_paths(modifier, expect)
def test_starts_with_a(self):
expect = [
"a", "a/a", "a/a/a", "a/a/b", "a/a/c",
"a/b", "a/b/a", "a/b/b", "a/b/c",
"a/c", "a/c/a", "a/c/b", "a/c/c"]
def modifier(path, names):
if not path:
names[:] = ["a"] if "a" in names else []
return False
else:
return True
self.check_paths(modifier, expect)
def modifier(path, names):
return path and path[0] == "a"
self.check_paths(modifier, expect)
def test_ends_with_a(self):
expect = [
"a", "a/a", "a/a/a", "a/b/a", "a/c/a",
"b/a", "b/a/a", "b/b/a", "b/c/a",
"c/a", "c/a/a", "c/b/a", "c/c/a"]
def modifier(path, names):
return path and path[-1] == "a"
self.check_paths(modifier, expect)
class Test_Manifest_walk_visits_all_paths_w_attrs(unittest.TestCase):
empty_sha1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
empty_attrs = { "size": 0, "sha1": empty_sha1 }
def check_visited_paths(self, tar_path, expect):
m = Manifest_from_walking_unpacked_tar(tar_path, ['size', 'sha1'])
self.assertEqual(
[("/".join(path), attrs) for path, entries, attrs in m.walk()],
expect)
def test_empty(self):
self.check_visited_paths("empty.tar", [("", {})])
def test_single_file(self):
self.check_visited_paths("single_file.tar",
[("", {}), ("foo", self.empty_attrs)])
def test_two_files(self):
self.check_visited_paths("two_files.tar", [
("", {}), ("bar", self.empty_attrs), ("foo", self.empty_attrs)])
def test_file_and_empty_subdir(self):
self.check_visited_paths("file_and_empty_subdir.tar", [
("", {}), ("file", self.empty_attrs), ("subdir", {})])
def test_file_and_subdir(self):
self.check_visited_paths("file_and_subdir.tar", [
("", {}), ("file", self.empty_attrs), ("subdir", {}),
("subdir/foo", self.empty_attrs)])
def test_files_at_many_levels(self):
self.check_visited_paths("files_at_many_levels.tar", [
("", {}),
("bar", self.empty_attrs),
("baz", {}),
("baz/bar", self.empty_attrs),
("baz/baz", {}),
("baz/baz/bar", self.empty_attrs),
("baz/baz/baz", self.empty_attrs),
("baz/baz/foo", self.empty_attrs),
("baz/foo", self.empty_attrs),
("foo", self.empty_attrs),
])
def test_files_with_contents(self):
self.check_visited_paths("files_with_contents.tar", [
("", {}),
("bar", {}),
("bar/baz", {
"size": 12,
"sha1": "7508a86c26bcda1d3f298f67de33f7c48a3fe047"}),
("foo", {
"size": 12,
"sha1": "fc6da897c87c7b9c3b67d1d5af32085e561db793"}),
("symlink_to_bar_baz", {}),
])
class Test_Manifest_paths(unittest.TestCase):
def must_equal(self, tar_path, expect):
m = Manifest_from_walking_unpacked_tar(tar_path)
self.assertEqual(list(m.paths()), expect)
def test_empty(self):
self.must_equal("empty.tar", [])
def test_single_file(self):
self.must_equal("single_file.tar", ["foo"])
def test_two_files(self):
self.must_equal("two_files.tar", ["bar", "foo"])
def test_file_and_empty_subdir(self):
self.must_equal("file_and_empty_subdir.tar", ["file", "subdir"])
def test_file_and_subdir(self):
self.must_equal("file_and_subdir.tar", ["file", "subdir", "subdir/foo"])
def test_file_and_subdir_trailing_slash(self):
with unpacked_tar("file_and_subdir.tar") as d:
m = ManifestDirWalker().build(d + "/")
self.assertEqual(list(m.paths()), ["file", "subdir", "subdir/foo"])
def test_files_at_many_levels(self):
self.must_equal("files_at_many_levels.tar", [
"bar",
"baz",
"baz/bar",
"baz/baz",
"baz/baz/bar",
"baz/baz/baz",
"baz/baz/foo",
"baz/foo",
"foo",
])
def test_nonrecursive(self):
m = ManifestFileParser().build("""\
foo
child1
child2
bar
baz
child3
""".split("\n"))
self.assertEqual(list(m.paths(recursive = False)),
["bar", "baz", "foo"])
if __name__ == '__main__':
unittest.main()