/brz/remove-bazaar

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