Show More
@@ -1,365 +1,365 b'' | |||
|
1 | 1 | # Copyright (C) 2004, 2005 Canonical Ltd |
|
2 | 2 | # |
|
3 | 3 | # This program is free software; you can redistribute it and/or modify |
|
4 | 4 | # it under the terms of the GNU General Public License as published by |
|
5 | 5 | # the Free Software Foundation; either version 2 of the License, or |
|
6 | 6 | # (at your option) any later version. |
|
7 | 7 | # |
|
8 | 8 | # This program is distributed in the hope that it will be useful, |
|
9 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | 11 | # GNU General Public License for more details. |
|
12 | 12 | # |
|
13 | 13 | # You should have received a copy of the GNU General Public License |
|
14 | 14 | # along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
15 | 15 | |
|
16 | 16 | from __future__ import absolute_import |
|
17 | 17 | |
|
18 | 18 | import unittest |
|
19 | 19 | from mercurial import ( |
|
20 | 20 | error, |
|
21 | 21 | simplemerge, |
|
22 | 22 | util, |
|
23 | 23 | ) |
|
24 | 24 | |
|
25 | 25 | from mercurial.utils import ( |
|
26 | 26 | stringutil, |
|
27 | 27 | ) |
|
28 | 28 | |
|
29 | 29 | TestCase = unittest.TestCase |
|
30 | 30 | # bzr compatible interface, for the tests |
|
31 | 31 | class Merge3(simplemerge.Merge3Text): |
|
32 | 32 | """3-way merge of texts. |
|
33 | 33 | |
|
34 | 34 | Given BASE, OTHER, THIS, tries to produce a combined text |
|
35 | 35 | incorporating the changes from both BASE->OTHER and BASE->THIS. |
|
36 | 36 | All three will typically be sequences of lines.""" |
|
37 | 37 | def __init__(self, base, a, b): |
|
38 | 38 | basetext = b'\n'.join([i.strip(b'\n') for i in base] + [b'']) |
|
39 | 39 | atext = b'\n'.join([i.strip(b'\n') for i in a] + [b'']) |
|
40 | 40 | btext = b'\n'.join([i.strip(b'\n') for i in b] + [b'']) |
|
41 | 41 | if (stringutil.binary(basetext) or stringutil.binary(atext) |
|
42 | 42 | or stringutil.binary(btext)): |
|
43 | 43 | raise error.Abort(b"don't know how to merge binary files") |
|
44 | 44 | simplemerge.Merge3Text.__init__(self, basetext, atext, btext, |
|
45 | 45 | base, a, b) |
|
46 | 46 | |
|
47 | 47 | CantReprocessAndShowBase = simplemerge.CantReprocessAndShowBase |
|
48 | 48 | |
|
49 | 49 | def split_lines(t): |
|
50 | 50 | return util.stringio(t).readlines() |
|
51 | 51 | |
|
52 | 52 | ############################################################ |
|
53 | 53 | # test case data from the gnu diffutils manual |
|
54 | 54 | # common base |
|
55 | 55 | TZU = split_lines(b""" The Nameless is the origin of Heaven and Earth; |
|
56 | 56 | The named is the mother of all things. |
|
57 | 57 | |
|
58 | 58 | Therefore let there always be non-being, |
|
59 | 59 | so we may see their subtlety, |
|
60 | 60 | And let there always be being, |
|
61 | 61 | so we may see their outcome. |
|
62 | 62 | The two are the same, |
|
63 | 63 | But after they are produced, |
|
64 | 64 | they have different names. |
|
65 | 65 | They both may be called deep and profound. |
|
66 | 66 | Deeper and more profound, |
|
67 | 67 | The door of all subtleties! |
|
68 | 68 | """) |
|
69 | 69 | |
|
70 | 70 | LAO = split_lines(b""" The Way that can be told of is not the eternal Way; |
|
71 | 71 | The name that can be named is not the eternal name. |
|
72 | 72 | The Nameless is the origin of Heaven and Earth; |
|
73 | 73 | The Named is the mother of all things. |
|
74 | 74 | Therefore let there always be non-being, |
|
75 | 75 | so we may see their subtlety, |
|
76 | 76 | And let there always be being, |
|
77 | 77 | so we may see their outcome. |
|
78 | 78 | The two are the same, |
|
79 | 79 | But after they are produced, |
|
80 | 80 | they have different names. |
|
81 | 81 | """) |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | TAO = split_lines(b""" The Way that can be told of is not the eternal Way; |
|
85 | 85 | The name that can be named is not the eternal name. |
|
86 | 86 | The Nameless is the origin of Heaven and Earth; |
|
87 | 87 | The named is the mother of all things. |
|
88 | 88 | |
|
89 | 89 | Therefore let there always be non-being, |
|
90 | 90 | so we may see their subtlety, |
|
91 | 91 | And let there always be being, |
|
92 | 92 | so we may see their result. |
|
93 | 93 | The two are the same, |
|
94 | 94 | But after they are produced, |
|
95 | 95 | they have different names. |
|
96 | 96 | |
|
97 | 97 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
|
98 | 98 | |
|
99 | 99 | """) |
|
100 | 100 | |
|
101 | 101 | MERGED_RESULT = split_lines(b"""\ |
|
102 | 102 | The Way that can be told of is not the eternal Way; |
|
103 | 103 | The name that can be named is not the eternal name. |
|
104 | 104 | The Nameless is the origin of Heaven and Earth; |
|
105 | 105 | The Named is the mother of all things. |
|
106 | 106 | Therefore let there always be non-being, |
|
107 | 107 | so we may see their subtlety, |
|
108 | 108 | And let there always be being, |
|
109 | 109 | so we may see their result. |
|
110 | 110 | The two are the same, |
|
111 | 111 | But after they are produced, |
|
112 | they have different names. | |
|
113 | <<<<<<< LAO | |
|
114 | ======= | |
|
112 | they have different names.\ | |
|
113 | \n<<<<<<< LAO\ | |
|
114 | \n======= | |
|
115 | 115 | |
|
116 | 116 | -- The Way of Lao-Tzu, tr. Wing-tsit Chan |
|
117 | ||
|
118 | >>>>>>> TAO | |
|
117 | \ | |
|
118 | \n>>>>>>> TAO | |
|
119 | 119 | """) |
|
120 | 120 | |
|
121 | 121 | class TestMerge3(TestCase): |
|
122 | 122 | def log(self, msg): |
|
123 | 123 | pass |
|
124 | 124 | |
|
125 | 125 | def test_no_changes(self): |
|
126 | 126 | """No conflicts because nothing changed""" |
|
127 | 127 | m3 = Merge3([b'aaa', b'bbb'], |
|
128 | 128 | [b'aaa', b'bbb'], |
|
129 | 129 | [b'aaa', b'bbb']) |
|
130 | 130 | |
|
131 | 131 | self.assertEqual(m3.find_unconflicted(), |
|
132 | 132 | [(0, 2)]) |
|
133 | 133 | |
|
134 | 134 | self.assertEqual(list(m3.find_sync_regions()), |
|
135 | 135 | [(0, 2, |
|
136 | 136 | 0, 2, |
|
137 | 137 | 0, 2), |
|
138 | 138 | (2, 2, 2, 2, 2, 2)]) |
|
139 | 139 | |
|
140 | 140 | self.assertEqual(list(m3.merge_regions()), |
|
141 | 141 | [(b'unchanged', 0, 2)]) |
|
142 | 142 | |
|
143 | 143 | self.assertEqual(list(m3.merge_groups()), |
|
144 | 144 | [(b'unchanged', [b'aaa', b'bbb'])]) |
|
145 | 145 | |
|
146 | 146 | def test_front_insert(self): |
|
147 | 147 | m3 = Merge3([b'zz'], |
|
148 | 148 | [b'aaa', b'bbb', b'zz'], |
|
149 | 149 | [b'zz']) |
|
150 | 150 | |
|
151 | 151 | # todo: should use a sentinel at end as from get_matching_blocks |
|
152 | 152 | # to match without zz |
|
153 | 153 | self.assertEqual(list(m3.find_sync_regions()), |
|
154 | 154 | [(0, 1, 2, 3, 0, 1), |
|
155 | 155 | (1, 1, 3, 3, 1, 1)]) |
|
156 | 156 | |
|
157 | 157 | self.assertEqual(list(m3.merge_regions()), |
|
158 | 158 | [(b'a', 0, 2), |
|
159 | 159 | (b'unchanged', 0, 1)]) |
|
160 | 160 | |
|
161 | 161 | self.assertEqual(list(m3.merge_groups()), |
|
162 | 162 | [(b'a', [b'aaa', b'bbb']), |
|
163 | 163 | (b'unchanged', [b'zz'])]) |
|
164 | 164 | |
|
165 | 165 | def test_null_insert(self): |
|
166 | 166 | m3 = Merge3([], |
|
167 | 167 | [b'aaa', b'bbb'], |
|
168 | 168 | []) |
|
169 | 169 | # todo: should use a sentinel at end as from get_matching_blocks |
|
170 | 170 | # to match without zz |
|
171 | 171 | self.assertEqual(list(m3.find_sync_regions()), |
|
172 | 172 | [(0, 0, 2, 2, 0, 0)]) |
|
173 | 173 | |
|
174 | 174 | self.assertEqual(list(m3.merge_regions()), |
|
175 | 175 | [(b'a', 0, 2)]) |
|
176 | 176 | |
|
177 | 177 | self.assertEqual(list(m3.merge_lines()), |
|
178 | 178 | [b'aaa', b'bbb']) |
|
179 | 179 | |
|
180 | 180 | def test_no_conflicts(self): |
|
181 | 181 | """No conflicts because only one side changed""" |
|
182 | 182 | m3 = Merge3([b'aaa', b'bbb'], |
|
183 | 183 | [b'aaa', b'111', b'bbb'], |
|
184 | 184 | [b'aaa', b'bbb']) |
|
185 | 185 | |
|
186 | 186 | self.assertEqual(m3.find_unconflicted(), |
|
187 | 187 | [(0, 1), (1, 2)]) |
|
188 | 188 | |
|
189 | 189 | self.assertEqual(list(m3.find_sync_regions()), |
|
190 | 190 | [(0, 1, 0, 1, 0, 1), |
|
191 | 191 | (1, 2, 2, 3, 1, 2), |
|
192 | 192 | (2, 2, 3, 3, 2, 2)]) |
|
193 | 193 | |
|
194 | 194 | self.assertEqual(list(m3.merge_regions()), |
|
195 | 195 | [(b'unchanged', 0, 1), |
|
196 | 196 | (b'a', 1, 2), |
|
197 | 197 | (b'unchanged', 1, 2)]) |
|
198 | 198 | |
|
199 | 199 | def test_append_a(self): |
|
200 | 200 | m3 = Merge3([b'aaa\n', b'bbb\n'], |
|
201 | 201 | [b'aaa\n', b'bbb\n', b'222\n'], |
|
202 | 202 | [b'aaa\n', b'bbb\n']) |
|
203 | 203 | |
|
204 | 204 | self.assertEqual(b''.join(m3.merge_lines()), |
|
205 | 205 | b'aaa\nbbb\n222\n') |
|
206 | 206 | |
|
207 | 207 | def test_append_b(self): |
|
208 | 208 | m3 = Merge3([b'aaa\n', b'bbb\n'], |
|
209 | 209 | [b'aaa\n', b'bbb\n'], |
|
210 | 210 | [b'aaa\n', b'bbb\n', b'222\n']) |
|
211 | 211 | |
|
212 | 212 | self.assertEqual(b''.join(m3.merge_lines()), |
|
213 | 213 | b'aaa\nbbb\n222\n') |
|
214 | 214 | |
|
215 | 215 | def test_append_agreement(self): |
|
216 | 216 | m3 = Merge3([b'aaa\n', b'bbb\n'], |
|
217 | 217 | [b'aaa\n', b'bbb\n', b'222\n'], |
|
218 | 218 | [b'aaa\n', b'bbb\n', b'222\n']) |
|
219 | 219 | |
|
220 | 220 | self.assertEqual(b''.join(m3.merge_lines()), |
|
221 | 221 | b'aaa\nbbb\n222\n') |
|
222 | 222 | |
|
223 | 223 | def test_append_clash(self): |
|
224 | 224 | m3 = Merge3([b'aaa\n', b'bbb\n'], |
|
225 | 225 | [b'aaa\n', b'bbb\n', b'222\n'], |
|
226 | 226 | [b'aaa\n', b'bbb\n', b'333\n']) |
|
227 | 227 | |
|
228 | 228 | ml = m3.merge_lines(name_a=b'a', |
|
229 | 229 | name_b=b'b', |
|
230 | 230 | start_marker=b'<<', |
|
231 | 231 | mid_marker=b'--', |
|
232 | 232 | end_marker=b'>>') |
|
233 | 233 | self.assertEqual(b''.join(ml), |
|
234 | 234 | b'aaa\n' |
|
235 | 235 | b'bbb\n' |
|
236 | 236 | b'<< a\n' |
|
237 | 237 | b'222\n' |
|
238 | 238 | b'--\n' |
|
239 | 239 | b'333\n' |
|
240 | 240 | b'>> b\n' |
|
241 | 241 | ) |
|
242 | 242 | |
|
243 | 243 | def test_insert_agreement(self): |
|
244 | 244 | m3 = Merge3([b'aaa\n', b'bbb\n'], |
|
245 | 245 | [b'aaa\n', b'222\n', b'bbb\n'], |
|
246 | 246 | [b'aaa\n', b'222\n', b'bbb\n']) |
|
247 | 247 | |
|
248 | 248 | ml = m3.merge_lines(name_a=b'a', |
|
249 | 249 | name_b=b'b', |
|
250 | 250 | start_marker=b'<<', |
|
251 | 251 | mid_marker=b'--', |
|
252 | 252 | end_marker=b'>>') |
|
253 | 253 | self.assertEqual(b''.join(ml), b'aaa\n222\nbbb\n') |
|
254 | 254 | |
|
255 | 255 | |
|
256 | 256 | def test_insert_clash(self): |
|
257 | 257 | """Both try to insert lines in the same place.""" |
|
258 | 258 | m3 = Merge3([b'aaa\n', b'bbb\n'], |
|
259 | 259 | [b'aaa\n', b'111\n', b'bbb\n'], |
|
260 | 260 | [b'aaa\n', b'222\n', b'bbb\n']) |
|
261 | 261 | |
|
262 | 262 | self.assertEqual(m3.find_unconflicted(), |
|
263 | 263 | [(0, 1), (1, 2)]) |
|
264 | 264 | |
|
265 | 265 | self.assertEqual(list(m3.find_sync_regions()), |
|
266 | 266 | [(0, 1, 0, 1, 0, 1), |
|
267 | 267 | (1, 2, 2, 3, 2, 3), |
|
268 | 268 | (2, 2, 3, 3, 3, 3)]) |
|
269 | 269 | |
|
270 | 270 | self.assertEqual(list(m3.merge_regions()), |
|
271 | 271 | [(b'unchanged', 0, 1), |
|
272 | 272 | (b'conflict', 1, 1, 1, 2, 1, 2), |
|
273 | 273 | (b'unchanged', 1, 2)]) |
|
274 | 274 | |
|
275 | 275 | self.assertEqual(list(m3.merge_groups()), |
|
276 | 276 | [(b'unchanged', [b'aaa\n']), |
|
277 | 277 | (b'conflict', [], [b'111\n'], [b'222\n']), |
|
278 | 278 | (b'unchanged', [b'bbb\n']), |
|
279 | 279 | ]) |
|
280 | 280 | |
|
281 | 281 | ml = m3.merge_lines(name_a=b'a', |
|
282 | 282 | name_b=b'b', |
|
283 | 283 | start_marker=b'<<', |
|
284 | 284 | mid_marker=b'--', |
|
285 | 285 | end_marker=b'>>') |
|
286 | 286 | self.assertEqual(b''.join(ml), |
|
287 | 287 | b'''aaa |
|
288 | 288 | << a |
|
289 | 289 | 111 |
|
290 | 290 | -- |
|
291 | 291 | 222 |
|
292 | 292 | >> b |
|
293 | 293 | bbb |
|
294 | 294 | ''') |
|
295 | 295 | |
|
296 | 296 | def test_replace_clash(self): |
|
297 | 297 | """Both try to insert lines in the same place.""" |
|
298 | 298 | m3 = Merge3([b'aaa', b'000', b'bbb'], |
|
299 | 299 | [b'aaa', b'111', b'bbb'], |
|
300 | 300 | [b'aaa', b'222', b'bbb']) |
|
301 | 301 | |
|
302 | 302 | self.assertEqual(m3.find_unconflicted(), |
|
303 | 303 | [(0, 1), (2, 3)]) |
|
304 | 304 | |
|
305 | 305 | self.assertEqual(list(m3.find_sync_regions()), |
|
306 | 306 | [(0, 1, 0, 1, 0, 1), |
|
307 | 307 | (2, 3, 2, 3, 2, 3), |
|
308 | 308 | (3, 3, 3, 3, 3, 3)]) |
|
309 | 309 | |
|
310 | 310 | def test_replace_multi(self): |
|
311 | 311 | """Replacement with regions of different size.""" |
|
312 | 312 | m3 = Merge3([b'aaa', b'000', b'000', b'bbb'], |
|
313 | 313 | [b'aaa', b'111', b'111', b'111', b'bbb'], |
|
314 | 314 | [b'aaa', b'222', b'222', b'222', b'222', b'bbb']) |
|
315 | 315 | |
|
316 | 316 | self.assertEqual(m3.find_unconflicted(), |
|
317 | 317 | [(0, 1), (3, 4)]) |
|
318 | 318 | |
|
319 | 319 | |
|
320 | 320 | self.assertEqual(list(m3.find_sync_regions()), |
|
321 | 321 | [(0, 1, 0, 1, 0, 1), |
|
322 | 322 | (3, 4, 4, 5, 5, 6), |
|
323 | 323 | (4, 4, 5, 5, 6, 6)]) |
|
324 | 324 | |
|
325 | 325 | def test_merge_poem(self): |
|
326 | 326 | """Test case from diff3 manual""" |
|
327 | 327 | m3 = Merge3(TZU, LAO, TAO) |
|
328 | 328 | ml = list(m3.merge_lines(b'LAO', b'TAO')) |
|
329 | 329 | self.log(b'merge result:') |
|
330 | 330 | self.log(b''.join(ml)) |
|
331 | 331 | self.assertEqual(ml, MERGED_RESULT) |
|
332 | 332 | |
|
333 | 333 | def test_binary(self): |
|
334 | 334 | with self.assertRaises(error.Abort): |
|
335 | 335 | Merge3([b'\x00'], [b'a'], [b'b']) |
|
336 | 336 | |
|
337 | 337 | def test_dos_text(self): |
|
338 | 338 | base_text = b'a\r\n' |
|
339 | 339 | this_text = b'b\r\n' |
|
340 | 340 | other_text = b'c\r\n' |
|
341 | 341 | m3 = Merge3(base_text.splitlines(True), other_text.splitlines(True), |
|
342 | 342 | this_text.splitlines(True)) |
|
343 | 343 | m_lines = m3.merge_lines(b'OTHER', b'THIS') |
|
344 | 344 | self.assertEqual(b'<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n' |
|
345 | 345 | b'>>>>>>> THIS\r\n'.splitlines(True), list(m_lines)) |
|
346 | 346 | |
|
347 | 347 | def test_mac_text(self): |
|
348 | 348 | base_text = b'a\r' |
|
349 | 349 | this_text = b'b\r' |
|
350 | 350 | other_text = b'c\r' |
|
351 | 351 | m3 = Merge3(base_text.splitlines(True), other_text.splitlines(True), |
|
352 | 352 | this_text.splitlines(True)) |
|
353 | 353 | m_lines = m3.merge_lines(b'OTHER', b'THIS') |
|
354 | 354 | self.assertEqual(b'<<<<<<< OTHER\rc\r=======\rb\r' |
|
355 | 355 | b'>>>>>>> THIS\r'.splitlines(True), list(m_lines)) |
|
356 | 356 | |
|
357 | 357 | if __name__ == '__main__': |
|
358 | 358 | # hide the timer |
|
359 | 359 | import time |
|
360 | 360 | orig = time.time |
|
361 | 361 | try: |
|
362 | 362 | time.time = lambda: 0 |
|
363 | 363 | unittest.main() |
|
364 | 364 | finally: |
|
365 | 365 | time.time = orig |
General Comments 0
You need to be logged in to leave comments.
Login now