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