~bzr-pqm/bzr/bzr.dev

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