Show More
@@ -1,263 +1,262 | |||
|
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 | 22 | HUGE_MANIFEST_ENTRIES = 200001 |
|
23 | 23 | |
|
24 | 24 | A_HUGE_MANIFEST = ''.join(sorted( |
|
25 | 25 | 'file%d\0%s%s\n' % (i, h, f) for i, h, f in |
|
26 | 26 | itertools.izip(xrange(200001), |
|
27 | 27 | itertools.cycle((HASH_1, HASH_2)), |
|
28 | 28 | itertools.cycle(('', 'x', 'l'))))) |
|
29 | 29 | |
|
30 | 30 | def parsemanifest(text): |
|
31 | 31 | return manifestmod.manifestdict(text) |
|
32 | 32 | |
|
33 | 33 | class testmanifest(unittest.TestCase): |
|
34 | 34 | |
|
35 | 35 | def assertIn(self, thing, container, msg=None): |
|
36 | 36 | # assertIn new in 2.7, use it if available, otherwise polyfill |
|
37 | 37 | sup = getattr(unittest.TestCase, 'assertIn', False) |
|
38 | 38 | if sup: |
|
39 | 39 | return sup(self, thing, container, msg=msg) |
|
40 | 40 | if not msg: |
|
41 | 41 | msg = 'Expected %r in %r' % (thing, container) |
|
42 | 42 | self.assert_(thing in container, msg) |
|
43 | 43 | |
|
44 | 44 | def testEmptyManifest(self): |
|
45 | 45 | m = parsemanifest('') |
|
46 | 46 | self.assertEqual(0, len(m)) |
|
47 | 47 | self.assertEqual([], list(m)) |
|
48 | 48 | |
|
49 | 49 | def testManifest(self): |
|
50 | 50 | m = parsemanifest(A_SHORT_MANIFEST) |
|
51 | 51 | self.assertEqual(['bar/baz/qux.py', 'foo'], list(m)) |
|
52 | 52 | self.assertEqual(binascii.unhexlify(HASH_2), m['bar/baz/qux.py']) |
|
53 | 53 | self.assertEqual('l', m.flags('bar/baz/qux.py')) |
|
54 | 54 | self.assertEqual(binascii.unhexlify(HASH_1), m['foo']) |
|
55 | 55 | self.assertEqual('', m.flags('foo')) |
|
56 | 56 | self.assertRaises(KeyError, lambda : m['wat']) |
|
57 | 57 | |
|
58 | 58 | def testSetItem(self): |
|
59 | 59 | want = binascii.unhexlify(HASH_1) |
|
60 | 60 | |
|
61 | 61 | m = parsemanifest('') |
|
62 | 62 | m['a'] = want |
|
63 | 63 | self.assertIn('a', m) |
|
64 | 64 | self.assertEqual(want, m['a']) |
|
65 | 65 | self.assertEqual('a\0' + HASH_1 + '\n', m.text()) |
|
66 | 66 | |
|
67 | 67 | m = parsemanifest(A_SHORT_MANIFEST) |
|
68 | 68 | m['a'] = want |
|
69 | 69 | self.assertEqual(want, m['a']) |
|
70 | 70 | self.assertEqual('a\0' + HASH_1 + '\n' + A_SHORT_MANIFEST, |
|
71 | 71 | m.text()) |
|
72 | 72 | |
|
73 | 73 | def testSetFlag(self): |
|
74 | 74 | want = 'x' |
|
75 | 75 | |
|
76 | 76 | m = parsemanifest('') |
|
77 | 77 | # first add a file; a file-less flag makes no sense |
|
78 | 78 | m['a'] = binascii.unhexlify(HASH_1) |
|
79 | 79 | m.setflag('a', want) |
|
80 | 80 | self.assertEqual(want, m.flags('a')) |
|
81 | 81 | self.assertEqual('a\0' + HASH_1 + want + '\n', m.text()) |
|
82 | 82 | |
|
83 | 83 | m = parsemanifest(A_SHORT_MANIFEST) |
|
84 | 84 | # first add a file; a file-less flag makes no sense |
|
85 | 85 | m['a'] = binascii.unhexlify(HASH_1) |
|
86 | 86 | m.setflag('a', want) |
|
87 | 87 | self.assertEqual(want, m.flags('a')) |
|
88 | 88 | self.assertEqual('a\0' + HASH_1 + want + '\n' + A_SHORT_MANIFEST, |
|
89 | 89 | m.text()) |
|
90 | 90 | |
|
91 | 91 | def testCopy(self): |
|
92 | 92 | m = parsemanifest(A_SHORT_MANIFEST) |
|
93 | 93 | m['a'] = binascii.unhexlify(HASH_1) |
|
94 | 94 | m2 = m.copy() |
|
95 | 95 | del m |
|
96 | 96 | del m2 # make sure we don't double free() anything |
|
97 | 97 | |
|
98 | 98 | def testCompaction(self): |
|
99 | 99 | unhex = binascii.unhexlify |
|
100 | 100 | h1, h2 = unhex(HASH_1), unhex(HASH_2) |
|
101 | 101 | m = parsemanifest(A_SHORT_MANIFEST) |
|
102 | 102 | m['alpha'] = h1 |
|
103 | 103 | m['beta'] = h2 |
|
104 | 104 | del m['foo'] |
|
105 | 105 | want = 'alpha\0%s\nbar/baz/qux.py\0%sl\nbeta\0%s\n' % ( |
|
106 | 106 | HASH_1, HASH_2, HASH_2) |
|
107 | 107 | self.assertEqual(want, m.text()) |
|
108 | 108 | self.assertEqual(3, len(m)) |
|
109 | 109 | self.assertEqual(['alpha', 'bar/baz/qux.py', 'beta'], list(m)) |
|
110 | 110 | self.assertEqual(h1, m['alpha']) |
|
111 | 111 | self.assertEqual(h2, m['bar/baz/qux.py']) |
|
112 | 112 | self.assertEqual(h2, m['beta']) |
|
113 | 113 | self.assertEqual('', m.flags('alpha')) |
|
114 | 114 | self.assertEqual('l', m.flags('bar/baz/qux.py')) |
|
115 | 115 | self.assertEqual('', m.flags('beta')) |
|
116 | 116 | self.assertRaises(KeyError, lambda : m['foo']) |
|
117 | 117 | |
|
118 | 118 | def testSetGetNodeSuffix(self): |
|
119 | 119 | clean = parsemanifest(A_SHORT_MANIFEST) |
|
120 | 120 | m = parsemanifest(A_SHORT_MANIFEST) |
|
121 | 121 | h = m['foo'] |
|
122 | 122 | f = m.flags('foo') |
|
123 | 123 | want = h + 'a' |
|
124 | 124 | # Merge code wants to set 21-byte fake hashes at times |
|
125 | 125 | m['foo'] = want |
|
126 | 126 | self.assertEqual(want, m['foo']) |
|
127 | 127 | self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2)), |
|
128 | 128 | ('foo', binascii.unhexlify(HASH_1) + 'a')], |
|
129 | 129 | list(m.iteritems())) |
|
130 | 130 | # Sometimes it even tries a 22-byte fake hash, but we can |
|
131 | 131 | # return 21 and it'll work out |
|
132 | 132 | m['foo'] = want + '+' |
|
133 | 133 | self.assertEqual(want, m['foo']) |
|
134 | 134 | # make sure the suffix survives a copy |
|
135 | 135 | match = matchmod.match('', '', ['re:foo']) |
|
136 | 136 | m2 = m.matches(match) |
|
137 | 137 | self.assertEqual(want, m2['foo']) |
|
138 | 138 | self.assertEqual(1, len(m2)) |
|
139 | self.assertEqual(('foo\0%s\n' % HASH_1), m2.text()) | |
|
140 | 139 | m2 = m.copy() |
|
141 | 140 | self.assertEqual(want, m2['foo']) |
|
142 | 141 | # suffix with iteration |
|
143 | 142 | self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2)), |
|
144 | 143 | ('foo', want)], |
|
145 | 144 | list(m.iteritems())) |
|
146 | 145 | |
|
147 | 146 | # shows up in diff |
|
148 | 147 | self.assertEqual({'foo': ((want, f), (h, ''))}, m.diff(clean)) |
|
149 | 148 | self.assertEqual({'foo': ((h, ''), (want, f))}, clean.diff(m)) |
|
150 | 149 | |
|
151 | 150 | def testMatchException(self): |
|
152 | 151 | m = parsemanifest(A_SHORT_MANIFEST) |
|
153 | 152 | match = matchmod.match('', '', ['re:.*']) |
|
154 | 153 | def filt(path): |
|
155 | 154 | if path == 'foo': |
|
156 | 155 | assert False |
|
157 | 156 | return True |
|
158 | 157 | match.matchfn = filt |
|
159 | 158 | self.assertRaises(AssertionError, m.matches, match) |
|
160 | 159 | |
|
161 | 160 | def testRemoveItem(self): |
|
162 | 161 | m = parsemanifest(A_SHORT_MANIFEST) |
|
163 | 162 | del m['foo'] |
|
164 | 163 | self.assertRaises(KeyError, lambda : m['foo']) |
|
165 | 164 | self.assertEqual(1, len(m)) |
|
166 | 165 | self.assertEqual(1, len(list(m))) |
|
167 | 166 | # now restore and make sure everything works right |
|
168 | 167 | m['foo'] = 'a' * 20 |
|
169 | 168 | self.assertEqual(2, len(m)) |
|
170 | 169 | self.assertEqual(2, len(list(m))) |
|
171 | 170 | |
|
172 | 171 | def testManifestDiff(self): |
|
173 | 172 | MISSING = (None, '') |
|
174 | 173 | addl = 'z-only-in-left\0' + HASH_1 + '\n' |
|
175 | 174 | addr = 'z-only-in-right\0' + HASH_2 + 'x\n' |
|
176 | 175 | left = parsemanifest( |
|
177 | 176 | A_SHORT_MANIFEST.replace(HASH_1, HASH_3 + 'x') + addl) |
|
178 | 177 | right = parsemanifest(A_SHORT_MANIFEST + addr) |
|
179 | 178 | want = { |
|
180 | 179 | 'foo': ((binascii.unhexlify(HASH_3), 'x'), |
|
181 | 180 | (binascii.unhexlify(HASH_1), '')), |
|
182 | 181 | 'z-only-in-left': ((binascii.unhexlify(HASH_1), ''), MISSING), |
|
183 | 182 | 'z-only-in-right': (MISSING, (binascii.unhexlify(HASH_2), 'x')), |
|
184 | 183 | } |
|
185 | 184 | self.assertEqual(want, left.diff(right)) |
|
186 | 185 | |
|
187 | 186 | want = { |
|
188 | 187 | 'bar/baz/qux.py': (MISSING, (binascii.unhexlify(HASH_2), 'l')), |
|
189 | 188 | 'foo': (MISSING, (binascii.unhexlify(HASH_3), 'x')), |
|
190 | 189 | 'z-only-in-left': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
191 | 190 | } |
|
192 | 191 | self.assertEqual(want, parsemanifest('').diff(left)) |
|
193 | 192 | |
|
194 | 193 | want = { |
|
195 | 194 | 'bar/baz/qux.py': ((binascii.unhexlify(HASH_2), 'l'), MISSING), |
|
196 | 195 | 'foo': ((binascii.unhexlify(HASH_3), 'x'), MISSING), |
|
197 | 196 | 'z-only-in-left': ((binascii.unhexlify(HASH_1), ''), MISSING), |
|
198 | 197 | } |
|
199 | 198 | self.assertEqual(want, left.diff(parsemanifest(''))) |
|
200 | 199 | copy = right.copy() |
|
201 | 200 | del copy['z-only-in-right'] |
|
202 | 201 | del right['foo'] |
|
203 | 202 | want = { |
|
204 | 203 | 'foo': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
205 | 204 | 'z-only-in-right': ((binascii.unhexlify(HASH_2), 'x'), MISSING), |
|
206 | 205 | } |
|
207 | 206 | self.assertEqual(want, right.diff(copy)) |
|
208 | 207 | |
|
209 | 208 | short = parsemanifest(A_SHORT_MANIFEST) |
|
210 | 209 | pruned = short.copy() |
|
211 | 210 | del pruned['foo'] |
|
212 | 211 | want = { |
|
213 | 212 | 'foo': ((binascii.unhexlify(HASH_1), ''), MISSING), |
|
214 | 213 | } |
|
215 | 214 | self.assertEqual(want, short.diff(pruned)) |
|
216 | 215 | want = { |
|
217 | 216 | 'foo': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
218 | 217 | } |
|
219 | 218 | self.assertEqual(want, pruned.diff(short)) |
|
220 | 219 | want = { |
|
221 | 220 | 'bar/baz/qux.py': None, |
|
222 | 221 | 'foo': (MISSING, (binascii.unhexlify(HASH_1), '')), |
|
223 | 222 | } |
|
224 | 223 | self.assertEqual(want, pruned.diff(short, True)) |
|
225 | 224 | |
|
226 | 225 | def testReversedLines(self): |
|
227 | 226 | backwards = ''.join( |
|
228 | 227 | l + '\n' for l in reversed(A_SHORT_MANIFEST.split('\n')) if l) |
|
229 | 228 | try: |
|
230 | 229 | parsemanifest(backwards) |
|
231 | 230 | self.fail('Should have raised ValueError') |
|
232 | 231 | except ValueError, v: |
|
233 | 232 | self.assertIn('Manifest lines not in sorted order.', str(v)) |
|
234 | 233 | |
|
235 | 234 | def testNoTerminalNewline(self): |
|
236 | 235 | try: |
|
237 | 236 | parsemanifest(A_SHORT_MANIFEST + 'wat') |
|
238 | 237 | self.fail('Should have raised ValueError') |
|
239 | 238 | except ValueError, v: |
|
240 | 239 | self.assertIn('Manifest did not end in a newline.', str(v)) |
|
241 | 240 | |
|
242 | 241 | def testNoNewLineAtAll(self): |
|
243 | 242 | try: |
|
244 | 243 | parsemanifest('wat') |
|
245 | 244 | self.fail('Should have raised ValueError') |
|
246 | 245 | except ValueError, v: |
|
247 | 246 | self.assertIn('Manifest did not end in a newline.', str(v)) |
|
248 | 247 | |
|
249 | 248 | def testHugeManifest(self): |
|
250 | 249 | m = parsemanifest(A_HUGE_MANIFEST) |
|
251 | 250 | self.assertEqual(HUGE_MANIFEST_ENTRIES, len(m)) |
|
252 | 251 | self.assertEqual(len(m), len(list(m))) |
|
253 | 252 | |
|
254 | 253 | def testIntersectFiles(self): |
|
255 | 254 | m = parsemanifest(A_HUGE_MANIFEST) |
|
256 | 255 | m2 = m.intersectfiles(['file1', 'file200', 'file300']) |
|
257 | 256 | w = ('file1\0%sx\n' |
|
258 | 257 | 'file200\0%sl\n' |
|
259 | 258 | 'file300\0%s\n') % (HASH_2, HASH_1, HASH_1) |
|
260 | 259 | self.assertEqual(w, m2.text()) |
|
261 | 260 | |
|
262 | 261 | if __name__ == '__main__': |
|
263 | 262 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now