/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())
1908.1.1 by Robert Collins
Relax WorkingTree.set_last-revision to allow any revision to be set.
281
        # set last-revision to one not in the history
282
        wt.set_last_revision('A')
283
        # set it back to None for an empty tree.
284
        wt.set_last_revision(None)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
285
        wt.commit('A', allow_pointless=True, rev_id='A')
286
        self.assertEqual('A', wt.last_revision())
287
        # None is aways in the branch
288
        wt.set_last_revision(None)
289
        self.assertEqual(None, wt.last_revision())
290
        # and now we can set it to 'A'
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
291
        # because some formats mutate the branch to set it on the tree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
292
        # we need to alter the branch to let this pass.
293
        wt.branch.set_revision_history(['A', 'B'])
294
        wt.set_last_revision('A')
295
        self.assertEqual('A', wt.last_revision())
296
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
297
    def test_set_last_revision_different_to_branch(self):
298
        # working tree formats from the meta-dir format and newer support
299
        # setting the last revision on a tree independently of that on the 
300
        # branch. Its concievable that some future formats may want to 
301
        # couple them again (i.e. because its really a smart server and
302
        # the working tree will always match the branch). So we test
303
        # that formats where initialising a branch does not initialise a 
304
        # tree - and thus have separable entities - support skewing the 
305
        # two things.
306
        branch = self.make_branch('tree')
307
        try:
308
            # if there is a working tree now, this is not supported.
309
            branch.bzrdir.open_workingtree()
310
            return
311
        except errors.NoWorkingTree:
312
            pass
313
        wt = branch.bzrdir.create_workingtree()
314
        wt.commit('A', allow_pointless=True, rev_id='A')
315
        wt.set_last_revision(None)
316
        self.assertEqual(None, wt.last_revision())
317
        self.assertEqual('A', wt.branch.last_revision())
318
        # and now we can set it back to 'A'
319
        wt.set_last_revision('A')
320
        self.assertEqual('A', wt.last_revision())
321
        self.assertEqual('A', wt.branch.last_revision())
322
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
323
    def test_clone_and_commit_preserves_last_revision(self):
324
        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.
325
        cloned_dir = wt.bzrdir.clone('target')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
326
        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.
327
        self.assertNotEqual(cloned_dir.open_workingtree().last_revision(),
328
                            wt.last_revision())
329
330
    def test_clone_preserves_content(self):
331
        wt = self.make_branch_and_tree('source')
332
        self.build_tree(['added', 'deleted', 'notadded'], transport=wt.bzrdir.transport.clone('..'))
333
        wt.add('deleted', 'deleted')
334
        wt.commit('add deleted')
335
        wt.remove('deleted')
336
        wt.add('added', 'added')
337
        cloned_dir = wt.bzrdir.clone('target')
338
        cloned = cloned_dir.open_workingtree()
339
        cloned_transport = cloned.bzrdir.transport.clone('..')
340
        self.assertFalse(cloned_transport.has('deleted'))
341
        self.assertTrue(cloned_transport.has('added'))
342
        self.assertFalse(cloned_transport.has('notadded'))
343
        self.assertEqual('added', cloned.path2id('added'))
344
        self.assertEqual(None, cloned.path2id('deleted'))
345
        self.assertEqual(None, cloned.path2id('notadded'))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
346
        
347
    def test_basis_tree_returns_last_revision(self):
348
        wt = self.make_branch_and_tree('.')
349
        self.build_tree(['foo'])
350
        wt.add('foo', 'foo-id')
351
        wt.commit('A', rev_id='A')
352
        wt.rename_one('foo', 'bar')
