Show More
@@ -1,266 +1,389 b'' | |||
|
1 | 1 | import binascii |
|
2 | 2 | import unittest |
|
3 | 3 | import itertools |
|
4 | 4 | |
|
5 | 5 | import silenttestrunner |
|
6 | 6 | |
|
7 | 7 | from mercurial import manifest as manifestmod |
|
8 | 8 | from mercurial import match as matchmod |
|
9 | 9 | |
|
10 | 10 | HASH_1 = '1' * 40 |
|
11 | 11 | HASH_2 = 'f' * 40 |
|
12 | 12 | HASH_3 = '1234567890abcdef0987654321deadbeef0fcafe' |
|
13 | 13 | A_SHORT_MANIFEST = ( |
|
14 | 14 | 'bar/baz/qux.py\0%(hash2)s%(flag2)s\n' |
|
15 | 15 | 'foo\0%(hash1)s%(flag1)s\n' |
|
16 | 16 | ) % {'hash1': HASH_1, |
|
17 | 17 | 'flag1': '', |
|
18 | 18 | 'hash2': HASH_2, |
|
19 | 19 | 'flag2': 'l', |
|
20 | 20 | } |
|
21 | 21 | |
|
22 | A_DEEPER_MANIFEST = ( | |
|
23 | 'a/b/c/bar.py\0%(hash3)s%(flag1)s\n' | |
|
24 | 'a/b/c/bar.txt\0%(hash1)s%(flag1)s\n' | |
|
25 | 'a/b/c/foo.py\0%(hash3)s%(flag1)s\n' | |
|
26 | 'a/b/c/foo.txt\0%(hash2)s%(flag2)s\n' | |
|
27 | 'a/b/d/baz.py\0%(hash3)s%(flag1)s\n' | |
|
28 | 'a/b/d/qux.py\0%(hash1)s%(flag2)s\n' | |
|
29 | 'a/b/d/ten.txt\0%(hash3)s%(flag2)s\n' | |
|
30 | 'a/b/dog.py\0%(hash3)s%(flag1)s\n' | |
|
31 | 'a/b/fish.py\0%(hash2)s%(flag1)s\n' | |
|
32 | 'a/c/london.py\0%(hash3)s%(flag2)s\n' | |
|
33 | 'a/c/paper.txt\0%(hash2)s%(flag2)s\n' | |
|
34 | 'a/c/paris.py\0%(hash2)s%(flag1)s\n' | |
|
35 | 'a/d/apple.py\0%(hash3)s%(flag1)s\n' | |
|
36 | 'a/d/pizza.py\0%(hash3)s%(flag2)s\n' | |
|
37 | 'a/green.py\0%(hash1)s%(flag2)s\n' | |
|
38 | 'a/purple.py\0%(hash2)s%(flag1)s\n' | |
|
39 | 'app.py\0%(hash3)s%(flag1)s\n' | |
|
40 | 'readme.txt\0%(hash2)s%(flag1)s\n' | |
|
41 | ) % {'hash1': HASH_1, | |
|
42 | 'flag1': '', | |
|
43 | 'hash2': HASH_2, | |
|
44 | 'flag2': 'l', | |
|
45 | 'hash3': HASH_3, | |
|
46 | } | |
|
47 | ||
|
22 | 48 | HUGE_MANIFEST_ENTRIES = 200001 |
|
23 | 49 | |
|
24 | 50 | A_HUGE_MANIFEST = ''.join(sorted( |
|
25 | 51 | 'file%d\0%s%s\n' % (i, h, f) for i, h, f in |
|
26 | 52 | itertools.izip(xrange(200001), |
|
27 | 53 | itertools.cycle((HASH_1, HASH_2)), |
|
28 | 54 | itertools.cycle(('', 'x', 'l'))))) |
|
29 | 55 | |
|
30 | 56 | def parsemanifest(text): |
|
31 | 57 | return manifestmod.manifestdict(text) |
|
32 | 58 | |
|
33 | 59 | class testmanifest(unittest.TestCase): |
|
34 | 60 | |
|
35 | 61 | def assertIn(self, thing, container, msg=None): |
|
36 | 62 | # assertIn new in 2.7, use it if available, otherwise polyfill |
|
37 | 63 | sup = getattr(unittest.TestCase, 'assertIn', False) |
|
38 | 64 | if sup: |
|
39 | 65 | return sup(self, thing, container, msg=msg) |
|
40 | 66 | if not msg: |
|
41 | 67 | msg = 'Expected %r in %r' % (thing, container) |
|
42 | 68 | self.assert_(thing in container, msg) |
|
43 | 69 | |
|
44 | 70 | def testEmptyManifest(self): |
|
45 | 71 | m = parsemanifest('') |
|
46 | 72 | self.assertEqual(0, len(m)) |
|
47 | 73 | self.assertEqual([], list(m)) |
|
48 | 74 | |
|
49 | 75 | def testManifest(self): |
|
50 | 76 | m = parsemanifest(A_SHORT_MANIFEST) |
|
51 | 77 | self.assertEqual(['bar/baz/qux.py', 'foo'], list(m)) |
|
52 | 78 | self.assertEqual(binascii.unhexlify(HASH_2), m['bar/baz/qux.py']) |
|
53 | 79 | self.assertEqual('l', m.flags('bar/baz/qux.py')) |
|
54 | 80 | self.assertEqual(binascii.unhexlify(HASH_1), m['foo']) |
|
55 | 81 | self.assertEqual('', m.flags('foo')) |
|
56 | 82 | self.assertRaises(KeyError, lambda : m['wat']) |
|
57 | 83 | |
|
58 | 84 | def testSetItem(self): |
|
59 | 85 | want = binascii.unhexlify(HASH_1) |
|
60 | 86 | |
|
61 | 87 | m = parsemanifest('') |
|
62 | 88 | m['a'] = want |
|
63 | 89 | self.assertIn('a', m) |
|
64 | 90 | self.assertEqual(want, m['a']) |
|
65 | 91 | self.assertEqual('a\0' + HASH_1 + '\n', m.text()) |
|
66 | 92 | |
|
67 | 93 | m = parsemanifest(A_SHORT_MANIFEST) |
|
68 | 94 | m['a'] = want |
|
69 | 95 | self.assertEqual(want, m['a']) |
|
70 | 96 | self.assertEqual('a\0' + HASH_1 + '\n' + A_SHORT_MANIFEST, |
|
71 | 97 | m.text()) |
|
72 | 98 | |
|
73 | 99 | def testSetFlag(self): |
|
74 | 100 | want = 'x' |
|
75 | 101 | |
|
76 | 102 | m = parsemanifest('') |
|
77 | 103 | # first add a file; a file-less flag makes no sense |
|
78 | 104 | m['a'] = binascii.unhexlify(HASH_1) |
|
79 | 105 | m.setflag('a', want) |
|
80 | 106 | self.assertEqual(want, m.flags('a')) |
|
81 | 107 | self.assertEqual('a\0' + HASH_1 + want + '\n', m.text()) |
|
82 | 108 | |
|
83 | 109 | m = parsemanifest(A_SHORT_MANIFEST) |
|
84 | 110 | # first add a file; a file-less flag makes no sense |
|
85 | 111 | m['a'] = binascii.unhexlify(HASH_1) |
|
86 | 112 | m.setflag('a', want) |
|
87 | 113 | self.assertEqual(want, m.flags('a')) |
|
88 | 114 | self.assertEqual('a\0' + HASH_1 + want + '\n' + A_SHORT_MANIFEST, |
|
89 | 115 | m.text()) |
|
90 | 116 | |
|
91 | 117 | def testCopy(self): |
|
92 | 118 | m = parsemanifest(A_SHORT_MANIFEST) |
|
93 | 119 | m['a'] = binascii.unhexlify(HASH_1) |
|
94 | 120 | m2 = m.copy() |
|
95 | 121 | del m |
|
96 | 122 | del m2 # make sure we don't double free() anything |
|
97 | 123 | |
|
98 | 124 | def testCompaction(self): |
|
99 | 125 | unhex = binascii.unhexlify |
|
100 | 126 | h1, h2 = unhex(HASH_1), unhex(HASH_2) |
|
101 | 127 | m = parsemanifest(A_SHORT_MANIFEST) |
|
102 | 128 | m['alpha'] = h1 |
|
103 | 129 | m['beta'] = h2 |
|
104 | 130 | del m['foo'] |
|
105 | 131 | want = 'alpha\0%s\nbar/baz/qux.py\0%sl\nbeta\0%s\n' % ( |
|
106 | 132 | HASH_1, HASH_2, HASH_2) |
|
107 | 133 | self.assertEqual(want, m.text()) |
|
108 | 134 | self.assertEqual(3, len(m)) |
|
109 | 135 | self.assertEqual(['alpha', 'bar/baz/qux.py', 'beta'], list(m)) |
|
110 | 136 | self.assertEqual(h1, m['alpha']) |
|
111 | 137 | self.assertEqual(h2, m['bar/baz/qux.py']) |
|
112 | 138 | self.assertEqual(h2, m['beta']) |
|
113 | 139 | self.assertEqual('', m.flags('alpha')) |
|
114 | 140 | self.assertEqual('l', m.flags('bar/baz/qux.py')) |
|
115 | 141 | self.assertEqual('', m.flags('beta')) |
|
116 | 142 | self.assertRaises(KeyError, lambda : m['foo']) |
|
117 | 143 | |
|
118 | 144 | def testSetGetNodeSuffix(self): |
|
119 | 145 | clean = parsemanifest(A_SHORT_MANIFEST) |
|
120 | 146 | m = parsemanifest(A_SHORT_MANIFEST) |
|
121 | 147 | h = m['foo'] |
|
122 | 148 | f = m.flags('foo') |
|
123 | 149 | want = h + 'a' |
|
124 | 150 | # Merge code wants to set 21-byte fake hashes at times |
|
125 | 151 | m['foo'] = want |
|
126 | 152 | self.assertEqual(want, m['foo']) |
|
127 | 153 | self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2)), |
|
128 | 154 | ('foo', binascii.unhexlify(HASH_1) + 'a')], |
|
129 | 155 | list(m.iteritems())) |
|
130 | 156 | # Sometimes it even tries a 22-byte fake hash, but we can |
|
131 | 157 | # return 21 and it'll work out |
|
132 | 158 | m['foo'] = want + '+' |
|
133 | 159 | self.assertEqual(want, m['foo']) |
|
134 | 160 | # make sure the suffix survives a copy |
|
135 | 161 | match = matchmod.match('', '', ['re:foo']) |
|
136 | 162 | m2 = m.matches(match) |
|
137 | 163 | self.assertEqual(want, m2['foo']) |
|
138 | 164 | self.assertEqual(1, len(m2)) |
|
139 | 165 | m2 = m.copy() |
|
140 | 166 | self.assertEqual(want, m2['foo']) |
|
141 | 167 | # suffix with iteration |
|
142 | 168 | self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2)), |
|
143 | 169 | ('foo', want)], |
|
144 | 170 | list(m.iteritems())) |
|
145 | 171 | |
|
146 | 172 | # shows up in diff |
|
147 | 173 | self.assertEqual({'foo': ((want, f), (h, ''))}, m.diff(clean)) |
|
148 | 174 | self.assertEqual({'foo': ((h, ''), (want, f))}, clean.diff(m)) |
|
149 | 175 | |
|
150 | 176 | def testMatchException(self): |
|
151 | 177 | m = parsemanifest(A_SHORT_MANIFEST) |
|
152 | 178 | match = matchmod.match('', '', ['re:.*']) |
|
153 | 179 | def filt(path): |
|
154 | 180 | if path == 'foo': |
|
155 | 181 | assert False |
|
156 | 182 | return True |
|
157 | 183 | match.matchfn = filt |
|
158 | 184 | self.assertRaises(AssertionError, m.matches, match) |
|
159 | 185 | |
|
160 | 186 | def testRemoveItem(self): |
|
161 | 187 | m = parsemanifest(A_SHORT_MANIFEST) |
|
162 | 188 | del m['foo'] |
|
163 | 189 | self.assertRaises(KeyError, lambda : m['foo']) |
|
164 | 190 | self.assertEqual(1, len(m)) |
|
165 | 191 | self.assertEqual(1, len(list(m))) |
|
166 | 192 | # now restore and make sure everything works right |
|
167 | 193 | m['foo'] = 'a' * 20 |
|
168 | 194 | self.assertEqual(2, len(m)) |
|
169 | 195 | self.assertEqual(2, len(list(m))) |
|
170 | 196 | |
|
171 | 197 | def testManifestDiff(self): |
|
172 | 198 | MISSING = (None, '') |
|
173 | 199 | addl = 'z-only-in-left\0' + HASH_1 + '\n' |
|
174 | 200 | addr = 'z-only-in-right\0' + HASH_2 + 'x\n' |
|
175 | 201 | left = parsemanifest( |
|
176 | 202 | A_SHORT_MANIFEST.replace(HASH_1, HASH_3 + 'x') + addl) |
|
177 | 203 | right = parsemanifest(A_SHORT_MANIFEST + addr) |
|
178 | 204 | want = { |
|
179 | 205 | 'foo': ((binascii.unhexlify(HASH_3), 'x'), |
|
180 | 206 | (binascii.unhexlify(HASH_1), '')), |
|
181 | 207 | 'z-only-in-left': ((binascii.unhexlify(HASH_1), ''), MISSING), |
|
182 | 208 | 'z-only-in-right': (MISSING, (binascii.unhexlify(HASH_2), 'x')), |
|
183 | 209 | } |
|
184 | 210 | self.assertEqual(want, left.diff(right)) |
|
185 | 211 | |
|
186 | 212 | want = { |
|
187 | 213 | 'bar/baz/qux.py': (MISSING, (binascii.unhexlify(HASH_2), 'l')), |
|
188 | 214 | 'foo': (MISSING, (binascii.unhexlify(HASH_3), 'x')), |
|
189 | 215 | 'z-only-in-left': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
190 | 216 | } |
|
191 | 217 | self.assertEqual(want, parsemanifest('').diff(left)) |
|
192 | 218 | |
|
193 | 219 | want = { |
|
194 | 220 | 'bar/baz/qux.py': ((binascii.unhexlify(HASH_2), 'l'), MISSING), |
|
195 | 221 | 'foo': ((binascii.unhexlify(HASH_3), 'x'), MISSING), |
|
196 | 222 | 'z-only-in-left': ((binascii.unhexlify(HASH_1), ''), MISSING), |
|
197 | 223 | } |
|
198 | 224 | self.assertEqual(want, left.diff(parsemanifest(''))) |
|
199 | 225 | copy = right.copy() |
|
200 | 226 | del copy['z-only-in-right'] |
|
201 | 227 | del right['foo'] |
|
202 | 228 | want = { |
|
203 | 229 | 'foo': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
204 | 230 | 'z-only-in-right': ((binascii.unhexlify(HASH_2), 'x'), MISSING), |
|
205 | 231 | } |
|
206 | 232 | self.assertEqual(want, right.diff(copy)) |
|
207 | 233 | |
|
208 | 234 | short = parsemanifest(A_SHORT_MANIFEST) |
|
209 | 235 | pruned = short.copy() |
|
210 | 236 | del pruned['foo'] |
|
211 | 237 | want = { |
|
212 | 238 | 'foo': ((binascii.unhexlify(HASH_1), ''), MISSING), |
|
213 | 239 | } |
|
214 | 240 | self.assertEqual(want, short.diff(pruned)) |
|
215 | 241 | want = { |
|
216 | 242 | 'foo': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
217 | 243 | } |
|
218 | 244 | self.assertEqual(want, pruned.diff(short)) |
|
219 | 245 | want = { |
|
220 | 246 | 'bar/baz/qux.py': None, |
|
221 | 247 | 'foo': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
222 | 248 | } |
|
223 | 249 | self.assertEqual(want, pruned.diff(short, True)) |
|
224 | 250 | |
|
225 | 251 | def testReversedLines(self): |
|
226 | 252 | backwards = ''.join( |
|
227 | 253 | l + '\n' for l in reversed(A_SHORT_MANIFEST.split('\n')) if l) |
|
228 | 254 | try: |
|
229 | 255 | parsemanifest(backwards) |
|
230 | 256 | self.fail('Should have raised ValueError') |
|
231 | 257 | except ValueError, v: |
|
232 | 258 | self.assertIn('Manifest lines not in sorted order.', str(v)) |
|
233 | 259 | |
|
234 | 260 | def testNoTerminalNewline(self): |
|
235 | 261 | try: |
|
236 | 262 | parsemanifest(A_SHORT_MANIFEST + 'wat') |
|
237 | 263 | self.fail('Should have raised ValueError') |
|
238 | 264 | except ValueError, v: |
|
239 | 265 | self.assertIn('Manifest did not end in a newline.', str(v)) |
|
240 | 266 | |
|
241 | 267 | def testNoNewLineAtAll(self): |
|
242 | 268 | try: |
|
243 | 269 | parsemanifest('wat') |
|
244 | 270 | self.fail('Should have raised ValueError') |
|
245 | 271 | except ValueError, v: |
|
246 | 272 | self.assertIn('Manifest did not end in a newline.', str(v)) |
|
247 | 273 | |
|
248 | 274 | def testHugeManifest(self): |
|
249 | 275 | m = parsemanifest(A_HUGE_MANIFEST) |
|
250 | 276 | self.assertEqual(HUGE_MANIFEST_ENTRIES, len(m)) |
|
251 | 277 | self.assertEqual(len(m), len(list(m))) |
|
252 | 278 | |
|
253 | def testMatches(self): | |
|
279 | def testMatchesMetadata(self): | |
|
280 | '''Tests matches() for a few specific files to make sure that both | |
|
281 | the set of files as well as their flags and nodeids are correct in | |
|
282 | the resulting manifest.''' | |
|
254 | 283 | m = parsemanifest(A_HUGE_MANIFEST) |
|
255 | 284 | |
|
256 | 285 | match = matchmod.match('/', '', |
|
257 | 286 | ['file1', 'file200', 'file300'], exact=True) |
|
258 | 287 | m2 = m.matches(match) |
|
259 | 288 | |
|
260 | 289 | w = ('file1\0%sx\n' |
|
261 | 290 | 'file200\0%sl\n' |
|
262 | 291 | 'file300\0%s\n') % (HASH_2, HASH_1, HASH_1) |
|
263 | 292 | self.assertEqual(w, m2.text()) |
|
264 | 293 | |
|
294 | def testMatchesNonexistentFile(self): | |
|
295 | '''Tests matches() for a small set of specific files, including one | |
|
296 | nonexistent file to make sure in only matches against existing files. | |
|
297 | ''' | |
|
298 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
299 | ||
|
300 | match = matchmod.match('/', '', | |
|
301 | ['a/b/c/bar.txt', 'a/b/d/qux.py', 'readme.txt', 'nonexistent'], | |
|
302 | exact=True) | |
|
303 | m2 = m.matches(match) | |
|
304 | ||
|
305 | self.assertEqual( | |
|
306 | ['a/b/c/bar.txt', 'a/b/d/qux.py', 'readme.txt'], | |
|
307 | m2.keys()) | |
|
308 | ||
|
309 | def testMatchesNonexistentDirectory(self): | |
|
310 | '''Tests matches() for a relpath match on a directory that doesn't | |
|
311 | actually exist.''' | |
|
312 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
313 | ||
|
314 | match = matchmod.match('/', '', ['a/f'], default='relpath') | |
|
315 | m2 = m.matches(match) | |
|
316 | ||
|
317 | self.assertEqual([], m2.keys()) | |
|
318 | ||
|
319 | def testMatchesExactLarge(self): | |
|
320 | '''Tests matches() for files matching a large list of exact files. | |
|
321 | ''' | |
|
322 | m = parsemanifest(A_HUGE_MANIFEST) | |
|
323 | ||
|
324 | flist = m.keys()[80:300] | |
|
325 | match = matchmod.match('/', '', flist, exact=True) | |
|
326 | m2 = m.matches(match) | |
|
327 | ||
|
328 | self.assertEqual(flist, m2.keys()) | |
|
329 | ||
|
330 | def testMatchesFull(self): | |
|
331 | '''Tests matches() for what should be a full match.''' | |
|
332 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
333 | ||
|
334 | match = matchmod.match('/', '', ['']) | |
|
335 | m2 = m.matches(match) | |
|
336 | ||
|
337 | self.assertEqual(m.keys(), m2.keys()) | |
|
338 | ||
|
339 | def testMatchesDirectory(self): | |
|
340 | '''Tests matches() on a relpath match on a directory, which should | |
|
341 | match against all files within said directory.''' | |
|
342 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
343 | ||
|
344 | match = matchmod.match('/', '', ['a/b'], default='relpath') | |
|
345 | m2 = m.matches(match) | |
|
346 | ||
|
347 | self.assertEqual([ | |
|
348 | 'a/b/c/bar.py', 'a/b/c/bar.txt', 'a/b/c/foo.py', 'a/b/c/foo.txt', | |
|
349 | 'a/b/d/baz.py', 'a/b/d/qux.py', 'a/b/d/ten.txt', 'a/b/dog.py', | |
|
350 | 'a/b/fish.py'], m2.keys()) | |
|
351 | ||
|
352 | def testMatchesExactPath(self): | |
|
353 | '''Tests matches() on an exact match on a directory, which should | |
|
354 | result in an empty manifest because you can't perform an exact match | |
|
355 | against a directory.''' | |
|
356 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
357 | ||
|
358 | match = matchmod.match('/', '', ['a/b'], exact=True) | |
|
359 | m2 = m.matches(match) | |
|
360 | ||
|
361 | self.assertEqual([], m2.keys()) | |
|
362 | ||
|
363 | def testMatchesCwd(self): | |
|
364 | '''Tests matches() on a relpath match with the current directory ('.') | |
|
365 | when not in the root directory.''' | |
|
366 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
367 | ||
|
368 | match = matchmod.match('/', 'a/b', ['.'], default='relpath') | |
|
369 | m2 = m.matches(match) | |
|
370 | ||
|
371 | self.assertEqual([ | |
|
372 | 'a/b/c/bar.py', 'a/b/c/bar.txt', 'a/b/c/foo.py', 'a/b/c/foo.txt', | |
|
373 | 'a/b/d/baz.py', 'a/b/d/qux.py', 'a/b/d/ten.txt', 'a/b/dog.py', | |
|
374 | 'a/b/fish.py'], m2.keys()) | |
|
375 | ||
|
376 | def testMatchesWithPattern(self): | |
|
377 | '''Tests matches() for files matching a pattern that reside | |
|
378 | deeper than the specified directory.''' | |
|
379 | m = parsemanifest(A_DEEPER_MANIFEST) | |
|
380 | ||
|
381 | match = matchmod.match('/', '', ['a/b/*/*.txt']) | |
|
382 | m2 = m.matches(match) | |
|
383 | ||
|
384 | self.assertEqual( | |
|
385 | ['a/b/c/bar.txt', 'a/b/c/foo.txt', 'a/b/d/ten.txt'], | |
|
386 | m2.keys()) | |
|
387 | ||
|
265 | 388 | if __name__ == '__main__': |
|
266 | 389 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now