~bzr-pqm/bzr/bzr.dev

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