353
        wt.commit('B', rev_id='B')
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
354
        wt.set_parent_ids(['B'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
355
        tree = wt.basis_tree()
356
        self.failUnless(tree.has_filename('bar'))
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
357
        wt.set_parent_ids(['A'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
358
        tree = wt.basis_tree()
359
        self.failUnless(tree.has_filename('foo'))
360
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.
361
    def test_clone_tree_revision(self):
362
        # make a tree with a last-revision,
363
        # and clone it with a different last-revision, this should switch
364
        # do it.
365
        #
366
        # also test that the content is merged
367
        # and conflicts recorded.
368
        # This should merge between the trees - local edits should be preserved
369
        # but other changes occured.
370
        # we test this by having one file that does
371
        # not change between two revisions, and another that does -
372
        # if the changed one is not changed, fail,
373
        # if the one that did not change has lost a local change, fail.
374
        # 
375
        raise TestSkipped('revision limiting is not implemented yet.')
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
376
377
    def test_initialize_with_revision_id(self):
378
        # a bzrdir can construct a working tree for itself @ a specific revision.
379
        source = self.make_branch_and_tree('source')
380
        source.commit('a', rev_id='a', allow_pointless=True)
381
        source.commit('b', rev_id='b', allow_pointless=True)
382
        self.build_tree(['new/'])
383
        made_control = self.bzrdir_format.initialize('new')
384
        source.branch.repository.clone(made_control)
385
        source.branch.clone(made_control)
386
        made_tree = self.workingtree_format.initialize(made_control, revision_id='a')
387
        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.
388
1508.1.24 by Robert Collins
Add update command for use with checkouts.
389
    def test_update_sets_last_revision(self):
390
        # working tree formats from the meta-dir format and newer support
391
        # setting the last revision on a tree independently of that on the 
392
        # branch. Its concievable that some future formats may want to 
393
        # couple them again (i.e. because its really a smart server and
394
        # the working tree will always match the branch). So we test
395
        # that formats where initialising a branch does not initialise a 
396
        # tree - and thus have separable entities - support skewing the 
397
        # two things.
398
        main_branch = self.make_branch('tree')
399
        try:
400
            # if there is a working tree now, this is not supported.
401
            main_branch.bzrdir.open_workingtree()
402
            return
403
        except errors.NoWorkingTree:
404
            pass
405
        wt = main_branch.bzrdir.create_workingtree()
406
        # create an out of date working tree by making a checkout in this
407
        # current format
408
        self.build_tree(['checkout/', 'tree/file'])
409
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
410
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
411
        old_tree = self.workingtree_format.initialize(checkout)
412
        # now commit to 'tree'
413
        wt.add('file')
414
        wt.commit('A', rev_id='A')
415
        # and update old_tree
416
        self.assertEqual(0, old_tree.update())
417
        self.failUnlessExists('checkout/file')
418
        self.assertEqual('A', old_tree.last_revision())
419
420
    def test_update_returns_conflict_count(self):
421
        # working tree formats from the meta-dir format and newer support
422
        # setting the last revision on a tree independently of that on the 
423
        # branch. Its concievable that some future formats may want to 
424
        # couple them again (i.e. because its really a smart server and
425
        # the working tree will always match the branch). So we test
426
        # that formats where initialising a branch does not initialise a 
427
        # tree - and thus have separable entities - support skewing the 
428
        # two things.
429
        main_branch = self.make_branch('tree')
430
        try:
431
            # if there is a working tree now, this is not supported.
432
            main_branch.bzrdir.open_workingtree()
433
            return
434
        except errors.NoWorkingTree:
435
            pass
436
        wt = main_branch.bzrdir.create_workingtree()
437
        # create an out of date working tree by making a checkout in this
438
        # current format
439
        self.build_tree(['checkout/', 'tree/file'])
440
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
441
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
442
        old_tree = self.workingtree_format.initialize(checkout)
443
        # now commit to 'tree'
444
        wt.add('file')
445
        wt.commit('A', rev_id='A')
446
        # and add a file file to the checkout
447
        self.build_tree(['checkout/file'])
448
        old_tree.add('file')
449
        # and update old_tree
450
        self.assertEqual(1, old_tree.update())
451
        self.assertEqual('A', old_tree.last_revision())
452
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
453
    def test_merge_revert(self):
454
        from bzrlib.merge import merge_inner
455
        this = self.make_branch_and_tree('b1')
456
        open('b1/a', 'wb').write('a test\n')
457
        this.add('a')
458
        open('b1/b', 'wb').write('b test\n')
459
        this.add('b')
460
        this.commit(message='')
461
        base = this.bzrdir.clone('b2').open_workingtree()
462
        open('b2/a', 'wb').write('b test\n')
463
        other = this.bzrdir.clone('b3').open_workingtree()
464
        open('b3/a', 'wb').write('c test\n')
465
        open('b3/c', 'wb').write('c test\n')
466
        other.add('c')
467
468
        open('b1/b', 'wb').write('q test\n')
469
        open('b1/d', 'wb').write('d test\n')
470
        merge_inner(this.branch, other, base, this_tree=this)
471
        self.assertNotEqual(open('b1/a', 'rb').read(), 'a test\n')
472
        this.revert([])
473
        self.assertEqual(open('b1/a', 'rb').read(), 'a test\n')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
474
        self.assertIs(os.path.exists('b1/b.~1~'), True)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
475
        self.assertIs(os.path.exists('b1/c'), False)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
476
        self.assertIs(os.path.exists('b1/a.~1~'), False)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
477
        self.assertIs(os.path.exists('b1/d'), True)
1534.7.200 by Aaron Bentley
Merge from mainline
478
1587.1.10 by Robert Collins
update updates working tree and branch together.
479
    def test_update_updates_bound_branch_no_local_commits(self):
480
        # doing an update in a tree updates the branch its bound to too.
481
        master_tree = self.make_branch_and_tree('master')
482
        tree = self.make_branch_and_tree('tree')
483
        try:
484
            tree.branch.bind(master_tree.branch)
485
        except errors.UpgradeRequired:
486
            # legacy branches cannot bind
487
            return
488
        master_tree.commit('foo', rev_id='foo', allow_pointless=True)
489
        tree.update()
490
        self.assertEqual('foo', tree.last_revision())
491
        self.assertEqual('foo', tree.branch.last_revision())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
492
493
    def test_update_turns_local_commit_into_merge(self):
494
        # 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.
495
        # makes pending-merges. 
496
        # this is done so that 'bzr update; bzr revert' will always produce
497
        # an exact copy of the 'logical branch' - the referenced branch for
498
        # a checkout, and the master for a bound branch.
499
        # its possible that we should instead have 'bzr update' when there
500
        # is nothing new on the master leave the current commits intact and
501
        # alter 'revert' to revert to the master always. But for now, its
502
        # good.
1587.1.11 by Robert Collins
Local commits appear to be working properly.
503
        master_tree = self.make_branch_and_tree('master')
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
504
        master_tip = master_tree.commit('first master commit')
1587.1.11 by Robert Collins
Local commits appear to be working properly.
505
        tree = self.make_branch_and_tree('tree')
506
        try:
507
            tree.branch.bind(master_tree.branch)
508
        except errors.UpgradeRequired:
509
            # legacy branches cannot bind
510
            return
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
511
        # sync with master
512
        tree.update()
513
        # work locally
1587.1.11 by Robert Collins
Local commits appear to be working properly.
514
        tree.commit('foo', rev_id='foo', allow_pointless=True, local=True)
515
        tree.commit('bar', rev_id='bar', allow_pointless=True, local=True)
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
516
        # sync with master prepatory to committing
1587.1.11 by Robert Collins
Local commits appear to be working properly.
517
        tree.update()
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
518
        # which should have pivoted the local tip into a merge
519
        self.assertEqual(master_tip, tree.last_revision())
520
        self.assertEqual([master_tip], tree.branch.revision_history())
521
        self.assertEqual([master_tip, 'bar'], tree.get_parent_ids())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
522
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
523
    def test_merge_modified(self):
524
        tree = self.make_branch_and_tree('master')
525
        tree._control_files.put('merge-hashes', StringIO('asdfasdf'))
526
        self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
527
1534.10.22 by Aaron Bentley
Got ConflictList implemented
528
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
529
        from bzrlib.tests.test_conflicts import example_conflicts
530
        tree = self.make_branch_and_tree('master')
531
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
532
            tree.set_conflicts(example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
533
        except UnsupportedOperation:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
534
            raise TestSkipped('set_conflicts not supported')
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
535
            
536
        tree2 = WorkingTree.open('master')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
537
        self.assertEqual(tree2.conflicts(), example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
538
        tree2._control_files.put('conflicts', StringIO(''))
539
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
540
                          tree2.conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
541
        tree2._control_files.put('conflicts', StringIO('a'))
542
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
543
                          tree2.conflicts)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
544
545
    def make_merge_conflicts(self):
546
        from bzrlib.merge import merge_inner 
547
        tree = self.make_branch_and_tree('mine')
548
        file('mine/bloo', 'wb').write('one')
549
        tree.add('bloo')
1534.10.14 by Aaron Bentley
Made revert clear conflicts
550
        file('mine/blo', 'wb').write('on')
551
        tree.add('blo')
1534.10.12 by Aaron Bentley
Merge produces new conflicts
552
        tree.commit("blah", allow_pointless=False)
553
        base = tree.basis_tree()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
554
        bzrdir.BzrDir.open("mine").sprout("other")
1534.10.12 by Aaron Bentley
Merge produces new conflicts
555
        file('other/bloo', 'wb').write('two')
556
        othertree = WorkingTree.open('other')
557
        othertree.commit('blah', allow_pointless=False)
558
        file('mine/bloo', 'wb').write('three')
559
        tree.commit("blah", allow_pointless=False)
560
        merge_inner(tree.branch, othertree, base, this_tree=tree)
561
        return tree
562
563
    def test_merge_conflicts(self):
564
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
565
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
566
567
    def test_clear_merge_conflicts(self):
568
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
569
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
570
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
571
            tree.set_conflicts(ConflictList())
1534.10.12 by Aaron Bentley
Merge produces new conflicts
572
        except UnsupportedOperation:
573
            raise TestSkipped
1534.10.22 by Aaron Bentley
Got ConflictList implemented
574
        self.assertEqual(tree.conflicts(), ConflictList())
1534.10.14 by Aaron Bentley
Made revert clear conflicts
575
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
576
    def test_add_conflicts(self):
577
        tree = self.make_branch_and_tree('tree')
578
        try:
579
            tree.add_conflicts([TextConflict('path_a')])
580
        except UnsupportedOperation:
581
            raise TestSkipped()
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
582
        self.assertEqual(ConflictList([TextConflict('path_a')]),
583
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
584
        tree.add_conflicts([TextConflict('path_a')])
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
585
        self.assertEqual(ConflictList([TextConflict('path_a')]), 
586
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
587
        tree.add_conflicts([ContentsConflict('path_a')])
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
588
        self.assertEqual(ConflictList([ContentsConflict('path_a'), 
589
                                       TextConflict('path_a')]),
590
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
591
        tree.add_conflicts([TextConflict('path_b')])
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
592
        self.assertEqual(ConflictList([ContentsConflict('path_a'), 
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
593
                                       TextConflict('path_a'),
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
594
                                       TextConflict('path_b')]),
595
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
596
1534.10.14 by Aaron Bentley
Made revert clear conflicts
597
    def test_revert_clear_conflicts(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(["blo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
601
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
602
        tree.revert(["bloo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
603
        self.assertEqual(len(tree.conflicts()), 0)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
604
605
    def test_revert_clear_conflicts2(self):
606
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
607
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
608
        tree.revert([])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
609
        self.assertEqual(len(tree.conflicts()), 0)
1624.3.22 by Olaf Conradi
Merge bzr.dev
610
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
611
    def test_format_description(self):
612
        tree = self.make_branch_and_tree('tree')
613
        text = tree._format.get_format_description()
614
        self.failUnless(len(text))
1681.1.1 by Robert Collins
Make WorkingTree.branch a read only property. (Robert Collins)
615
616
    def test_branch_attribute_is_not_settable(self):
617
        # the branch attribute is an aspect of the working tree, not a
618
        # configurable attribute
619
        tree = self.make_branch_and_tree('tree')
620
        def set_branch():
621
            tree.branch = tree.branch
622
        self.assertRaises(AttributeError, set_branch)
623
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
624
    def test_list_files_versioned_before_ignored(self):
625
        """A versioned file matching an ignore rule should not be ignored."""
626
        tree = self.make_branch_and_tree('.')
627
        self.build_tree(['foo.pyc'])
628
        # ensure that foo.pyc is ignored
629
        self.build_tree_contents([('.bzrignore', 'foo.pyc')])
630
        tree.add('foo.pyc', 'anid')
631
        files = sorted(list(tree.list_files()))
632
        self.assertEqual((u'.bzrignore', '?', 'file', None), files[0][:-1])
633
        self.assertEqual((u'foo.pyc', 'V', 'file', 'anid'), files[1][:-1])
634
        self.assertEqual(2, len(files))
1711.8.2 by John Arbash Meinel
Test that WorkingTree locks Branch before self, and unlocks self before Branch
635
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
636
    def test_non_normalized_add_accessible(self):
637
        try:
638
            self.build_tree([u'a\u030a'])
639
        except UnicodeError:
640
            raise TestSkipped('Filesystem does not support unicode filenames')
641
        tree = self.make_branch_and_tree('.')
642
        orig = osutils.normalized_filename
643
        osutils.normalized_filename = osutils._accessible_normalized_filename
644
        try:
645
            tree.add([u'a\u030a'])
1907.1.3 by Aaron Bentley
Fixed unicode test cases
646
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
1830.3.17 by John Arbash Meinel
list_files() with wrong normalized_filename code raises exceptions. Fix this
647
                    [(path, ie.kind) for path,ie in 
648
                                tree.inventory.iter_entries()])
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
649
        finally:
650
            osutils.normalized_filename = orig
651
652
    def test_non_normalized_add_inaccessible(self):
653
        try:
654
            self.build_tree([u'a\u030a'])
655
        except UnicodeError:
656
            raise TestSkipped('Filesystem does not support unicode filenames')
657
        tree = self.make_branch_and_tree('.')
658
        orig = osutils.normalized_filename
659
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
660
        try:
661
            self.assertRaises(errors.InvalidNormalization,
662
                tree.add, [u'a\u030a'])
663
        finally:
664
            osutils.normalized_filename = orig