~bzr-pqm/bzr/bzr.dev

5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2005-2011 Canonical Ltd
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
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, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
16
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
17
import os
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
18
import sys
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
19
1559.1.1 by Robert Collins
Merge in InterRepository API support.
20
import bzrlib
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
21
from bzrlib import (
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
22
    errors,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
23
    generate_ids,
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
24
    merge_directive,
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
25
    osutils,
26
    )
5579.3.1 by Jelmer Vernooij
Remove unused imports.
27
from bzrlib.conflicts import (
28
    ContentsConflict,
29
    TextConflict,
30
    PathConflict,
31
    )
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
32
from bzrlib.merge import (
33
    Merge3Merger,
34
    Diff3Merger,
35
    WeaveMerger,
36
    Merger,
37
    )
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
38
from bzrlib.osutils import getcwd, pathjoin
1534.7.140 by Aaron Bentley
Moved the merge stuff into merge.py
39
from bzrlib.transform import TreeTransform
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
40
from bzrlib.tests import TestCaseWithTransport, TestSkipped
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
41
from bzrlib.workingtree import WorkingTree
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
42
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
43
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
44
class MergeBuilder(object):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
45
    def __init__(self, dir=None):
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
46
        self.dir = osutils.mkdtemp(prefix="merge-test", dir=dir)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
47
        self.tree_root = generate_ids.gen_root_id()
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
48
        def wt(name):
49
           path = pathjoin(self.dir, name)
50
           os.mkdir(path)
1559.1.1 by Robert Collins
Merge in InterRepository API support.
51
           wt = bzrlib.bzrdir.BzrDir.create_standalone_workingtree(path)
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
52
           # the tests perform pulls, so need a branch that is writeable.
53
           wt.lock_write()
1731.1.33 by Aaron Bentley
Revert no-special-root changes
54
           wt.set_root_id(self.tree_root)
2255.7.14 by John Arbash Meinel
Make set_root_id() a no-op if the id doesn't change,
55
           wt.flush()
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
56
           tt = TreeTransform(wt)
57
           return wt, tt
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
58
        self.base, self.base_tt = wt('base')
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
59
        self.this, self.this_tt = wt('this')
60
        self.other, self.other_tt = wt('other')
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
61
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
62
    def get_cset_path(self, parent, name):
63
        if name is None:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
64
            if parent is not None:
65
                raise AssertionError()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
66
            return None
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
67
        return pathjoin(self.cset.entries[parent].path, name)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
68
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
69
    def add_file(self, id, parent, name, contents, executable, this=True,
1558.7.12 by Aaron Bentley
Additional spurious conflict test
70
                 base=True, other=True):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
71
        def new_file(tt):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
72
            parent_id = tt.trans_id_file_id(parent)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
73
            tt.new_file(name, parent_id, contents, id, executable)
1558.7.12 by Aaron Bentley
Additional spurious conflict test
74
        for option, tt in self.selected_transforms(this, base, other):
75
            if option is True:
76
                new_file(tt)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
77
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
78
    def merge(self, merge_type=Merge3Merger, interesting_ids=None, **kwargs):
4797.21.1 by Aaron Bentley
Fix merge when this_tree is not a WorkingTree.
79
        merger = self.make_merger(merge_type, interesting_ids, **kwargs)
80
        merger.do_merge()
81
        return merger.cooked_conflicts
82
83
    def make_preview_transform(self):
84
        merger = self.make_merger(Merge3Merger, None, this_revision_tree=True)
85
        return merger.make_preview_transform()
86
87
    def make_merger(self, merge_type, interesting_ids,
88
            this_revision_tree=False, **kwargs):
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
89
        self.base_tt.apply()
90
        self.base.commit('base commit')
91
        for tt, wt in ((self.this_tt, self.this), (self.other_tt, self.other)):
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
92
            # why does this not do wt.pull() ?
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
93
            wt.branch.pull(self.base.branch)
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
94
            wt.set_parent_ids([wt.branch.last_revision()])
2255.7.14 by John Arbash Meinel
Make set_root_id() a no-op if the id doesn't change,
95
            wt.flush()
96
            # We maintain a write lock, so make sure changes are flushed to
97
            # disk first
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
98
            tt.apply()
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
99
            wt.commit('branch commit')
2255.7.14 by John Arbash Meinel
Make set_root_id() a no-op if the id doesn't change,
100
            wt.flush()
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
101
            if len(wt.branch.revision_history()) != 2:
102
                raise AssertionError()
1559.1.1 by Robert Collins
Merge in InterRepository API support.
103
        self.this.branch.fetch(self.other.branch)
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
104
        other_basis = self.other.branch.basis_tree()
4797.21.1 by Aaron Bentley
Fix merge when this_tree is not a WorkingTree.
105
        if this_revision_tree:
106
            self.this.commit('message')
107
            this_tree = self.this.basis_tree()
108
        else:
109
            this_tree = self.this
110
        merger = merge_type(this_tree, self.this, self.base, other_basis,
111
                            interesting_ids=interesting_ids, do_merge=False,
112
                            this_branch=self.this.branch, **kwargs)
113
        return merger
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
114
115
    def list_transforms(self):
116
        return [self.this_tt, self.base_tt, self.other_tt]
117
118
    def selected_transforms(self, this, base, other):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
