/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1
# Copyright (C) 2005, 2006 Canonical Ltd
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
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
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
22
from bzrlib import branch, bzrdir, errors, urlutils, workingtree
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
23
from bzrlib.errors import (NotBranchError, NotVersionedError, 
24
                           UnsupportedOperation)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
25
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.
26
from bzrlib.tests import TestSkipped
27
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
28
from bzrlib.trace import mutter
29
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
30
                                WorkingTree)
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
31
from bzrlib.conflicts import ConflictList
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
32
33
class TestWorkingTree(TestCaseWithWorkingTree):
34
1732.1.8 by John Arbash Meinel
Adding a test for list_files
35
    def test_list_files(self):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
36
        tree = self.make_branch_and_tree('.')
1732.1.8 by John Arbash Meinel
Adding a test for list_files
37
        self.build_tree(['dir/', 'file'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
38
        if has_symlinks():
39
            os.symlink('target', 'symlink')
40
        files = list(tree.list_files())
41
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
42
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
43
        if has_symlinks():
44
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
45
1732.1.8 by John Arbash Meinel
Adding a test for list_files
46
    def test_list_files_sorted(self):
47
        tree = self.make_branch_and_tree('.')
1732.1.22 by John Arbash Meinel
Bug in list_files if the last entry in a directory is another directory
48
        self.build_tree(['dir/', 'file', 'dir/file', 'dir/b', 'dir/subdir/', 'a', 'dir/subfile',
49
                'zz_dir/', 'zz_dir/subfile'])
1732.1.8 by John Arbash Meinel
Adding a test for list_files
50
        files = [(path, kind) for (path, versioned, kind, file_id, entry) in tree.list_files()]
51
        self.assertEqual([
52
            ('a', 'file'),
53
            ('dir', 'directory'),
54
            ('file', 'file'),
1732.1.25 by John Arbash Meinel
Fix list_files test, we don't need to check if children are empty if we fall off the loop.
55
            ('zz_dir', 'directory'),
1732.1.8 by John Arbash Meinel
Adding a test for list_files
56
            ], files)
57
1732.1.25 by John Arbash Meinel
Fix list_files test, we don't need to check if children are empty if we fall off the loop.
58
        tree.add(['dir', 'zz_dir'])
1732.1.8 by John Arbash Meinel
Adding a test for list_files
59
        files = [(path, kind) for (path, versioned, kind, file_id, entry) in tree.list_files()]
60
        self.assertEqual([
61
            ('a', 'file'),
62
            ('dir', 'directory'),
63
            ('dir/b', 'file'),
64
            ('dir/file', 'file'),
65
            ('dir/subdir', 'directory'),
66
            ('dir/subfile', 'file'),
67
            ('file', 'file'),
1732.1.22 by John Arbash Meinel
Bug in list_files if the last entry in a directory is another directory
68
            ('zz_dir', 'directory'),
69
            ('zz_dir/subfile', 'file'),
1732.1.8 by John Arbash Meinel
Adding a test for list_files
70
            ], files)
71
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
72
    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.
73
        branch = self.make_branch_and_tree('.').branch
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
74
        wt, relpath = WorkingTree.open_containing()
75
        self.assertEqual('', relpath)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
76
        self.assertEqual(wt.basedir + '/', urlutils.local_path_from_url(branch.base))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
77
        wt, relpath = WorkingTree.open_containing(u'.')
78
        self.assertEqual('', relpath)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
79
        self.assertEqual(wt.basedir + '/', urlutils.local_path_from_url(branch.base))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
80
        wt, relpath = WorkingTree.open_containing('./foo')
81
        self.assertEqual('foo', relpath)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
82
        self.assertEqual(wt.basedir + '/', urlutils.local_path_from_url(branch.base))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
83
        wt, relpath = WorkingTree.open_containing('file://' + getcwd() + '/foo')
84
        self.assertEqual('foo', relpath)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
85
        self.assertEqual(wt.basedir + '/', urlutils.local_path_from_url(branch.base))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
86
87
    def test_basic_relpath(self):
88
        # 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.
89
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
90
        self.assertEqual('child',
91
                         tree.relpath(pathjoin(getcwd(), 'child')))
92
93
    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.
94
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
95
        tree.lock_read()
96
        self.assertEqual('r', tree.branch.peek_lock_mode())
97
        tree.unlock()
98
        self.assertEqual(None, tree.branch.peek_lock_mode())
99
        tree.lock_write()
100
        self.assertEqual('w', tree.branch.peek_lock_mode())
101
        tree.unlock()
102
        self.assertEqual(None, tree.branch.peek_lock_mode())
103
 
104
    def test_revert(self):
105
        """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.
106
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
107
108
        self.build_tree(['hello.txt'])
109
        file('hello.txt', 'w').write('initial hello')
110
111
        self.assertRaises(NotVersionedError,
112
                          tree.revert, ['hello.txt'])
113
        tree.add(['hello.txt'])
114
        tree.commit('create initial hello.txt')
115
116
        self.check_file_contents('hello.txt', 'initial hello')
117
        file('hello.txt', 'w').write('new hello')
118
        self.check_file_contents('hello.txt', 'new hello')
119
120
        # revert file modified since last revision
121
        tree.revert(['hello.txt'])
122
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
123
        self.check_file_contents('hello.txt.~1~', 'new hello')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
124
125
        # reverting again does not clobber the backup
126
        tree.revert(['hello.txt'])
127
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
128
        self.check_file_contents('hello.txt.~1~', 'new hello')
1534.10.28 by Aaron Bentley
Use numbered backup files
129
        
130
        # backup files are numbered
131
        file('hello.txt', 'w').write('new hello2')
132
        tree.revert(['hello.txt'])
133
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
134
        self.check_file_contents('hello.txt.~1~', 'new hello')
135
        self.check_file_contents('hello.txt.~2~', 'new hello2')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
136
1558.12.7 by Aaron Bentley
Fixed revert with missing files
137
    def test_revert_missing(self):
138
        # Revert a file that has been deleted since last commit
139
        tree = self.make_branch_and_tree('.')
140
        file('hello.txt', 'w').write('initial hello')
141
        tree.add('hello.txt')
142
        tree.commit('added hello.txt')
143
        os.unlink('hello.txt')
144
        tree.remove('hello.txt')
145
        tree.revert(['hello.txt'])
146
        self.failUnlessExists('hello.txt')
147
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
148
    def test_unknowns_by_default_patterns(self):
149
        """Backup files are ignored by default"""
150
        tree = self.make_branch_and_tree('.')
151
        self.build_tree(['hello.txt',
152
                         'hello.txt.~1~'])
153
        self.assertEquals(list(tree.unknowns()),
154
                          ['hello.txt'])
155
156
    def test_versioned_files_not_unknown(self):
157
        tree = self.make_branch_and_tree('.')
158
        self.build_tree(['hello.txt',
159
                         'hello.txt.~1~'])
160
        tree.add('hello.txt')
161
        self.assertEquals(list(tree.unknowns()),
162
                          [])
163
        tree.remove('hello.txt')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
164
        self.assertEquals(list(tree.unknowns()),
165
                          ['hello.txt'])
166
167
    def test_hashcache(self):
168
        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.
169
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
170
        self.build_tree(['hello.txt',
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
171
                         'hello.txt.~1~'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
172
        tree.add('hello.txt')
173
        pause()
174
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
175
        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.
176
        tree2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
177
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
178
        self.assertEqual(0, tree2._hashcache.miss_count)
179
        self.assertEqual(1, tree2._hashcache.hit_count)
180
181
    def test_initialize(self):
182
        # 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.
183
        t = self.make_branch_and_tree('.')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
184
        b = branch.Branch.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
185
        self.assertEqual(t.branch.base, b.base)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
186
        t2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
187
        self.assertEqual(t.basedir, t2.basedir)
188
        self.assertEqual(b.base, t2.branch.base)
189
        # TODO maybe we should check the branch format? not sure if its
190
        # appropriate here.
191
192
    def test_rename_dirs(self):
193
        """Test renaming directories and the files within them."""
194
        wt = self.make_branch_and_tree('.')
195
        b = wt.branch
196
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
197
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
198
199
        wt.commit('create initial state')
200
201
        revid = b.revision_history()[0]
202
        self.log('first revision_id is {%s}' % revid)
203
        
204
        inv = b.repository.get_revision_inventory(revid)
205
        self.log('contents of inventory: %r' % inv.entries())
206
207
        self.check_inventory_shape(inv,
208
                                   ['dir', 'dir/sub', 'dir/sub/file'])
209
210
        wt.rename_one('dir', 'newdir')
211
212
        self.check_inventory_shape(wt.read_working_inventory(),
213
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
214
215
        wt.rename_one('newdir/sub', 'newdir/newsub')
216
        self.check_inventory_shape(wt.read_working_inventory(),
217
                                   ['newdir', 'newdir/newsub',
218
                                    'newdir/newsub/file'])
219
220
    def test_add_in_unversioned(self):
221
        """Try to add a file in an unversioned directory.
222
223
        "bzr add" adds the parent as necessary, but simple working tree add
224
        doesn't do that.
225
        """
226
        from bzrlib.errors import NotVersionedError
227
        wt = self.make_branch_and_tree('.')
228
        self.build_tree(['foo/',
229
                         'foo/hello'])
230
        self.assertRaises(NotVersionedError,
231
                          wt.add,
232
                          'foo/hello')
233
234
    def test_add_missing(self):
235
        # adding a msising file -> NoSuchFile
236
        wt = self.make_branch_and_tree('.')
237
        self.assertRaises(errors.NoSuchFile, wt.add, 'fpp')
238
239
    def test_remove_verbose(self):
240
        #FIXME the remove api should not print or otherwise depend on the
241
        # text UI - RBC 20060124
242
        wt = self.make_branch_and_tree('.')
243
        self.build_tree(['hello'])
244
        wt.add(['hello'])
245
        wt.commit(message='add hello')
246
        stdout = StringIO()
247
        stderr = StringIO()
248
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
249
                                                     wt.remove,
250
                                                     ['hello'],
251
                                                     verbose=True))
252
        self.assertEqual('?       hello\n', stdout.getvalue())
253
        self.assertEqual('', stderr.getvalue())
254
255
    def test_clone_trivial(self):
256
        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.
257
        cloned_dir = wt.bzrdir.clone('target')
258
        cloned = cloned_dir.open_workingtree()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
259
        self.assertEqual(cloned.last_revision(), wt.last_revision())
260
261
    def test_last_revision(self):
262
        wt = self.make_branch_and_tree('source')
263
        self.assertEqual(None, wt.last_revision())
264
        wt.commit('A', allow_pointless=True, rev_id='A')
265
        self.assertEqual('A', wt.last_revision())
266
267
    def test_set_last_revision(self):
268
        wt = self.make_branch_and_tree('source')
269
        self.assertEqual(None, wt.last_revision())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
270
        # cannot set the last revision to one not in the branch history.
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
271
        self.assertRaises(errors.NoSuchRevision, wt.set_last_revision, 'A')
272
        wt.commit('A', allow_pointless=True, rev_id='A')
273
        self.assertEqual('A', wt.last_revision())
274
        # None is aways in the branch
275
        wt.set_last_revision(None)
276
        self.assertEqual(None, wt.last_revision())
277
        # and now we can set it to 'A'
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
278
        # because some formats mutate the branch to set it on the tree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
279
        # we need to alter the branch to let this pass.
280
        wt.branch.set_revision_history(['A', 'B'])
281
        wt.set_last_revision('A')
282
        self.assertEqual('A', wt.last_revision())
283
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
284
    def test_set_last_revision_different_to_branch(self):
285
        # working tree formats from the meta-dir format and newer support
286
        # setting the last revision on a tree independently of that on the 
287
        # branch. Its concievable that some future formats may want to 
288
        # couple them again (i.e. because its really a smart server and
289
        # the working tree will always match the branch). So we test
290
        # that formats where initialising a branch does not initialise a 
291
        # tree - and thus have separable entities - support skewing the 
292
        # two things.
293
        branch = self.make_branch('tree')
294
        try:
295
            # if there is a working tree now, this is not supported.
296
            branch.bzrdir.open_workingtree()
297
            return
298
        except errors.NoWorkingTree:
299
            pass
300
        wt = branch.bzrdir.create_workingtree()
301
        wt.commit('A', allow_pointless=True, rev_id='A')
302
        wt.set_last_revision(None)
303
        self.assertEqual(None, wt.last_revision())
304
        self.assertEqual('A', wt.branch.last_revision())
305
        # and now we can set it back to 'A'
306
        wt.set_last_revision('A')
307
        self.assertEqual('A', wt.last_revision())
308
        self.assertEqual('A', wt.branch.last_revision())
309
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
310
    def test_clone_and_commit_preserves_last_revision(self):
311
        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.
312
        cloned_dir = wt.bzrdir.clone('target')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
313
        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.
314
        self.assertNotEqual(cloned_dir.open_workingtree().last_revision(),
315
                            wt.last_revision())
316
317
    def test_clone_preserves_content(self):
318
        wt = self.make_branch_and_tree('source')
319
        self.build_tree(['added', 'deleted', 'notadded'], transport=wt.bzrdir.transport.clone('..'))
320
        wt.add('deleted', 'deleted')
321
        wt.commit('add deleted')
322
        wt.remove('deleted')
323
        wt.add('added', 'added')
324
        cloned_dir = wt.bzrdir.clone('target')
325
        cloned = cloned_dir.open_workingtree()
326
        cloned_transport = cloned.bzrdir.transport.clone('..')
327
        self.assertFalse(cloned_transport.has('deleted'))
328
        self.assertTrue(cloned_transport.has('added'))
329
        self.assertFalse(cloned_transport.has('notadded'))
330
        self.assertEqual('added', cloned.path2id('added'))
331
        self.assertEqual(None, cloned.path2id('deleted'))
332
        self.assertEqual(None, cloned.path2id('notadded'))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
333
        
334
    def test_basis_tree_returns_last_revision(self):
335
        wt = self.make_branch_and_tree('.')
336
        self.build_tree(['foo'])
337
        wt.add('foo', 'foo-id')
338
        wt.commit('A', rev_id='A')
339
        wt.rename_one('foo', 'bar')
340
        wt.commit('B', rev_id='B')
341
        wt.set_last_revision('B')
342
        tree = wt.basis_tree()
343
        self.failUnless(tree.has_filename('bar'))
344
        wt.set_last_revision('A')
345
        tree = wt.basis_tree()
346
        self.failUnless(tree.has_filename('foo'))
347
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.
348
    def test_clone_tree_revision(self):
349
        # make a tree with a last-revision,
350
        # and clone it with a different last-revision, this should switch
351
        # do it.
352
        #
353
        # also test that the content is merged
354
        # and conflicts recorded.
355
        # This should merge between the trees - local edits should be preserved
356
        # but other changes occured.
357
        # we test this by having one file that does
358
        # not change between two revisions, and another that does -
359
        # if the changed one is not changed, fail,
360
        # if the one that did not change has lost a local change, fail.
361
        # 
362
        raise TestSkipped('revision limiting is not implemented yet.')
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
363
364
    def test_initialize_with_revision_id(self):
365
        # a bzrdir can construct a working tree for itself @ a specific revision.
366
        source = self.make_branch_and_tree('source')
367
        source.commit('a', rev_id='a', allow_pointless=True)
368
        source.commit('b', rev_id='b', allow_pointless=True)
369
        self.build_tree(['new/'])
370
        made_control = self.bzrdir_format.initialize('new')
371
        source.branch.repository.clone(made_control)
372
        source.branch.clone(made_control)
373
        made_tree = self.workingtree_format.initialize(made_control, revision_id='a')
374
        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.
375
1508.1.24 by Robert Collins
Add update command for use with checkouts.
376
    def test_update_sets_last_revision(self):
377
        # working tree formats from the meta-dir format and newer support
378
        # setting the last revision on a tree independently of that on the 
379
        # branch. Its concievable that some future formats may want to 
380
        # couple them again (i.e. because its really a smart server and
381
        # the working tree will always match the branch). So we test
382
        # that formats where initialising a branch does not initialise a 
383
        # tree - and thus have separable entities - support skewing the 
384
        # two things.
385
        main_branch = self.make_branch('tree')
386
        try:
387
            # if there is a working tree now, this is not supported.
388
            main_branch.bzrdir.open_workingtree()
389
            return
390
        except errors.NoWorkingTree:
391
            pass
392
        wt = main_branch.bzrdir.create_workingtree()
393
        # create an out of date working tree by making a checkout in this
394
        # current format
395
        self.build_tree(['checkout/', 'tree/file'])
396
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
397
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
398
        old_tree = self.workingtree_format.initialize(checkout)
399
        # now commit to 'tree'
400
        wt.add('file')
401
        wt.commit('A', rev_id='A')
402
        # and update old_tree
403
        self.assertEqual(0, old_tree.update())
404
        self.failUnlessExists('checkout/file')
405
        self.assertEqual('A', old_tree.last_revision())
406
407
    def test_update_returns_conflict_count(self):
408
        # working tree formats from the meta-dir format and newer support
409
        # setting the last revision on a tree independently of that on the 
410
        # branch. Its concievable that some future formats may want to 
411
        # couple them again (i.e. because its really a smart server and
412
        # the working tree will always match the branch). So we test
413
        # that formats where initialising a branch does not initialise a 
414
        # tree - and thus have separable entities - support skewing the 
415
        # two things.
416
        main_branch = self.make_branch('tree')
417
        try:
418
            # if there is a working tree now, this is not supported.
419
            main_branch.bzrdir.open_workingtree()
420
            return
421
        except errors.NoWorkingTree:
422
            pass
423
        wt = main_branch.bzrdir.create_workingtree()
424
        # create an out of date working tree by making a checkout in this
425
        # current format
426
        self.build_tree(['checkout/', 'tree/file'])
427
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
428
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
429
        old_tree = self.workingtree_format.initialize(checkout)
430
        # now commit to 'tree'
431
        wt.add('file')
432
        wt.commit('A', rev_id='A')
433
        # and add a file file to the checkout
434
        self.build_tree(['checkout/file'])
435
        old_tree.add('file')
436
        # and update old_tree
437
        self.assertEqual(1, old_tree.update())
438
        self.assertEqual('A', old_tree.last_revision())
439
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
440
    def test_merge_revert(self):
441
        from bzrlib.merge import merge_inner
442
        this = self.make_branch_and_tree('b1')
443
        open('b1/a', 'wb').write('a test\n')
444
        this.add('a')
445
        open('b1/b', 'wb').write('b test\n')
446
        this.add('b')
447
        this.commit(message='')
448
        base = this.bzrdir.clone('b2').open_workingtree()
449
        open('b2/a', 'wb').write('b test\n')
450
        other = this.bzrdir.clone('b3').open_workingtree()
451
        open('b3/a', 'wb').write('c test\n')
452
        open('b3/c', 'wb').write('c test\n')
453
        other.add('c')
454
455
        open('b1/b', 'wb').write('q test\n')
456
        open('b1/d', 'wb').write('d test\n')
457
        merge_inner(this.branch, other, base, this_tree=this)
458
        self.assertNotEqual(open('b1/a', 'rb').read(), 'a test\n')
459
        this.revert([])
460
        self.assertEqual(open('b1/a', 'rb').read(), 'a test\n')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
461
        self.assertIs(os.path.exists('b1/b.~1~'), True)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
462
        self.assertIs(os.path.exists('b1/c'), False)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
463
        self.assertIs(os.path.exists('b1/a.~1~'), False)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
464
        self.assertIs(os.path.exists('b1/d'), True)
1534.7.200 by Aaron Bentley
Merge from mainline
465
1587.1.10 by Robert Collins
update updates working tree and branch together.
466
    def test_update_updates_bound_branch_no_local_commits(self):
467
        # doing an update in a tree updates the branch its bound to too.
468
        master_tree = self.make_branch_and_tree('master')
469
        tree = self.make_branch_and_tree('tree')
470
        try:
471
            tree.branch.bind(master_tree.branch)
472
        except errors.UpgradeRequired:
473
            # legacy branches cannot bind
474
            return
475
        master_tree.commit('foo', rev_id='foo', allow_pointless=True)
476
        tree.update()
477
        self.assertEqual('foo', tree.last_revision())
478
        self.assertEqual('foo', tree.branch.last_revision())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
479
480
    def test_update_turns_local_commit_into_merge(self):
481
        # 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.
482
        # makes pending-merges. 
483
        # this is done so that 'bzr update; bzr revert' will always produce
484
        # an exact copy of the 'logical branch' - the referenced branch for
485
        # a checkout, and the master for a bound branch.
486
        # its possible that we should instead have 'bzr update' when there
487
        # is nothing new on the master leave the current commits intact and
488
        # alter 'revert' to revert to the master always. But for now, its
489
        # good.
1587.1.11 by Robert Collins
Local commits appear to be working properly.
490
        master_tree = self.make_branch_and_tree('master')
491
        tree = self.make_branch_and_tree('tree')
492
        try:
493
            tree.branch.bind(master_tree.branch)
494
        except errors.UpgradeRequired:
495
            # legacy branches cannot bind
496
            return
497
        tree.commit('foo', rev_id='foo', allow_pointless=True, local=True)
498
        tree.commit('bar', rev_id='bar', allow_pointless=True, local=True)
499
        tree.update()
500
        self.assertEqual(None, tree.last_revision())
501
        self.assertEqual([], tree.branch.revision_history())
502
        self.assertEqual(['bar'], tree.pending_merges())
503
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
504
    def test_merge_modified(self):
505
        tree = self.make_branch_and_tree('master')
506
        tree._control_files.put('merge-hashes', StringIO('asdfasdf'))
507
        self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
508
1534.10.22 by Aaron Bentley
Got ConflictList implemented
509
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
510
        from bzrlib.tests.test_conflicts import example_conflicts
511
        tree = self.make_branch_and_tree('master')
512
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
513
            tree.set_conflicts(example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
514
        except UnsupportedOperation:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
515
            raise TestSkipped('set_conflicts not supported')
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
516
            
517
        tree2 = WorkingTree.open('master')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
518
        self.assertEqual(tree2.conflicts(), example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
519
        tree2._control_files.put('conflicts', StringIO(''))
520
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
521
                          tree2.conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
522
        tree2._control_files.put('conflicts', StringIO('a'))
523
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
524
                          tree2.conflicts)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
525
526
    def make_merge_conflicts(self):
527
        from bzrlib.merge import merge_inner 
528
        tree = self.make_branch_and_tree('mine')
529
        file('mine/bloo', 'wb').write('one')
530
        tree.add('bloo')
1534.10.14 by Aaron Bentley
Made revert clear conflicts
531
        file('mine/blo', 'wb').write('on')
532
        tree.add('blo')
1534.10.12 by Aaron Bentley
Merge produces new conflicts
533
        tree.commit("blah", allow_pointless=False)
534
        base = tree.basis_tree()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
535
        bzrdir.BzrDir.open("mine").sprout("other")
1534.10.12 by Aaron Bentley
Merge produces new conflicts
536
        file('other/bloo', 'wb').write('two')
537
        othertree = WorkingTree.open('other')
538
        othertree.commit('blah', allow_pointless=False)
539
        file('mine/bloo', 'wb').write('three')
540
        tree.commit("blah", allow_pointless=False)
541
        merge_inner(tree.branch, othertree, base, this_tree=tree)
542
        return tree
543
544
    def test_merge_conflicts(self):
545
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
546
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
547
548
    def test_clear_merge_conflicts(self):
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)
1624.3.22 by Olaf Conradi
Merge bzr.dev
570
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
571
    def test_format_description(self):
572
        tree = self.make_branch_and_tree('tree')
573
        text = tree._format.get_format_description()
574
        self.failUnless(len(text))
1681.1.1 by Robert Collins
Make WorkingTree.branch a read only property. (Robert Collins)
575
576
    def test_branch_attribute_is_not_settable(self):
577
        # the branch attribute is an aspect of the working tree, not a
578
        # configurable attribute
579
        tree = self.make_branch_and_tree('tree')
580
        def set_branch():
581
            tree.branch = tree.branch
582
        self.assertRaises(AttributeError, set_branch)
583
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
584
    def test_list_files_versioned_before_ignored(self):
585
        """A versioned file matching an ignore rule should not be ignored."""
586
        tree = self.make_branch_and_tree('.')
587
        self.build_tree(['foo.pyc'])
588
        # ensure that foo.pyc is ignored
589
        self.build_tree_contents([('.bzrignore', 'foo.pyc')])
590
        tree.add('foo.pyc', 'anid')
591
        files = sorted(list(tree.list_files()))
592
        self.assertEqual((u'.bzrignore', '?', 'file', None), files[0][:-1])
593
        self.assertEqual((u'foo.pyc', 'V', 'file', 'anid'), files[1][:-1])
594
        self.assertEqual(2, len(files))