/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)
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
31
from bzrlib.conflicts import ConflictList
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
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
148
    def test_unknowns(self):
149
        tree = self.make_branch_and_tree('.')
150
        self.build_tree(['hello.txt',
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
151
                         'hello.txt.~1~'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
152
        self.assertEquals(list(tree.unknowns()),
153
                          ['hello.txt'])
154
155
    def test_hashcache(self):
156
        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.
157
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
158
        self.build_tree(['hello.txt',
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
159
                         'hello.txt.~1~'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
160
        tree.add('hello.txt')
161
        pause()
162
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
163
        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.
164
        tree2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
165
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
166
        self.assertEqual(0, tree2._hashcache.miss_count)
167
        self.assertEqual(1, tree2._hashcache.hit_count)
168
169
    def test_initialize(self):
170
        # 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.
171
        t = self.make_branch_and_tree('.')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
172
        b = branch.Branch.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
173
        self.assertEqual(t.branch.base, b.base)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
174
        t2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
175
        self.assertEqual(t.basedir, t2.basedir)
176
        self.assertEqual(b.base, t2.branch.base)
177
        # TODO maybe we should check the branch format? not sure if its
178
        # appropriate here.
179
180
    def test_rename_dirs(self):
181
        """Test renaming directories and the files within them."""
182
        wt = self.make_branch_and_tree('.')
183
        b = wt.branch
184
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
185
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
186
187
        wt.commit('create initial state')
188
189
        revid = b.revision_history()[0]
190
        self.log('first revision_id is {%s}' % revid)
191
        
192
        inv = b.repository.get_revision_inventory(revid)
193
        self.log('contents of inventory: %r' % inv.entries())
194
195
        self.check_inventory_shape(inv,
196
                                   ['dir', 'dir/sub', 'dir/sub/file'])
197
198
        wt.rename_one('dir', 'newdir')
199
200
        self.check_inventory_shape(wt.read_working_inventory(),
201
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
202
203
        wt.rename_one('newdir/sub', 'newdir/newsub')
204
        self.check_inventory_shape(wt.read_working_inventory(),
205
                                   ['newdir', 'newdir/newsub',
206
                                    'newdir/newsub/file'])
207
208
    def test_add_in_unversioned(self):
209
        """Try to add a file in an unversioned directory.
210
211
        "bzr add" adds the parent as necessary, but simple working tree add
212
        doesn't do that.
213
        """
214
        from bzrlib.errors import NotVersionedError
215
        wt = self.make_branch_and_tree('.')
216
        self.build_tree(['foo/',
217
                         'foo/hello'])
218
        self.assertRaises(NotVersionedError,
219
                          wt.add,
220
                          'foo/hello')
221
222
    def test_add_missing(self):
223
        # adding a msising file -> NoSuchFile
224
        wt = self.make_branch_and_tree('.')
225
        self.assertRaises(errors.NoSuchFile, wt.add, 'fpp')
226
227
    def test_remove_verbose(self):
228
        #FIXME the remove api should not print or otherwise depend on the
229
        # text UI - RBC 20060124
230
        wt = self.make_branch_and_tree('.')
231
        self.build_tree(['hello'])
232
        wt.add(['hello'])
233
        wt.commit(message='add hello')
234
        stdout = StringIO()
235
        stderr = StringIO()
236
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
237
                                                     wt.remove,
238
                                                     ['hello'],
239
                                                     verbose=True))
240
        self.assertEqual('?       hello\n', stdout.getvalue())
241
        self.assertEqual('', stderr.getvalue())
242
243
    def test_clone_trivial(self):
244
        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.
245
        cloned_dir = wt.bzrdir.clone('target')
246
        cloned = cloned_dir.open_workingtree()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
247
        self.assertEqual(cloned.last_revision(), wt.last_revision())
248
249
    def test_last_revision(self):
250
        wt = self.make_branch_and_tree('source')
251
        self.assertEqual(None, wt.last_revision())
252
        wt.commit('A', allow_pointless=True, rev_id='A')
253
        self.assertEqual('A', wt.last_revision())
254
255
    def test_set_last_revision(self):
256
        wt = self.make_branch_and_tree('source')
257
        self.assertEqual(None, wt.last_revision())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
258
        # cannot set the last revision to one not in the branch history.
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
259
        self.assertRaises(errors.NoSuchRevision, wt.set_last_revision, 'A')
260
        wt.commit('A', allow_pointless=True, rev_id='A')
261
        self.assertEqual('A', wt.last_revision())
262
        # None is aways in the branch
263
        wt.set_last_revision(None)
264
        self.assertEqual(None, wt.last_revision())
265
        # and now we can set it to 'A'
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
266
        # because some formats mutate the branch to set it on the tree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
267
        # we need to alter the branch to let this pass.
268
        wt.branch.set_revision_history(['A', 'B'])
269
        wt.set_last_revision('A')
270
        self.assertEqual('A', wt.last_revision())
271
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
272
    def test_set_last_revision_different_to_branch(self):
273
        # working tree formats from the meta-dir format and newer support
274
        # setting the last revision on a tree independently of that on the 
275
        # branch. Its concievable that some future formats may want to 
276
        # couple them again (i.e. because its really a smart server and
277
        # the working tree will always match the branch). So we test
278
        # that formats where initialising a branch does not initialise a 
279
        # tree - and thus have separable entities - support skewing the 
280
        # two things.
281
        branch = self.make_branch('tree')
282
        try:
283
            # if there is a working tree now, this is not supported.
284
            branch.bzrdir.open_workingtree()
285
            return
286
        except errors.NoWorkingTree:
287
            pass
288
        wt = branch.bzrdir.create_workingtree()
289
        wt.commit('A', allow_pointless=True, rev_id='A')
290
        wt.set_last_revision(None)
291
        self.assertEqual(None, wt.last_revision())
292
        self.assertEqual('A', wt.branch.last_revision())
293
        # and now we can set it back to 'A'
294
        wt.set_last_revision('A')
295
        self.assertEqual('A', wt.last_revision())
296
        self.assertEqual('A', wt.branch.last_revision())
297
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
298
    def test_clone_and_commit_preserves_last_revision(self):
299
        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.
300
        cloned_dir = wt.bzrdir.clone('target')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
301
        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.
302
        self.assertNotEqual(cloned_dir.open_workingtree().last_revision(),
303
                            wt.last_revision())
304
305
    def test_clone_preserves_content(self):
306
        wt = self.make_branch_and_tree('source')
307
        self.build_tree(['added', 'deleted', 'notadded'], transport=wt.bzrdir.transport.clone('..'))
308
        wt.add('deleted', 'deleted')
309
        wt.commit('add deleted')
310
        wt.remove('deleted')
311
        wt.add('added', 'added')
312
        cloned_dir = wt.bzrdir.clone('target')
313
        cloned = cloned_dir.open_workingtree()
314
        cloned_transport = cloned.bzrdir.transport.clone('..')
315
        self.assertFalse(cloned_transport.has('deleted'))
316
        self.assertTrue(cloned_transport.has('added'))
317
        self.assertFalse(cloned_transport.has('notadded'))
318
        self.assertEqual('added', cloned.path2id('added'))
319
        self.assertEqual(None, cloned.path2id('deleted'))
320
        self.assertEqual(None, cloned.path2id('notadded'))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
321
        
322
    def test_basis_tree_returns_last_revision(self):
323
        wt = self.make_branch_and_tree('.')
324
        self.build_tree(['foo'])
325
        wt.add('foo', 'foo-id')
326
        wt.commit('A', rev_id='A')
327
        wt.rename_one('foo', 'bar')
328
        wt.commit('B', rev_id='B')
329
        wt.set_last_revision('B')
330
        tree = wt.basis_tree()
331
        self.failUnless(tree.has_filename('bar'))
332
        wt.set_last_revision('A')
333
        tree = wt.basis_tree()
334
        self.failUnless(tree.has_filename('foo'))
335
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.
336
    def test_clone_tree_revision(self):
337
        # make a tree with a last-revision,
338
        # and clone it with a different last-revision, this should switch
339
        # do it.
340
        #
341
        # also test that the content is merged
342
        # and conflicts recorded.
343
        # This should merge between the trees - local edits should be preserved
344
        # but other changes occured.
345
        # we test this by having one file that does
346
        # not change between two revisions, and another that does -
347
        # if the changed one is not changed, fail,
348
        # if the one that did not change has lost a local change, fail.
349
        # 
350
        raise TestSkipped('revision limiting is not implemented yet.')
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
351
352
    def test_initialize_with_revision_id(self):
353
        # a bzrdir can construct a working tree for itself @ a specific revision.
354
        source = self.make_branch_and_tree('source')
355
        source.commit('a', rev_id='a', allow_pointless=True)
356
        source.commit('b', rev_id='b', allow_pointless=True)
357
        self.build_tree(['new/'])
358
        made_control = self.bzrdir_format.initialize('new')
359
        source.branch.repository.clone(made_control)
360
        source.branch.clone(made_control)
361
        made_tree = self.workingtree_format.initialize(made_control, revision_id='a')
362
        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.
363
1508.1.24 by Robert Collins
Add update command for use with checkouts.
364
    def test_update_sets_last_revision(self):
365
        # working tree formats from the meta-dir format and newer support
366
        # setting the last revision on a tree independently of that on the 
367
        # branch. Its concievable that some future formats may want to 
368
        # couple them again (i.e. because its really a smart server and
369
        # the working tree will always match the branch). So we test
370
        # that formats where initialising a branch does not initialise a 
371
        # tree - and thus have separable entities - support skewing the 
372
        # two things.
373
        main_branch = self.make_branch('tree')
374
        try:
375
            # if there is a working tree now, this is not supported.
376
            main_branch.bzrdir.open_workingtree()
377
            return
378
        except errors.NoWorkingTree:
379
            pass
380
        wt = main_branch.bzrdir.create_workingtree()
381
        # create an out of date working tree by making a checkout in this
382
        # current format
383
        self.build_tree(['checkout/', 'tree/file'])
384
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
385
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
386
        old_tree = self.workingtree_format.initialize(checkout)
387
        # now commit to 'tree'
388
        wt.add('file')
389
        wt.commit('A', rev_id='A')
390
        # and update old_tree
391
        self.assertEqual(0, old_tree.update())
392
        self.failUnlessExists('checkout/file')
393
        self.assertEqual('A', old_tree.last_revision())
394
395
    def test_update_returns_conflict_count(self):
396
        # working tree formats from the meta-dir format and newer support
397
        # setting the last revision on a tree independently of that on the 
398
        # branch. Its concievable that some future formats may want to 
399
        # couple them again (i.e. because its really a smart server and
400
        # the working tree will always match the branch). So we test
401
        # that formats where initialising a branch does not initialise a 
402
        # tree - and thus have separable entities - support skewing the 
403
        # two things.
404
        main_branch = self.make_branch('tree')
405
        try:
406
            # if there is a working tree now, this is not supported.
407
            main_branch.bzrdir.open_workingtree()
408
            return
409
        except errors.NoWorkingTree:
410
            pass
411
        wt = main_branch.bzrdir.create_workingtree()
412
        # create an out of date working tree by making a checkout in this
413
        # current format
414
        self.build_tree(['checkout/', 'tree/file'])
415
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
416
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
417
        old_tree = self.workingtree_format.initialize(checkout)
418
        # now commit to 'tree'
419
        wt.add('file')
420
        wt.commit('A', rev_id='A')
421
        # and add a file file to the checkout
422
        self.build_tree(['checkout/file'])
423
        old_tree.add('file')
424
        # and update old_tree
425
        self.assertEqual(1, old_tree.update())
426
        self.assertEqual('A', old_tree.last_revision())
427
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
428
    def test_merge_revert(self):
429
        from bzrlib.merge import merge_inner
430
        this = self.make_branch_and_tree('b1')
431
        open('b1/a', 'wb').write('a test\n')
432
        this.add('a')
433
        open('b1/b', 'wb').write('b test\n')
434
        this.add('b')
435
        this.commit(message='')
436
        base = this.bzrdir.clone('b2').open_workingtree()
437
        open('b2/a', 'wb').write('b test\n')
438
        other = this.bzrdir.clone('b3').open_workingtree()
439
        open('b3/a', 'wb').write('c test\n')
440
        open('b3/c', 'wb').write('c test\n')
441
        other.add('c')
442
443
        open('b1/b', 'wb').write('q test\n')
444
        open('b1/d', 'wb').write('d test\n')
445
        merge_inner(this.branch, other, base, this_tree=this)
446
        self.assertNotEqual(open('b1/a', 'rb').read(), 'a test\n')
447
        this.revert([])
448
        self.assertEqual(open('b1/a', 'rb').read(), 'a test\n')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
449
        self.assertIs(os.path.exists('b1/b.~1~'), True)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
450
        self.assertIs(os.path.exists('b1/c'), False)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
451
        self.assertIs(os.path.exists('b1/a.~1~'), False)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
452
        self.assertIs(os.path.exists('b1/d'), True)
1534.7.200 by Aaron Bentley
Merge from mainline
453
1587.1.10 by Robert Collins
update updates working tree and branch together.
454
    def test_update_updates_bound_branch_no_local_commits(self):
455
        # doing an update in a tree updates the branch its bound to too.
456
        master_tree = self.make_branch_and_tree('master')
457
        tree = self.make_branch_and_tree('tree')
458
        try:
459
            tree.branch.bind(master_tree.branch)
460
        except errors.UpgradeRequired:
461
            # legacy branches cannot bind
462
            return
463
        master_tree.commit('foo', rev_id='foo', allow_pointless=True)
464
        tree.update()
465
        self.assertEqual('foo', tree.last_revision())
466
        self.assertEqual('foo', tree.branch.last_revision())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
467
468
    def test_update_turns_local_commit_into_merge(self):
469
        # 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.
470
        # makes pending-merges. 
471
        # this is done so that 'bzr update; bzr revert' will always produce
472
        # an exact copy of the 'logical branch' - the referenced branch for
473
        # a checkout, and the master for a bound branch.
474
        # its possible that we should instead have 'bzr update' when there
475
        # is nothing new on the master leave the current commits intact and
476
        # alter 'revert' to revert to the master always. But for now, its
477
        # good.
1587.1.11 by Robert Collins
Local commits appear to be working properly.
478
        master_tree = self.make_branch_and_tree('master')
479
        tree = self.make_branch_and_tree('tree')
480
        try:
481
            tree.branch.bind(master_tree.branch)
482
        except errors.UpgradeRequired:
483
            # legacy branches cannot bind
484
            return
485
        tree.commit('foo', rev_id='foo', allow_pointless=True, local=True)
486
        tree.commit('bar', rev_id='bar', allow_pointless=True, local=True)
487
        tree.update()
488
        self.assertEqual(None, tree.last_revision())
489
        self.assertEqual([], tree.branch.revision_history())
490
        self.assertEqual(['bar'], tree.pending_merges())
491
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
492
    def test_merge_modified(self):
493
        tree = self.make_branch_and_tree('master')
494
        tree._control_files.put('merge-hashes', StringIO('asdfasdf'))
495
        self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
496
1534.10.22 by Aaron Bentley
Got ConflictList implemented
497
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
498
        from bzrlib.tests.test_conflicts import example_conflicts
499
        tree = self.make_branch_and_tree('master')
500
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
501
            tree.set_conflicts(example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
502
        except UnsupportedOperation:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
503
            raise TestSkipped('set_conflicts not supported')
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
504
            
505
        tree2 = WorkingTree.open('master')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
506
        self.assertEqual(tree2.conflicts(), example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
507
        tree2._control_files.put('conflicts', StringIO(''))
508
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
509
                          tree2.conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
510
        tree2._control_files.put('conflicts', StringIO('a'))
511
        self.assertRaises(errors.ConflictFormatError, 
1534.10.22 by Aaron Bentley
Got ConflictList implemented
512
                          tree2.conflicts)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
513
514
    def make_merge_conflicts(self):
515
        from bzrlib.merge import merge_inner 
516
        tree = self.make_branch_and_tree('mine')
517
        file('mine/bloo', 'wb').write('one')
518
        tree.add('bloo')
1534.10.14 by Aaron Bentley
Made revert clear conflicts
519
        file('mine/blo', 'wb').write('on')
520
        tree.add('blo')
1534.10.12 by Aaron Bentley
Merge produces new conflicts
521
        tree.commit("blah", allow_pointless=False)
522
        base = tree.basis_tree()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
523
        bzrdir.BzrDir.open("mine").sprout("other")
1534.10.12 by Aaron Bentley
Merge produces new conflicts
524
        file('other/bloo', 'wb').write('two')
525
        othertree = WorkingTree.open('other')
526
        othertree.commit('blah', allow_pointless=False)
527
        file('mine/bloo', 'wb').write('three')
528
        tree.commit("blah", allow_pointless=False)
529
        merge_inner(tree.branch, othertree, base, this_tree=tree)
530
        return tree
531
532
    def test_merge_conflicts(self):
533
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
534
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
535
536
    def test_clear_merge_conflicts(self):
537
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
538
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
539
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
540
            tree.set_conflicts(ConflictList())
1534.10.12 by Aaron Bentley
Merge produces new conflicts
541
        except UnsupportedOperation:
542
            raise TestSkipped
1534.10.22 by Aaron Bentley
Got ConflictList implemented
543
        self.assertEqual(tree.conflicts(), ConflictList())
1534.10.14 by Aaron Bentley
Made revert clear conflicts
544
545
    def test_revert_clear_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.14 by Aaron Bentley
Made revert clear conflicts
548
        tree.revert(["blo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
549
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
550
        tree.revert(["bloo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
551
        self.assertEqual(len(tree.conflicts()), 0)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
552
553
    def test_revert_clear_conflicts2(self):
554
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
555
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
556
        tree.revert([])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
557
        self.assertEqual(len(tree.conflicts()), 0)
1624.3.22 by Olaf Conradi
Merge bzr.dev
558
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
559
    def test_format_description(self):
560
        tree = self.make_branch_and_tree('tree')
561
        text = tree._format.get_format_description()
562
        self.failUnless(len(text))
1681.1.1 by Robert Collins
Make WorkingTree.branch a read only property. (Robert Collins)
563
564
    def test_branch_attribute_is_not_settable(self):
565
        # the branch attribute is an aspect of the working tree, not a
566
        # configurable attribute
567
        tree = self.make_branch_and_tree('tree')
568
        def set_branch():
569
            tree.branch = tree.branch
570
        self.assertRaises(AttributeError, set_branch)
571
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
572
    def test_list_files_versioned_before_ignored(self):
573
        """A versioned file matching an ignore rule should not be ignored."""
574
        tree = self.make_branch_and_tree('.')
575
        self.build_tree(['foo.pyc'])
576
        # ensure that foo.pyc is ignored
577
        self.build_tree_contents([('.bzrignore', 'foo.pyc')])
578
        tree.add('foo.pyc', 'anid')
579
        files = sorted(list(tree.list_files()))
580
        self.assertEqual((u'.bzrignore', '?', 'file', None), files[0][:-1])
581
        self.assertEqual((u'foo.pyc', 'V', 'file', 'anid'), files[1][:-1])
582
        self.assertEqual(2, len(files))