119
        pairs = [(this, self.this_tt), (base, self.base_tt),
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
120
                 (other, self.other_tt)]
121
        return [(v, tt) for (v, tt) in pairs if v is not None]
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
122
1185.12.34 by Aaron Bentley
Added symlink three-way tests
123
    def add_symlink(self, id, parent, name, contents):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
124
        for tt in self.list_transforms():
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
125
            parent_id = tt.trans_id_file_id(parent)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
126
            tt.new_symlink(name, parent_id, contents, id)
1185.12.34 by Aaron Bentley
Added symlink three-way tests
127
1534.7.130 by Aaron Bentley
More conflict handling, test porting
128
    def remove_file(self, file_id, base=False, this=False, other=False):
129
        for option, tt in self.selected_transforms(this, base, other):
130
            if option is True:
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
131
                trans_id = tt.trans_id_file_id(file_id)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
132
                tt.cancel_creation(trans_id)
133
                tt.cancel_versioning(trans_id)
134
                tt.set_executability(None, trans_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
135
4797.9.2 by Vincent Ladeuil
More tests.
136
    def add_dir(self, file_id, parent, name, this=True, base=True, other=True):
137
        for option, tt in self.selected_transforms(this, base, other):
138
            if option is True:
139
                parent_id = tt.trans_id_file_id(parent)
140
                tt.new_directory(name, parent_id, file_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
141
142
    def change_name(self, id, base=None, this=None, other=None):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
143
        for val, tt in ((base, self.base_tt), (this, self.this_tt),
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
144
                        (other, self.other_tt)):
145
            if val is None:
146
                continue
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
147
            trans_id = tt.trans_id_file_id(id)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
148
            parent_id = tt.final_parent(trans_id)
149
            tt.adjust_path(val, parent_id, trans_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
150
1534.7.130 by Aaron Bentley
More conflict handling, test porting
151
    def change_parent(self, file_id, base=None, this=None, other=None):
152
        for parent, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
153
            trans_id  = tt.trans_id_file_id(file_id)
154
            parent_id = tt.trans_id_file_id(parent)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
155
            tt.adjust_path(tt.final_name(trans_id), parent_id, trans_id)
156
157
    def change_contents(self, file_id, base=None, this=None, other=None):
158
        for contents, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
159
            trans_id = tt.trans_id_file_id(file_id)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
160
            tt.cancel_creation(trans_id)
161
            tt.create_file(contents, trans_id)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
162
1185.12.34 by Aaron Bentley
Added symlink three-way tests
163
    def change_target(self, id, base=None, this=None, other=None):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
164
        for target, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
165
            trans_id = tt.trans_id_file_id(id)
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
166
            tt.cancel_creation(trans_id)
167
            tt.create_symlink(target, trans_id)
1185.12.34 by Aaron Bentley
Added symlink three-way tests
168
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
169
    def change_perms(self, id, base=None, this=None, other=None):
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
170
        for executability, tt in self.selected_transforms(this, base, other):
1534.7.181 by Aaron Bentley
Renamed a bunch of functions
171
            trans_id = tt.trans_id_file_id(id)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
172
            tt.set_executability(None, trans_id)
173
            tt.set_executability(executability, trans_id)
1185.12.34 by Aaron Bentley
Added symlink three-way tests
174
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
175
    def change_perms_tree(self, id, tree, mode):
176
        os.chmod(tree.full_path(id), mode)
177
178
    def apply_inv_change(self, inventory_change, orig_inventory):
179
        orig_inventory_by_path = {}
180
        for file_id, path in orig_inventory.iteritems():
181
            orig_inventory_by_path[path] = file_id
182
183
        def parent_id(file_id):
184
            try:
185
                parent_dir = os.path.dirname(orig_inventory[file_id])
186
            except:
187
                print file_id
188
                raise
189
            if parent_dir == "":
190
                return None
191
            return orig_inventory_by_path[parent_dir]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
192
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
193
        def new_path(file_id):
1963.2.1 by Robey Pointer
remove usage of has_key()
194
            if fild_id in inventory_change:
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
195
                return inventory_change[file_id]
196
            else:
197
                parent = parent_id(file_id)
198
                if parent is None:
199
                    return orig_inventory[file_id]
200
                dirname = new_path(parent)
1185.50.31 by John Arbash Meinel
[merge] Denys Duchier, more tests for test_merge_core, and fixup of one test.
201
                return pathjoin(dirname, os.path.basename(orig_inventory[file_id]))
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
202
203
        new_inventory = {}
204
        for file_id in orig_inventory.iterkeys():
205
            path = new_path(file_id)
206
            if path is None:
207
                continue
208
            new_inventory[file_id] = path
209
210
        for file_id, path in inventory_change.iteritems():
1963.2.1 by Robey Pointer
remove usage of has_key()
211
            if file_id in orig_inventory:
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
212
                continue
213
            new_inventory[file_id] = path
214
        return new_inventory
215
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
216
    def unlock(self):
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
217
        self.base.unlock()
218
        self.this.unlock()
219
        self.other.unlock()
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
220
221
    def cleanup(self):
222
        self.unlock()
1996.3.18 by John Arbash Meinel
Now that mkdtemp and rmtree are lazy, they should not be directly improted.
223
        osutils.rmtree(self.dir)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
224
1448 by Robert Collins
revert symlinks correctly
225
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
226
class MergeTest(TestCaseWithTransport):
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
227
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
228
    def test_change_name(self):
229
        """Test renames"""
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
230
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
231
        builder.add_file("1", builder.tree_root, "name1", "hello1", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
232
        builder.change_name("1", other="name2")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
233
        builder.add_file("2", builder.tree_root, "name3", "hello2", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
234
        builder.change_name("2", base="name4")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
235
        builder.add_file("3", builder.tree_root, "name5", "hello3", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
236
        builder.change_name("3", this="name6")
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
237
        builder.merge()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
238
        builder.cleanup()
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
239
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
240
        builder.add_file("1", builder.tree_root, "name1", "hello1", False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
241
        builder.change_name("1", other="name2", this="name3")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
242
        conflicts = builder.merge()
1534.10.20 by Aaron Bentley
Got all tests passing
243
        self.assertEqual(conflicts, [PathConflict('name3', 'name2', '1')])
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
244
        builder.cleanup()
1558.2.2 by Aaron Bentley
Make remerge honour interesting-ids
245
246
    def test_merge_one(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
247
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
248
        builder.add_file("1", builder.tree_root, "name1", "hello1", True)
1558.2.2 by Aaron Bentley
Make remerge honour interesting-ids
249
        builder.change_contents("1", other="text4")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
250
        builder.add_file("2", builder.tree_root, "name2", "hello1", True)
1558.2.2 by Aaron Bentley
Make remerge honour interesting-ids
251
        builder.change_contents("2", other="text4")
252
        builder.merge(interesting_ids=["1"])
253
        self.assertEqual(builder.this.get_file("1").read(), "text4" )
254
        self.assertEqual(builder.this.get_file("2").read(), "hello1" )
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
255
        builder.cleanup()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
256
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
257
    def test_file_moves(self):
258
        """Test moves"""
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
259
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
260
        builder.add_dir("1", builder.tree_root, "dir1")
261
        builder.add_dir("2", builder.tree_root, "dir2")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
262
        builder.add_file("3", "1", "file1", "hello1", True)
263
        builder.add_file("4", "1", "file2", "hello2", True)
264
        builder.add_file("5", "1", "file3", "hello3", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
265
        builder.change_parent("3", other="2")
266
        builder.change_parent("4", this="2")
267
        builder.change_parent("5", base="2")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
268
        builder.merge()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
269
        builder.cleanup()
270
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
271
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
272
        builder.add_dir("1", builder.tree_root, "dir1")
273
        builder.add_dir("2", builder.tree_root, "dir2")
274
        builder.add_dir("3", builder.tree_root, "dir3")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
275
        builder.add_file("4", "1", "file1", "hello1", False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
276
        builder.change_parent("4", other="2", this="3")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
277
        conflicts = builder.merge()
1534.7.176 by abentley
Fixed up tests for Windows
278
        path2 = pathjoin('dir2', 'file1')
279
        path3 = pathjoin('dir3', 'file1')
1534.10.20 by Aaron Bentley
Got all tests passing
280
        self.assertEqual(conflicts, [PathConflict(path3, path2, '4')])
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
281
        builder.cleanup()
282
283
    def test_contents_merge(self):
284
        """Test merge3 merging"""
1534.7.130 by Aaron Bentley
More conflict handling, test porting
285
        self.do_contents_test(Merge3Merger)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
286
287
    def test_contents_merge2(self):
288
        """Test diff3 merging"""
2240.1.2 by Alexander Belchenko
test_contents_merge2: skip this test on win32
289
        if sys.platform == 'win32':
290
            raise TestSkipped("diff3 does not have --binary flag"
291
                              " and therefore always fails on win32")
1534.7.130 by Aaron Bentley
More conflict handling, test porting
292
        try:
293
            self.do_contents_test(Diff3Merger)
4869.2.4 by Andrew Bennetts
Delete some unused imports in test_merge_core.
294
        except errors.NoDiff3:
1534.7.130 by Aaron Bentley
More conflict handling, test porting
295
            raise TestSkipped("diff3 not available")
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
296
1534.7.136 by Aaron Bentley
Got WeaveMerger under test
297
    def test_contents_merge3(self):
298
        """Test diff3 merging"""
299
        self.do_contents_test(WeaveMerger)
300
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
301
    def test_reprocess_weave(self):
302
        # Reprocess works on weaves, and behaves as expected
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
303
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
304
        builder.add_file('a', builder.tree_root, 'blah', 'a', False)
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
305
        builder.change_contents('a', this='b\nc\nd\ne\n', other='z\nc\nd\ny\n')
306
        builder.merge(WeaveMerger, reprocess=True)
307
        expected = """<<<<<<< TREE
308
b
309
=======
310
z
311
>>>>>>> MERGE-SOURCE
312
c
313
d
314
<<<<<<< TREE
315
e
316
=======
317
y
318
>>>>>>> MERGE-SOURCE
319
"""
320
        self.assertEqualDiff(builder.this.get_file("a").read(), expected)
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
321
        builder.cleanup()
1551.6.8 by Aaron Bentley
Implemented reprocess for weave
322
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
323
    def do_contents_test(self, merge_factory):
324
        """Test merging with specified ContentsChange factory"""
325
        builder = self.contents_test_success(merge_factory)
326
        builder.cleanup()
327
        self.contents_test_conflicts(merge_factory)
328
329
    def contents_test_success(self, merge_factory):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
330
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
331
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
332
        builder.change_contents("1", other="text4")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
333
        builder.add_file("2", builder.tree_root, "name3", "text2", False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
334
        builder.change_contents("2", base="text5")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
335
        builder.add_file("3", builder.tree_root, "name5", "text3", True)
336
        builder.add_file("4", builder.tree_root, "name6", "text4", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
337
        builder.remove_file("4", base=True)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
338
        builder.add_file("5", builder.tree_root, "name7", "a\nb\nc\nd\ne\nf\n",
339
                         True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
340
        builder.change_contents("5", other="a\nz\nc\nd\ne\nf\n",
1558.6.4 by Aaron Bentley
Fixed merge-type weave
341
                                     this="a\nb\nc\nd\ne\nz\n")
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
342
        conflicts = builder.merge(merge_factory)
343
        try:
344
            self.assertEqual([], conflicts)
1551.8.40 by Aaron Bentley
Update test case style
345
            self.assertEqual("text4", builder.this.get_file("1").read())
346
            self.assertEqual("text2", builder.this.get_file("2").read())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
347
            self.assertEqual("a\nz\nc\nd\ne\nz\n",
1551.8.40 by Aaron Bentley
Update test case style
348
                             builder.this.get_file("5").read())
349
            self.assertTrue(builder.this.is_executable("1"))
350
            self.assertFalse(builder.this.is_executable("2"))
351
            self.assertTrue(builder.this.is_executable("3"))
1551.8.39 by Aaron Bentley
Fix diff3 conflict-reporting bug
352
        except:
353
            builder.unlock()
354
            raise
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
355
        return builder
356
357
    def contents_test_conflicts(self, merge_factory):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
358
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
359
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
360
        builder.change_contents("1", other="text4", this="text3")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
361
        builder.add_file("2", builder.tree_root, "name2", "text1", True)
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
362
        builder.change_contents("2", other="\x00", this="text3")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
363
        builder.add_file("3", builder.tree_root, "name3", "text5", False)
1534.10.35 by Aaron Bentley
Merge handles contents + executable + deletion conflict
364
        builder.change_perms("3", this=True)
365
        builder.change_contents('3', this='moretext')
366
        builder.remove_file('3', other=True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
367
        conflicts = builder.merge(merge_factory)
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
368
        self.assertEqual(conflicts, [TextConflict('name1', file_id='1'),
1534.10.35 by Aaron Bentley
Merge handles contents + executable + deletion conflict
369
                                     ContentsConflict('name2', file_id='2'),
370
                                     ContentsConflict('name3', file_id='3')])
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
371
        self.assertEqual(builder.this.get_file('2').read(), '\x00')
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
372
        builder.cleanup()
373
1185.12.34 by Aaron Bentley
Added symlink three-way tests
374
    def test_symlink_conflicts(self):
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
375
        if sys.platform != "win32":
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
376
            builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
377
            builder.add_symlink("2", builder.tree_root, "name2", "target1")
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
378
            builder.change_target("2", other="target4", base="text3")
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
379
            conflicts = builder.merge()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
380
            self.assertEqual(conflicts, [ContentsConflict('name2',
1534.10.20 by Aaron Bentley
Got all tests passing
381
                                                          file_id='2')])
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
382
            builder.cleanup()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
383
1185.12.34 by Aaron Bentley
Added symlink three-way tests
384
    def test_symlink_merge(self):
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
385
        if sys.platform != "win32":
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
386
            builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
387
            builder.add_symlink("1", builder.tree_root, "name1", "target1")
388
            builder.add_symlink("2", builder.tree_root, "name2", "target1")
389
            builder.add_symlink("3", builder.tree_root, "name3", "target1")
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
390
            builder.change_target("1", this="target2")
391
            builder.change_target("2", base="target2")
392
            builder.change_target("3", other="target2")
1534.7.129 by Aaron Bentley
Converted test cases to Tree Transform
393
            builder.merge()
1185.38.9 by John Arbash Meinel
[patch] Alexander Belchenko patch #9, skip stat tests for win32
394
            self.assertEqual(builder.this.get_symlink_target("1"), "target2")
395
            self.assertEqual(builder.this.get_symlink_target("2"), "target1")
396
            self.assertEqual(builder.this.get_symlink_target("3"), "target2")
397
            builder.cleanup()
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
398
1534.7.143 by Aaron Bentley
Prevented get_trans_id from automatically versioning file ids
399
    def test_no_passive_add(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
400
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
401
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1534.7.143 by Aaron Bentley
Prevented get_trans_id from automatically versioning file ids
402
        builder.remove_file("1", this=True)
403
        builder.merge()
1534.7.146 by Aaron Bentley
Fixed merge so tree root is auto-preserved, not by conflict resolution
404
        builder.cleanup()
1534.7.143 by Aaron Bentley
Prevented get_trans_id from automatically versioning file ids
405
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
406
    def test_perms_merge(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
407
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
408
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
409
        builder.change_perms("1", other=False)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
410
        builder.add_file("2", builder.tree_root, "name2", "text2", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
411
        builder.change_perms("2", base=False)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
412
        builder.add_file("3", builder.tree_root, "name3", "text3", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
413
        builder.change_perms("3", this=False)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
414
        builder.add_file('4', builder.tree_root, 'name4', 'text4', False)
1534.7.142 by Aaron Bentley
Fixed executability conflicts
415
        builder.change_perms('4', this=True)
416
        builder.remove_file('4', base=True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
417
        builder.merge()
418
        self.assertIs(builder.this.is_executable("1"), False)
419
        self.assertIs(builder.this.is_executable("2"), True)
420
        self.assertIs(builder.this.is_executable("3"), False)
1092.1.24 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
421
        builder.cleanup();
1092.1.25 by Robert Collins
prepare to write merge tests
422
1185.50.50 by John Arbash Meinel
[patch] Aaron Bentley: Merge deletes foo.new
423
    def test_new_suffix(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
424
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
425
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
426
        builder.change_contents("1", other="text3")
1731.1.33 by Aaron Bentley
Revert no-special-root changes
427
        builder.add_file("2", builder.tree_root, "name1.new", "text2", True)
1534.7.130 by Aaron Bentley
More conflict handling, test porting
428
        builder.merge()
429
        os.lstat(builder.this.id2abspath("2"))
430
        builder.cleanup()
1185.50.50 by John Arbash Meinel
[patch] Aaron Bentley: Merge deletes foo.new
431
1558.7.12 by Aaron Bentley
Additional spurious conflict test
432
    def test_spurious_conflict(self):
1711.7.21 by John Arbash Meinel
Run the merge_core tests underneath the current test directory, rather than TEMP
433
        builder = MergeBuilder(getcwd())
1731.1.33 by Aaron Bentley
Revert no-special-root changes
434
        builder.add_file("1", builder.tree_root, "name1", "text1", False)
1558.7.12 by Aaron Bentley
Additional spurious conflict test
435
        builder.remove_file("1", other=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
436
        builder.add_file("2", builder.tree_root, "name1", "text1", False,
1731.1.33 by Aaron Bentley
Revert no-special-root changes
437
                         this=False, base=False)
1558.7.12 by Aaron Bentley
Additional spurious conflict test
438
        conflicts = builder.merge()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
439
        self.assertEqual(conflicts, [])
1997.1.3 by Robert Collins
All WorkingTree methods which write to the tree, but not to the branch
440
        builder.cleanup()
1558.7.12 by Aaron Bentley
Additional spurious conflict test
441
2590.2.5 by Aaron Bentley
Allow selected files to be specified instead of selected ids
442
    def test_merge_one_renamed(self):
443
        builder = MergeBuilder(getcwd())
444
        builder.add_file('1', builder.tree_root, 'name1', 'text1a', False)
445
        builder.change_name('1', this='name2')
446
        builder.change_contents('1', other='text2')
447
        builder.merge(interesting_files=['name2'])
448
        self.assertEqual('text2', builder.this.get_file('1').read())
449
        builder.cleanup()
1448 by Robert Collins
revert symlinks correctly
450
4869.3.1 by Andrew Bennetts
Basic per-file merge hook.
451
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
452
class FunctionalMergeTest(TestCaseWithTransport):
1092.1.25 by Robert Collins
prepare to write merge tests
453
454
    def test_trivial_star_merge(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
455
        """Test that merges in a star shape Just Work."""
1092.1.33 by Robert Collins
pull the important stuff out of cmd_branch.run to branch.copy_branch
456
        # John starts a branch
1092.1.26 by Robert Collins
start writing star-topology test, realise we need smart-add change
457
        self.build_tree(("original/", "original/file1", "original/file2"))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
458
        tree = self.make_branch_and_tree('original')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
459
        branch = tree.branch
2568.2.7 by Robert Collins
Fix missed tests.
460
        tree.smart_add(["original"])
1508.1.10 by Robert Collins
bzrlib.add.smart_add_branch is now smart_add_tree. (Robert Collins)
461
        tree.commit("start branch.", verbose=False)
1092.1.33 by Robert Collins
pull the important stuff out of cmd_branch.run to branch.copy_branch
462
        # Mary branches it.
1092.1.34 by Robert Collins
unbreak cmd_branch now that something tests the core of it..
463
        self.build_tree(("mary/",))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
464
        branch.bzrdir.clone("mary")
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
465
        # Now John commits a change
466
        file = open("original/file1", "wt")
467
        file.write("John\n")
468
        file.close()
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
469
        tree.commit("change file1")
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
470
        # Mary does too
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
471
        mary_tree = WorkingTree.open('mary')
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
472
        mary_branch = mary_tree.branch
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
473
        file = open("mary/file2", "wt")
474
        file.write("Mary\n")
475
        file.close()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
476
        mary_tree.commit("change file2")
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
477
        # john should be able to merge with no conflicts.
1092.1.41 by Robert Collins
merge from abently, take his fixes for merge in preference
478
        base = [None, None]
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
479
        other = ("mary", -1)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
480
        tree.merge_from_branch(mary_tree.branch)
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
481
        self.assertEqual("John\n", open("original/file1", "rt").read())
482
        self.assertEqual("Mary\n", open("original/file2", "rt").read())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
483
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
484
    def test_conflicts(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
485
        wta = self.make_branch_and_tree('a')
4634.101.12 by John Arbash Meinel
Clean up the test_conflicts file for newer apis.
486
        self.build_tree_contents([('a/file', 'contents\n')])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
487
        wta.add('file')
488
        wta.commit('base revision', allow_pointless=False)
4634.101.12 by John Arbash Meinel
Clean up the test_conflicts file for newer apis.
489
        d_b = wta.branch.bzrdir.clone('b')
490
        self.build_tree_contents([('a/file', 'other contents\n')])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
491
        wta.commit('other revision', allow_pointless=False)
4634.101.12 by John Arbash Meinel
Clean up the test_conflicts file for newer apis.
492
        self.build_tree_contents([('b/file', 'this contents contents\n')])
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
493
        wtb = d_b.open_workingtree()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
494
        wtb.commit('this revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
495
        self.assertEqual(1, wtb.merge_from_branch(wta.branch))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
496
        self.assertPathExists('b/file.THIS')
497
        self.assertPathExists('b/file.BASE')
498
        self.assertPathExists('b/file.OTHER')
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
499
        wtb.revert()
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
500
        self.assertEqual(1, wtb.merge_from_branch(wta.branch,
501
                                                  merge_type=WeaveMerger))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
502
        self.assertPathExists('b/file')
503
        self.assertPathExists('b/file.THIS')
504
        self.assertPathExists('b/file.BASE')
505
        self.assertPathExists('b/file.OTHER')
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
506
4634.101.14 by John Arbash Meinel
Add a knownFailure case where we could drop a .BASE but fail to.
507
    def test_weave_conflicts_not_in_base(self):
508
        builder = self.make_branch_builder('source')
509
        builder.start_series()
4634.101.15 by John Arbash Meinel
Add bug #494197 as the source of the KnownFailure test.
510
        # See bug #494197
4634.101.14 by John Arbash Meinel
Add a knownFailure case where we could drop a .BASE but fail to.
511
        #  A        base revision (before criss-cross)
512
        #  |\
513
        #  B C      B does nothing, C adds 'foo'
514
        #  |X|
515
        #  D E      D and E modify foo in incompatible ways
516
        #
517
        # Merging will conflict, with C as a clean base text. However, the
518
        # current code uses A as the global base and 'foo' doesn't exist there.
519
        # It isn't trivial to create foo.BASE because it tries to look up
520
        # attributes like 'executable' in A.
521
        builder.build_snapshot('A-id', None, [
522
            ('add', ('', 'TREE_ROOT', 'directory', None))])
523
        builder.build_snapshot('B-id', ['A-id'], [])
524
        builder.build_snapshot('C-id', ['A-id'], [
525
            ('add', ('foo', 'foo-id', 'file', 'orig\ncontents\n'))])
526
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [
527
            ('add', ('foo', 'foo-id', 'file', 'orig\ncontents\nand D\n'))])
528
        builder.build_snapshot('E-id', ['C-id', 'B-id'], [
529
            ('modify', ('foo-id', 'orig\ncontents\nand E\n'))])
530
        builder.finish_series()
531
        tree = builder.get_branch().create_checkout('tree', lightweight=True)
532
        self.assertEqual(1, tree.merge_from_branch(tree.branch,
533
                                                   to_revision='D-id',
534
                                                   merge_type=WeaveMerger))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
535
        self.assertPathExists('tree/foo.THIS')
536
        self.assertPathExists('tree/foo.OTHER')
4634.101.14 by John Arbash Meinel
Add a knownFailure case where we could drop a .BASE but fail to.
537
        self.expectFailure('fail to create .BASE in some criss-cross merges',
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
538
            self.assertPathExists, 'tree/foo.BASE')
539
        self.assertPathExists('tree/foo.BASE')
4634.101.14 by John Arbash Meinel
Add a knownFailure case where we could drop a .BASE but fail to.
540
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
541
    def test_merge_unrelated(self):
542
        """Sucessfully merges unrelated branches with no common names"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
543
        wta = self.make_branch_and_tree('a')
544
        a = wta.branch
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
545
        file('a/a_file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
546
        wta.add('a_file')
547
        wta.commit('a_revision', allow_pointless=False)
548
        wtb = self.make_branch_and_tree('b')
549
        b = wtb.branch
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
550
        file('b/b_file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
551
        wtb.add('b_file')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
552
        b_rev = wtb.commit('b_revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
553
        wta.merge_from_branch(wtb.branch, b_rev, 'null:')
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
554
        self.assert_(os.path.lexists('a/b_file'))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
555
        self.assertEqual([b_rev], wta.get_parent_ids()[1:])
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
556
557
    def test_merge_unrelated_conflicting(self):
558
        """Sucessfully merges unrelated branches with common names"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
559
        wta = self.make_branch_and_tree('a')
560
        a = wta.branch
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
561
        file('a/file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
562
        wta.add('file')
563
        wta.commit('a_revision', allow_pointless=False)
564
        wtb = self.make_branch_and_tree('b')
565
        b = wtb.branch
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
566
        file('b/file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
567
        wtb.add('file')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
568
        b_rev = wtb.commit('b_revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
569
        wta.merge_from_branch(wtb.branch, b_rev, 'null:')
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
570
        self.assert_(os.path.lexists('a/file'))
571
        self.assert_(os.path.lexists('a/file.moved'))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
572
        self.assertEqual([b_rev], wta.get_parent_ids()[1:])
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
573
574
    def test_merge_deleted_conflicts(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
575
        wta = self.make_branch_and_tree('a')
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
576
        file('a/file', 'wb').write('contents\n')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
577
        wta.add('file')
578
        wta.commit('a_revision', allow_pointless=False)
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
579
        self.run_bzr('branch a b')
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
580
        os.remove('a/file')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
581
        wta.commit('removed file', allow_pointless=False)
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
582
        file('b/file', 'wb').write('changed contents\n')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
583
        wtb = WorkingTree.open('b')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
584
        wtb.commit('changed file', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
585
        wtb.merge_from_branch(wta.branch, wta.branch.last_revision(),
586
                              wta.branch.get_rev_id(1))
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
587
        self.assertFalse(os.path.lexists('b/file'))
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
588
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
589
    def test_merge_metadata_vs_deletion(self):
590
        """Conflict deletion vs metadata change"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
591
        a_wt = self.make_branch_and_tree('a')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
592
        file('a/file', 'wb').write('contents\n')
1508.1.15 by Robert Collins
Merge from mpool.
593
        a_wt.add('file')
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
594
        a_wt.commit('r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
595
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
596
        b_wt = WorkingTree.open('b')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
597
        os.chmod('b/file', 0755)
598
        os.remove('a/file')
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
599
        a_wt.commit('removed a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
600
        self.assertEqual(a_wt.branch.revno(), 2)
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
601
        self.assertFalse(os.path.exists('a/file'))
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
602
        b_wt.commit('exec a')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
603
        a_wt.merge_from_branch(b_wt.branch, b_wt.last_revision(), 'null:')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
604
        self.assert_(os.path.exists('a/file'))
1185.31.14 by John Arbash Meinel
[merge] bzr.dev
605
1185.33.102 by Denys Duchier
two new tests suggested by abentley
606
    def test_merge_swapping_renames(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
607
        a_wt = self.make_branch_and_tree('a')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
608
        file('a/un','wb').write('UN')
609
        file('a/deux','wb').write('DEUX')
2323.6.14 by Martin Pool
clearer test data for test merge_swapping_rename
610
        a_wt.add('un', 'un-id')
611
        a_wt.add('deux', 'deux-id')
1508.1.24 by Robert Collins
Add update command for use with checkouts.
612
        a_wt.commit('r0', rev_id='r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
613
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
614
        b_wt = WorkingTree.open('b')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
615
        b_wt.rename_one('un','tmp')
616
        b_wt.rename_one('deux','un')
617
        b_wt.rename_one('tmp','deux')
1508.1.24 by Robert Collins
Add update command for use with checkouts.
618
        b_wt.commit('r1', rev_id='r1')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
619
        self.assertEqual(0, a_wt.merge_from_branch(b_wt.branch,
620
            b_wt.branch.last_revision(), b_wt.branch.get_rev_id(1)))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
621
        self.assertPathExists('a/un')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
622
        self.assertTrue('a/deux')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
623
        self.assertFalse(os.path.exists('a/tmp'))
624
        self.assertEqual(file('a/un').read(),'DEUX')
625
        self.assertEqual(file('a/deux').read(),'UN')
626
627
    def test_merge_delete_and_add_same(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
628
        a_wt = self.make_branch_and_tree('a')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
629
        file('a/file', 'wb').write('THIS')
630
        a_wt.add('file')
631
        a_wt.commit('r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
632
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
633
        b_wt = WorkingTree.open('b')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
634
        os.remove('b/file')
635
        b_wt.commit('r1')
636
        file('b/file', 'wb').write('THAT')
637
        b_wt.add('file')
638
        b_wt.commit('r2')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
639
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
640
                               b_wt.branch.get_rev_id(1))
1185.33.102 by Denys Duchier
two new tests suggested by abentley
641
        self.assert_(os.path.exists('a/file'))
642
        self.assertEqual(file('a/file').read(),'THAT')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
643
644
    def test_merge_rename_before_create(self):
645
        """rename before create
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
646
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
647
        This case requires that you must not do creates
648
        before move-into-place:
649
650
        $ touch foo
651
        $ bzr add foo
652
        $ bzr commit
653
        $ bzr mv foo bar
654
        $ touch foo
655
        $ bzr add foo
656
        $ bzr commit
657
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
658
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
659
        file('a/foo', 'wb').write('A/FOO')
660
        a_wt.add('foo')
661
        a_wt.commit('added foo')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
662
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
663
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
664
        b_wt.rename_one('foo', 'bar')
665
        file('b/foo', 'wb').write('B/FOO')
666
        b_wt.add('foo')
667
        b_wt.commit('moved foo to bar, added new foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
668
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
669
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
670
671
    def test_merge_create_before_rename(self):
672
        """create before rename, target parents before children
673
674
        This case requires that you must not do move-into-place
675
        before creates, and that you must not do children after
676
        parents:
677
678
        $ touch foo
679
        $ bzr add foo
680
        $ bzr commit
681
        $ bzr mkdir bar
682
        $ bzr add bar
683
        $ bzr mv foo bar/foo
684
        $ bzr commit
685
        """
686
        os.mkdir('a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
687
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
688
        file('a/foo', 'wb').write('A/FOO')
689
        a_wt.add('foo')
690
        a_wt.commit('added foo')
2530.3.2 by Martin Pool
Refactoring run_bzr code into more of a common base.
691
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
692
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
693
        os.mkdir('b/bar')
694
        b_wt.add('bar')
695
        b_wt.rename_one('foo', 'bar/foo')
696
        b_wt.commit('created bar dir, moved foo into bar')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
697
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
698
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
699
700
    def test_merge_rename_to_temp_before_delete(self):
701
        """rename to temp before delete, source children before parents
702
703
        This case requires that you must not do deletes before
704
        move-out-of-the-way, and that you must not do children
705
        after parents:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
706
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
707
        $ mkdir foo
708
        $ touch foo/bar
709
        $ bzr add foo/bar
710
        $ bzr commit
711
        $ bzr mv foo/bar bar
712
        $ rmdir foo
713
        $ bzr commit
714
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
715
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
716
        os.mkdir('a/foo')
717
        file('a/foo/bar', 'wb').write('A/FOO/BAR')
718
        a_wt.add('foo')
719
        a_wt.add('foo/bar')
720
        a_wt.commit('added foo/bar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
721
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
722
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
723
        b_wt.rename_one('foo/bar', 'bar')
724
        os.rmdir('b/foo')
725
        b_wt.remove('foo')
726
        b_wt.commit('moved foo/bar to bar, deleted foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
727
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
728
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
729
730
    def test_merge_delete_before_rename_to_temp(self):
731
        """delete before rename to temp
732
733
        This case requires that you must not do
734
        move-out-of-the-way before deletes:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
735
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
736
        $ touch foo
737
        $ touch bar
738
        $ bzr add foo bar
739
        $ bzr commit
740
        $ rm foo
741
        $ bzr rm foo
742
        $ bzr mv bar foo
743
        $ bzr commit
744
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
745
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
746
        file('a/foo', 'wb').write('A/FOO')
747
        file('a/bar', 'wb').write('A/BAR')
748
        a_wt.add('foo')
749
        a_wt.add('bar')
750
        a_wt.commit('added foo and bar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
751
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
752
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
753
        os.unlink('b/foo')
754
        b_wt.remove('foo')
755
        b_wt.rename_one('bar', 'foo')
756
        b_wt.commit('deleted foo, renamed bar to foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
757
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
758
                               b_wt.branch.get_rev_id(1))
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
759
760
761
class TestMerger(TestCaseWithTransport):
762
763
    def set_up_trees(self):
764
        this = self.make_branch_and_tree('this')
765
        this.commit('rev1', rev_id='rev1')
766
        other = this.bzrdir.sprout('other').open_workingtree()
767
        this.commit('rev2a', rev_id='rev2a')
768
        other.commit('rev2b', rev_id='rev2b')
769
        return this, other
770
771
    def test_from_revision_ids(self):
772
        this, other = self.set_up_trees()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
773
        self.assertRaises(errors.NoSuchRevision, Merger.from_revision_ids,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
774
                          None, this, 'rev2b')
3010.1.8 by Robert Collins
test_merge_core locking correctness.
775
        this.lock_write()
776
        self.addCleanup(this.unlock)
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
777
        merger = Merger.from_revision_ids(None, this,
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
778
            'rev2b', other_branch=other.branch)
779
        self.assertEqual('rev2b', merger.other_rev_id)
780
        self.assertEqual('rev1', merger.base_rev_id)
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
781
        merger = Merger.from_revision_ids(None, this,
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
782
            'rev2b', 'rev2a', other_branch=other.branch)
783
        self.assertEqual('rev2a', merger.base_rev_id)
784
785
    def test_from_uncommitted(self):
786
        this, other = self.set_up_trees()
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
787
        merger = Merger.from_uncommitted(this, other, None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
788
        self.assertIs(other, merger.other_tree)
789
        self.assertIs(None, merger.other_rev_id)
790
        self.assertEqual('rev2b', merger.base_rev_id)
791
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
792
    def prepare_for_merging(self):
793
        this, other = self.set_up_trees()
794
        other.commit('rev3', rev_id='rev3')
795
        this.lock_write()
796
        self.addCleanup(this.unlock)
797
        return this, other
798
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
799
    def test_from_mergeable(self):
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
800
        this, other = self.prepare_for_merging()
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
801
        md = merge_directive.MergeDirective2.from_objects(
802
            other.branch.repository, 'rev3', 0, 0, 'this')
3010.1.8 by Robert Collins
test_merge_core locking correctness.
803
        other.lock_read()
804
        self.addCleanup(other.unlock)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
805
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
806
            None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
807
        md.patch = None
808
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
809
            None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
810
        self.assertEqual('inapplicable', verified)
811
        self.assertEqual('rev3', merger.other_rev_id)
812
        self.assertEqual('rev1', merger.base_rev_id)
813
        md.base_revision_id = 'rev2b'
814
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
815
            None)
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
816
        self.assertEqual('rev2b', merger.base_rev_id)
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
817
818
    def test_from_mergeable_old_merge_directive(self):
819
        this, other = self.prepare_for_merging()
820
        other.lock_write()
821
        self.addCleanup(other.unlock)
822
        md = merge_directive.MergeDirective.from_objects(
823
            other.branch.repository, 'rev3', 0, 0, 'this')
824
        merger, verified = Merger.from_mergeable(this, md,
4961.2.9 by Martin Pool
Rip out most remaining uses of DummyProgressBar
825
            None)
1551.19.44 by Aaron Bentley
Fix handling of old merge directives with stricter get_parent_map
826
        self.assertEqual('rev3', merger.other_rev_id)
827
        self.assertEqual('rev1', merger.base_rev_id)