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