/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1852.15.19 by John Arbash Meinel
[merge] bzr.dev 2255
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
2
# Authors:  Robert Collins <robert.collins@canonical.com>
2255.13.4 by Martin Pool
merge
3
#           and others
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
from cStringIO import StringIO
20
import os
1711.7.19 by John Arbash Meinel
file:// urls look slightly different on win32
21
import sys
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
22
23
import bzrlib
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
24
from bzrlib import branch, bzrdir, errors, osutils, urlutils, workingtree
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
25
from bzrlib.errors import (NotBranchError, NotVersionedError,
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
26
                           UnsupportedOperation, PathsNotVersionedError)
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
27
from bzrlib.inventory import Inventory
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
28
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.
29
from bzrlib.tests import TestSkipped
30
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
31
from bzrlib.trace import mutter
32
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
33
                                WorkingTree)
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
34
from bzrlib.conflicts import ConflictList, TextConflict, ContentsConflict
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
35
1711.7.19 by John Arbash Meinel
file:// urls look slightly different on win32
36
1711.8.2 by John Arbash Meinel
Test that WorkingTree locks Branch before self, and unlocks self before Branch
37
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
38
class TestWorkingTree(TestCaseWithWorkingTree):
39
1732.1.8 by John Arbash Meinel
Adding a test for list_files
40
    def test_list_files(self):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
41
        tree = self.make_branch_and_tree('.')
