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