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