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