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