Debian Codegraph @

tests/test-rust-revlog.py

raw
1 import os
2 import struct
3 import sys
4
5 if 'rust' not in os.environ.get('HGMODULEPOLICY', ''):
6 print("skipped: not running a rust flavor")
7 sys.exit(80)
8
9 from mercurial.node import (
10 bin as node_bin,
11 hex,
12 )
13 from mercurial import error
14
15 try:
16 from mercurial import pyo3_rustext
17
18 pyo3_rustext.__name__ # trigger immediate actual import
19 except ImportError:
20 pyo3_rustext = None
21 else:
22 # this would fail already without appropriate ancestor.__package__
23 from mercurial.pyo3_rustext.ancestor import LazyAncestors
24
25 from mercurial.testing import revlog as revlogtesting
26
27 header = struct.unpack(">I", revlogtesting.data_non_inlined[:4])[0]
28
29
30 # Conditional skipping done by the base class
31 class RustInnerRevlogTest(revlogtesting.RustRevlogBasedTestBase):
32 node_hex0 = b'd1f4bbb0befc13bd8cd39d0fcdd93b8c078c4a2f'
33 node0 = node_bin(node_hex0)
34 bogus_node_hex = b'cafe' * 10
35 bogus_node = node_bin(bogus_node_hex)
36 node_hex2 = b"020a0ec626a192ae360b0269fe2de5ba6f05d1e7"
37 node2 = node_bin(node_hex2)
38
39 def test_index_nodemap(self):
40 idx = self.parserustindex()
41 self.assertTrue(idx.has_node(self.node0))
42 self.assertFalse(idx.has_node(self.bogus_node))
43
44 self.assertEqual(idx.get_rev(self.node0), 0)
45 self.assertEqual(idx.get_rev(self.node0), 0)
46
47 self.assertEqual(idx.rev(self.node0), 0)
48 with self.assertRaises(error.RevlogError) as exc_info:
49 idx.rev(self.bogus_node)
50 self.assertEqual(exc_info.exception.args, (b'None',))
51
52 self.assertEqual(idx.partialmatch(self.node_hex0[:3]), self.node0)
53 self.assertIsNone(idx.partialmatch(self.bogus_node_hex[:3]))
54 self.assertEqual(idx.shortest(self.node0), 1)
55
56 def test_len(self):
57 idx = self.parserustindex()
58 self.assertEqual(len(idx), 4)
59
60 def test_getitem(self):
61 idx = self.parserustindex()
62 as_tuple = (0, 82969, 484626, 0, 0, -1, -1, self.node0, 0, 0, 2, 2, -1)
63 self.assertEqual(idx[0], as_tuple)
64 self.assertEqual(idx[self.node0], 0)
65
66 def test_heads(self):
67 idx = self.parserustindex()
68 self.assertEqual(idx.headrevs(), [3])
69
70 def test_index_append(self):
71 idx = self.parserustindex(data=b'')
72 self.assertEqual(len(idx), 0)
73 self.assertIsNone(idx.get_rev(self.node0))
74
75 non_empty_index = self.parserustindex()
76 idx.append(non_empty_index[0])
77 self.assertEqual(len(idx), 1)
78 self.assertEqual(idx.get_rev(self.node0), 0)
79
80 def test_index_delitem_single(self):
81 idx = self.parserustindex()
82 del idx[2]
83 self.assertEqual(len(idx), 2)
84
85 # the nodetree is consistent
86 self.assertEqual(idx.get_rev(self.node0), 0)
87 self.assertIsNone(idx.get_rev(self.node2))
88
89 # not an error and does nothing
90 del idx[-1]
91 self.assertEqual(len(idx), 2)
92
93 for bogus in (-2, 17):
94 try:
95 del idx[bogus]
96 except ValueError as exc:
97 # this underlines that we should do better with this message
98 assert exc.args[0] == (
99 f"Inconsistency: Revision {bogus} found in nodemap "
100 "is not in revlog index"
101 )
102 else:
103 raise AssertionError(
104 f"an exception was expected for `del idx[{bogus}]`"
105 )
106
107 def test_index_delitem_slice(self):
108 idx = self.parserustindex()
109 del idx[2:3]
110 self.assertEqual(len(idx), 2)
111
112 # not an error and not equivalent to `del idx[0::]` but to
113 # `del idx[-1]` instead and thus does nothing.
114 del idx[-1::]
115 self.assertEqual(len(idx), 2)
116
117 for start, stop in (
118 (-2, None),
119 (17, None),
120 ):
121 try:
122 del idx[start:stop]
123 except ValueError as exc:
124 # this underlines that we should do better with this message
125 assert exc.args[0] == (
126 f"Inconsistency: Revision {start} found in nodemap "
127 "is not in revlog index"
128 )
129 else:
130 raise AssertionError(
131 f"an exception was expected for `del idx[{start}:{stop}]`"
132 )
133
134 # although the upper bound is way too big, this is not an error:
135 del idx[0::17]
136 self.assertEqual(len(idx), 0)
137
138 def test_standalone_nodetree(self):
139 idx = self.parserustindex()
140 nt = self.nodetree(idx)
141 for i in range(4):
142 nt.insert(i)
143
144 # invalidation is upon mutation *of the index*
145 self.assertFalse(nt.is_invalidated())
146
147 bin_nodes = [entry[7] for entry in idx]
148 hex_nodes = [hex(n) for n in bin_nodes]
149
150 for i, node in enumerate(hex_nodes):
151 self.assertEqual(nt.prefix_rev_lookup(node), i)
152 self.assertEqual(nt.prefix_rev_lookup(node[:5]), i)
153
154 # all 4 revisions in idx (standard data set) have different
155 # first nybbles in their Node IDs,
156 # hence `nt.shortest()` should return 1 for them, except when
157 # the leading nybble is 0 (ambiguity with NULL_NODE)
158 for i, (bin_node, hex_node) in enumerate(zip(bin_nodes, hex_nodes)):
159 shortest = nt.shortest(bin_node)
160 expected = 2 if hex_node[0] == ord('0') else 1
161 self.assertEqual(shortest, expected)
162 self.assertEqual(nt.prefix_rev_lookup(hex_node[:shortest]), i)
163
164 # test invalidation (generation poisoning) detection
165 del idx[3]
166 self.assertTrue(nt.is_invalidated())
167
168 def test_reading_context_manager(self):
169 irl = self.make_inner_revlog()
170 try:
171 with irl.reading():
172 # not much to do yet
173 pass
174 except error.RevlogError as exc:
175 # well our data file does not even exist
176 self.assertTrue(b"when reading Just a path/test.d" in exc.args[0])
177
178 def test_ancestors(self):
179 rustidx = self.parserustindex()
180 lazy = LazyAncestors(rustidx, [3], 0, True)
181 # we have two more references to the index:
182 # - in its inner iterator for __contains__ and __bool__
183 # - in the LazyAncestors instance itself (to spawn new iterators)
184 self.assertTrue(2 in lazy)
185 self.assertTrue(bool(lazy))
186 self.assertEqual(list(lazy), [3, 2, 1, 0])
187 # a second time to validate that we spawn new iterators
188 self.assertEqual(list(lazy), [3, 2, 1, 0])
189
190 # let's check bool for an empty one
191 self.assertFalse(LazyAncestors(rustidx, [0], 0, False))
192
193 def test_canonical_index_file(self):
194 irl = self.make_inner_revlog()
195 self.assertEqual(irl.canonical_index_file, b'test.i')
196
197
198 if __name__ == '__main__':
199 import silenttestrunner
200
201 silenttestrunner.main(__name__)