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