1732.1.8 by John Arbash Meinel
Adding a test for list_files
42
        self.build_tree(['dir/', 'file'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
43
        if has_symlinks():
44
            os.symlink('target', 'symlink')
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
45
        tree.lock_read()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
46
        files = list(tree.list_files())
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
47
        tree.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
48
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
49
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
50
        if has_symlinks():
51
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
52
1732.1.8 by John Arbash Meinel
Adding a test for list_files
53
    def test_list_files_sorted(self):
54
        tree = self.make_branch_and_tree('.')
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
55
        self.build_tree(['dir/', 'file', 'dir/file', 'dir/b',
56
                         'dir/subdir/', 'a', 'dir/subfile',
57
                         'zz_dir/', 'zz_dir/subfile'])
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
58
        tree.lock_read()
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
59
        files = [(path, kind) for (path, v, kind, file_id, entry)
60
                               in tree.list_files()]
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
61
        tree.unlock()
1732.1.8 by John Arbash Meinel
Adding a test for list_files
62
        self.assertEqual([
63
            ('a', 'file'),
64
            ('dir', 'directory'),
65
            ('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.
66
            ('zz_dir', 'directory'),
1732.1.8 by John Arbash Meinel
Adding a test for list_files
67
            ], files)
68
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.
69
        tree.add(['dir', 'zz_dir'])
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
70
        tree.lock_read()
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
71
        files = [(path, kind) for (path, v, kind, file_id, entry)
72
                               in tree.list_files()]
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
73
        tree.unlock()
1732.1.8 by John Arbash Meinel
Adding a test for list_files
74
        self.assertEqual([
75
            ('a', 'file'),
76
            ('dir', 'directory'),
77
            ('dir/b', 'file'),
78
            ('dir/file', 'file'),
79
            ('dir/subdir', 'directory'),
80
            ('dir/subfile', 'file'),
81
            ('file', 'file'),
1732.1.22 by John Arbash Meinel
Bug in list_files if the last entry in a directory is another directory
82
            ('zz_dir', 'directory'),
83
            ('zz_dir/subfile', 'file'),
1732.1.8 by John Arbash Meinel
Adding a test for list_files
84
            ], files)
85
1551.10.29 by Aaron Bentley
Fix tree.list_files when file kind changes
86
    def test_list_files_kind_change(self):
87
        tree = self.make_branch_and_tree('tree')
88
        self.build_tree(['tree/filename'])
89
        tree.add('filename', 'file-id')
90
        os.unlink('tree/filename')
91
        self.build_tree(['tree/filename/'])
92
        tree.lock_read()
93
        self.addCleanup(tree.unlock)
94
        result = list(tree.list_files())
95
        self.assertEqual(1, len(result))
96
        self.assertEqual(('filename', 'V', 'directory', 'file-id'),
97
                         result[0][:4])
98
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
99
    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.
100
        branch = self.make_branch_and_tree('.').branch
1711.7.28 by John Arbash Meinel
clean up the WorkingTree.open_containing tests
101
        local_base = urlutils.local_path_from_url(branch.base)
102
103
        # Empty opens '.'
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
104
        wt, relpath = WorkingTree.open_containing()
105
        self.assertEqual('', relpath)
1711.7.28 by John Arbash Meinel
clean up the WorkingTree.open_containing tests
106
        self.assertEqual(wt.basedir + '/', local_base)
107
108
        # '.' opens this dir
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
109
        wt, relpath = WorkingTree.open_containing(u'.')
110
        self.assertEqual('', relpath)
1711.7.28 by John Arbash Meinel
clean up the WorkingTree.open_containing tests
111
        self.assertEqual(wt.basedir + '/', local_base)
112
113
        # './foo' finds '.' and a relpath of 'foo'
114
        wt, relpath = WorkingTree.open_containing('./foo')
115
        self.assertEqual('foo', relpath)
116
        self.assertEqual(wt.basedir + '/', local_base)
117
118
        # abspath(foo) finds '.' and relpath of 'foo'
119
        wt, relpath = WorkingTree.open_containing('./foo')
120
        wt, relpath = WorkingTree.open_containing(getcwd() + '/foo')
121
        self.assertEqual('foo', relpath)
122
        self.assertEqual(wt.basedir + '/', local_base)
123
124
        # can even be a url: finds '.' and relpath of 'foo'
125
        wt, relpath = WorkingTree.open_containing('./foo')
126
        wt, relpath = WorkingTree.open_containing(
127
                    urlutils.local_path_to_url(getcwd() + '/foo'))
128
        self.assertEqual('foo', relpath)
129
        self.assertEqual(wt.basedir + '/', local_base)
130
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
131
    def test_basic_relpath(self):
132
        # 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.
133
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
134
        self.assertEqual('child',
135
                         tree.relpath(pathjoin(getcwd(), 'child')))
136
137
    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.
138
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
139
        tree.lock_read()
140
        self.assertEqual('r', tree.branch.peek_lock_mode())
141
        tree.unlock()
142
        self.assertEqual(None, tree.branch.peek_lock_mode())
143
        tree.lock_write()
144
        self.assertEqual('w', tree.branch.peek_lock_mode())
145
        tree.unlock()
146
        self.assertEqual(None, tree.branch.peek_lock_mode())
147
 
148
    def test_revert(self):
149
        """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.
150
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
151
152
        self.build_tree(['hello.txt'])
153
        file('hello.txt', 'w').write('initial hello')
154
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
155
        self.assertRaises(PathsNotVersionedError,
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
156
                          tree.revert, ['hello.txt'])
157
        tree.add(['hello.txt'])
158
        tree.commit('create initial hello.txt')
159
160
        self.check_file_contents('hello.txt', 'initial hello')
161
        file('hello.txt', 'w').write('new hello')
162
        self.check_file_contents('hello.txt', 'new hello')
163
164
        # revert file modified since last revision
165
        tree.revert(['hello.txt'])
166
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
167
        self.check_file_contents('hello.txt.~1~', 'new hello')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
168
169
        # reverting again does not clobber the backup
170
        tree.revert(['hello.txt'])
171
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
172
        self.check_file_contents('hello.txt.~1~', 'new hello')
1534.10.28 by Aaron Bentley
Use numbered backup files
173
        
174
        # backup files are numbered
175
        file('hello.txt', 'w').write('new hello2')
176
        tree.revert(['hello.txt'])
177
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
178
        self.check_file_contents('hello.txt.~1~', 'new hello')
179
        self.check_file_contents('hello.txt.~2~', 'new hello2')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
180
1558.12.7 by Aaron Bentley
Fixed revert with missing files
181
    def test_revert_missing(self):
182
        # Revert a file that has been deleted since last commit
183
        tree = self.make_branch_and_tree('.')
184
        file('hello.txt', 'w').write('initial hello')
185
        tree.add('hello.txt')
186
        tree.commit('added hello.txt')
187
        os.unlink('hello.txt')
188
        tree.remove('hello.txt')
189
        tree.revert(['hello.txt'])
190
        self.failUnlessExists('hello.txt')
191
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
192
    def test_versioned_files_not_unknown(self):
193
        tree = self.make_branch_and_tree('.')
1831.1.1 by Martin Pool
[merge] remove default ignore list & update
194
        self.build_tree(['hello.txt'])
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
195
        tree.add('hello.txt')
196
        self.assertEquals(list(tree.unknowns()),
197
                          [])
1831.1.1 by Martin Pool
[merge] remove default ignore list & update
198
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
199
    def test_unknowns(self):
200
        tree = self.make_branch_and_tree('.')
201
        self.build_tree(['hello.txt',
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
202
                         'hello.txt.~1~'])
1765.1.1 by Robert Collins
Remove the default ignores list from bzr, lowering the minimum overhead in bzr add.
203
        self.build_tree_contents([('.bzrignore', '*.~*\n')])
204
        tree.add('.bzrignore')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
205
        self.assertEquals(list(tree.unknowns()),
206
                          ['hello.txt'])
207
208
    def test_initialize(self):
209
        # 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.
210
        t = self.make_branch_and_tree('.')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
211
        b = branch.Branch.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
212
        self.assertEqual(t.branch.base, b.base)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
213
        t2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
214
        self.assertEqual(t.basedir, t2.basedir)
215
        self.assertEqual(b.base, t2.branch.base)
216
        # TODO maybe we should check the branch format? not sure if its
217
        # appropriate here.
218
219
    def test_rename_dirs(self):
220
        """Test renaming directories and the files within them."""
221
        wt = self.make_branch_and_tree('.')
222
        b = wt.branch
223
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
224
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
225
226
        wt.commit('create initial state')
227
228
        revid = b.revision_history()[0]
229
        self.log('first revision_id is {%s}' % revid)
230
        
231
        inv = b.repository.get_revision_inventory(revid)
232
        self.log('contents of inventory: %r' % inv.entries())
233
234
        self.check_inventory_shape(inv,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
235
                                   ['dir/', 'dir/sub/', 'dir/sub/file'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
236
        wt.rename_one('dir', 'newdir')
237
2255.2.57 by Robert Collins
Dirstate test change: TestWorkingTree.test_rename_dirs should lock around accessing the trees inventory.
238
        wt.lock_read()
239
        self.check_inventory_shape(wt.inventory,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
240
                                   ['newdir/', 'newdir/sub/', 'newdir/sub/file'])
2255.2.57 by Robert Collins
Dirstate test change: TestWorkingTree.test_rename_dirs should lock around accessing the trees inventory.
241
        wt.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
242
        wt.rename_one('newdir/sub', 'newdir/newsub')
2255.2.57 by Robert Collins
Dirstate test change: TestWorkingTree.test_rename_dirs should lock around accessing the trees inventory.
243
        wt.lock_read()
244
        self.check_inventory_shape(wt.inventory,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
245
                                   ['newdir/', 'newdir/newsub/',
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
246
                                    'newdir/newsub/file'])
2255.2.57 by Robert Collins
Dirstate test change: TestWorkingTree.test_rename_dirs should lock around accessing the trees inventory.
247
        wt.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
248
249
    def test_add_in_unversioned(self):
250
        """Try to add a file in an unversioned directory.
251
252
        "bzr add" adds the parent as necessary, but simple working tree add
253
        doesn't do that.
254
        """
255
        from bzrlib.errors import NotVersionedError
256
        wt = self.make_branch_and_tree('.')
257
        self.build_tree(['foo/',
258
                         'foo/hello'])
259
        self.assertRaises(NotVersionedError,
260
                          wt.add,
261
                          'foo/hello')
262
263
    def test_add_missing(self):
264
        # adding a msising file -> NoSuchFile
265
        wt = self.make_branch_and_tree('.')
266
        self.assertRaises(errors.NoSuchFile, wt.add, 'fpp')
267
268
    def test_remove_verbose(self):
269
        #FIXME the remove api should not print or otherwise depend on the
270
        # text UI - RBC 20060124
271
        wt = self.make_branch_and_tree('.')
272
        self.build_tree(['hello'])
273
        wt.add(['hello'])
274
        wt.commit(message='add hello')
275
        stdout = StringIO()
276
        stderr = StringIO()
277
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
278
                                                     wt.remove,
279
                                                     ['hello'],
280
                                                     verbose=True))
281
        self.assertEqual('?       hello\n', stdout.getvalue())
282
        self.assertEqual('', stderr.getvalue())
283
284
    def test_clone_trivial(self):
285
        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.
286
        cloned_dir = wt.bzrdir.clone('target')
287
        cloned = cloned_dir.open_workingtree()
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
288
        self.assertEqual(cloned.get_parent_ids(), wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
289
290
    def test_last_revision(self):
291
        wt = self.make_branch_and_tree('source')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
292
        self.assertEqual([], wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
293
        wt.commit('A', allow_pointless=True, rev_id='A')
2249.5.7 by John Arbash Meinel
Make sure WorkingTree revision_ids are also returned as utf8 strings
294
        parent_ids = wt.get_parent_ids()
295
        self.assertEqual(['A'], parent_ids)
296
        for parent_id in parent_ids:
297
            self.assertIsInstance(parent_id, str)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
298
299
    def test_set_last_revision(self):
300
        wt = self.make_branch_and_tree('source')
1908.1.1 by Robert Collins
Relax WorkingTree.set_last-revision to allow any revision to be set.
301
        # set last-revision to one not in the history
302
        wt.set_last_revision('A')
303
        # set it back to None for an empty tree.
304
        wt.set_last_revision(None)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
305
        wt.commit('A', allow_pointless=True, rev_id='A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
306
        self.assertEqual(['A'], wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
307
        # None is aways in the branch
308
        wt.set_last_revision(None)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
309
        self.assertEqual([], wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
310
        # and now we can set it to 'A'
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
311
        # because some formats mutate the branch to set it on the tree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
312
        # we need to alter the branch to let this pass.
2230.3.27 by Aaron Bentley
Skip arbitrary revision-history test for Branch6
313
        try:
314
            wt.branch.set_revision_history(['A', 'B'])
315
        except errors.NoSuchRevision, e:
316
            self.assertEqual('B', e.revision)
317
            raise TestSkipped("Branch format does not permit arbitrary"
318
                              " history")
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
319
        wt.set_last_revision('A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
320
        self.assertEqual(['A'], wt.get_parent_ids())
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
321
        self.assertRaises(errors.ReservedId, wt.set_last_revision, 'A:')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
322
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
323
    def test_set_last_revision_different_to_branch(self):
324
        # working tree formats from the meta-dir format and newer support
325
        # setting the last revision on a tree independently of that on the 
326
        # branch. Its concievable that some future formats may want to 
327
        # couple them again (i.e. because its really a smart server and
328
        # the working tree will always match the branch). So we test
329
        # that formats where initialising a branch does not initialise a 
330
        # tree - and thus have separable entities - support skewing the 
331
        # two things.
332
        branch = self.make_branch('tree')
333
        try:
334
            # if there is a working tree now, this is not supported.
335
            branch.bzrdir.open_workingtree()
336
            return
337
        except errors.NoWorkingTree:
338
            pass
339
        wt = branch.bzrdir.create_workingtree()
340
        wt.commit('A', allow_pointless=True, rev_id='A')
341
        wt.set_last_revision(None)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
342
        self.assertEqual([], wt.get_parent_ids())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
343
        self.assertEqual('A', wt.branch.last_revision())
344
        # and now we can set it back to 'A'
345
        wt.set_last_revision('A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
346
        self.assertEqual(['A'], wt.get_parent_ids())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
347
        self.assertEqual('A', wt.branch.last_revision())
348
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
349
    def test_clone_and_commit_preserves_last_revision(self):
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
350
        """Doing a commit into a clone tree does not affect the source."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
351
        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.
352
        cloned_dir = wt.bzrdir.clone('target')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
353
        wt.commit('A', allow_pointless=True, rev_id='A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
354
        self.assertNotEqual(cloned_dir.open_workingtree().get_parent_ids(),
355
                            wt.get_parent_ids())
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.
356
357
    def test_clone_preserves_content(self):
358
        wt = self.make_branch_and_tree('source')
2255.2.51 by John Arbash Meinel
simple rewrap for 79 char lines
359
        self.build_tree(['added', 'deleted', 'notadded'],
360
                        transport=wt.bzrdir.transport.clone('..'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
361
        wt.add('deleted', 'deleted')
362
        wt.commit('add deleted')
363
        wt.remove('deleted')
364
        wt.add('added', 'added')
365
        cloned_dir = wt.bzrdir.clone('target')
366
        cloned = cloned_dir.open_workingtree()
367
        cloned_transport = cloned.bzrdir.transport.clone('..')
368
        self.assertFalse(cloned_transport.has('deleted'))
369
        self.assertTrue(cloned_transport.has('added'))
370
        self.assertFalse(cloned_transport.has('notadded'))
371
        self.assertEqual('added', cloned.path2id('added'))
372
        self.assertEqual(None, cloned.path2id('deleted'))
373
        self.assertEqual(None, cloned.path2id('notadded'))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
374
        
375
    def test_basis_tree_returns_last_revision(self):
376
        wt = self.make_branch_and_tree('.')
377
        self.build_tree(['foo'])
378
        wt.add('foo', 'foo-id')
379
        wt.commit('A', rev_id='A')
380
        wt.rename_one('foo', 'bar')
381
        wt.commit('B', rev_id='B')
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
382
        wt.set_parent_ids(['B'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
383
        tree = wt.basis_tree()
2255.2.30 by Robert Collins
Some workingtree_implementations/test_workingtree.py test work - add DirStateRevisionTree.has_filename, locks around appropriate calls in tests.
384
        tree.lock_read()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
385
        self.failUnless(tree.has_filename('bar'))
2255.2.30 by Robert Collins
Some workingtree_implementations/test_workingtree.py test work - add DirStateRevisionTree.has_filename, locks around appropriate calls in tests.
386
        tree.unlock()
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
387
        wt.set_parent_ids(['A'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
388
        tree = wt.basis_tree()
2255.2.30 by Robert Collins
Some workingtree_implementations/test_workingtree.py test work - add DirStateRevisionTree.has_filename, locks around appropriate calls in tests.
389
        tree.lock_read()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
390
        self.failUnless(tree.has_filename('foo'))
2255.2.30 by Robert Collins
Some workingtree_implementations/test_workingtree.py test work - add DirStateRevisionTree.has_filename, locks around appropriate calls in tests.
391
        tree.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
392
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.
393
    def test_clone_tree_revision(self):
394
        # make a tree with a last-revision,
395
        # and clone it with a different last-revision, this should switch
396
        # do it.
397
        #
398
        # also test that the content is merged
399
        # and conflicts recorded.
400
        # This should merge between the trees - local edits should be preserved
401
        # but other changes occured.
402
        # we test this by having one file that does
403
        # not change between two revisions, and another that does -
404
        # if the changed one is not changed, fail,
405
        # if the one that did not change has lost a local change, fail.
406
        # 
407
        raise TestSkipped('revision limiting is not implemented yet.')
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
408
409
    def test_initialize_with_revision_id(self):
410
        # a bzrdir can construct a working tree for itself @ a specific revision.
411
        source = self.make_branch_and_tree('source')
412
        source.commit('a', rev_id='a', allow_pointless=True)
413
        source.commit('b', rev_id='b', allow_pointless=True)
414
        self.build_tree(['new/'])
415
        made_control = self.bzrdir_format.initialize('new')
416
        source.branch.repository.clone(made_control)
417
        source.branch.clone(made_control)
418
        made_tree = self.workingtree_format.initialize(made_control, revision_id='a')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
419
        self.assertEqual(['a'], made_tree.get_parent_ids())
1508.1.23 by Robert Collins
Test that the working tree last revision is indeed set during commit.
420
1508.1.24 by Robert Collins
Add update command for use with checkouts.
421
    def test_update_sets_last_revision(self):
422
        # working tree formats from the meta-dir format and newer support
423
        # setting the last revision on a tree independently of that on the 
424
        # branch. Its concievable that some future formats may want to 
425
        # couple them again (i.e. because its really a smart server and
426
        # the working tree will always match the branch). So we test
427
        # that formats where initialising a branch does not initialise a 
428
        # tree - and thus have separable entities - support skewing the 
429
        # two things.
430
        main_branch = self.make_branch('tree')
431
        try:
432
            # if there is a working tree now, this is not supported.
433
            main_branch.bzrdir.open_workingtree()
434
            return
435
        except errors.NoWorkingTree:
436
            pass
437
        wt = main_branch.bzrdir.create_workingtree()
438
        # create an out of date working tree by making a checkout in this
439
        # current format
440
        self.build_tree(['checkout/', 'tree/file'])
441
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
442
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
443
        old_tree = self.workingtree_format.initialize(checkout)
444
        # now commit to 'tree'
445
        wt.add('file')
446
        wt.commit('A', rev_id='A')
447
        # and update old_tree
448
        self.assertEqual(0, old_tree.update())
449
        self.failUnlessExists('checkout/file')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
450
        self.assertEqual(['A'], old_tree.get_parent_ids())
1508.1.24 by Robert Collins
Add update command for use with checkouts.
451
1731.1.33 by Aaron Bentley
Revert no-special-root changes
452
    def test_update_sets_root_id(self):
453
        """Ensure tree root is set properly by update.
454
        
455
        Since empty trees don't have root_ids, but workingtrees do,
456
        an update of a checkout of revision 0 to a new revision,  should set
457
        the root id.
458
        """
459
        wt = self.make_branch_and_tree('tree')
460
        main_branch = wt.branch
461
        # create an out of date working tree by making a checkout in this
462
        # current format
463
        self.build_tree(['checkout/', 'tree/file'])
1731.1.43 by Aaron Bentley
Merge more checkout changes
464
        checkout = main_branch.create_checkout('checkout')
1731.1.33 by Aaron Bentley
Revert no-special-root changes
465
        # now commit to 'tree'
466
        wt.add('file')
467
        wt.commit('A', rev_id='A')
468
        # and update checkout 
469
        self.assertEqual(0, checkout.update())
470
        self.failUnlessExists('checkout/file')
471
        self.assertEqual(wt.get_root_id(), checkout.get_root_id())
472
        self.assertNotEqual(None, wt.get_root_id())
473
1508.1.24 by Robert Collins
Add update command for use with checkouts.
474
    def test_update_returns_conflict_count(self):
475
        # working tree formats from the meta-dir format and newer support
476
        # setting the last revision on a tree independently of that on the 
477
        # branch. Its concievable that some future formats may want to 
478
        # couple them again (i.e. because its really a smart server and
479
        # the working tree will always match the branch). So we test
480
        # that formats where initialising a branch does not initialise a 
481
        # tree - and thus have separable entities - support skewing the 
482
        # two things.
483
        main_branch = self.make_branch('tree')
484
        try:
485
            # if there is a working tree now, this is not supported.
486
            main_branch.bzrdir.open_workingtree()
487
            return
488
        except errors.NoWorkingTree:
489
            pass
490
        wt = main_branch.bzrdir.create_workingtree()
491
        # create an out of date working tree by making a checkout in this
492
        # current format
493
        self.build_tree(['checkout/', 'tree/file'])
494
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
495
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
496
        old_tree = self.workingtree_format.initialize(checkout)
497
        # now commit to 'tree'
498
        wt.add('file')
499
        wt.commit('A', rev_id='A')
500
        # and add a file file to the checkout
501
        self.build_tree(['checkout/file'])
502
        old_tree.add('file')
503
        # and update old_tree
504
        self.assertEqual(1, old_tree.update())
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
505
        self.assertEqual(['A'], old_tree.get_parent_ids())
1508.1.24 by Robert Collins
Add update command for use with checkouts.
506
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
507
    def test_merge_revert(self):
508
        from bzrlib.merge import merge_inner
509
        this = self.make_branch_and_tree('b1')
510
        open('b1/a', 'wb').write('a test\n')
511
        this.add('a')
512
        open('b1/b', 'wb').write('b test\n')
513
        this.add('b')
514
        this.commit(message='')
515
        base = this.bzrdir.clone('b2').open_workingtree()
516
        open('b2/a', 'wb').write('b test\n')
517
        other = this.bzrdir.clone('b3').open_workingtree()
518
        open('b3/a', 'wb').write('c test\n')
519
        open('b3/c', 'wb').write('c test\n')
520
        other.add('c')
521
522
        open('b1/b', 'wb').write('q test\n')
523
        open('b1/d', 'wb').write('d test\n')
524
        merge_inner(this.branch, other, base, this_tree=this)
525
        self.assertNotEqual(open('b1/a', 'rb').read(), 'a test\n')
526
        this.revert([])
527
        self.assertEqual(open('b1/a', 'rb').read(), 'a test\n')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
528
        self.assertIs(os.path.exists('b1/b.~1~'), True)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
529
        self.assertIs(os.path.exists('b1/c'), False)
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
530
        self.assertIs(os.path.exists('b1/a.~1~'), False)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
531
        self.assertIs(os.path.exists('b1/d'), True)
1534.7.200 by Aaron Bentley
Merge from mainline
532
1587.1.10 by Robert Collins
update updates working tree and branch together.
533
    def test_update_updates_bound_branch_no_local_commits(self):
534
        # doing an update in a tree updates the branch its bound to too.
535
        master_tree = self.make_branch_and_tree('master')
536
        tree = self.make_branch_and_tree('tree')
537
        try:
538
            tree.branch.bind(master_tree.branch)
539
        except errors.UpgradeRequired:
540
            # legacy branches cannot bind
541
            return
542
        master_tree.commit('foo', rev_id='foo', allow_pointless=True)
543
        tree.update()
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
544
        self.assertEqual(['foo'], tree.get_parent_ids())
1587.1.10 by Robert Collins
update updates working tree and branch together.
545
        self.assertEqual('foo', tree.branch.last_revision())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
546
547
    def test_update_turns_local_commit_into_merge(self):
548
        # 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.
549
        # makes pending-merges. 
550
        # this is done so that 'bzr update; bzr revert' will always produce
551
        # an exact copy of the 'logical branch' - the referenced branch for
552
        # a checkout, and the master for a bound branch.
553
        # its possible that we should instead have 'bzr update' when there
554
        # is nothing new on the master leave the current commits intact and
555
        # alter 'revert' to revert to the master always. But for now, its
556
        # good.
1587.1.11 by Robert Collins
Local commits appear to be working properly.
557
        master_tree = self.make_branch_and_tree('master')
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
558
        master_tip = master_tree.commit('first master commit')
1587.1.11 by Robert Collins
Local commits appear to be working properly.
559
        tree = self.make_branch_and_tree('tree')
560
        try:
561
            tree.branch.bind(master_tree.branch)
562
        except errors.UpgradeRequired:
563
            # legacy branches cannot bind
564
            return
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
565
        # sync with master
566
        tree.update()
567
        # work locally
1587.1.11 by Robert Collins
Local commits appear to be working properly.
568
        tree.commit('foo', rev_id='foo', allow_pointless=True, local=True)
569
        tree.commit('bar', rev_id='bar', allow_pointless=True, local=True)
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
570
        # sync with master prepatory to committing
1587.1.11 by Robert Collins
Local commits appear to be working properly.
571
        tree.update()
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
572
        # which should have pivoted the local tip into a merge
573
        self.assertEqual([master_tip, 'bar'], tree.get_parent_ids())
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
574
        # and the local branch history should match the masters now.
575
        self.assertEqual(master_tree.branch.revision_history(),
576
            tree.branch.revision_history())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
577
2255.2.156 by Martin Pool
Merge WorkingTree implementation back from trunk
578
    def test_merge_modified_detects_corruption(self):
579
        # FIXME: This doesn't really test that it works; also this is not
580
        # implementation-independent. mbp 20070226
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
581
        tree = self.make_branch_and_tree('master')
1955.3.14 by John Arbash Meinel
Correctly fix the workingtree put() test fixes
582
        tree._control_files.put('merge-hashes', StringIO('asdfasdf'))
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
583
        self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
584
2298.1.1 by Martin Pool
Add test for merge_modified
585
    def test_merge_modified(self):
586
        # merge_modified stores a map from file id to hash
587
        tree = self.make_branch_and_tree('tree')
588
        d = {'file-id': osutils.sha_string('hello')}
589
        self.build_tree_contents([('tree/somefile', 'hello')])
590
        tree.lock_write()
591
        try:
592
            tree.add(['somefile'], ['file-id'])
593
            tree.set_merge_modified(d)
594
            mm = tree.merge_modified()
595
            self.assertEquals(mm, d)
596
        finally:
597
            tree.unlock()
598
        mm = tree.merge_modified()
599
        self.assertEquals(mm, d)
600
1534.10.22 by Aaron Bentley
Got ConflictList implemented
601
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
602
        from bzrlib.tests.test_conflicts import example_conflicts
603
        tree = self.make_branch_and_tree('master')
604
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
605
            tree.set_conflicts(example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
606
        except UnsupportedOperation:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
607
            raise TestSkipped('set_conflicts not supported')
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
608
            
609
        tree2 = WorkingTree.open('master')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
610
        self.assertEqual(tree2.conflicts(), example_conflicts)
1955.3.14 by John Arbash Meinel
Correctly fix the workingtree put() test fixes
611
        tree2._control_files.put('conflicts', StringIO(''))
612
        self.assertRaises(errors.ConflictFormatError, 
613
                          tree2.conflicts)
614
        tree2._control_files.put('conflicts', StringIO('a'))
615
        self.assertRaises(errors.ConflictFormatError, 
616
                          tree2.conflicts)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
617
618
    def make_merge_conflicts(self):
2255.2.32 by Robert Collins
Make test_clear_merge_conflicts pass for dirstate. This involved working
619
        from bzrlib.merge import merge_inner
1534.10.12 by Aaron Bentley
Merge produces new conflicts
620
        tree = self.make_branch_and_tree('mine')
621
        file('mine/bloo', 'wb').write('one')
1534.10.14 by Aaron Bentley
Made revert clear conflicts
622
        file('mine/blo', 'wb').write('on')
2255.2.32 by Robert Collins
Make test_clear_merge_conflicts pass for dirstate. This involved working
623
        tree.add(['bloo', 'blo'])
1534.10.12 by Aaron Bentley
Merge produces new conflicts
624
        tree.commit("blah", allow_pointless=False)
2255.5.3 by John Arbash Meinel
XXX Workaround the DirStateRevisionTree bug until we get a proper fix, tests pass again
625
        base = tree.branch.repository.revision_tree(tree.last_revision())
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
626
        bzrdir.BzrDir.open("mine").sprout("other")
1534.10.12 by Aaron Bentley
Merge produces new conflicts
627
        file('other/bloo', 'wb').write('two')
628
        othertree = WorkingTree.open('other')
629
        othertree.commit('blah', allow_pointless=False)
630
        file('mine/bloo', 'wb').write('three')
631
        tree.commit("blah", allow_pointless=False)
632
        merge_inner(tree.branch, othertree, base, this_tree=tree)
633
        return tree
634
635
    def test_merge_conflicts(self):
636
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
637
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
638
639
    def test_clear_merge_conflicts(self):
640
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
641
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
642
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
643
            tree.set_conflicts(ConflictList())
1534.10.12 by Aaron Bentley
Merge produces new conflicts
644
        except UnsupportedOperation:
645
            raise TestSkipped
1534.10.22 by Aaron Bentley
Got ConflictList implemented
646
        self.assertEqual(tree.conflicts(), ConflictList())
1534.10.14 by Aaron Bentley
Made revert clear conflicts
647
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
648
    def test_add_conflicts(self):
649
        tree = self.make_branch_and_tree('tree')
650
        try:
651
            tree.add_conflicts([TextConflict('path_a')])
652
        except UnsupportedOperation:
653
            raise TestSkipped()
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
654
        self.assertEqual(ConflictList([TextConflict('path_a')]),
655
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
656
        tree.add_conflicts([TextConflict('path_a')])
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
657
        self.assertEqual(ConflictList([TextConflict('path_a')]), 
658
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
659
        tree.add_conflicts([ContentsConflict('path_a')])
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
660
        self.assertEqual(ConflictList([ContentsConflict('path_a'), 
661
                                       TextConflict('path_a')]),
662
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
663
        tree.add_conflicts([TextConflict('path_b')])
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
664
        self.assertEqual(ConflictList([ContentsConflict('path_a'), 
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
665
                                       TextConflict('path_a'),
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
666
                                       TextConflict('path_b')]),
667
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
668
1534.10.14 by Aaron Bentley
Made revert clear conflicts
669
    def test_revert_clear_conflicts(self):
670
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
671
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
672
        tree.revert(["blo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
673
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
674
        tree.revert(["bloo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
675
        self.assertEqual(len(tree.conflicts()), 0)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
676
677
    def test_revert_clear_conflicts2(self):
678
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
679
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
680
        tree.revert([])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
681
        self.assertEqual(len(tree.conflicts()), 0)
1624.3.22 by Olaf Conradi
Merge bzr.dev
682
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
683
    def test_format_description(self):
684
        tree = self.make_branch_and_tree('tree')
685
        text = tree._format.get_format_description()
686
        self.failUnless(len(text))
1681.1.1 by Robert Collins
Make WorkingTree.branch a read only property. (Robert Collins)
687
688
    def test_branch_attribute_is_not_settable(self):
689
        # the branch attribute is an aspect of the working tree, not a
690
        # configurable attribute
691
        tree = self.make_branch_and_tree('tree')
692
        def set_branch():
693
            tree.branch = tree.branch
694
        self.assertRaises(AttributeError, set_branch)
695
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
696
    def test_list_files_versioned_before_ignored(self):
697
        """A versioned file matching an ignore rule should not be ignored."""
698
        tree = self.make_branch_and_tree('.')
699
        self.build_tree(['foo.pyc'])
700
        # ensure that foo.pyc is ignored
701
        self.build_tree_contents([('.bzrignore', 'foo.pyc')])
702
        tree.add('foo.pyc', 'anid')
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
703
        tree.lock_read()
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
704
        files = sorted(list(tree.list_files()))
2255.2.52 by Robert Collins
Dirstate - fix workingtree.list_files to use the public interface to access the trees inventory.
705
        tree.unlock()
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
706
        self.assertEqual((u'.bzrignore', '?', 'file', None), files[0][:-1])
707
        self.assertEqual((u'foo.pyc', 'V', 'file', 'anid'), files[1][:-1])
708
        self.assertEqual(2, len(files))
1711.8.2 by John Arbash Meinel
Test that WorkingTree locks Branch before self, and unlocks self before Branch
709
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
710
    def test_non_normalized_add_accessible(self):
711
        try:
712
            self.build_tree([u'a\u030a'])
713
        except UnicodeError:
714
            raise TestSkipped('Filesystem does not support unicode filenames')
715
        tree = self.make_branch_and_tree('.')
716
        orig = osutils.normalized_filename
717
        osutils.normalized_filename = osutils._accessible_normalized_filename
718
        try:
719
            tree.add([u'a\u030a'])
2255.2.58 by Robert Collins
Fix the way we used osutils.normalized_filename in dirstate to support overriding in tests - and document this in the original location it was used.
720
            tree.lock_read()
1907.1.3 by Aaron Bentley
Fixed unicode test cases
721
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
1830.3.17 by John Arbash Meinel
list_files() with wrong normalized_filename code raises exceptions. Fix this
722
                    [(path, ie.kind) for path,ie in 
723
                                tree.inventory.iter_entries()])
2255.2.58 by Robert Collins
Fix the way we used osutils.normalized_filename in dirstate to support overriding in tests - and document this in the original location it was used.
724
            tree.unlock()
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
725
        finally:
726
            osutils.normalized_filename = orig
727
728
    def test_non_normalized_add_inaccessible(self):
729
        try:
730
            self.build_tree([u'a\u030a'])
731
        except UnicodeError:
732
            raise TestSkipped('Filesystem does not support unicode filenames')
733
        tree = self.make_branch_and_tree('.')
734
        orig = osutils.normalized_filename
735
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
736
        try:
737
            self.assertRaises(errors.InvalidNormalization,
738
                tree.add, [u'a\u030a'])
739
        finally:
740
            osutils.normalized_filename = orig
2123.3.9 by Steffen Eichenberg
added tests for deprecated API workingtree.move
741
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
742
    def test__write_inventory(self):
743
        # The private interface _write_inventory is currently used by transform.
744
        tree = self.make_branch_and_tree('.')
745
        # if we write write an inventory then do a walkdirs we should get back
746
        # missing entries, and actual, and unknowns as appropriate.
747
        self.build_tree(['present', 'unknown'])
2255.2.28 by Robert Collins
TestWorkingTree.test__write_inventory needs to lock the tree before calling _write_inventory for dirstate.
748
        inventory = Inventory(tree.path2id(''))
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
749
        inventory.add_path('missing', 'file', 'missing-id')
750
        inventory.add_path('present', 'file', 'present-id')
2255.2.28 by Robert Collins
TestWorkingTree.test__write_inventory needs to lock the tree before calling _write_inventory for dirstate.
751
        # there is no point in being able to write an inventory to an unlocked
752
        # tree object - its a low level api not a convenience api.
753
        tree.lock_write()
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
754
        tree._write_inventory(inventory)
2255.2.28 by Robert Collins
TestWorkingTree.test__write_inventory needs to lock the tree before calling _write_inventory for dirstate.
755
        tree.unlock()
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
756
        tree.lock_read()
757
        try:
758
            present_stat = os.lstat('present')
759
            unknown_stat = os.lstat('unknown')
760
            expected_results = [
761
                (('', tree.inventory.root.file_id),
762
                 [('missing', 'missing', 'unknown', None, 'missing-id', 'file'),
763
                  ('present', 'present', 'file', present_stat, 'present-id', 'file'),
764
                  ('unknown', 'unknown', 'file', unknown_stat, None, None),
765
                 ]
766
                )]
767
            self.assertEqual(expected_results, list(tree.walkdirs()))
768
        finally:
769
            tree.unlock()
2255.7.56 by Robert Collins
Document behaviour of tree.path2id("path/").
770
771
    def test_path2id(self):
2255.7.62 by Robert Collins
Update the Tree.filter_unversioned_files docstring to reflect what the existing implementations actually do, and change the WorkingTree4 implementation to match a newly created test for it.
772
        # smoke test for path2id
2255.7.56 by Robert Collins
Document behaviour of tree.path2id("path/").
773
        tree = self.make_branch_and_tree('.')
774
        self.build_tree(['foo'])
775
        tree.add(['foo'], ['foo-id'])
776
        self.assertEqual('foo-id', tree.path2id('foo'))
777
        # the next assertion is for backwards compatability with WorkingTree3,
778
        # though its probably a bad idea, it makes things work. Perhaps
779
        # it should raise a deprecation warning?
780
        self.assertEqual('foo-id', tree.path2id('foo/'))
2255.7.62 by Robert Collins
Update the Tree.filter_unversioned_files docstring to reflect what the existing implementations actually do, and change the WorkingTree4 implementation to match a newly created test for it.
781
782
    def test_filter_unversioned_files(self):
783
        # smoke test for filter_unversioned_files
784
        tree = self.make_branch_and_tree('.')
785
        paths = ['here-and-versioned', 'here-and-not-versioned',
786
            'not-here-and-versioned', 'not-here-and-not-versioned']
787
        tree.add(['here-and-versioned', 'not-here-and-versioned'],
788
            kinds=['file', 'file'])
789
        self.build_tree(['here-and-versioned', 'here-and-not-versioned'])
790
        tree.lock_read()
791
        self.addCleanup(tree.unlock)
792
        self.assertEqual(
793
            set(['not-here-and-not-versioned', 'here-and-not-versioned']),
794
            tree.filter_unversioned_files(paths))
2255.2.200 by Martin Pool
Add simple test for WorkingTree.kind
795
796
    def test_detect_real_kind(self):
797
        # working trees report the real kind of the file on disk, not the kind
798
        # they had when they were first added
799
        # create one file of every interesting type
800
        tree = self.make_branch_and_tree('.')
801
        self.build_tree(['file', 'directory/'])
802
        names = ['file', 'directory']
803
        if has_symlinks():
804
            os.symlink('target', 'symlink')
805
            names.append('symlink')
806
        tree.add(names, [n + '-id' for n in names])
807
        if tree.supports_tree_reference():
808
            sub_tree = self.make_branch_and_tree('tree-reference')
809
            sub_tree.set_root_id('tree-reference-id')
810
            sub_tree.commit('message')
811
            names.append('tree-reference')
812
            tree.add_reference(sub_tree)
813
        # now when we first look, we should see everything with the same kind
814
        # with which they were initially added
815
        for n in names:
816
            actual_kind = tree.kind(n + '-id')
817
            self.assertEqual(n, actual_kind)
818
        # move them around so the names no longer correspond to the types
819
        os.rename(names[0], 'tmp')
820
        for i in range(1, len(names)):
821
            os.rename(names[i], names[i-1])
822
        os.rename('tmp', names[-1])
2255.2.202 by Martin Pool
WorkingTree_4.kind should report tree-references if they're
823
        # now look and expect to see the correct types again
824
        for i in range(len(names)):
825
            actual_kind = tree.kind(names[i-1] + '-id')
826
            expected_kind = names[i]
827
            self.assertEqual(expected_kind, actual_kind)
2499.3.1 by Aaron Bentley
Fix Workingtree4.get_file_sha1 on missing files
828
829
    def test_missing_file_sha1(self):
830
        """If a file is missing, its sha1 should be reported as None."""
831
        tree = self.make_branch_and_tree('.')
832
        tree.lock_write()
833
        self.addCleanup(tree.unlock)
834
        self.build_tree(['file'])
835
        tree.add('file', 'file-id')
836
        tree.commit('file added')
837
        os.unlink('file')
838
        self.assertIs(None, tree.get_file_sha1('file-id'))