Show More
@@ -1,198 +1,200 b'' | |||||
1 | from __future__ import absolute_import, print_function |
|
1 | from __future__ import absolute_import, print_function | |
2 |
|
2 | |||
3 | import os |
|
3 | import os | |
4 | import tempfile |
|
4 | import tempfile | |
5 |
|
5 | |||
6 | from mercurial import ( |
|
6 | from mercurial import ( | |
7 | pycompat, |
|
7 | pycompat, | |
8 | util, |
|
8 | util, | |
9 | ) |
|
9 | ) | |
10 |
|
10 | |||
11 | from hgext.fastannotate import error, revmap |
|
11 | from hgext.fastannotate import error, revmap | |
12 |
|
12 | |||
13 | if pycompat.ispy3: |
|
13 | if pycompat.ispy3: | |
14 | xrange = range |
|
14 | xrange = range | |
15 |
|
15 | |||
16 | def genhsh(i): |
|
16 | def genhsh(i): | |
17 | return pycompat.bytechr(i) + b'\0' * 19 |
|
17 | return pycompat.bytechr(i) + b'\0' * 19 | |
18 |
|
18 | |||
19 | def gettemppath(): |
|
19 | def gettemppath(): | |
20 | fd, path = tempfile.mkstemp() |
|
20 | fd, path = tempfile.mkstemp() | |
21 | os.close(fd) |
|
21 | os.close(fd) | |
22 | os.unlink(path) |
|
22 | os.unlink(path) | |
23 | return path |
|
23 | return path | |
24 |
|
24 | |||
25 | def ensure(condition): |
|
25 | def ensure(condition): | |
26 | if not condition: |
|
26 | if not condition: | |
27 | raise RuntimeError('Unexpected') |
|
27 | raise RuntimeError('Unexpected') | |
28 |
|
28 | |||
29 | def testbasicreadwrite(): |
|
29 | def testbasicreadwrite(): | |
30 | path = gettemppath() |
|
30 | path = gettemppath() | |
31 |
|
31 | |||
32 | rm = revmap.revmap(path) |
|
32 | rm = revmap.revmap(path) | |
33 | ensure(rm.maxrev == 0) |
|
33 | ensure(rm.maxrev == 0) | |
34 | for i in xrange(5): |
|
34 | for i in xrange(5): | |
35 | ensure(rm.rev2hsh(i) is None) |
|
35 | ensure(rm.rev2hsh(i) is None) | |
36 | ensure(rm.hsh2rev(b'\0' * 20) is None) |
|
36 | ensure(rm.hsh2rev(b'\0' * 20) is None) | |
37 |
|
37 | |||
38 | paths = ['', 'a', None, 'b', 'b', 'c', 'c', None, 'a', 'b', 'a', 'a'] |
|
38 | paths = [ | |
|
39 | b'', b'a', None, b'b', b'b', b'c', b'c', None, b'a', b'b', b'a', b'a'] | |||
39 | for i in xrange(1, 5): |
|
40 | for i in xrange(1, 5): | |
40 | ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i]) == i) |
|
41 | ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i]) == i) | |
41 |
|
42 | |||
42 | ensure(rm.maxrev == 4) |
|
43 | ensure(rm.maxrev == 4) | |
43 | for i in xrange(1, 5): |
|
44 | for i in xrange(1, 5): | |
44 | ensure(rm.hsh2rev(genhsh(i)) == i) |
|
45 | ensure(rm.hsh2rev(genhsh(i)) == i) | |
45 | ensure(rm.rev2hsh(i) == genhsh(i)) |
|
46 | ensure(rm.rev2hsh(i) == genhsh(i)) | |
46 |
|
47 | |||
47 | # re-load and verify |
|
48 | # re-load and verify | |
48 | rm.flush() |
|
49 | rm.flush() | |
49 | rm = revmap.revmap(path) |
|
50 | rm = revmap.revmap(path) | |
50 | ensure(rm.maxrev == 4) |
|
51 | ensure(rm.maxrev == 4) | |
51 | for i in xrange(1, 5): |
|
52 | for i in xrange(1, 5): | |
52 | ensure(rm.hsh2rev(genhsh(i)) == i) |
|
53 | ensure(rm.hsh2rev(genhsh(i)) == i) | |
53 | ensure(rm.rev2hsh(i) == genhsh(i)) |
|
54 | ensure(rm.rev2hsh(i) == genhsh(i)) | |
54 | ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1)) |
|
55 | ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1)) | |
55 |
|
56 | |||
56 | # append without calling save() explicitly |
|
57 | # append without calling save() explicitly | |
57 | for i in xrange(5, 12): |
|
58 | for i in xrange(5, 12): | |
58 | ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i], |
|
59 | ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i], | |
59 | flush=True) == i) |
|
60 | flush=True) == i) | |
60 |
|
61 | |||
61 | # re-load and verify |
|
62 | # re-load and verify | |
62 | rm = revmap.revmap(path) |
|
63 | rm = revmap.revmap(path) | |
63 | ensure(rm.maxrev == 11) |
|
64 | ensure(rm.maxrev == 11) | |
64 | for i in xrange(1, 12): |
|
65 | for i in xrange(1, 12): | |
65 | ensure(rm.hsh2rev(genhsh(i)) == i) |
|
66 | ensure(rm.hsh2rev(genhsh(i)) == i) | |
66 | ensure(rm.rev2hsh(i) == genhsh(i)) |
|
67 | ensure(rm.rev2hsh(i) == genhsh(i)) | |
67 | ensure(rm.rev2path(i) == paths[i] or paths[i - 1]) |
|
68 | ensure(rm.rev2path(i) == paths[i] or paths[i - 1]) | |
68 | ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1)) |
|
69 | ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1)) | |
69 |
|
70 | |||
70 | os.unlink(path) |
|
71 | os.unlink(path) | |
71 |
|
72 | |||
72 | # missing keys |
|
73 | # missing keys | |
73 | ensure(rm.rev2hsh(12) is None) |
|
74 | ensure(rm.rev2hsh(12) is None) | |
74 | ensure(rm.rev2hsh(0) is None) |
|
75 | ensure(rm.rev2hsh(0) is None) | |
75 | ensure(rm.rev2hsh(-1) is None) |
|
76 | ensure(rm.rev2hsh(-1) is None) | |
76 | ensure(rm.rev2flag(12) is None) |
|
77 | ensure(rm.rev2flag(12) is None) | |
77 | ensure(rm.rev2path(12) is None) |
|
78 | ensure(rm.rev2path(12) is None) | |
78 | ensure(rm.hsh2rev(b'\1' * 20) is None) |
|
79 | ensure(rm.hsh2rev(b'\1' * 20) is None) | |
79 |
|
80 | |||
80 | # illformed hash (not 20 bytes) |
|
81 | # illformed hash (not 20 bytes) | |
81 | try: |
|
82 | try: | |
82 | rm.append(b'\0') |
|
83 | rm.append(b'\0') | |
83 | ensure(False) |
|
84 | ensure(False) | |
84 | except Exception: |
|
85 | except Exception: | |
85 | pass |
|
86 | pass | |
86 |
|
87 | |||
87 | def testcorruptformat(): |
|
88 | def testcorruptformat(): | |
88 | path = gettemppath() |
|
89 | path = gettemppath() | |
89 |
|
90 | |||
90 | # incorrect header |
|
91 | # incorrect header | |
91 | with open(path, 'w') as f: |
|
92 | with open(path, 'wb') as f: | |
92 | f.write(b'NOT A VALID HEADER') |
|
93 | f.write(b'NOT A VALID HEADER') | |
93 | try: |
|
94 | try: | |
94 | revmap.revmap(path) |
|
95 | revmap.revmap(path) | |
95 | ensure(False) |
|
96 | ensure(False) | |
96 | except error.CorruptedFileError: |
|
97 | except error.CorruptedFileError: | |
97 | pass |
|
98 | pass | |
98 |
|
99 | |||
99 | # rewrite the file |
|
100 | # rewrite the file | |
100 | os.unlink(path) |
|
101 | os.unlink(path) | |
101 | rm = revmap.revmap(path) |
|
102 | rm = revmap.revmap(path) | |
102 | rm.append(genhsh(0), flush=True) |
|
103 | rm.append(genhsh(0), flush=True) | |
103 |
|
104 | |||
104 | rm = revmap.revmap(path) |
|
105 | rm = revmap.revmap(path) | |
105 | ensure(rm.maxrev == 1) |
|
106 | ensure(rm.maxrev == 1) | |
106 |
|
107 | |||
107 | # corrupt the file by appending a byte |
|
108 | # corrupt the file by appending a byte | |
108 | size = os.stat(path).st_size |
|
109 | size = os.stat(path).st_size | |
109 | with open(path, 'a') as f: |
|
110 | with open(path, 'ab') as f: | |
110 | f.write('\xff') |
|
111 | f.write(b'\xff') | |
111 | try: |
|
112 | try: | |
112 | revmap.revmap(path) |
|
113 | revmap.revmap(path) | |
113 | ensure(False) |
|
114 | ensure(False) | |
114 | except error.CorruptedFileError: |
|
115 | except error.CorruptedFileError: | |
115 | pass |
|
116 | pass | |
116 |
|
117 | |||
117 | # corrupt the file by removing the last byte |
|
118 | # corrupt the file by removing the last byte | |
118 | ensure(size > 0) |
|
119 | ensure(size > 0) | |
119 | with open(path, 'w') as f: |
|
120 | with open(path, 'wb') as f: | |
120 | f.truncate(size - 1) |
|
121 | f.truncate(size - 1) | |
121 | try: |
|
122 | try: | |
122 | revmap.revmap(path) |
|
123 | revmap.revmap(path) | |
123 | ensure(False) |
|
124 | ensure(False) | |
124 | except error.CorruptedFileError: |
|
125 | except error.CorruptedFileError: | |
125 | pass |
|
126 | pass | |
126 |
|
127 | |||
127 | os.unlink(path) |
|
128 | os.unlink(path) | |
128 |
|
129 | |||
129 | def testcopyfrom(): |
|
130 | def testcopyfrom(): | |
130 | path = gettemppath() |
|
131 | path = gettemppath() | |
131 | rm = revmap.revmap(path) |
|
132 | rm = revmap.revmap(path) | |
132 | for i in xrange(1, 10): |
|
133 | for i in xrange(1, 10): | |
133 |
ensure(rm.append(genhsh(i), |
|
134 | ensure(rm.append(genhsh(i), | |
|
135 | sidebranch=(i & 1), path=(b'%d' % (i // 3))) == i) | |||
134 | rm.flush() |
|
136 | rm.flush() | |
135 |
|
137 | |||
136 | # copy rm to rm2 |
|
138 | # copy rm to rm2 | |
137 | rm2 = revmap.revmap() |
|
139 | rm2 = revmap.revmap() | |
138 | rm2.copyfrom(rm) |
|
140 | rm2.copyfrom(rm) | |
139 | path2 = gettemppath() |
|
141 | path2 = gettemppath() | |
140 | rm2.path = path2 |
|
142 | rm2.path = path2 | |
141 | rm2.flush() |
|
143 | rm2.flush() | |
142 |
|
144 | |||
143 | # two files should be the same |
|
145 | # two files should be the same | |
144 | ensure(len(set(util.readfile(p) for p in [path, path2])) == 1) |
|
146 | ensure(len(set(util.readfile(p) for p in [path, path2])) == 1) | |
145 |
|
147 | |||
146 | os.unlink(path) |
|
148 | os.unlink(path) | |
147 | os.unlink(path2) |
|
149 | os.unlink(path2) | |
148 |
|
150 | |||
149 | class fakefctx(object): |
|
151 | class fakefctx(object): | |
150 | def __init__(self, node, path=None): |
|
152 | def __init__(self, node, path=None): | |
151 | self._node = node |
|
153 | self._node = node | |
152 | self._path = path |
|
154 | self._path = path | |
153 |
|
155 | |||
154 | def node(self): |
|
156 | def node(self): | |
155 | return self._node |
|
157 | return self._node | |
156 |
|
158 | |||
157 | def path(self): |
|
159 | def path(self): | |
158 | return self._path |
|
160 | return self._path | |
159 |
|
161 | |||
160 | def testcontains(): |
|
162 | def testcontains(): | |
161 | path = gettemppath() |
|
163 | path = gettemppath() | |
162 |
|
164 | |||
163 | rm = revmap.revmap(path) |
|
165 | rm = revmap.revmap(path) | |
164 | for i in xrange(1, 5): |
|
166 | for i in xrange(1, 5): | |
165 | ensure(rm.append(genhsh(i), sidebranch=(i & 1)) == i) |
|
167 | ensure(rm.append(genhsh(i), sidebranch=(i & 1)) == i) | |
166 |
|
168 | |||
167 | for i in xrange(1, 5): |
|
169 | for i in xrange(1, 5): | |
168 | ensure(((genhsh(i), None) in rm) == ((i & 1) == 0)) |
|
170 | ensure(((genhsh(i), None) in rm) == ((i & 1) == 0)) | |
169 | ensure((fakefctx(genhsh(i)) in rm) == ((i & 1) == 0)) |
|
171 | ensure((fakefctx(genhsh(i)) in rm) == ((i & 1) == 0)) | |
170 | for i in xrange(5, 10): |
|
172 | for i in xrange(5, 10): | |
171 | ensure(fakefctx(genhsh(i)) not in rm) |
|
173 | ensure(fakefctx(genhsh(i)) not in rm) | |
172 | ensure((genhsh(i), None) not in rm) |
|
174 | ensure((genhsh(i), None) not in rm) | |
173 |
|
175 | |||
174 | # "contains" checks paths |
|
176 | # "contains" checks paths | |
175 | rm = revmap.revmap() |
|
177 | rm = revmap.revmap() | |
176 | for i in xrange(1, 5): |
|
178 | for i in xrange(1, 5): | |
177 |
ensure(rm.append(genhsh(i), path= |
|
179 | ensure(rm.append(genhsh(i), path=(b'%d' % (i // 2))) == i) | |
178 | for i in xrange(1, 5): |
|
180 | for i in xrange(1, 5): | |
179 |
ensure(fakefctx(genhsh(i), path= |
|
181 | ensure(fakefctx(genhsh(i), path=(b'%d' % (i // 2))) in rm) | |
180 | ensure(fakefctx(genhsh(i), path='a') not in rm) |
|
182 | ensure(fakefctx(genhsh(i), path=b'a') not in rm) | |
181 |
|
183 | |||
182 | def testlastnode(): |
|
184 | def testlastnode(): | |
183 | path = gettemppath() |
|
185 | path = gettemppath() | |
184 | ensure(revmap.getlastnode(path) is None) |
|
186 | ensure(revmap.getlastnode(path) is None) | |
185 | rm = revmap.revmap(path) |
|
187 | rm = revmap.revmap(path) | |
186 | ensure(revmap.getlastnode(path) is None) |
|
188 | ensure(revmap.getlastnode(path) is None) | |
187 | for i in xrange(1, 10): |
|
189 | for i in xrange(1, 10): | |
188 | hsh = genhsh(i) |
|
190 | hsh = genhsh(i) | |
189 |
rm.append(hsh, path= |
|
191 | rm.append(hsh, path=(b'%d' % (i // 2)), flush=True) | |
190 | ensure(revmap.getlastnode(path) == hsh) |
|
192 | ensure(revmap.getlastnode(path) == hsh) | |
191 | rm2 = revmap.revmap(path) |
|
193 | rm2 = revmap.revmap(path) | |
192 | ensure(rm2.rev2hsh(rm2.maxrev) == hsh) |
|
194 | ensure(rm2.rev2hsh(rm2.maxrev) == hsh) | |
193 |
|
195 | |||
194 | testbasicreadwrite() |
|
196 | testbasicreadwrite() | |
195 | testcorruptformat() |
|
197 | testcorruptformat() | |
196 | testcopyfrom() |
|
198 | testcopyfrom() | |
197 | testcontains() |
|
199 | testcontains() | |
198 | testlastnode() |
|
200 | testlastnode() |
General Comments 0
You need to be logged in to leave comments.
Login now