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