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