Debian Codegraph @

tests/test-absorb-filefixupstate.py

raw
1 import itertools
2 from mercurial import pycompat
3 from hgext import absorb
4
5
6 class simplefctx:
7 def __init__(self, content):
8 self.content = content
9
10 def data(self):
11 return self.content
12
13
14 def insertreturns(x):
15 # insert "\n"s after each single char
16 if isinstance(x, bytes):
17 return b''.join(ch + b'\n' for ch in pycompat.bytestr(x))
18 else:
19 return pycompat.maplist(insertreturns, x)
20
21
22 def removereturns(x):
23 # the revert of "insertreturns"
24 if isinstance(x, bytes):
25 return x.replace(b'\n', b'')
26 else:
27 return pycompat.maplist(removereturns, x)
28
29
30 def assertlistequal(lhs, rhs, decorator=lambda x: x):
31 if lhs != rhs:
32 raise RuntimeError(
33 'mismatch:\n actual: %r\n expected: %r'
34 % tuple(map(decorator, [lhs, rhs]))
35 )
36
37
38 def testfilefixup(oldcontents, workingcopy, expectedcontents, fixups=None):
39 """([str], str, [str], [(rev, a1, a2, b1, b2)]?) -> None
40
41 workingcopy is a string, of which every character denotes a single line.
42
43 oldcontents, expectedcontents are lists of strings, every character of
44 every string denots a single line.
45
46 if fixups is not None, it's the expected fixups list and will be checked.
47 """
48 expectedcontents = insertreturns(expectedcontents)
49 oldcontents = insertreturns(oldcontents)
50 workingcopy = insertreturns(workingcopy)
51 state = absorb.filefixupstate(
52 pycompat.maplist(simplefctx, oldcontents), 'path'
53 )
54 state.diffwith(simplefctx(workingcopy))
55 if fixups is not None:
56 assertlistequal(state.fixups, fixups)
57 state.apply()
58 assertlistequal(state.finalcontents, expectedcontents, removereturns)
59
60
61 def buildcontents(linesrevs):
62 # linesrevs: [(linecontent : str, revs : [int])]
63 revs = set(itertools.chain(*[revs for line, revs in linesrevs]))
64 return [b''] + [
65 b''.join([l for l, rs in linesrevs if r in rs]) for r in sorted(revs)
66 ]
67
68
69 # input case 0: one single commit
70 case0 = [b'', b'11']
71
72 # replace a single chunk
73 testfilefixup(case0, b'', [b'', b''])
74 testfilefixup(case0, b'2', [b'', b'2'])
75 testfilefixup(case0, b'22', [b'', b'22'])
76 testfilefixup(case0, b'222', [b'', b'222'])
77
78 # input case 1: 3 lines, each commit adds one line
79 case1 = buildcontents(
80 [
81 (b'1', [1, 2, 3]),
82 (b'2', [2, 3]),
83 (b'3', [3]),
84 ]
85 )
86
87 # 1:1 line mapping
88 testfilefixup(case1, b'123', case1)
89 testfilefixup(case1, b'12c', [b'', b'1', b'12', b'12c'])
90 testfilefixup(case1, b'1b3', [b'', b'1', b'1b', b'1b3'])
91 testfilefixup(case1, b'1bc', [b'', b'1', b'1b', b'1bc'])
92 testfilefixup(case1, b'a23', [b'', b'a', b'a2', b'a23'])
93 testfilefixup(case1, b'a2c', [b'', b'a', b'a2', b'a2c'])
94 testfilefixup(case1, b'ab3', [b'', b'a', b'ab', b'ab3'])
95 testfilefixup(case1, b'abc', [b'', b'a', b'ab', b'abc'])
96
97 # non 1:1 edits
98 testfilefixup(case1, b'abcd', case1)
99 testfilefixup(case1, b'ab', case1)
100
101 # deletion
102 testfilefixup(case1, b'', [b'', b'', b'', b''])
103 testfilefixup(case1, b'1', [b'', b'1', b'1', b'1'])
104 testfilefixup(case1, b'2', [b'', b'', b'2', b'2'])
105 testfilefixup(case1, b'3', [b'', b'', b'', b'3'])
106 testfilefixup(case1, b'13', [b'', b'1', b'1', b'13'])
107
108 # replaces
109 testfilefixup(case1, b'1bb3', [b'', b'1', b'1bb', b'1bb3'])
110
111 # (confusing) replaces
112 testfilefixup(case1, b'1bbb', case1)
113 testfilefixup(case1, b'bbbb', case1)
114 testfilefixup(case1, b'bbb3', case1)
115 testfilefixup(case1, b'1b', case1)
116 testfilefixup(case1, b'bb', case1)
117 testfilefixup(case1, b'b3', case1)
118
119 # insertions at the beginning and the end
120 testfilefixup(case1, b'123c', [b'', b'1', b'12', b'123c'])
121 testfilefixup(case1, b'a123', [b'', b'a1', b'a12', b'a123'])
122
123 # (confusing) insertions
124 testfilefixup(case1, b'1a23', case1)
125 testfilefixup(case1, b'12b3', case1)
126
127 # input case 2: delete in the middle
128 case2 = buildcontents(
129 [
130 (b'11', [1, 2]),
131 (b'22', [1]),
132 (b'33', [1, 2]),
133 ]
134 )
135
136 # deletion (optimize code should make it 2 chunks)
137 testfilefixup(
138 case2, b'', [b'', b'22', b''], fixups=[(4, 0, 2, 0, 0), (4, 2, 4, 0, 0)]
139 )
140
141 # 1:1 line mapping
142 testfilefixup(case2, b'aaaa', [b'', b'aa22aa', b'aaaa'])
143
144 # non 1:1 edits
145 # note: unlike case0, the chunk is not "continuous" and no edit allowed
146 testfilefixup(case2, b'aaa', case2)
147
148 # input case 3: rev 3 reverts rev 2
149 case3 = buildcontents(
150 [
151 (b'1', [1, 2, 3]),
152 (b'2', [2]),
153 (b'3', [1, 2, 3]),
154 ]
155 )
156
157 # 1:1 line mapping
158 testfilefixup(case3, b'13', case3)
159 testfilefixup(case3, b'1b', [b'', b'1b', b'12b', b'1b'])
160 testfilefixup(case3, b'a3', [b'', b'a3', b'a23', b'a3'])
161 testfilefixup(case3, b'ab', [b'', b'ab', b'a2b', b'ab'])
162
163 # non 1:1 edits
164 testfilefixup(case3, b'a', case3)
165 testfilefixup(case3, b'abc', case3)
166
167 # deletion
168 testfilefixup(case3, b'', [b'', b'', b'2', b''])
169
170 # insertion
171 testfilefixup(case3, b'a13c', [b'', b'a13c', b'a123c', b'a13c'])
172
173 # input case 4: a slightly complex case
174 case4 = buildcontents(
175 [
176 (b'1', [1, 2, 3]),
177 (b'2', [2, 3]),
178 (
179 b'3',
180 [
181 1,
182 2,
183 ],
184 ),
185 (b'4', [1, 3]),
186 (b'5', [3]),
187 (b'6', [2, 3]),
188 (b'7', [2]),
189 (b'8', [2, 3]),
190 (b'9', [3]),
191 ]
192 )
193
194 testfilefixup(case4, b'1245689', case4)
195 testfilefixup(case4, b'1a2456bbb', case4)
196 testfilefixup(case4, b'1abc5689', case4)
197 testfilefixup(case4, b'1ab5689', [b'', b'134', b'1a3678', b'1ab5689'])
198 testfilefixup(case4, b'aa2bcd8ee', [b'', b'aa34', b'aa23d78', b'aa2bcd8ee'])
199 testfilefixup(case4, b'aa2bcdd8ee', [b'', b'aa34', b'aa23678', b'aa24568ee'])
200 testfilefixup(case4, b'aaaaaa', case4)
201 testfilefixup(case4, b'aa258b', [b'', b'aa34', b'aa2378', b'aa258b'])
202 testfilefixup(case4, b'25bb', [b'', b'34', b'23678', b'25689'])
203 testfilefixup(case4, b'27', [b'', b'34', b'23678', b'245689'])
204 testfilefixup(case4, b'28', [b'', b'34', b'2378', b'28'])
205 testfilefixup(case4, b'', [b'', b'34', b'37', b''])
206
207 # input case 5: replace a small chunk which is near a deleted line
208 case5 = buildcontents(
209 [
210 (b'12', [1, 2]),
211 (b'3', [1]),
212 (b'4', [1, 2]),
213 ]
214 )
215
216 testfilefixup(case5, b'1cd4', [b'', b'1cd34', b'1cd4'])
217
218 # input case 6: base "changeset" is immutable
219 case6 = [b'1357', b'0125678']
220
221 testfilefixup(case6, b'0125678', case6)
222 testfilefixup(case6, b'0a25678', case6)
223 testfilefixup(case6, b'0a256b8', case6)
224 testfilefixup(case6, b'abcdefg', [b'1357', b'a1c5e7g'])
225 testfilefixup(case6, b'abcdef', case6)
226 testfilefixup(case6, b'', [b'1357', b'157'])
227 testfilefixup(case6, b'0123456789', [b'1357', b'0123456789'])
228
229 # input case 7: change an empty file
230 case7 = [b'']
231
232 testfilefixup(case7, b'1', case7)