/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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.
1534.7.84 by Aaron Bentley
Added reprocess support, support for varying merge types
461
        merge_type = Merge3Merger
1092.1.41 by Robert Collins
merge from abently, take his fixes for merge in preference
462
        base = [None, None]
1092.1.38 by Robert Collins
make a default merge choose a sane base with branch.common_ancestor
463
        other = ("mary", -1)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
464
        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
465
        self.assertEqual("John\n", open("original/file1", "rt").read())
466
        self.assertEqual("Mary\n", open("original/file2", "rt").read())
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
467
 
468
    def test_conflicts(self):
469
        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.
470
        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.
471
        a = wta.branch
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
472
        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.
473
        wta.add('file')
474
        wta.commit('base revision', allow_pointless=False)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
475
        d_b = a.bzrdir.clone('b')
476
        b = d_b.open_branch()
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
477
        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.
478
        wta.commit('other revision', allow_pointless=False)
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
479
        file('b/file', 'wb').write('this contents contents\n')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
480
        wtb = d_b.open_workingtree()
1534.4.36 by Robert Collins
Finish deprecating Branch.working_tree()
481
        wtb.commit('this revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
482
        self.assertEqual(1, wtb.merge_from_branch(wta.branch))
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
483
        self.assert_(os.path.lexists('b/file.THIS'))
484
        self.assert_(os.path.lexists('b/file.BASE'))
485
        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'
486
        wtb.revert()
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
487
        self.assertEqual(1, wtb.merge_from_branch(wta.branch,
488
                                                  merge_type=WeaveMerger))
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
489
        self.assert_(os.path.lexists('b/file'))
490
        self.assert_(os.path.lexists('b/file.THIS'))
491
        self.assert_(not os.path.lexists('b/file.BASE'))
492
        self.assert_(os.path.lexists('b/file.OTHER'))
1185.12.85 by Aaron Bentley
Added conflict handling for weave merges
493
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
494
    def test_merge_unrelated(self):
495
        """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.
496
        wta = self.make_branch_and_tree('a')
497
        a = wta.branch
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
498
        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.
499
        wta.add('a_file')
500
        wta.commit('a_revision', allow_pointless=False)
501
        wtb = self.make_branch_and_tree('b')
502
        b = wtb.branch
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
503
        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.
504
        wtb.add('b_file')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
505
        b_rev = wtb.commit('b_revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
506
        wta.merge_from_branch(wtb.branch, b_rev, 'null:')
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
507
        self.assert_(os.path.lexists('a/b_file'))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
508
        self.assertEqual([b_rev], wta.get_parent_ids()[1:])
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
509
510
    def test_merge_unrelated_conflicting(self):
511
        """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.
512
        wta = self.make_branch_and_tree('a')
513
        a = wta.branch
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
514
        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.
515
        wta.add('file')
516
        wta.commit('a_revision', allow_pointless=False)
517
        wtb = self.make_branch_and_tree('b')
518
        b = wtb.branch
1185.12.99 by Aaron Bentley
Handled conflicts with versioned files better
519
        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.
520
        wtb.add('file')
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
521
        b_rev = wtb.commit('b_revision', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
522
        wta.merge_from_branch(wtb.branch, b_rev, 'null:')
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
523
        self.assert_(os.path.lexists('a/file'))
524
        self.assert_(os.path.lexists('a/file.moved'))
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
525
        self.assertEqual([b_rev], wta.get_parent_ids()[1:])
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
526
527
    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.
528
        wta = self.make_branch_and_tree('a')
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
529
        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.
530
        wta.add('file')
531
        wta.commit('a_revision', allow_pointless=False)
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
532
        self.run_bzr('branch a b')
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
533
        os.remove('a/file')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
534
        wta.commit('removed file', allow_pointless=False)
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
535
        file('b/file', 'wb').write('changed contents\n')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
536
        wtb = WorkingTree.open('b')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
537
        wtb.commit('changed file', allow_pointless=False)
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
538
        wtb.merge_from_branch(wta.branch, wta.branch.last_revision(),
539
                              wta.branch.get_rev_id(1))
1185.31.10 by John Arbash Meinel
Adding merge-delete-conflicts test case.
540
        self.failIf(os.path.lexists('b/file'))
541
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
542
    def test_merge_metadata_vs_deletion(self):
543
        """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.
544
        a_wt = self.make_branch_and_tree('a')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
545
        file('a/file', 'wb').write('contents\n')
1508.1.15 by Robert Collins
Merge from mpool.
546
        a_wt.add('file')
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
547
        a_wt.commit('r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
548
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
549
        b_wt = WorkingTree.open('b')
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
550
        os.chmod('b/file', 0755)
551
        os.remove('a/file')
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
552
        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.
553
        self.assertEqual(a_wt.branch.revno(), 2)
1185.33.23 by Martin Pool
Add test for merging metadata change vs file deletion
554
        self.assertFalse(os.path.exists('a/file'))
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
555
        b_wt.commit('exec a')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
556
        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
557
        self.assert_(os.path.exists('a/file'))
1185.31.14 by John Arbash Meinel
[merge] bzr.dev
558
1185.33.102 by Denys Duchier
two new tests suggested by abentley
559
    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.
560
        a_wt = self.make_branch_and_tree('a')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
561
        file('a/un','wb').write('UN')
562
        file('a/deux','wb').write('DEUX')
2323.6.14 by Martin Pool
clearer test data for test merge_swapping_rename
563
        a_wt.add('un', 'un-id')
564
        a_wt.add('deux', 'deux-id')
1508.1.24 by Robert Collins
Add update command for use with checkouts.
565
        a_wt.commit('r0', rev_id='r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
566
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
567
        b_wt = WorkingTree.open('b')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
568
        b_wt.rename_one('un','tmp')
569
        b_wt.rename_one('deux','un')
570
        b_wt.rename_one('tmp','deux')
1508.1.24 by Robert Collins
Add update command for use with checkouts.
571
        b_wt.commit('r1', rev_id='r1')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
572
        self.assertEqual(0, a_wt.merge_from_branch(b_wt.branch,
573
            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.
574
        self.failUnlessExists('a/un')
575
        self.failUnless('a/deux')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
576
        self.assertFalse(os.path.exists('a/tmp'))
577
        self.assertEqual(file('a/un').read(),'DEUX')
578
        self.assertEqual(file('a/deux').read(),'UN')
579
580
    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.
581
        a_wt = self.make_branch_and_tree('a')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
582
        file('a/file', 'wb').write('THIS')
583
        a_wt.add('file')
584
        a_wt.commit('r0')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
585
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
586
        b_wt = WorkingTree.open('b')
1185.33.102 by Denys Duchier
two new tests suggested by abentley
587
        os.remove('b/file')
588
        b_wt.commit('r1')
589
        file('b/file', 'wb').write('THAT')
590
        b_wt.add('file')
591
        b_wt.commit('r2')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
592
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
593
                               b_wt.branch.get_rev_id(1))
1185.33.102 by Denys Duchier
two new tests suggested by abentley
594
        self.assert_(os.path.exists('a/file'))
595
        self.assertEqual(file('a/file').read(),'THAT')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
596
597
    def test_merge_rename_before_create(self):
598
        """rename before create
599
        
600
        This case requires that you must not do creates
601
        before move-into-place:
602
603
        $ touch foo
604
        $ bzr add foo
605
        $ bzr commit
606
        $ bzr mv foo bar
607
        $ touch foo
608
        $ bzr add foo
609
        $ bzr commit
610
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
611
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
612
        file('a/foo', 'wb').write('A/FOO')
613
        a_wt.add('foo')
614
        a_wt.commit('added foo')
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.103 by Denys Duchier
4 new merge tests suggested by abentley
617
        b_wt.rename_one('foo', 'bar')
618
        file('b/foo', 'wb').write('B/FOO')
619
        b_wt.add('foo')
620
        b_wt.commit('moved foo to bar, added new foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
621
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
622
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
623
624
    def test_merge_create_before_rename(self):
625
        """create before rename, target parents before children
626
627
        This case requires that you must not do move-into-place
628
        before creates, and that you must not do children after
629
        parents:
630
631
        $ touch foo
632
        $ bzr add foo
633
        $ bzr commit
634
        $ bzr mkdir bar
635
        $ bzr add bar
636
        $ bzr mv foo bar/foo
637
        $ bzr commit
638
        """
639
        os.mkdir('a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
640
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
641
        file('a/foo', 'wb').write('A/FOO')
642
        a_wt.add('foo')
643
        a_wt.commit('added foo')
2530.3.2 by Martin Pool
Refactoring run_bzr code into more of a common base.
644
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
645
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
646
        os.mkdir('b/bar')
647
        b_wt.add('bar')
648
        b_wt.rename_one('foo', 'bar/foo')
649
        b_wt.commit('created bar dir, moved foo into bar')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
650
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
651
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
652
653
    def test_merge_rename_to_temp_before_delete(self):
654
        """rename to temp before delete, source children before parents
655
656
        This case requires that you must not do deletes before
657
        move-out-of-the-way, and that you must not do children
658
        after parents:
659
        
660
        $ mkdir foo
661
        $ touch foo/bar
662
        $ bzr add foo/bar
663
        $ bzr commit
664
        $ bzr mv foo/bar bar
665
        $ rmdir foo
666
        $ bzr commit
667
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
668
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
669
        os.mkdir('a/foo')
670
        file('a/foo/bar', 'wb').write('A/FOO/BAR')
671
        a_wt.add('foo')
672
        a_wt.add('foo/bar')
673
        a_wt.commit('added foo/bar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
674
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
675
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
676
        b_wt.rename_one('foo/bar', 'bar')
677
        os.rmdir('b/foo')
678
        b_wt.remove('foo')
679
        b_wt.commit('moved foo/bar to bar, deleted foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
680
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
681
                               b_wt.branch.get_rev_id(1))
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
682
683
    def test_merge_delete_before_rename_to_temp(self):
684
        """delete before rename to temp
685
686
        This case requires that you must not do
687
        move-out-of-the-way before deletes:
688
        
689
        $ touch foo
690
        $ touch bar
691
        $ bzr add foo bar
692
        $ bzr commit
693
        $ rm foo
694
        $ bzr rm foo
695
        $ bzr mv bar foo
696
        $ bzr commit
697
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
698
        a_wt = self.make_branch_and_tree('a')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
699
        file('a/foo', 'wb').write('A/FOO')
700
        file('a/bar', 'wb').write('A/BAR')
701
        a_wt.add('foo')
702
        a_wt.add('bar')
703
        a_wt.commit('added foo and bar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
704
        self.run_bzr('branch a b')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
705
        b_wt = WorkingTree.open('b')
1185.33.103 by Denys Duchier
4 new merge tests suggested by abentley
706
        os.unlink('b/foo')
707
        b_wt.remove('foo')
708
        b_wt.rename_one('bar', 'foo')
709
        b_wt.commit('deleted foo, renamed bar to foo')
1551.15.70 by Aaron Bentley
Avoid using builtins.merge
710
        a_wt.merge_from_branch(b_wt.branch, b_wt.branch.last_revision(),
711
                               b_wt.branch.get_rev_id(1))
1551.15.76 by Aaron Bentley
Add unit tests from Merger.from_*
712
713
714
class TestMerger(TestCaseWithTransport):
715
716
    def set_up_trees(self):
717
        this = self.make_branch_and_tree('this')
718
        this.commit('rev1', rev_id='rev1')
719
        other = this.bzrdir.sprout('other').open_workingtree()
720
        this.commit('rev2a', rev_id='rev2a')
721
        other.commit('rev2b', rev_id='rev2b')
722
        return this, other
723
724
    def test_from_revision_ids(self):
725
        this, other = self.set_up_trees()
726
        self.assertRaises(errors.RevisionNotPresent, Merger.from_revision_ids,
727
                          progress.DummyProgress(), this, 'rev2b')
728
        merger = Merger.from_revision_ids(progress.DummyProgress(), this,
729
            'rev2b', other_branch=other.branch)
730
        self.assertEqual('rev2b', merger.other_rev_id)
731
        self.assertEqual('rev1', merger.base_rev_id)
732
        merger = Merger.from_revision_ids(progress.DummyProgress(), this,
733
            'rev2b', 'rev2a', other_branch=other.branch)
734
        self.assertEqual('rev2a', merger.base_rev_id)
735
736
    def test_from_uncommitted(self):
737
        this, other = self.set_up_trees()
738
        merger = Merger.from_uncommitted(this, other, progress.DummyProgress())
739
        self.assertIs(other, merger.other_tree)
740
        self.assertIs(None, merger.other_rev_id)
741
        self.assertEqual('rev2b', merger.base_rev_id)
742
743
    def test_from_mergeable(self):
744
        this, other = self.set_up_trees()
745
        other.commit('rev3', rev_id='rev3')
746
        md = merge_directive.MergeDirective2.from_objects(
747
            other.branch.repository, 'rev3', 0, 0, 'this')
748
        merger, verified = Merger.from_mergeable(this, md,
749
            progress.DummyProgress())
750
        md.patch = None
751
        merger, verified = Merger.from_mergeable(this, md,
752
            progress.DummyProgress())
753
        self.assertEqual('inapplicable', verified)
754
        self.assertEqual('rev3', merger.other_rev_id)
755
        self.assertEqual('rev1', merger.base_rev_id)
756
        md.base_revision_id = 'rev2b'
757
        merger, verified = Merger.from_mergeable(this, md,
758
            progress.DummyProgress())
759
        self.assertEqual('rev2b', merger.base_rev_id)