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