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