/brz/remove-bazaar

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