##// END OF EJS Templates
tests: add test coverage for revlogindex[-1] which was previously missing...
Augie Fackler -
r39102:daedb70f default
parent child Browse files
Show More
@@ -1,212 +1,222 b''
1 """This unit test primarily tests parsers.parse_index2().
1 """This unit test primarily tests parsers.parse_index2().
2
2
3 It also checks certain aspects of the parsers module as a whole.
3 It also checks certain aspects of the parsers module as a whole.
4 """
4 """
5
5
6 from __future__ import absolute_import, print_function
6 from __future__ import absolute_import, print_function
7
7
8 import struct
8 import struct
9 import subprocess
9 import subprocess
10 import sys
10 import sys
11 import unittest
11 import unittest
12
12
13 from mercurial.node import (
13 from mercurial.node import (
14 nullid,
14 nullid,
15 nullrev,
15 nullrev,
16 )
16 )
17 from mercurial import (
17 from mercurial import (
18 node as nodemod,
18 node as nodemod,
19 policy,
19 policy,
20 pycompat,
20 pycompat,
21 )
21 )
22
22
23 parsers = policy.importmod(r'parsers')
23 parsers = policy.importmod(r'parsers')
24
24
25 # original python implementation
25 # original python implementation
26 def gettype(q):
26 def gettype(q):
27 return int(q & 0xFFFF)
27 return int(q & 0xFFFF)
28
28
29 def offset_type(offset, type):
29 def offset_type(offset, type):
30 return int(int(offset) << 16 | type)
30 return int(int(offset) << 16 | type)
31
31
32 indexformatng = ">Qiiiiii20s12x"
32 indexformatng = ">Qiiiiii20s12x"
33
33
34 def py_parseindex(data, inline) :
34 def py_parseindex(data, inline) :
35 s = 64
35 s = 64
36 cache = None
36 cache = None
37 index = []
37 index = []
38 nodemap = {nullid: nullrev}
38 nodemap = {nullid: nullrev}
39 n = off = 0
39 n = off = 0
40
40
41 l = len(data) - s
41 l = len(data) - s
42 append = index.append
42 append = index.append
43 if inline:
43 if inline:
44 cache = (0, data)
44 cache = (0, data)
45 while off <= l:
45 while off <= l:
46 e = struct.unpack(indexformatng, data[off:off + s])
46 e = struct.unpack(indexformatng, data[off:off + s])
47 nodemap[e[7]] = n
47 nodemap[e[7]] = n
48 append(e)
48 append(e)
49 n += 1
49 n += 1
50 if e[1] < 0:
50 if e[1] < 0:
51 break
51 break
52 off += e[1] + s
52 off += e[1] + s
53 else:
53 else:
54 while off <= l:
54 while off <= l:
55 e = struct.unpack(indexformatng, data[off:off + s])
55 e = struct.unpack(indexformatng, data[off:off + s])
56 nodemap[e[7]] = n
56 nodemap[e[7]] = n
57 append(e)
57 append(e)
58 n += 1
58 n += 1
59 off += s
59 off += s
60
60
61 e = list(index[0])
61 e = list(index[0])
62 type = gettype(e[0])
62 type = gettype(e[0])
63 e[0] = offset_type(0, type)
63 e[0] = offset_type(0, type)
64 index[0] = tuple(e)
64 index[0] = tuple(e)
65
65
66 return index, cache
66 return index, cache
67
67
68 data_inlined = (
68 data_inlined = (
69 b'\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c'
69 b'\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c'
70 b'\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff'
70 b'\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff'
71 b'\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b'
71 b'\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b'
72 b'w\xdduR\xda\xc6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
72 b'w\xdduR\xda\xc6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
73 b'x\x9c\x9d\x93?O\xc30\x10\xc5\xf7|\x8a\xdb\x9a\xa8m\x06\xd8*\x95'
73 b'x\x9c\x9d\x93?O\xc30\x10\xc5\xf7|\x8a\xdb\x9a\xa8m\x06\xd8*\x95'
74 b'\x81B\xa1\xa2\xa2R\xcb\x86Pd\x9a\x0b5$vd_\x04\xfd\xf6\x9c\xff@'
74 b'\x81B\xa1\xa2\xa2R\xcb\x86Pd\x9a\x0b5$vd_\x04\xfd\xf6\x9c\xff@'
75 b'\x11!\x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\x1a\xc5\xe7\x17*\x93Oz:>\x0e'
91 b'\xbc\xb2\x93\x1f\xf2\x95\xff\xdf,\x1a\xc5\xe7\x17*\x93Oz:>\x0e'
92 )
92 )
93
93
94 data_non_inlined = (
94 data_non_inlined = (
95 b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19'
95 b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19'
96 b'\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
96 b'\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
97 b'\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d'
97 b'\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d'
98 b'\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00'
98 b'\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00'
99 b'\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00'
99 b'\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00'
100 b'\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff'
100 b'\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff'
101 b'\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh'
101 b'\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh'
102 b'\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
102 b'\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
103 b'\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00'
103 b'\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00'
104 b'\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n'
104 b'\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n'
105 b'\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00'
105 b'\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00'
106 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F'
106 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F'
107 b'\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01'
107 b'\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01'
108 b'\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1'
108 b'\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1'
109 b'\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00'
109 b'\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00'
110 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00'
110 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00'
111 )
111 )
112
112
113 def parse_index2(data, inline):
113 def parse_index2(data, inline):
114 index, chunkcache = parsers.parse_index2(data, inline)
114 index, chunkcache = parsers.parse_index2(data, inline)
115 return list(index), chunkcache
115 return list(index), chunkcache
116
116
117 def importparsers(hexversion):
117 def importparsers(hexversion):
118 """Import mercurial.parsers with the given sys.hexversion."""
118 """Import mercurial.parsers with the given sys.hexversion."""
119 # The file parsers.c inspects sys.hexversion to determine the version
119 # The file parsers.c inspects sys.hexversion to determine the version
120 # of the currently-running Python interpreter, so we monkey-patch
120 # of the currently-running Python interpreter, so we monkey-patch
121 # sys.hexversion to simulate using different versions.
121 # sys.hexversion to simulate using different versions.
122 code = ("import sys; sys.hexversion=%s; "
122 code = ("import sys; sys.hexversion=%s; "
123 "import mercurial.cext.parsers" % hexversion)
123 "import mercurial.cext.parsers" % hexversion)
124 cmd = "python -c \"%s\"" % code
124 cmd = "python -c \"%s\"" % code
125 # We need to do these tests inside a subprocess because parser.c's
125 # We need to do these tests inside a subprocess because parser.c's
126 # version-checking code happens inside the module init function, and
126 # version-checking code happens inside the module init function, and
127 # when using reload() to reimport an extension module, "The init function
127 # when using reload() to reimport an extension module, "The init function
128 # of extension modules is not called a second time"
128 # of extension modules is not called a second time"
129 # (from http://docs.python.org/2/library/functions.html?#reload).
129 # (from http://docs.python.org/2/library/functions.html?#reload).
130 p = subprocess.Popen(cmd, shell=True,
130 p = subprocess.Popen(cmd, shell=True,
131 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
131 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
132 return p.communicate() # returns stdout, stderr
132 return p.communicate() # returns stdout, stderr
133
133
134 def hexfailmsg(testnumber, hexversion, stdout, expected):
134 def hexfailmsg(testnumber, hexversion, stdout, expected):
135 try:
135 try:
136 hexstring = hex(hexversion)
136 hexstring = hex(hexversion)
137 except TypeError:
137 except TypeError:
138 hexstring = None
138 hexstring = None
139 return ("FAILED: version test #%s with Python %s and patched "
139 return ("FAILED: version test #%s with Python %s and patched "
140 "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" %
140 "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" %
141 (testnumber, sys.version_info, hexversion, hexstring, expected,
141 (testnumber, sys.version_info, hexversion, hexstring, expected,
142 stdout))
142 stdout))
143
143
144 def makehex(major, minor, micro):
144 def makehex(major, minor, micro):
145 return int("%x%02x%02x00" % (major, minor, micro), 16)
145 return int("%x%02x%02x00" % (major, minor, micro), 16)
146
146
147 class parseindex2tests(unittest.TestCase):
147 class parseindex2tests(unittest.TestCase):
148
148
149 def assertversionokay(self, testnumber, hexversion):
149 def assertversionokay(self, testnumber, hexversion):
150 stdout, stderr = importparsers(hexversion)
150 stdout, stderr = importparsers(hexversion)
151 self.assertFalse(
151 self.assertFalse(
152 stdout, hexfailmsg(testnumber, hexversion, stdout, 'no stdout'))
152 stdout, hexfailmsg(testnumber, hexversion, stdout, 'no stdout'))
153
153
154 def assertversionfail(self, testnumber, hexversion):
154 def assertversionfail(self, testnumber, hexversion):
155 stdout, stderr = importparsers(hexversion)
155 stdout, stderr = importparsers(hexversion)
156 # We include versionerrortext to distinguish from other ImportErrors.
156 # We include versionerrortext to distinguish from other ImportErrors.
157 errtext = b"ImportError: %s" % pycompat.sysbytes(
157 errtext = b"ImportError: %s" % pycompat.sysbytes(
158 parsers.versionerrortext)
158 parsers.versionerrortext)
159 self.assertIn(errtext, stdout,
159 self.assertIn(errtext, stdout,
160 hexfailmsg(testnumber, hexversion, stdout,
160 hexfailmsg(testnumber, hexversion, stdout,
161 expected="stdout to contain %r" % errtext))
161 expected="stdout to contain %r" % errtext))
162
162
163 def testversiondetection(self):
163 def testversiondetection(self):
164 """Check the version-detection logic when importing parsers."""
164 """Check the version-detection logic when importing parsers."""
165 # Only test the version-detection logic if it is present.
165 # Only test the version-detection logic if it is present.
166 try:
166 try:
167 parsers.versionerrortext
167 parsers.versionerrortext
168 except AttributeError:
168 except AttributeError:
169 return
169 return
170 info = sys.version_info
170 info = sys.version_info
171 major, minor, micro = info[0], info[1], info[2]
171 major, minor, micro = info[0], info[1], info[2]
172 # Test same major-minor versions.
172 # Test same major-minor versions.
173 self.assertversionokay(1, makehex(major, minor, micro))
173 self.assertversionokay(1, makehex(major, minor, micro))
174 self.assertversionokay(2, makehex(major, minor, micro + 1))
174 self.assertversionokay(2, makehex(major, minor, micro + 1))
175 # Test different major-minor versions.
175 # Test different major-minor versions.
176 self.assertversionfail(3, makehex(major + 1, minor, micro))
176 self.assertversionfail(3, makehex(major + 1, minor, micro))
177 self.assertversionfail(4, makehex(major, minor + 1, micro))
177 self.assertversionfail(4, makehex(major, minor + 1, micro))
178 self.assertversionfail(5, "'foo'")
178 self.assertversionfail(5, "'foo'")
179
179
180 def testbadargs(self):
180 def testbadargs(self):
181 # Check that parse_index2() raises TypeError on bad arguments.
181 # Check that parse_index2() raises TypeError on bad arguments.
182 with self.assertRaises(TypeError):
182 with self.assertRaises(TypeError):
183 parse_index2(0, True)
183 parse_index2(0, True)
184
184
185 def testparseindexfile(self):
185 def testparseindexfile(self):
186 # Check parsers.parse_index2() on an index file against the
186 # Check parsers.parse_index2() on an index file against the
187 # original Python implementation of parseindex, both with and
187 # original Python implementation of parseindex, both with and
188 # without inlined data.
188 # without inlined data.
189
189
190 want = py_parseindex(data_inlined, True)
190 want = py_parseindex(data_inlined, True)
191 got = parse_index2(data_inlined, True)
191 got = parse_index2(data_inlined, True)
192 self.assertEqual(want, got) # inline data
192 self.assertEqual(want, got) # inline data
193
193
194 want = py_parseindex(data_non_inlined, False)
194 want = py_parseindex(data_non_inlined, False)
195 got = parse_index2(data_non_inlined, False)
195 got = parse_index2(data_non_inlined, False)
196 self.assertEqual(want, got) # no inline data
196 self.assertEqual(want, got) # no inline data
197
197
198 ix = parsers.parse_index2(data_inlined, True)[0]
198 ix = parsers.parse_index2(data_inlined, True)[0]
199 for i, r in enumerate(ix):
199 for i, r in enumerate(ix):
200 if r[7] == nullid:
200 if r[7] == nullid:
201 i = -1
201 i = -1
202 try:
202 try:
203 self.assertEqual(
203 self.assertEqual(
204 ix[r[7]], i,
204 ix[r[7]], i,
205 'Reverse lookup inconsistent for %r' % nodemod.hex(r[7]))
205 'Reverse lookup inconsistent for %r' % nodemod.hex(r[7]))
206 except TypeError:
206 except TypeError:
207 # pure version doesn't support this
207 # pure version doesn't support this
208 break
208 break
209
209
210 def testminusone(self):
211 want = (0, 0, 0, -1, -1, -1, -1, nullid)
212 index, junk = parsers.parse_index2(data_inlined, True)
213 got = index[-1]
214 self.assertEqual(want, got) # inline data
215
216 index, junk = parsers.parse_index2(data_non_inlined, False)
217 got = index[-1]
218 self.assertEqual(want, got) # no inline data
219
210 if __name__ == '__main__':
220 if __name__ == '__main__':
211 import silenttestrunner
221 import silenttestrunner
212 silenttestrunner.main(__name__)
222 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now