Debian Codegraph @

tests/test-parseindex2.py

raw
1 """This unit test primarily tests parsers.parse_index2().
2
3 It also checks certain aspects of the parsers module as a whole.
4 """
5
6
7 import os
8 import struct
9 import subprocess
10 import sys
11 import unittest
12
13 from mercurial.node import (
14 bin,
15 hex,
16 nullrev,
17 sha1nodeconstants,
18 )
19 from mercurial import (
20 policy,
21 pycompat,
22 )
23 from mercurial.revlogutils import (
24 constants,
25 )
26
27 parsers = policy.importmod('parsers')
28
29
30 # original python implementation
31 def gettype(q):
32 return int(q & 0xFFFF)
33
34
35 def offset_type(offset, type):
36 return int(int(offset) << 16 | type)
37
38
39 indexformatng = ">Qiiiiii20s12x"
40
41
42 def py_parseindex(data, inline):
43 s = 64
44 cache = None
45 index = []
46 nodemap = {sha1nodeconstants.nullid: nullrev}
47 n = off = 0
48
49 l = len(data) - s
50 append = index.append
51 if inline:
52 cache = (0, data)
53 while off <= l:
54 e = struct.unpack(indexformatng, data[off : off + s])
55 e = e + (
56 0,
57 0,
58 constants.COMP_MODE_INLINE,
59 constants.COMP_MODE_INLINE,
60 constants.RANK_UNKNOWN,
61 )
62 nodemap[e[7]] = n
63 append(e)
64 n += 1
65 if e[1] < 0:
66 break
67 off += e[1] + s
68 else:
69 while off <= l:
70 e = struct.unpack(indexformatng, data[off : off + s])
71 e = e + (
72 0,
73 0,
74 constants.COMP_MODE_INLINE,
75 constants.COMP_MODE_INLINE,
76 constants.RANK_UNKNOWN,
77 )
78 nodemap[e[7]] = n
79 append(e)
80 n += 1
81 off += s
82
83 e = list(index[0])
84 type = gettype(e[0])
85 e[0] = offset_type(0, type)
86 index[0] = tuple(e)
87
88 return index, cache
89
90
91 data_inlined = (
92 b'\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c'
93 b'\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff'
94 b'\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b'
95 b'w\xdduR\xda\xc6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
96 b'x\x9c\x9d\x93?O\xc30\x10\xc5\xf7|\x8a\xdb\x9a\xa8m\x06\xd8*\x95'
97 b'\x81B\xa1\xa2\xa2R\xcb\x86Pd\x9a\x0b5$vd_\x04\xfd\xf6\x9c\xff@'
98 b'\x11!\x0b\xd9\xec\xf7\xbbw\xe7gG6\xad6\x04\xdaN\xc0\x92\xa0$)'
99 b'\xb1\x82\xa2\xd1%\x16\xa4\x8b7\xa9\xca\xd4-\xb2Y\x02\xfc\xc9'
100 b'\xcaS\xf9\xaeX\xed\xb6\xd77Q\x02\x83\xd4\x19\xf5--Y\xea\xe1W'
101 b'\xab\xed\x10\xceR\x0f_\xdf\xdf\r\xe1,\xf5\xf0\xcb\xf5 \xceR\x0f'
102 b'_\xdc\x0e\x0e\xc3R\x0f_\xae\x96\x9b!\x9e\xa5\x1e\xbf\xdb,\x06'
103 b'\xc7q\x9a/\x88\x82\xc3B\xea\xb5\xb4TJ\x93\xb6\x82\x0e\xe16\xe6'
104 b'KQ\xdb\xaf\xecG\xa3\xd1 \x01\xd3\x0b_^\xe8\xaa\xa0\xae\xad\xd1'
105 b'&\xbef\x1bz\x08\xb0|\xc9Xz\x06\xf6Z\x91\x90J\xaa\x17\x90\xaa'
106 b'\xd2\xa6\x11$5C\xcf\xba#\xa0\x03\x02*2\x92-\xfc\xb1\x94\xdf\xe2'
107 b'\xae\xb8\'m\x8ey0^\x85\xd3\x82\xb4\xf0`:\x9c\x00\x8a\xfd\x01'
108 b'\xb0\xc6\x86\x8b\xdd\xae\x80\xf3\xa9\x9fd\x16\n\x00R%\x1a\x06'
109 b'\xe9\xd8b\x98\x1d\xf4\xf3+\x9bf\x01\xd8p\x1b\xf3.\xed\x9f^g\xc3'
110 b'^\xd9W81T\xdb\xd5\x04sx|\xf2\xeb\xd6`%?x\xed"\x831\xbf\xf3\xdc'
111 b'b\xeb%gaY\xe1\xad\x9f\xb9f\'1w\xa9\xa5a\x83s\x82J\xb98\xbc4\x8b'
112 b'\x83\x00\x9f$z\xb8#\xa5\xb1\xdf\x98\xd9\xec\x1b\x89O\xe3Ts\x9a4'
113 b'\x17m\x8b\xfc\x8f\xa5\x95\x9a\xfc\xfa\xed,\xe5|\xa1\xfe\x15\xb9'
114 b'\xbc\xb2\x93\x1f\xf2\x95\xff\xdf,\x1a\xc5\xe7\x17*\x93Oz:>\x0e'
115 )
116
117 data_non_inlined = (
118 b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19'
119 b'\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
120 b'\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d'
121 b'\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00'
122 b'\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00'
123 b'\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff'
124 b'\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh'
125 b'\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
126 b'\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00'
127 b'\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n'
128 b'\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00'
129 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F'
130 b'\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01'
131 b'\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1'
132 b'\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00'
133 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00'
134 )
135
136
137 def parse_index2(data, inline, format=constants.REVLOGV1):
138 index, chunkcache = parsers.parse_index2(
139 data,
140 inlined=inline,
141 uses_generaldelta=False,
142 uses_delta_info=False,
143 format=format,
144 )
145 return list(index), chunkcache
146
147
148 def importparsers(hexversion):
149 """Import mercurial.parsers with the given sys.hexversion."""
150 # The file parsers.c inspects sys.hexversion to determine the version
151 # of the currently-running Python interpreter, so we monkey-patch
152 # sys.hexversion to simulate using different versions.
153 code = (
154 "import sys; sys.hexversion=%s; "
155 "import mercurial.cext.parsers" % hexversion
156 )
157 cmd = "\"%s\" -c \"%s\"" % (os.environ['PYTHON'], code)
158 # We need to do these tests inside a subprocess because parser.c's
159 # version-checking code happens inside the module init function, and
160 # when using reload() to reimport an extension module, "The init function
161 # of extension modules is not called a second time"
162 # (from http://docs.python.org/2/library/functions.html?#reload).
163 p = subprocess.Popen(
164 cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
165 )
166 return p.communicate() # returns stdout, stderr
167
168
169 def hexfailmsg(testnumber, hexversion, stdout, expected):
170 try:
171 hexstring = hex(hexversion)
172 except TypeError:
173 hexstring = None
174 return (
175 "FAILED: version test #%s with Python %s and patched "
176 "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n"
177 % (
178 testnumber,
179 sys.version_info,
180 hexversion,
181 hexstring,
182 expected,
183 stdout,
184 )
185 )
186
187
188 def makehex(major, minor, micro):
189 return int("%x%02x%02x00" % (major, minor, micro), 16)
190
191
192 class parseindex2tests(unittest.TestCase):
193 def assertversionokay(self, testnumber, hexversion):
194 stdout, stderr = importparsers(hexversion)
195 self.assertFalse(
196 stdout, hexfailmsg(testnumber, hexversion, stdout, 'no stdout')
197 )
198
199 def assertversionfail(self, testnumber, hexversion):
200 stdout, stderr = importparsers(hexversion)
201 # We include versionerrortext to distinguish from other ImportErrors.
202 errtext = b"ImportError: %s" % pycompat.sysbytes(
203 parsers.versionerrortext
204 )
205 self.assertIn(
206 errtext,
207 stdout,
208 hexfailmsg(
209 testnumber,
210 hexversion,
211 stdout,
212 expected="stdout to contain %r" % errtext,
213 ),
214 )
215
216 def testversiondetection(self):
217 """Check the version-detection logic when importing parsers."""
218 # Only test the version-detection logic if it is present.
219 try:
220 parsers.versionerrortext
221 except AttributeError:
222 return
223 info = sys.version_info
224 major, minor, micro = info[0], info[1], info[2]
225 # Test same major-minor versions.
226 self.assertversionokay(1, makehex(major, minor, micro))
227 self.assertversionokay(2, makehex(major, minor, micro + 1))
228 # Test different major-minor versions.
229 self.assertversionfail(3, makehex(major + 1, minor, micro))
230 self.assertversionfail(4, makehex(major, minor + 1, micro))
231
232 def testbadargs(self):
233 # Check that parse_index2() raises TypeError on bad arguments.
234 with self.assertRaises(TypeError):
235 parse_index2(0, True, False)
236
237 def testparseindexfile(self):
238 # Check parsers.parse_index2() on an index file against the
239 # original Python implementation of parseindex, both with and
240 # without inlined data.
241
242 want = py_parseindex(data_inlined, True)
243 got = parse_index2(data_inlined, True)
244 self.assertEqual(want, got) # inline data
245
246 want = py_parseindex(data_non_inlined, False)
247 got = parse_index2(data_non_inlined, False)
248 self.assertEqual(want, got) # no inline data
249
250 ix = parsers.parse_index2(
251 data_inlined,
252 inlined=True,
253 uses_generaldelta=False,
254 uses_delta_info=False,
255 )[0]
256 for i, r in enumerate(ix):
257 if r[7] == sha1nodeconstants.nullid:
258 i = -1
259 try:
260 self.assertEqual(
261 ix[r[7]],
262 i,
263 'Reverse lookup inconsistent for %r' % hex(r[7]),
264 )
265 except TypeError:
266 # pure version doesn't support this
267 break
268
269 def testminusone(self):
270 want = (
271 0,
272 0,
273 0,
274 -1,
275 -1,
276 -1,
277 -1,
278 sha1nodeconstants.nullid,
279 0,
280 0,
281 constants.COMP_MODE_INLINE,
282 constants.COMP_MODE_INLINE,
283 constants.RANK_UNKNOWN,
284 )
285 index, junk = parsers.parse_index2(
286 data_inlined,
287 inlined=True,
288 uses_generaldelta=False,
289 uses_delta_info=False,
290 )
291 got = index[-1]
292 self.assertEqual(want, got) # inline data
293
294 index, junk = parsers.parse_index2(
295 data_non_inlined,
296 inlined=False,
297 uses_generaldelta=False,
298 uses_delta_info=False,
299 )
300 got = index[-1]
301 self.assertEqual(want, got) # no inline data
302
303 def testdelitemwithoutnodetree(self):
304 index, _junk = parsers.parse_index2(
305 data_non_inlined,
306 inlined=False,
307 uses_generaldelta=False,
308 uses_delta_info=False,
309 )
310
311 def hexrev(rev):
312 if rev == nullrev:
313 return b'\xff\xff\xff\xff'
314 else:
315 return bin('%08x' % rev)
316
317 def appendrev(p1, p2=nullrev):
318 # node won't matter for this test, let's just make sure
319 # they don't collide. Other data don't matter either.
320 node = hexrev(p1) + hexrev(p2) + b'.' * 12
321 e = (
322 0,
323 0,
324 12,
325 1,
326 34,
327 p1,
328 p2,
329 node,
330 0,
331 0,
332 constants.COMP_MODE_INLINE,
333 constants.COMP_MODE_INLINE,
334 constants.RANK_UNKNOWN,
335 )
336 index.append(e)
337
338 appendrev(4)
339 appendrev(5)
340 appendrev(6)
341 self.assertEqual(len(index), 7)
342
343 del index[1:-1]
344
345 # assertions that failed before correction
346 self.assertEqual(len(index), 1) # was 4
347 headrevs = getattr(index, 'headrevs', None)
348 if headrevs is not None: # not implemented in pure
349 self.assertEqual(index.headrevs(), [0]) # gave ValueError
350
351
352 if __name__ == '__main__':
353 import silenttestrunner
354
355 silenttestrunner.main(__name__)