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