/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1
# (C) 2005,2006 Canonical Ltd
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
from cStringIO import StringIO
19
import os
20
21
import bzrlib
1508.1.25 by Robert Collins
Update per review comments.
22
import bzrlib.branch
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
23
from bzrlib.branch import Branch
24
import bzrlib.bzrdir as bzrdir
25
from bzrlib.bzrdir import BzrDir
26
import bzrlib.errors as errors
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
27
from bzrlib.errors import (NotBranchError, NotVersionedError, 
28
                           UnsupportedOperation)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
29
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
1534.5.5 by Robert Collins
Move is_control_file into WorkingTree.is_control_filename and test.
30
from bzrlib.tests import TestSkipped
31
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
32
from bzrlib.trace import mutter
33
import bzrlib.workingtree as workingtree
34
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
35
                                WorkingTree)
36
37
38
class TestWorkingTree(TestCaseWithWorkingTree):
39
40
    def test_listfiles(self):
41
        tree = self.make_branch_and_tree('.')
42
        os.mkdir('dir')
43
        print >> open('file', 'w'), "content"
44
        if has_symlinks():
45
            os.symlink('target', 'symlink')
46
        files = list(tree.list_files())
47
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
48
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
49
        if has_symlinks():
50
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
51
52
    def test_open_containing(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
53
        branch = self.make_branch_and_tree('.').branch
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
54
        wt, relpath = WorkingTree.open_containing()
55
        self.assertEqual('', relpath)
56
        self.assertEqual(wt.basedir + '/', branch.base)
57
        wt, relpath = WorkingTree.open_containing(u'.')
58
        self.assertEqual('', relpath)
59
        self.assertEqual(wt.basedir + '/', branch.base)
60
        wt, relpath = WorkingTree.open_containing('./foo')
61
        self.assertEqual('foo', relpath)
62
        self.assertEqual(wt.basedir + '/', branch.base)
63
        wt, relpath = WorkingTree.open_containing('file://' + getcwd() + '/foo')
64
        self.assertEqual('foo', relpath)
65
        self.assertEqual(wt.basedir + '/', branch.base)
66
67
    def test_basic_relpath(self):
68
        # for comprehensive relpath tests, see whitebox.py.
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.
69
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
70
        self.assertEqual('child',
71
                         tree.relpath(pathjoin(getcwd(), 'child')))
72
73
    def test_lock_locks_branch(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
74
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
75
        tree.lock_read()
76
        self.assertEqual('r', tree.branch.peek_lock_mode())
77
        tree.unlock()
78
        self.assertEqual(None, tree.branch.peek_lock_mode())
79
        tree.lock_write()
80
        self.assertEqual('w', tree.branch.peek_lock_mode())
81
        tree.unlock()
82
        self.assertEqual(None, tree.branch.peek_lock_mode())
83
 
84
    def test_revert(self):
85
        """Test selected-file revert"""
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.
86
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
87
88
        self.build_tree(['hello.txt'])
89
        file('hello.txt', 'w').write('initial hello')
90
91
        self.assertRaises(NotVersionedError,
92
                          tree.revert, ['hello.txt'])
93
        tree.add(['hello.txt'])
94
        tree.commit('create initial hello.txt')
95
96
        self.check_file_contents('hello.txt', 'initial hello')
97
        file('hello.txt', 'w').write('new hello')
98
        self.check_file_contents('hello.txt', 'new hello')
99
100
        # revert file modified since last revision
101
        tree.revert(['hello.txt'])
102
        self.check_file_contents('hello.txt', 'initial hello')
103
        self.check_file_contents('hello.txt~', 'new hello')
104
105
        # reverting again does not clobber the backup
106
        tree.revert(['hello.txt'])
107
        self.check_file_contents('hello.txt', 'initial hello')
108
        self.check_file_contents('hello.txt~', 'new hello')
109
110
    def test_unknowns(self):
111
        tree = self.make_branch_and_tree('.')
112
        self.build_tree(['hello.txt',
113
                         'hello.txt~'])
114
        self.assertEquals(list(tree.unknowns()),
115
                          ['hello.txt'])
116
117
    def test_hashcache(self):
118
        from bzrlib.tests.test_hashcache import pause
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.
119
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
120
        self.build_tree(['hello.txt',
121
                         'hello.txt~'])
122
        tree.add('hello.txt')
123
        pause()
124
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
125
        self.assertEqual(1, tree._hashcache.miss_count)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
126
        tree2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
127
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
128
        self.assertEqual(0, tree2._hashcache.miss_count)
129
        self.assertEqual(1, tree2._hashcache.hit_count)
130
131
    def test_initialize(self):
132
        # initialize should create a working tree and branch in an existing dir
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.
133
        t = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
134
        b = Branch.open('.')
135
        self.assertEqual(t.branch.base, b.base)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
136
        t2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
137
        self.assertEqual(t.basedir, t2.basedir)
138
        self.assertEqual(b.base, t2.branch.base)
139
        # TODO maybe we should check the branch format? not sure if its
140
        # appropriate here.
141
142
    def test_rename_dirs(self):
143
        """Test renaming directories and the files within them."""
144
        wt = self.make_branch_and_tree('.')
145
        b = wt.branch
146
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
147
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
148
149
        wt.commit('create initial state')
150
151
        revid = b.revision_history()[0]
152
        self.log('first revision_id is {%s}' % revid)
153
        
154
        inv = b.repository.get_revision_inventory(revid)
155
        self.log('contents of inventory: %r' % inv.entries())
156
157
        self.check_inventory_shape(inv,
158
                                   ['dir', 'dir/sub', 'dir/sub/file'])
159
160
        wt.rename_one('dir', 'newdir')
161
162
        self.check_inventory_shape(wt.read_working_inventory(),
163
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
164
165
        wt.rename_one('newdir/sub', 'newdir/newsub')
166
        self.check_inventory_shape(wt.read_working_inventory(),
167
                                   ['newdir', 'newdir/newsub',
168
                                    'newdir/newsub/file'])
169
170
    def test_add_in_unversioned(self):
171
        """Try to add a file in an unversioned directory.
172
173
        "bzr add" adds the parent as necessary, but simple working tree add
174
        doesn't do that.
175
        """
176
        from bzrlib.errors import NotVersionedError
177
        wt = self.make_branch_and_tree('.')
178
        self.build_tree(['foo/',
179
                         'foo/hello'])
180
        self.assertRaises(NotVersionedError,
181
                          wt.add,
182
                          'foo/hello')
183
184
    def test_add_missing(self):
185
        # adding a msising file -> NoSuchFile
186
        wt = self.make_branch_and_tree('.')
187
        self.assertRaises(errors.NoSuchFile, wt.add, 'fpp')
188
189
    def test_remove_verbose(self):
190
        #FIXME the remove api should not print or otherwise depend on the
191
        # text UI - RBC 20060124
192
        wt = self.make_branch_and_tree('.')
193
        self.build_tree(['hello'])
194
        wt.add(['hello'])
195
        wt.commit(message='add hello')
196
        stdout = StringIO()
197
        stderr = StringIO()
198
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
199
                                                     wt.remove,
200
                                                     ['hello'],
201
                                                     verbose=True))
202
        self.assertEqual('?       hello\n', stdout.getvalue())
203
        self.assertEqual('', stderr.getvalue())
204
205
    def test_clone_trivial(self):
206
        wt = self.make_branch_and_tree('source')
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.
207
        cloned_dir = wt.bzrdir.clone('target')
208
        cloned = cloned_dir.open_workingtree()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
209
        self.assertEqual(cloned.last_revision(), wt.last_revision())
210
211
    def test_last_revision(self):
212
        wt = self.make_branch_and_tree('source')
213
        self.assertEqual(None, wt.last_revision())
214
        wt.commit('A', allow_pointless=True, rev_id='A')
215
        self.assertEqual('A', wt.last_revision())
216
217
    def test_set_last_revision(self):
218
        wt = self.make_branch_and_tree('source')
219
        self.assertEqual(None, wt.last_revision())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
220
        # cannot set the last revision to one not in the branch history.
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
221
        self.assertRaises(errors.NoSuchRevision, wt.set_last_revision, 'A')
222
        wt.commit('A', allow_pointless=True, rev_id='A')
223
        self.assertEqual('A', wt.last_revision())
224
        # None is aways in the branch
225
        wt.set_last_revision(None)
226
        self.assertEqual(None, wt.last_revision())
227
        # and now we can set it to 'A'
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
228
        # because some formats mutate the branch to set it on the tree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
229
        # we need to alter the branch to let this pass.
230
        wt.branch.set_revision_history(['A', 'B'])
231
        wt.set_last_revision('A')
232
        self.assertEqual('A', wt.last_revision())
233
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
234
    def test_set_last_revision_different_to_branch(self):
235
        # working tree formats from the meta-dir format and newer support
236
        # setting the last revision on a tree independently of that on the 
237
        # branch. Its concievable that some future formats may want to 
238
        # couple them again (i.e. because its really a smart server and
239
        # the working tree will always match the branch). So we test
240
        # that formats where initialising a branch does not initialise a 
241
        # tree - and thus have separable entities - support skewing the 
242
        # two things.
243
        branch = self.make_branch('tree')
244
        try:
245
            # if there is a working tree now, this is not supported.
246
            branch.bzrdir.open_workingtree()
247
            return
248
        except errors.NoWorkingTree:
249
            pass
250
        wt = branch.bzrdir.create_workingtree()
251
        wt.commit('A', allow_pointless=True, rev_id='A')
252
        wt.set_last_revision(None)
253
        self.assertEqual(None, wt.last_revision())
254
        self.assertEqual('A', wt.branch.last_revision())
255
        # and now we can set it back to 'A'
256
        wt.set_last_revision('A')
257
        self.assertEqual('A', wt.last_revision())
258
        self.assertEqual('A', wt.branch.last_revision())
259
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
260
    def test_clone_and_commit_preserves_last_revision(self):
261
        wt = self.make_branch_and_tree('source')
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.
262
        cloned_dir = wt.bzrdir.clone('target')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
263
        wt.commit('A', allow_pointless=True, rev_id='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.
264
        self.assertNotEqual(cloned_dir.open_workingtree().last_revision(),
265
                            wt.last_revision())
266
267
    def test_clone_preserves_content(self):
268
        wt = self.make_branch_and_tree('source')
269
        self.build_tree(['added', 'deleted', 'notadded'], transport=wt.bzrdir.transport.clone('..'))
270
        wt.add('deleted', 'deleted')
271
        wt.commit('add deleted')
272
        wt.remove('deleted')
273
        wt.add('added', 'added')
274
        cloned_dir = wt.bzrdir.clone('target')
275
        cloned = cloned_dir.open_workingtree()
276
        cloned_transport = cloned.bzrdir.transport.clone('..')
277
        self.assertFalse(cloned_transport.has('deleted'))
278
        self.assertTrue(cloned_transport.has('added'))
279
        self.assertFalse(cloned_transport.has('notadded'))
280
        self.assertEqual('added', cloned.path2id('added'))
281
        self.assertEqual(None, cloned.path2id('deleted'))
282
        self.assertEqual(None, cloned.path2id('notadded'))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
283
        
284
    def test_basis_tree_returns_last_revision(self):
285
        wt = self.make_branch_and_tree('.')
286
        self.build_tree(['foo'])
287
        wt.add('foo', 'foo-id')
288
        wt.commit('A', rev_id='A')
289
        wt.rename_one('foo', 'bar')
290
        wt.commit('B', rev_id='B')
291
        wt.set_last_revision('B')
292
        tree = wt.basis_tree()
293
        self.failUnless(tree.has_filename('bar'))
294
        wt.set_last_revision('A')
295
        tree = wt.basis_tree()
296
        self.failUnless(tree.has_filename('foo'))
297
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.
298
    def test_clone_tree_revision(self):
299
        # make a tree with a last-revision,
300
        # and clone it with a different last-revision, this should switch
301
        # do it.
302
        #
303
        # also test that the content is merged
304
        # and conflicts recorded.
305
        # This should merge between the trees - local edits should be preserved
306
        # but other changes occured.
307
        # we test this by having one file that does
308
        # not change between two revisions, and another that does -
309
        # if the changed one is not changed, fail,
310
        # if the one that did not change has lost a local change, fail.
311
        # 
312
        raise TestSkipped('revision limiting is not implemented yet.')
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
313
314
    def test_initialize_with_revision_id(self):
315
        # a bzrdir can construct a working tree for itself @ a specific revision.
316
        source = self.make_branch_and_tree('source')
317
        source.commit('a', rev_id='a', allow_pointless=True)
318
        source.commit('b', rev_id='b', allow_pointless=True)
319
        self.build_tree(['new/'])
320
        made_control = self.bzrdir_format.initialize('new')
321
        source.branch.repository.clone(made_control)
322
        source.branch.clone(made_control)
323
        made_tree = self.workingtree_format.initialize(made_control, revision_id='a')
324
        self.assertEqual('a', made_tree.last_revision())
1508.1.23 by Robert Collins
Test that the working tree last revision is indeed set during commit.
325
326
    def test_commit_sets_last_revision(self):
327
        tree = self.make_branch_and_tree('tree')
328
        tree.commit('foo', rev_id='foo', allow_pointless=True)
329
        self.assertEqual('foo', tree.last_revision())
1508.1.24 by Robert Collins
Add update command for use with checkouts.
330
1587.1.8 by Robert Collins
Local commits on unbound branches fail.
331
    def test_commit_local_unbound(self):
332
        # using the library api to do a local commit on unbound branches is 
333
        # also an error
334
        tree = self.make_branch_and_tree('tree')
335
        self.assertRaises(errors.LocalRequiresBoundBranch,
336
                          tree.commit,
337
                          'foo',
338
                          local=True)
1587.1.9 by Robert Collins
Local commits do no alter or access the master branch.
339
 
340
    def test_local_commit_ignores_master(self):
341
        # a --local commit does not require access to the master branch
342
        # at all, or even for it to exist.
343
        # we test this by setting up a bound branch and then corrupting
344
        # the master.
345
        master = self.make_branch('master')
346
        tree = self.make_branch_and_tree('tree')
347
        try:
348
            tree.branch.bind(master)
349
        except errors.UpgradeRequired:
350
            # older format.
351
            return
352
        master.bzrdir.transport.put('branch-format', StringIO('garbage'))
353
        del master
354
        # check its corrupted.
355
        self.assertRaises(errors.UnknownFormatError,
356
                          bzrdir.BzrDir.open,
357
                          'master')
358
        tree.commit('foo', rev_id='foo', local=True)
359
 
360
    def test_local_commit_does_not_push_to_master(self):
361
        # a --local commit does not require access to the master branch
362
        # at all, or even for it to exist.
363
        # we test that even when its available it does not push to it.
364
        master = self.make_branch('master')
365
        tree = self.make_branch_and_tree('tree')
366
        try:
367
            tree.branch.bind(master)
368
        except errors.UpgradeRequired:
369
            # older format.
370
            return
371
        tree.commit('foo', rev_id='foo', local=True)
372
        self.failIf(master.repository.has_revision('foo'))
373
        self.assertEqual(None, master.last_revision())
374
        
1508.1.24 by Robert Collins
Add update command for use with checkouts.
375
    def test_update_sets_last_revision(self):
376
        # working tree formats from the meta-dir format and newer support
377
        # setting the last revision on a tree independently of that on the 
378
        # branch. Its concievable that some future formats may want to 
379
        # couple them again (i.e. because its really a smart server and
380
        # the working tree will always match the branch). So we test
381
        # that formats where initialising a branch does not initialise a 
382
        # tree - and thus have separable entities - support skewing the 
383
        # two things.
384
        main_branch = self.make_branch('tree')
385
        try:
386
            # if there is a working tree now, this is not supported.
387
            main_branch.bzrdir.open_workingtree()
388
            return
389
        except errors.NoWorkingTree:
390
            pass
391
        wt = main_branch.bzrdir.create_workingtree()
392
        # create an out of date working tree by making a checkout in this
393
        # current format
394
        self.build_tree(['checkout/', 'tree/file'])
395
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1508.1.25 by Robert Collins
Update per review comments.
396
        bzrlib.branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
397
        old_tree = self.workingtree_format.initialize(checkout)
398
        # now commit to 'tree'
399
        wt.add('file')
400
        wt.commit('A', rev_id='A')
401
        # and update old_tree
402
        self.assertEqual(0, old_tree.update())
403
        self.failUnlessExists('checkout/file')
404
        self.assertEqual('A', old_tree.last_revision())
405
406
    def test_update_returns_conflict_count(self):
407
        # working tree formats from the meta-dir format and newer support
408
        # setting the last revision on a tree independently of that on the 
409
        # branch. Its concievable that some future formats may want to 
410
        # couple them again (i.e. because its really a smart server and
411
        # the working tree will always match the branch). So we test
412
        # that formats where initialising a branch does not initialise a 
413
        # tree - and thus have separable entities - support skewing the 
414
        # two things.
415
        main_branch = self.make_branch('tree')
416
        try:
417
            # if there is a working tree now, this is not supported.
418
            main_branch.bzrdir.open_workingtree()
419
            return
420
        except errors.NoWorkingTree:
421
            pass
422
        wt = main_branch.bzrdir.create_workingtree()
423
        # create an out of date working tree by making a checkout in this
424
        # current format
425
        self.build_tree(['checkout/', 'tree/file'])
426
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1508.1.25 by Robert Collins
Update per review comments.
427
        bzrlib.branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
428
        old_tree = self.workingtree_format.initialize(checkout)
429
        # now commit to 'tree'
430
        wt.add('file')
431
        wt.commit('A', rev_id='A')
432
        # and add a file file to the checkout
433
        self.build_tree(['checkout/file'])
434
        old_tree.add('file')
435
        # and update old_tree
436
        self.assertEqual(1, old_tree.update())
437
        self.assertEqual('A', old_tree.last_revision())
438
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
439
    def test_merge_revert(self):
440
        from bzrlib.merge import merge_inner
441
        this = self.make_branch_and_tree('b1')
442
        open('b1/a', 'wb').write('a test\n')
443
        this.add('a')
444
        open('b1/b', 'wb').write('b test\n')
445
        this.add('b')
446
        this.commit(message='')
447
        base = this.bzrdir.clone('b2').open_workingtree()
448
        open('b2/a', 'wb').write('b test\n')
449
        other = this.bzrdir.clone('b3').open_workingtree()
450
        open('b3/a', 'wb').write('c test\n')
451
        open('b3/c', 'wb').write('c test\n')
452
        other.add('c')
453
454
        open('b1/b', 'wb').write('q test\n')
455
        open('b1/d', 'wb').write('d test\n')
456
        merge_inner(this.branch, other, base, this_tree=this)
457
        self.assertNotEqual(open('b1/a', 'rb').read(), 'a test\n')
458
        this.revert([])
459
        self.assertEqual(open('b1/a', 'rb').read(), 'a test\n')
460
        self.assertIs(os.path.exists('b1/b~'), True)
461
        self.assertIs(os.path.exists('b1/c'), False)
462
        self.assertIs(os.path.exists('b1/a~'), False)
463
        self.assertIs(os.path.exists('b1/d'), True)
1534.7.200 by Aaron Bentley
Merge from mainline
464
1587.1.10 by Robert Collins
update updates working tree and branch together.
465
    def test_update_updates_bound_branch_no_local_commits(self):
466
        # doing an update in a tree updates the branch its bound to too.
467
        master_tree = self.make_branch_and_tree('master')
468
        tree = self.make_branch_and_tree('tree')
469
        try:
470
            tree.branch.bind(master_tree.branch)
471
        except errors.UpgradeRequired:
472
            # legacy branches cannot bind
473
            return
474
        master_tree.commit('foo', rev_id='foo', allow_pointless=True)
475
        tree.update()
476
        self.assertEqual('foo', tree.last_revision())
477
        self.assertEqual('foo', tree.branch.last_revision())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
478
479
    def test_update_turns_local_commit_into_merge(self):
480
        # doing an update with a few local commits and no master commits
1587.1.13 by Robert Collins
Explain why update pivots more clearly in the relevant test.
481
        # makes pending-merges. 
482
        # this is done so that 'bzr update; bzr revert' will always produce
483
        # an exact copy of the 'logical branch' - the referenced branch for
484
        # a checkout, and the master for a bound branch.
485
        # its possible that we should instead have 'bzr update' when there
486
        # is nothing new on the master leave the current commits intact and
487
        # alter 'revert' to revert to the master always. But for now, its
488
        # good.
1587.1.11 by Robert Collins
Local commits appear to be working properly.
489
        master_tree = self.make_branch_and_tree('master')
490
        tree = self.make_branch_and_tree('tree')
491
        try:
492
            tree.branch.bind(master_tree.branch)
493
        except errors.UpgradeRequired:
494
            # legacy branches cannot bind
495
            return
496
        tree.commit('foo', rev_id='foo', allow_pointless=True, local=True)
497
        tree.commit('bar', rev_id='bar', allow_pointless=True, local=True)
498
        tree.update()
499
        self.assertEqual(None, tree.last_revision())
500
        self.assertEqual([], tree.branch.revision_history())
501
        self.assertEqual(['bar'], tree.pending_merges())
502
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
503
    def test_merge_modified(self):
504
        tree = self.make_branch_and_tree('master')
505
        tree._control_files.put('merge-hashes', StringIO('asdfasdf'))
506
        self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
507
1534.10.22 by Aaron Bentley
Got ConflictList implemented
508
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
509
        from bzrlib.tests.test_conflicts import example_conflicts
510
        tree = self.make_branch_and_tree('master')
511
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
512
            tree.set_conflicts(example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
513
        except UnsupportedOperation:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
514
            raise TestSkipped('set_conflicts not supported')
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
515
            
516
        tree2 = WorkingTree.open('master')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
517
        self.assertEqual(tree2.conflicts(), example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
518
        tree2._control_files.put('conflicts', StringIO(''))
519
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
520
                          tree2.conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
521
        tree2._control_files.put('conflicts', StringIO('a'))
522
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
523
                          tree2.conflicts)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
524
525
    def make_merge_conflicts(self):
526
        from bzrlib.merge import merge_inner 
527
        tree = self.make_branch_and_tree('mine')
528
        file('mine/bloo', 'wb').write('one')
529
        tree.add('bloo')
1534.10.14 by Aaron Bentley
Made revert clear conflicts
530
        file('mine/blo', 'wb').write('on')
531
        tree.add('blo')
1534.10.12 by Aaron Bentley
Merge produces new conflicts
532
        tree.commit("blah", allow_pointless=False)
533
        base = tree.basis_tree()
534
        BzrDir.open("mine").sprout("other")
535
        file('other/bloo', 'wb').write('two')
536
        othertree = WorkingTree.open('other')
537
        othertree.commit('blah', allow_pointless=False)
538
        file('mine/bloo', 'wb').write('three')
539
        tree.commit("blah", allow_pointless=False)
540
        merge_inner(tree.branch, othertree, base, this_tree=tree)
541
        return tree
542
543
    def test_merge_conflicts(self):
544
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
545
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
546
547
    def test_clear_merge_conflicts(self):
1534.10.22 by Aaron Bentley
Got ConflictList implemented
548
        from bzrlib.conflicts import ConflictList
1534.10.12 by Aaron Bentley
Merge produces new conflicts
549
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
550
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
551
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
552
            tree.set_conflicts(ConflictList())
1534.10.12 by Aaron Bentley
Merge produces new conflicts
553
        except UnsupportedOperation:
554
            raise TestSkipped
1534.10.22 by Aaron Bentley
Got ConflictList implemented
555
        self.assertEqual(tree.conflicts(), ConflictList())
1534.10.14 by Aaron Bentley
Made revert clear conflicts
556
557
    def test_revert_clear_conflicts(self):
558
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
559
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
560
        tree.revert(["blo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
561
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
562
        tree.revert(["bloo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
563
        self.assertEqual(len(tree.conflicts()), 0)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
564
565
    def test_revert_clear_conflicts2(self):
566
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
567
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
568
        tree.revert([])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
569
        self.assertEqual(len(tree.conflicts()), 0)