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