Debian Codegraph @

tests/test-manifest.py

raw
1 import binascii
2 import itertools
3 import silenttestrunner
4 import unittest
5 import zlib
6
7 from mercurial.node import sha1nodeconstants
8
9 from mercurial import (
10 manifest as manifestmod,
11 match as matchmod,
12 util,
13 )
14
15 EMTPY_MANIFEST = b''
16
17 HASH_1 = b'1' * 40
18 BIN_HASH_1 = binascii.unhexlify(HASH_1)
19 HASH_2 = b'f' * 40
20 BIN_HASH_2 = binascii.unhexlify(HASH_2)
21 HASH_3 = b'1234567890abcdef0987654321deadbeef0fcafe'
22 BIN_HASH_3 = binascii.unhexlify(HASH_3)
23 A_SHORT_MANIFEST = (
24 b'bar/baz/qux.py\0%(hash2)s%(flag2)s\n' b'foo\0%(hash1)s%(flag1)s\n'
25 ) % {
26 b'hash1': HASH_1,
27 b'flag1': b'',
28 b'hash2': HASH_2,
29 b'flag2': b'l',
30 }
31
32 A_DEEPER_MANIFEST = (
33 b'a/b/c/bar.py\0%(hash3)s%(flag1)s\n'
34 b'a/b/c/bar.txt\0%(hash1)s%(flag1)s\n'
35 b'a/b/c/foo.py\0%(hash3)s%(flag1)s\n'
36 b'a/b/c/foo.txt\0%(hash2)s%(flag2)s\n'
37 b'a/b/d/baz.py\0%(hash3)s%(flag1)s\n'
38 b'a/b/d/qux.py\0%(hash1)s%(flag2)s\n'
39 b'a/b/d/ten.txt\0%(hash3)s%(flag2)s\n'
40 b'a/b/dog.py\0%(hash3)s%(flag1)s\n'
41 b'a/b/fish.py\0%(hash2)s%(flag1)s\n'
42 b'a/c/london.py\0%(hash3)s%(flag2)s\n'
43 b'a/c/paper.txt\0%(hash2)s%(flag2)s\n'
44 b'a/c/paris.py\0%(hash2)s%(flag1)s\n'
45 b'a/d/apple.py\0%(hash3)s%(flag1)s\n'
46 b'a/d/pizza.py\0%(hash3)s%(flag2)s\n'
47 b'a/green.py\0%(hash1)s%(flag2)s\n'
48 b'a/purple.py\0%(hash2)s%(flag1)s\n'
49 b'app.py\0%(hash3)s%(flag1)s\n'
50 b'readme.txt\0%(hash2)s%(flag1)s\n'
51 ) % {
52 b'hash1': HASH_1,
53 b'flag1': b'',
54 b'hash2': HASH_2,
55 b'flag2': b'l',
56 b'hash3': HASH_3,
57 }
58
59 HUGE_MANIFEST_ENTRIES = 200001
60
61 A_HUGE_MANIFEST = b''.join(
62 sorted(
63 b'file%d\0%s%s\n' % (i, h, f)
64 for i, h, f in zip(
65 range(200001),
66 itertools.cycle((HASH_1, HASH_2)),
67 itertools.cycle((b'', b'x', b'l')),
68 )
69 )
70 )
71
72
73 class basemanifesttests:
74 def parsemanifest(self, text):
75 raise NotImplementedError('parsemanifest not implemented by test case')
76
77 def testEmptyManifest(self):
78 m = self.parsemanifest(20, EMTPY_MANIFEST)
79 self.assertEqual(0, len(m))
80 self.assertEqual([], list(m))
81
82 def testManifest(self):
83 m = self.parsemanifest(20, A_SHORT_MANIFEST)
84 self.assertEqual([b'bar/baz/qux.py', b'foo'], list(m))
85 self.assertEqual(BIN_HASH_2, m[b'bar/baz/qux.py'])
86 self.assertEqual(b'l', m.flags(b'bar/baz/qux.py'))
87 self.assertEqual(BIN_HASH_1, m[b'foo'])
88 self.assertEqual(b'', m.flags(b'foo'))
89 with self.assertRaises(KeyError):
90 m[b'wat']
91
92 def testSetItem(self):
93 want = BIN_HASH_1
94
95 m = self.parsemanifest(20, EMTPY_MANIFEST)
96 m[b'a'] = want
97 self.assertIn(b'a', m)
98 self.assertEqual(want, m[b'a'])
99 self.assertEqual(b'a\0' + HASH_1 + b'\n', m.text())
100
101 m = self.parsemanifest(20, A_SHORT_MANIFEST)
102 m[b'a'] = want
103 self.assertEqual(want, m[b'a'])
104 self.assertEqual(b'a\0' + HASH_1 + b'\n' + A_SHORT_MANIFEST, m.text())
105
106 def testSetFlag(self):
107 want = b'x'
108
109 m = self.parsemanifest(20, EMTPY_MANIFEST)
110 # first add a file; a file-less flag makes no sense
111 m[b'a'] = BIN_HASH_1
112 m.setflag(b'a', want)
113 self.assertEqual(want, m.flags(b'a'))
114 self.assertEqual(b'a\0' + HASH_1 + want + b'\n', m.text())
115
116 m = self.parsemanifest(20, A_SHORT_MANIFEST)
117 # first add a file; a file-less flag makes no sense
118 m[b'a'] = BIN_HASH_1
119 m.setflag(b'a', want)
120 self.assertEqual(want, m.flags(b'a'))
121 self.assertEqual(
122 b'a\0' + HASH_1 + want + b'\n' + A_SHORT_MANIFEST, m.text()
123 )
124
125 def testCopy(self):
126 m = self.parsemanifest(20, A_SHORT_MANIFEST)
127 m[b'a'] = BIN_HASH_1
128 m2 = m.copy()
129 del m
130 del m2 # make sure we don't double free() anything
131
132 def testCompaction(self):
133 unhex = binascii.unhexlify
134 h1, h2 = unhex(HASH_1), unhex(HASH_2)
135 m = self.parsemanifest(20, A_SHORT_MANIFEST)
136 m[b'alpha'] = h1
137 m[b'beta'] = h2
138 del m[b'foo']
139 want = b'alpha\0%s\nbar/baz/qux.py\0%sl\nbeta\0%s\n' % (
140 HASH_1,
141 HASH_2,
142 HASH_2,
143 )
144 self.assertEqual(want, m.text())
145 self.assertEqual(3, len(m))
146 self.assertEqual([b'alpha', b'bar/baz/qux.py', b'beta'], list(m))
147 self.assertEqual(h1, m[b'alpha'])
148 self.assertEqual(h2, m[b'bar/baz/qux.py'])
149 self.assertEqual(h2, m[b'beta'])
150 self.assertEqual(b'', m.flags(b'alpha'))
151 self.assertEqual(b'l', m.flags(b'bar/baz/qux.py'))
152 self.assertEqual(b'', m.flags(b'beta'))
153 with self.assertRaises(KeyError):
154 m[b'foo']
155
156 def testMatchException(self):
157 m = self.parsemanifest(20, A_SHORT_MANIFEST)
158 match = matchmod.match(util.localpath(b'/repo'), b'', [b're:.*'])
159
160 def filt(path):
161 if path == b'foo':
162 assert False
163 return True
164
165 match.matchfn = filt
166 with self.assertRaises(AssertionError):
167 m._matches(match)
168
169 def testRemoveItem(self):
170 m = self.parsemanifest(20, A_SHORT_MANIFEST)
171 del m[b'foo']
172 with self.assertRaises(KeyError):
173 m[b'foo']
174 self.assertEqual(1, len(m))
175 self.assertEqual(1, len(list(m)))
176 # now restore and make sure everything works right
177 m[b'foo'] = b'a' * 20
178 self.assertEqual(2, len(m))
179 self.assertEqual(2, len(list(m)))
180
181 def testManifestDiff(self):
182 MISSING = (None, b'')
183 addl = b'z-only-in-left\0' + HASH_1 + b'\n'
184 addr = b'z-only-in-right\0' + HASH_2 + b'x\n'
185 left = self.parsemanifest(
186 20, A_SHORT_MANIFEST.replace(HASH_1, HASH_3 + b'x') + addl
187 )
188 right = self.parsemanifest(20, A_SHORT_MANIFEST + addr)
189 want = {
190 b'foo': ((BIN_HASH_3, b'x'), (BIN_HASH_1, b'')),
191 b'z-only-in-left': ((BIN_HASH_1, b''), MISSING),
192 b'z-only-in-right': (MISSING, (BIN_HASH_2, b'x')),
193 }
194 self.assertEqual(want, left.diff(right))
195
196 want = {
197 b'bar/baz/qux.py': (MISSING, (BIN_HASH_2, b'l')),
198 b'foo': (MISSING, (BIN_HASH_3, b'x')),
199 b'z-only-in-left': (MISSING, (BIN_HASH_1, b'')),
200 }
201 self.assertEqual(
202 want, self.parsemanifest(20, EMTPY_MANIFEST).diff(left)
203 )
204
205 want = {
206 b'bar/baz/qux.py': ((BIN_HASH_2, b'l'), MISSING),
207 b'foo': ((BIN_HASH_3, b'x'), MISSING),
208 b'z-only-in-left': ((BIN_HASH_1, b''), MISSING),
209 }
210 self.assertEqual(
211 want, left.diff(self.parsemanifest(20, EMTPY_MANIFEST))
212 )
213 copy = right.copy()
214 del copy[b'z-only-in-right']
215 del right[b'foo']
216 want = {
217 b'foo': (MISSING, (BIN_HASH_1, b'')),
218 b'z-only-in-right': ((BIN_HASH_2, b'x'), MISSING),
219 }
220 self.assertEqual(want, right.diff(copy))
221
222 short = self.parsemanifest(20, A_SHORT_MANIFEST)
223 pruned = short.copy()
224 del pruned[b'foo']
225 want = {
226 b'foo': ((BIN_HASH_1, b''), MISSING),
227 }
228 self.assertEqual(want, short.diff(pruned))
229 want = {
230 b'foo': (MISSING, (BIN_HASH_1, b'')),
231 }
232 self.assertEqual(want, pruned.diff(short))
233 want = {
234 b'bar/baz/qux.py': None,
235 b'foo': (MISSING, (BIN_HASH_1, b'')),
236 }
237 self.assertEqual(want, pruned.diff(short, clean=True))
238
239 def testReversedLines(self):
240 backwards = b''.join(
241 l + b'\n' for l in reversed(A_SHORT_MANIFEST.split(b'\n')) if l
242 )
243 try:
244 self.parsemanifest(20, backwards)
245 self.fail('Should have raised ValueError')
246 except ValueError as v:
247 self.assertIn('Manifest lines not in sorted order.', str(v))
248
249 def testNoTerminalNewline(self):
250 try:
251 self.parsemanifest(20, A_SHORT_MANIFEST + b'wat')
252 self.fail('Should have raised ValueError')
253 except ValueError as v:
254 self.assertIn('Manifest did not end in a newline.', str(v))
255
256 def testNoNewLineAtAll(self):
257 try:
258 self.parsemanifest(20, b'wat')
259 self.fail('Should have raised ValueError')
260 except ValueError as v:
261 self.assertIn('Manifest did not end in a newline.', str(v))
262
263 def testHugeManifest(self):
264 m = self.parsemanifest(20, A_HUGE_MANIFEST)
265 self.assertEqual(HUGE_MANIFEST_ENTRIES, len(m))
266 self.assertEqual(len(m), len(list(m)))
267
268 def testMatchesMetadata(self):
269 """Tests matches() for a few specific files to make sure that both
270 the set of files as well as their flags and nodeids are correct in
271 the resulting manifest."""
272 m = self.parsemanifest(20, A_HUGE_MANIFEST)
273
274 match = matchmod.exact([b'file1', b'file200', b'file300'])
275 m2 = m._matches(match)
276
277 w = (b'file1\0%sx\n' b'file200\0%sl\n' b'file300\0%s\n') % (
278 HASH_2,
279 HASH_1,
280 HASH_1,
281 )
282 self.assertEqual(w, m2.text())
283
284 def testMatchesNonexistentFile(self):
285 """Tests matches() for a small set of specific files, including one
286 nonexistent file to make sure in only matches against existing files.
287 """
288 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
289
290 match = matchmod.exact(
291 [b'a/b/c/bar.txt', b'a/b/d/qux.py', b'readme.txt', b'nonexistent']
292 )
293 m2 = m._matches(match)
294
295 self.assertEqual(
296 [b'a/b/c/bar.txt', b'a/b/d/qux.py', b'readme.txt'], m2.keys()
297 )
298
299 def testMatchesNonexistentDirectory(self):
300 """Tests matches() for a relpath match on a directory that doesn't
301 actually exist."""
302 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
303
304 match = matchmod.match(
305 util.localpath(b'/repo'), b'', [b'a/f'], default=b'relpath'
306 )
307 m2 = m._matches(match)
308
309 self.assertEqual([], m2.keys())
310
311 def testMatchesExactLarge(self):
312 """Tests matches() for files matching a large list of exact files."""
313 m = self.parsemanifest(20, A_HUGE_MANIFEST)
314
315 flist = m.keys()[80:300]
316 match = matchmod.exact(flist)
317 m2 = m._matches(match)
318
319 self.assertEqual(flist, m2.keys())
320
321 def testMatchesFull(self):
322 '''Tests matches() for what should be a full match.'''
323 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
324
325 match = matchmod.match(util.localpath(b'/repo'), b'', [b''])
326 m2 = m._matches(match)
327
328 self.assertEqual(m.keys(), m2.keys())
329
330 def testMatchesDirectory(self):
331 """Tests matches() on a relpath match on a directory, which should
332 match against all files within said directory."""
333 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
334
335 match = matchmod.match(
336 util.localpath(b'/repo'), b'', [b'a/b'], default=b'relpath'
337 )
338 m2 = m._matches(match)
339
340 self.assertEqual(
341 [
342 b'a/b/c/bar.py',
343 b'a/b/c/bar.txt',
344 b'a/b/c/foo.py',
345 b'a/b/c/foo.txt',
346 b'a/b/d/baz.py',
347 b'a/b/d/qux.py',
348 b'a/b/d/ten.txt',
349 b'a/b/dog.py',
350 b'a/b/fish.py',
351 ],
352 m2.keys(),
353 )
354
355 def testMatchesExactPath(self):
356 """Tests matches() on an exact match on a directory, which should
357 result in an empty manifest because you can't perform an exact match
358 against a directory."""
359 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
360
361 match = matchmod.exact([b'a/b'])
362 m2 = m._matches(match)
363
364 self.assertEqual([], m2.keys())
365
366 def testMatchesCwd(self):
367 """Tests matches() on a relpath match with the current directory ('.')
368 when not in the root directory."""
369 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
370
371 match = matchmod.match(
372 util.localpath(b'/repo'), b'a/b', [b'.'], default=b'relpath'
373 )
374 m2 = m._matches(match)
375
376 self.assertEqual(
377 [
378 b'a/b/c/bar.py',
379 b'a/b/c/bar.txt',
380 b'a/b/c/foo.py',
381 b'a/b/c/foo.txt',
382 b'a/b/d/baz.py',
383 b'a/b/d/qux.py',
384 b'a/b/d/ten.txt',
385 b'a/b/dog.py',
386 b'a/b/fish.py',
387 ],
388 m2.keys(),
389 )
390
391 def testMatchesWithPattern(self):
392 """Tests matches() for files matching a pattern that reside
393 deeper than the specified directory."""
394 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
395
396 match = matchmod.match(util.localpath(b'/repo'), b'', [b'a/b/*/*.txt'])
397 m2 = m._matches(match)
398
399 self.assertEqual(
400 [b'a/b/c/bar.txt', b'a/b/c/foo.txt', b'a/b/d/ten.txt'], m2.keys()
401 )
402
403
404 class testmanifestdict(unittest.TestCase, basemanifesttests):
405 def parsemanifest(self, nodelen, text):
406 return manifestmod.manifestdict(nodelen, text)
407
408 def testManifestLongHashes(self):
409 m = self.parsemanifest(32, b'a\0' + b'f' * 64 + b'\n')
410 self.assertEqual(binascii.unhexlify(b'f' * 64), m[b'a'])
411
412 def testObviouslyBogusManifest(self):
413 # This is a 163k manifest that came from oss-fuzz. It was a
414 # timeout there, but when run normally it doesn't seem to
415 # present any particular slowness.
416 data = zlib.decompress(
417 b'x\x9c\xed\xce;\n\x83\x00\x10\x04\xd0\x8deNa\x93~\xf1\x03\xc9q\xf4'
418 b'\x14\xeaU\xbdB\xda\xd4\xe6Cj\xc1FA\xde+\x86\xe9f\xa2\xfci\xbb\xfb'
419 b'\xa3\xef\xea\xba\xca\x7fk\x86q\x9a\xc6\xc8\xcc&\xb3\xcf\xf8\xb8|#'
420 b'\x8a9\x00\xd8\xe6v\xf4\x01N\xe1\n\x00\x00\x00\x00\x00\x00\x00\x00'
421 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
422 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
423 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
424 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
425 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
426 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
427 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
428 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
429 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
430 b'\x00\x00\xc0\x8aey\x1d}\x01\xd8\xe0\xb9\xf3\xde\x1b\xcf\x17'
431 b'\xac\xbe'
432 )
433 with self.assertRaises(ValueError):
434 self.parsemanifest(20, data)
435
436
437 class testtreemanifest(unittest.TestCase, basemanifesttests):
438 def parsemanifest(self, nodelen, text):
439 return manifestmod.treemanifest(sha1nodeconstants, b'', text)
440
441 def testWalkSubtrees(self):
442 m = self.parsemanifest(20, A_DEEPER_MANIFEST)
443
444 dirs = [s._dir for s in m.walksubtrees()]
445 self.assertEqual(
446 sorted(
447 [b'', b'a/', b'a/c/', b'a/d/', b'a/b/', b'a/b/c/', b'a/b/d/']
448 ),
449 sorted(dirs),
450 )
451
452 match = matchmod.match(util.localpath(b'/repo'), b'', [b'path:a/b/'])
453 dirs = [s._dir for s in m.walksubtrees(matcher=match)]
454 self.assertEqual(sorted([b'a/b/', b'a/b/c/', b'a/b/d/']), sorted(dirs))
455
456
457 if __name__ == '__main__':
458 silenttestrunner.main(__name__)