/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4634.125.4 by John Arbash Meinel
Merge bzr/2.0@4725, resolve NEWS
1
# Copyright (C) 2006-2010 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
4183.7.1 by Sabin Iacob
update FSF mailing address
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
18
19
from cStringIO import StringIO
3136.1.10 by Aaron Bentley
Clean error if filesystem does not support hard-links
20
import errno
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
21
import os
1711.7.19 by John Arbash Meinel
file:// urls look slightly different on win32
22
import sys
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
23
3287.20.3 by John Arbash Meinel
Aaron recommended to make this a WT_impl test.
24
from bzrlib import (
25
    branch,
26
    bzrdir,
27
    errors,
28
    osutils,
29
    tests,
30
    urlutils,
31
    workingtree,
32
    )
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
33
from bzrlib.errors import (NotBranchError, NotVersionedError,
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
34
                           UnsupportedOperation, PathsNotVersionedError)
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
35
from bzrlib.inventory import Inventory
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
36
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
3619.6.7 by Mark Hammond
John asked for TestNotApplicable instead of TestSkipped when no os.link.
37
from bzrlib.tests import TestSkipped, TestNotApplicable
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
38
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
39
from bzrlib.trace import mutter
40
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
3034.4.9 by Alexander Belchenko
skip test_workingtree.TestWorkingTree.test_case_sensitive for WT2
41
                                WorkingTree, WorkingTree2)
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
42
from bzrlib.conflicts import ConflictList, TextConflict, ContentsConflict
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
43
1711.7.19 by John Arbash Meinel
file:// urls look slightly different on win32
44
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
45
class TestWorkingTree(TestCaseWithWorkingTree):
46
1732.1.8 by John Arbash Meinel
Adding a test for list_files
47
    def test_list_files(self):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
48
        tree = self.make_branch_and_tree('.')
1732.1.8 by John Arbash Meinel
Adding a test for list_files
49
        self.build_tree(['dir/', 'file'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
50
        if has_symlinks():
51
            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.
52
        tree.lock_read()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
53
        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.
54
        tree.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
55
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
56
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
57
        if has_symlinks():
58
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
59
1732.1.8 by John Arbash Meinel
Adding a test for list_files
60
    def test_list_files_sorted(self):
61
        tree = self.make_branch_and_tree('.')
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
62
        self.build_tree(['dir/', 'file', 'dir/file', 'dir/b',
63
                         'dir/subdir/', 'a', 'dir/subfile',
64
                         '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.
65
        tree.lock_read()
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
66
        files = [(path, kind) for (path, v, kind, file_id, entry)
67
                               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.
68
        tree.unlock()
1732.1.8 by John Arbash Meinel
Adding a test for list_files
69
        self.assertEqual([
70
            ('a', 'file'),
71
            ('dir', 'directory'),
72
            ('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.
73
            ('zz_dir', 'directory'),
1732.1.8 by John Arbash Meinel
Adding a test for list_files
74
            ], files)
75
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.
76
        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.
77
        tree.lock_read()
1836.1.18 by John Arbash Meinel
Cleaned up the last failing tests. All tests pass again.
78
        files = [(path, kind) for (path, v, kind, file_id, entry)
79
                               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.
80
        tree.unlock()
1732.1.8 by John Arbash Meinel
Adding a test for list_files
81
        self.assertEqual([
82
            ('a', 'file'),
83
            ('dir', 'directory'),
84
            ('dir/b', 'file'),
85
            ('dir/file', 'file'),
86
            ('dir/subdir', 'directory'),
87
            ('dir/subfile', 'file'),
88
            ('file', 'file'),
1732.1.22 by John Arbash Meinel
Bug in list_files if the last entry in a directory is another directory
89
            ('zz_dir', 'directory'),
90
            ('zz_dir/subfile', 'file'),
1732.1.8 by John Arbash Meinel
Adding a test for list_files
91
            ], files)
92
1551.10.29 by Aaron Bentley
Fix tree.list_files when file kind changes
93
    def test_list_files_kind_change(self):
94
        tree = self.make_branch_and_tree('tree')
95
        self.build_tree(['tree/filename'])
96
        tree.add('filename', 'file-id')
97
        os.unlink('tree/filename')
98
        self.build_tree(['tree/filename/'])
99
        tree.lock_read()
100
        self.addCleanup(tree.unlock)
101
        result = list(tree.list_files())
102
        self.assertEqual(1, len(result))
103
        self.assertEqual(('filename', 'V', 'directory', 'file-id'),
104
                         result[0][:4])
105
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
106
    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.
107
        branch = self.make_branch_and_tree('.').branch
1711.7.28 by John Arbash Meinel
clean up the WorkingTree.open_containing tests
108
        local_base = urlutils.local_path_from_url(branch.base)
109
110
        # Empty opens '.'
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
111
        wt, relpath = WorkingTree.open_containing()
112
        self.assertEqual('', relpath)
1711.7.28 by John Arbash Meinel
clean up the WorkingTree.open_containing tests
113
        self.assertEqual(wt.basedir + '/', local_base)
114
115
        # '.' opens this dir
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
116
        wt, relpath = WorkingTree.open_containing(u'.')
117
        self.assertEqual('', relpath)
1711.7.28 by John Arbash Meinel
clean up the WorkingTree.open_containing tests
118
        self.assertEqual(wt.basedir + '/', local_base)
119
120
        # './foo' finds '.' and a relpath of 'foo'
121
        wt, relpath = WorkingTree.open_containing('./foo')
122
        self.assertEqual('foo', relpath)
123
        self.assertEqual(wt.basedir + '/', local_base)
124
125
        # abspath(foo) finds '.' and relpath of 'foo'
126
        wt, relpath = WorkingTree.open_containing('./foo')
127
        wt, relpath = WorkingTree.open_containing(getcwd() + '/foo')
128
        self.assertEqual('foo', relpath)
129
        self.assertEqual(wt.basedir + '/', local_base)
130
131
        # can even be a url: finds '.' and relpath of 'foo'
132
        wt, relpath = WorkingTree.open_containing('./foo')
133
        wt, relpath = WorkingTree.open_containing(
134
                    urlutils.local_path_to_url(getcwd() + '/foo'))
135
        self.assertEqual('foo', relpath)
136
        self.assertEqual(wt.basedir + '/', local_base)
137
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
138
    def test_basic_relpath(self):
139
        # 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.
140
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
141
        self.assertEqual('child',
142
                         tree.relpath(pathjoin(getcwd(), 'child')))
143
144
    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.
145
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
146
        tree.lock_read()
147
        self.assertEqual('r', tree.branch.peek_lock_mode())
148
        tree.unlock()
149
        self.assertEqual(None, tree.branch.peek_lock_mode())
150
        tree.lock_write()
151
        self.assertEqual('w', tree.branch.peek_lock_mode())
152
        tree.unlock()
153
        self.assertEqual(None, tree.branch.peek_lock_mode())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
154
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
155
    def test_revert(self):
156
        """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.
157
        tree = self.make_branch_and_tree('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
158
159
        self.build_tree(['hello.txt'])
160
        file('hello.txt', 'w').write('initial hello')
161
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
162
        self.assertRaises(PathsNotVersionedError,
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
163
                          tree.revert, ['hello.txt'])
164
        tree.add(['hello.txt'])
165
        tree.commit('create initial hello.txt')
166
167
        self.check_file_contents('hello.txt', 'initial hello')
168
        file('hello.txt', 'w').write('new hello')
169
        self.check_file_contents('hello.txt', 'new hello')
170
171
        # revert file modified since last revision
172
        tree.revert(['hello.txt'])
173
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
174
        self.check_file_contents('hello.txt.~1~', 'new hello')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
175
176
        # reverting again does not clobber the backup
177
        tree.revert(['hello.txt'])
178
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
179
        self.check_file_contents('hello.txt.~1~', 'new hello')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
180
1534.10.28 by Aaron Bentley
Use numbered backup files
181
        # backup files are numbered
182
        file('hello.txt', 'w').write('new hello2')
183
        tree.revert(['hello.txt'])
184
        self.check_file_contents('hello.txt', 'initial hello')
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
185
        self.check_file_contents('hello.txt.~1~', 'new hello')
186
        self.check_file_contents('hello.txt.~2~', 'new hello2')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
187
1558.12.7 by Aaron Bentley
Fixed revert with missing files
188
    def test_revert_missing(self):
189
        # Revert a file that has been deleted since last commit
190
        tree = self.make_branch_and_tree('.')
191
        file('hello.txt', 'w').write('initial hello')
192
        tree.add('hello.txt')
193
        tree.commit('added hello.txt')
194
        os.unlink('hello.txt')
195
        tree.remove('hello.txt')
196
        tree.revert(['hello.txt'])
197
        self.failUnlessExists('hello.txt')
198
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
199
    def test_versioned_files_not_unknown(self):
200
        tree = self.make_branch_and_tree('.')
1831.1.1 by Martin Pool
[merge] remove default ignore list & update
201
        self.build_tree(['hello.txt'])
1740.6.1 by Martin Pool
Remove Scratch objects used by doctests
202
        tree.add('hello.txt')
203
        self.assertEquals(list(tree.unknowns()),
204
                          [])
1831.1.1 by Martin Pool
[merge] remove default ignore list & update
205
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
206
    def test_unknowns(self):
207
        tree = self.make_branch_and_tree('.')
208
        self.build_tree(['hello.txt',
1534.10.29 by Aaron Bentley
Fixed backup numbering to match GNU standard better
209
                         'hello.txt.~1~'])
1765.1.1 by Robert Collins
Remove the default ignores list from bzr, lowering the minimum overhead in bzr add.
210
        self.build_tree_contents([('.bzrignore', '*.~*\n')])
211
        tree.add('.bzrignore')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
212
        self.assertEquals(list(tree.unknowns()),
213
                          ['hello.txt'])
214
215
    def test_initialize(self):
216
        # 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.
217
        t = self.make_branch_and_tree('.')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
218
        b = branch.Branch.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
219
        self.assertEqual(t.branch.base, b.base)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
220
        t2 = WorkingTree.open('.')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
221
        self.assertEqual(t.basedir, t2.basedir)
222
        self.assertEqual(b.base, t2.branch.base)
223
        # TODO maybe we should check the branch format? not sure if its
224
        # appropriate here.
225
226
    def test_rename_dirs(self):
227
        """Test renaming directories and the files within them."""
228
        wt = self.make_branch_and_tree('.')
229
        b = wt.branch
230
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
231
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
232
233
        wt.commit('create initial state')
234
235
        revid = b.revision_history()[0]
236
        self.log('first revision_id is {%s}' % revid)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
237
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
238
        inv = b.repository.get_revision_inventory(revid)
239
        self.log('contents of inventory: %r' % inv.entries())
240
241
        self.check_inventory_shape(inv,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
242
                                   ['dir/', 'dir/sub/', 'dir/sub/file'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
243
        wt.rename_one('dir', 'newdir')
244
2255.2.57 by Robert Collins
Dirstate test change: TestWorkingTree.test_rename_dirs should lock around accessing the trees inventory.
245
        wt.lock_read()
246
        self.check_inventory_shape(wt.inventory,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
247
                                   ['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.
248
        wt.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
249
        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.
250
        wt.lock_read()
251
        self.check_inventory_shape(wt.inventory,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
252
                                   ['newdir/', 'newdir/newsub/',
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
253
                                    'newdir/newsub/file'])
2255.2.57 by Robert Collins
Dirstate test change: TestWorkingTree.test_rename_dirs should lock around accessing the trees inventory.
254
        wt.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
255
256
    def test_add_in_unversioned(self):
257
        """Try to add a file in an unversioned directory.
258
259
        "bzr add" adds the parent as necessary, but simple working tree add
260
        doesn't do that.
261
        """
262
        from bzrlib.errors import NotVersionedError
263
        wt = self.make_branch_and_tree('.')
264
        self.build_tree(['foo/',
265
                         'foo/hello'])
266
        self.assertRaises(NotVersionedError,
267
                          wt.add,
268
                          'foo/hello')
269
270
    def test_add_missing(self):
271
        # adding a msising file -> NoSuchFile
272
        wt = self.make_branch_and_tree('.')
273
        self.assertRaises(errors.NoSuchFile, wt.add, 'fpp')
274
275
    def test_remove_verbose(self):
276
        #FIXME the remove api should not print or otherwise depend on the
277
        # text UI - RBC 20060124
278
        wt = self.make_branch_and_tree('.')
279
        self.build_tree(['hello'])
280
        wt.add(['hello'])
281
        wt.commit(message='add hello')
282
        stdout = StringIO()
283
        stderr = StringIO()
284
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
285
                                                     wt.remove,
286
                                                     ['hello'],
287
                                                     verbose=True))
288
        self.assertEqual('?       hello\n', stdout.getvalue())
289
        self.assertEqual('', stderr.getvalue())
290
291
    def test_clone_trivial(self):
292
        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.
293
        cloned_dir = wt.bzrdir.clone('target')
294
        cloned = cloned_dir.open_workingtree()
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
295
        self.assertEqual(cloned.get_parent_ids(), wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
296
297
    def test_last_revision(self):
298
        wt = self.make_branch_and_tree('source')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
299
        self.assertEqual([], wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
300
        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
301
        parent_ids = wt.get_parent_ids()
302
        self.assertEqual(['A'], parent_ids)
303
        for parent_id in parent_ids:
304
            self.assertIsInstance(parent_id, str)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
305
306
    def test_set_last_revision(self):
307
        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.
308
        # set last-revision to one not in the history
309
        wt.set_last_revision('A')
310
        # set it back to None for an empty tree.
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
311
        wt.set_last_revision('null:')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
312
        wt.commit('A', allow_pointless=True, rev_id='A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
313
        self.assertEqual(['A'], wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
314
        # None is aways in the branch
2598.5.3 by Aaron Bentley
Push NULL_REVISION deeper
315
        wt.set_last_revision('null:')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
316
        self.assertEqual([], wt.get_parent_ids())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
317
        # and now we can set it to 'A'
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
318
        # because some formats mutate the branch to set it on the tree
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
319
        # we need to alter the branch to let this pass.
2230.3.27 by Aaron Bentley
Skip arbitrary revision-history test for Branch6
320
        try:
321
            wt.branch.set_revision_history(['A', 'B'])
322
        except errors.NoSuchRevision, e:
323
            self.assertEqual('B', e.revision)
324
            raise TestSkipped("Branch format does not permit arbitrary"
325
                              " history")
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
326
        wt.set_last_revision('A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
327
        self.assertEqual(['A'], wt.get_parent_ids())
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
328
        self.assertRaises(errors.ReservedId, wt.set_last_revision, 'A:')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
329
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
330
    def test_set_last_revision_different_to_branch(self):
331
        # working tree formats from the meta-dir format and newer support
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
332
        # setting the last revision on a tree independently of that on the
333
        # branch. Its concievable that some future formats may want to
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
334
        # couple them again (i.e. because its really a smart server and
335
        # the working tree will always match the branch). So we test
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
336
        # that formats where initialising a branch does not initialise a
337
        # tree - and thus have separable entities - support skewing the
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
338
        # two things.
339
        branch = self.make_branch('tree')
340
        try:
341
            # if there is a working tree now, this is not supported.
342
            branch.bzrdir.open_workingtree()
343
            return
344
        except errors.NoWorkingTree:
345
            pass
346
        wt = branch.bzrdir.create_workingtree()
347
        wt.commit('A', allow_pointless=True, rev_id='A')
348
        wt.set_last_revision(None)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
349
        self.assertEqual([], wt.get_parent_ids())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
350
        self.assertEqual('A', wt.branch.last_revision())
351
        # and now we can set it back to 'A'
352
        wt.set_last_revision('A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
353
        self.assertEqual(['A'], wt.get_parent_ids())
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
354
        self.assertEqual('A', wt.branch.last_revision())
355
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
356
    def test_clone_and_commit_preserves_last_revision(self):
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
357
        """Doing a commit into a clone tree does not affect the source."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
358
        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.
359
        cloned_dir = wt.bzrdir.clone('target')
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
360
        wt.commit('A', allow_pointless=True, rev_id='A')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
361
        self.assertNotEqual(cloned_dir.open_workingtree().get_parent_ids(),
362
                            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.
363
364
    def test_clone_preserves_content(self):
365
        wt = self.make_branch_and_tree('source')
2255.2.51 by John Arbash Meinel
simple rewrap for 79 char lines
366
        self.build_tree(['added', 'deleted', 'notadded'],
367
                        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.
368
        wt.add('deleted', 'deleted')
369
        wt.commit('add deleted')
370
        wt.remove('deleted')
371
        wt.add('added', 'added')
372
        cloned_dir = wt.bzrdir.clone('target')
373
        cloned = cloned_dir.open_workingtree()
374
        cloned_transport = cloned.bzrdir.transport.clone('..')
375
        self.assertFalse(cloned_transport.has('deleted'))
376
        self.assertTrue(cloned_transport.has('added'))
377
        self.assertFalse(cloned_transport.has('notadded'))
378
        self.assertEqual('added', cloned.path2id('added'))
379
        self.assertEqual(None, cloned.path2id('deleted'))
380
        self.assertEqual(None, cloned.path2id('notadded'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
381
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
382
    def test_basis_tree_returns_last_revision(self):
383
        wt = self.make_branch_and_tree('.')
384
        self.build_tree(['foo'])
385
        wt.add('foo', 'foo-id')
386
        wt.commit('A', rev_id='A')
387
        wt.rename_one('foo', 'bar')
388
        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.
389
        wt.set_parent_ids(['B'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
390
        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.
391
        tree.lock_read()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
392
        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.
393
        tree.unlock()
1908.6.3 by Robert Collins
Tidy up the last_revision_id and add_pending_merge conversion to use cleaner apis.
394
        wt.set_parent_ids(['A'])
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
395
        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.
396
        tree.lock_read()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
397
        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.
398
        tree.unlock()
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
399
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.
400
    def test_clone_tree_revision(self):
401
        # make a tree with a last-revision,
402
        # and clone it with a different last-revision, this should switch
403
        # do it.
404
        #
405
        # also test that the content is merged
406
        # and conflicts recorded.
407
        # This should merge between the trees - local edits should be preserved
408
        # but other changes occured.
409
        # we test this by having one file that does
410
        # not change between two revisions, and another that does -
411
        # if the changed one is not changed, fail,
412
        # if the one that did not change has lost a local change, fail.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
413
        #
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.
414
        raise TestSkipped('revision limiting is not implemented yet.')
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
415
416
    def test_initialize_with_revision_id(self):
417
        # a bzrdir can construct a working tree for itself @ a specific revision.
418
        source = self.make_branch_and_tree('source')
419
        source.commit('a', rev_id='a', allow_pointless=True)
420
        source.commit('b', rev_id='b', allow_pointless=True)
421
        self.build_tree(['new/'])
422
        made_control = self.bzrdir_format.initialize('new')
423
        source.branch.repository.clone(made_control)
424
        source.branch.clone(made_control)
425
        made_tree = self.workingtree_format.initialize(made_control, revision_id='a')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
426
        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.
427
1508.1.24 by Robert Collins
Add update command for use with checkouts.
428
    def test_update_sets_last_revision(self):
429
        # working tree formats from the meta-dir format and newer support
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
430
        # setting the last revision on a tree independently of that on the
431
        # branch. Its concievable that some future formats may want to
1508.1.24 by Robert Collins
Add update command for use with checkouts.
432
        # couple them again (i.e. because its really a smart server and
433
        # the working tree will always match the branch). So we test
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
434
        # that formats where initialising a branch does not initialise a
435
        # tree - and thus have separable entities - support skewing the
1508.1.24 by Robert Collins
Add update command for use with checkouts.
436
        # two things.
437
        main_branch = self.make_branch('tree')
438
        try:
439
            # if there is a working tree now, this is not supported.
440
            main_branch.bzrdir.open_workingtree()
441
            return
442
        except errors.NoWorkingTree:
443
            pass
444
        wt = main_branch.bzrdir.create_workingtree()
445
        # create an out of date working tree by making a checkout in this
446
        # current format
447
        self.build_tree(['checkout/', 'tree/file'])
448
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
449
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
450
        old_tree = self.workingtree_format.initialize(checkout)
451
        # now commit to 'tree'
452
        wt.add('file')
453
        wt.commit('A', rev_id='A')
454
        # and update old_tree
455
        self.assertEqual(0, old_tree.update())
456
        self.failUnlessExists('checkout/file')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
457
        self.assertEqual(['A'], old_tree.get_parent_ids())
1508.1.24 by Robert Collins
Add update command for use with checkouts.
458
1731.1.33 by Aaron Bentley
Revert no-special-root changes
459
    def test_update_sets_root_id(self):
460
        """Ensure tree root is set properly by update.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
461
1731.1.33 by Aaron Bentley
Revert no-special-root changes
462
        Since empty trees don't have root_ids, but workingtrees do,
463
        an update of a checkout of revision 0 to a new revision,  should set
464
        the root id.
465
        """
466
        wt = self.make_branch_and_tree('tree')
467
        main_branch = wt.branch
468
        # create an out of date working tree by making a checkout in this
469
        # current format
470
        self.build_tree(['checkout/', 'tree/file'])
1731.1.43 by Aaron Bentley
Merge more checkout changes
471
        checkout = main_branch.create_checkout('checkout')
1731.1.33 by Aaron Bentley
Revert no-special-root changes
472
        # now commit to 'tree'
473
        wt.add('file')
474
        wt.commit('A', rev_id='A')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
475
        # and update checkout
1731.1.33 by Aaron Bentley
Revert no-special-root changes
476
        self.assertEqual(0, checkout.update())
477
        self.failUnlessExists('checkout/file')
478
        self.assertEqual(wt.get_root_id(), checkout.get_root_id())
479
        self.assertNotEqual(None, wt.get_root_id())
480
4634.123.1 by John Arbash Meinel
Add a failing test for 'update'. When this branch lands, update should work.
481
    def test_update_sets_updated_root_id(self):
482
        wt = self.make_branch_and_tree('tree')
483
        wt.set_root_id('first_root_id')
484
        self.assertEqual('first_root_id', wt.get_root_id())
485
        self.build_tree(['tree/file'])
486
        wt.add(['file'])
487
        wt.commit('first')
488
        co = wt.branch.create_checkout('checkout')
489
        wt.set_root_id('second_root_id')
490
        wt.commit('second')
491
        self.assertEqual('second_root_id', wt.get_root_id())
492
        self.assertEqual(0, co.update())
493
        self.assertEqual('second_root_id', co.get_root_id())
494
1508.1.24 by Robert Collins
Add update command for use with checkouts.
495
    def test_update_returns_conflict_count(self):
496
        # working tree formats from the meta-dir format and newer support
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
497
        # setting the last revision on a tree independently of that on the
498
        # branch. Its concievable that some future formats may want to
1508.1.24 by Robert Collins
Add update command for use with checkouts.
499
        # couple them again (i.e. because its really a smart server and
500
        # the working tree will always match the branch). So we test
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
501
        # that formats where initialising a branch does not initialise a
502
        # tree - and thus have separable entities - support skewing the
1508.1.24 by Robert Collins
Add update command for use with checkouts.
503
        # two things.
504
        main_branch = self.make_branch('tree')
505
        try:
506
            # if there is a working tree now, this is not supported.
507
            main_branch.bzrdir.open_workingtree()
508
            return
509
        except errors.NoWorkingTree:
510
            pass
511
        wt = main_branch.bzrdir.create_workingtree()
512
        # create an out of date working tree by making a checkout in this
513
        # current format
514
        self.build_tree(['checkout/', 'tree/file'])
515
        checkout = bzrdir.BzrDirMetaFormat1().initialize('checkout')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
516
        branch.BranchReferenceFormat().initialize(checkout, main_branch)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
517
        old_tree = self.workingtree_format.initialize(checkout)
518
        # now commit to 'tree'
519
        wt.add('file')
520
        wt.commit('A', rev_id='A')
521
        # and add a file file to the checkout
522
        self.build_tree(['checkout/file'])
523
        old_tree.add('file')
524
        # and update old_tree
525
        self.assertEqual(1, old_tree.update())
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
526
        self.assertEqual(['A'], old_tree.get_parent_ids())
1508.1.24 by Robert Collins
Add update command for use with checkouts.
527
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
528
    def test_merge_revert(self):
529
        from bzrlib.merge import merge_inner
530
        this = self.make_branch_and_tree('b1')
4789.12.1 by John Arbash Meinel
Fix per_workingtree.test_workingtree.test_merge_revert
531
        self.build_tree_contents([('b1/a', 'a test\n'), ('b1/b', 'b test\n')])
532
        this.add(['a', 'b'])
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
533
        this.commit(message='')
534
        base = this.bzrdir.clone('b2').open_workingtree()
4789.12.1 by John Arbash Meinel
Fix per_workingtree.test_workingtree.test_merge_revert
535
        self.build_tree_contents([('b2/a', 'b test\n')])
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
536
        other = this.bzrdir.clone('b3').open_workingtree()
4789.12.1 by John Arbash Meinel
Fix per_workingtree.test_workingtree.test_merge_revert
537
        self.build_tree_contents([('b3/a', 'c test\n'), ('b3/c', 'c test\n')])
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
538
        other.add('c')
539
4789.12.1 by John Arbash Meinel
Fix per_workingtree.test_workingtree.test_merge_revert
540
        self.build_tree_contents([('b1/b', 'q test\n'), ('b1/d', 'd test\n')])
541
        # Note: If we don't lock this before calling merge_inner, then we get a
542
        #       lock-contention failure. This probably indicates something
543
        #       weird going on inside merge_inner. Probably something about
544
        #       calling bt = this_tree.basis_tree() in one lock, and then
545
        #       locking both this_tree and bt separately, causing a dirstate
546
        #       locking race.
547
        this.lock_write()
548
        self.addCleanup(this.unlock)
1534.7.199 by Aaron Bentley
Moved merge/revert tests into test_workingtree.py
549
        merge_inner(this.branch, other, base, this_tree=this)
4789.12.1 by John Arbash Meinel
Fix per_workingtree.test_workingtree.test_merge_revert
550
        a = open('b1/a', 'rb')
551
        try:
552
            self.assertNotEqual(a.read(), 'a test\n')
553
        finally:
554
            a.close()
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
555
        this.revert()
4789.12.1 by John Arbash Meinel
Fix per_workingtree.test_workingtree.test_merge_revert
556
        self.assertFileEqual('a test\n', 'b1/a')
557
        self.failUnlessExists('b1/b.~1~')
558
        self.failIfExists('b1/c')
559
        self.failIfExists('b1/a.~1~')
560
        self.failUnlessExists('b1/d')
1534.7.200 by Aaron Bentley
Merge from mainline
561
1587.1.10 by Robert Collins
update updates working tree and branch together.
562
    def test_update_updates_bound_branch_no_local_commits(self):
563
        # doing an update in a tree updates the branch its bound to too.
564
        master_tree = self.make_branch_and_tree('master')
565
        tree = self.make_branch_and_tree('tree')
566
        try:
567
            tree.branch.bind(master_tree.branch)
568
        except errors.UpgradeRequired:
569
            # legacy branches cannot bind
570
            return
571
        master_tree.commit('foo', rev_id='foo', allow_pointless=True)
572
        tree.update()
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
573
        self.assertEqual(['foo'], tree.get_parent_ids())
1587.1.10 by Robert Collins
update updates working tree and branch together.
574
        self.assertEqual('foo', tree.branch.last_revision())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
575
576
    def test_update_turns_local_commit_into_merge(self):
577
        # doing an update with a few local commits and no master commits
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
578
        # makes pending-merges.
1587.1.13 by Robert Collins
Explain why update pivots more clearly in the relevant test.
579
        # this is done so that 'bzr update; bzr revert' will always produce
580
        # an exact copy of the 'logical branch' - the referenced branch for
581
        # a checkout, and the master for a bound branch.
582
        # its possible that we should instead have 'bzr update' when there
583
        # is nothing new on the master leave the current commits intact and
584
        # alter 'revert' to revert to the master always. But for now, its
585
        # good.
1587.1.11 by Robert Collins
Local commits appear to be working properly.
586
        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.
587
        master_tip = master_tree.commit('first master commit')
1587.1.11 by Robert Collins
Local commits appear to be working properly.
588
        tree = self.make_branch_and_tree('tree')
589
        try:
590
            tree.branch.bind(master_tree.branch)
591
        except errors.UpgradeRequired:
592
            # legacy branches cannot bind
593
            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.
594
        # sync with master
595
        tree.update()
596
        # work locally
1587.1.11 by Robert Collins
Local commits appear to be working properly.
597
        tree.commit('foo', rev_id='foo', allow_pointless=True, local=True)
598
        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.
599
        # sync with master prepatory to committing
1587.1.11 by Robert Collins
Local commits appear to be working properly.
600
        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.
601
        # which should have pivoted the local tip into a merge
602
        self.assertEqual([master_tip, 'bar'], tree.get_parent_ids())
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
603
        # and the local branch history should match the masters now.
604
        self.assertEqual(master_tree.branch.revision_history(),
605
            tree.branch.revision_history())
1587.1.11 by Robert Collins
Local commits appear to be working properly.
606
4916.1.7 by Martin Pool
Add per_workingtree test you can update to arbitrary revisions
607
    def test_update_takes_revision_parameter(self):
608
        wt = self.make_branch_and_tree('wt')
609
        self.build_tree_contents([('wt/a', 'old content')])
610
        wt.add(['a'])
611
        rev1 = wt.commit('first master commit')
612
        self.build_tree_contents([('wt/a', 'new content')])
613
        rev2 = wt.commit('second master commit')
614
        # https://bugs.edge.launchpad.net/bzr/+bug/45719/comments/20
615
        # when adding 'update -r' we should make sure all wt formats support
616
        # it
617
        conflicts = wt.update(revision=rev1)
618
        self.assertFileEqual('old content', 'wt/a')
619
        self.assertEqual([rev1], wt.get_parent_ids())
620
2255.2.156 by Martin Pool
Merge WorkingTree implementation back from trunk
621
    def test_merge_modified_detects_corruption(self):
622
        # FIXME: This doesn't really test that it works; also this is not
623
        # implementation-independent. mbp 20070226
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
624
        tree = self.make_branch_and_tree('master')
3407.2.7 by Martin Pool
Deprecate LockableFiles.put_utf8 and put_bytes.
625
        tree._transport.put_bytes('merge-hashes', 'asdfasdf')
1558.3.3 by Aaron Bentley
Fix error handling for merge_modified
626
        self.assertRaises(errors.MergeModifiedFormatError, tree.merge_modified)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
627
2298.1.1 by Martin Pool
Add test for merge_modified
628
    def test_merge_modified(self):
629
        # merge_modified stores a map from file id to hash
630
        tree = self.make_branch_and_tree('tree')
631
        d = {'file-id': osutils.sha_string('hello')}
632
        self.build_tree_contents([('tree/somefile', 'hello')])
633
        tree.lock_write()
634
        try:
635
            tree.add(['somefile'], ['file-id'])
636
            tree.set_merge_modified(d)
637
            mm = tree.merge_modified()
638
            self.assertEquals(mm, d)
639
        finally:
640
            tree.unlock()
641
        mm = tree.merge_modified()
642
        self.assertEquals(mm, d)
643
1534.10.22 by Aaron Bentley
Got ConflictList implemented
644
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
645
        from bzrlib.tests.test_conflicts import example_conflicts
646
        tree = self.make_branch_and_tree('master')
647
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
648
            tree.set_conflicts(example_conflicts)
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
649
        except UnsupportedOperation:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
650
            raise TestSkipped('set_conflicts not supported')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
651
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
652
        tree2 = WorkingTree.open('master')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
653
        self.assertEqual(tree2.conflicts(), example_conflicts)
3407.2.7 by Martin Pool
Deprecate LockableFiles.put_utf8 and put_bytes.
654
        tree2._transport.put_bytes('conflicts', '')
655
        self.assertRaises(errors.ConflictFormatError,
1955.3.14 by John Arbash Meinel
Correctly fix the workingtree put() test fixes
656
                          tree2.conflicts)
3407.2.7 by Martin Pool
Deprecate LockableFiles.put_utf8 and put_bytes.
657
        tree2._transport.put_bytes('conflicts', 'a')
658
        self.assertRaises(errors.ConflictFormatError,
1955.3.14 by John Arbash Meinel
Correctly fix the workingtree put() test fixes
659
                          tree2.conflicts)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
660
661
    def make_merge_conflicts(self):
2255.2.32 by Robert Collins
Make test_clear_merge_conflicts pass for dirstate. This involved working
662
        from bzrlib.merge import merge_inner
1534.10.12 by Aaron Bentley
Merge produces new conflicts
663
        tree = self.make_branch_and_tree('mine')
664
        file('mine/bloo', 'wb').write('one')
1534.10.14 by Aaron Bentley
Made revert clear conflicts
665
        file('mine/blo', 'wb').write('on')
2255.2.32 by Robert Collins
Make test_clear_merge_conflicts pass for dirstate. This involved working
666
        tree.add(['bloo', 'blo'])
1534.10.12 by Aaron Bentley
Merge produces new conflicts
667
        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
668
        base = tree.branch.repository.revision_tree(tree.last_revision())
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
669
        bzrdir.BzrDir.open("mine").sprout("other")
1534.10.12 by Aaron Bentley
Merge produces new conflicts
670
        file('other/bloo', 'wb').write('two')
671
        othertree = WorkingTree.open('other')
672
        othertree.commit('blah', allow_pointless=False)
673
        file('mine/bloo', 'wb').write('three')
674
        tree.commit("blah", allow_pointless=False)
675
        merge_inner(tree.branch, othertree, base, this_tree=tree)
676
        return tree
677
678
    def test_merge_conflicts(self):
679
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
680
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
681
682
    def test_clear_merge_conflicts(self):
683
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
684
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.12 by Aaron Bentley
Merge produces new conflicts
685
        try:
1534.10.22 by Aaron Bentley
Got ConflictList implemented
686
            tree.set_conflicts(ConflictList())
1534.10.12 by Aaron Bentley
Merge produces new conflicts
687
        except UnsupportedOperation:
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
688
            raise TestSkipped('unsupported operation')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
689
        self.assertEqual(tree.conflicts(), ConflictList())
1534.10.14 by Aaron Bentley
Made revert clear conflicts
690
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
691
    def test_add_conflicts(self):
692
        tree = self.make_branch_and_tree('tree')
693
        try:
694
            tree.add_conflicts([TextConflict('path_a')])
695
        except UnsupportedOperation:
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
696
            raise TestSkipped('unsupported operation')
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
697
        self.assertEqual(ConflictList([TextConflict('path_a')]),
698
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
699
        tree.add_conflicts([TextConflict('path_a')])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
700
        self.assertEqual(ConflictList([TextConflict('path_a')]),
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
701
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
702
        tree.add_conflicts([ContentsConflict('path_a')])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
703
        self.assertEqual(ConflictList([ContentsConflict('path_a'),
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
704
                                       TextConflict('path_a')]),
705
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
706
        tree.add_conflicts([TextConflict('path_b')])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
707
        self.assertEqual(ConflictList([ContentsConflict('path_a'),
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
708
                                       TextConflict('path_a'),
1551.7.13 by Aaron Bentley
Switched from actual, expected to expected, actual, for John.
709
                                       TextConflict('path_b')]),
710
                         tree.conflicts())
1551.7.11 by Aaron Bentley
Add WorkingTree.add_conflicts
711
1534.10.14 by Aaron Bentley
Made revert clear conflicts
712
    def test_revert_clear_conflicts(self):
713
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
714
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
715
        tree.revert(["blo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
716
        self.assertEqual(len(tree.conflicts()), 1)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
717
        tree.revert(["bloo"])
1534.10.22 by Aaron Bentley
Got ConflictList implemented
718
        self.assertEqual(len(tree.conflicts()), 0)
1534.10.14 by Aaron Bentley
Made revert clear conflicts
719
720
    def test_revert_clear_conflicts2(self):
721
        tree = self.make_merge_conflicts()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
722
        self.assertEqual(len(tree.conflicts()), 1)
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
723
        tree.revert()
1534.10.22 by Aaron Bentley
Got ConflictList implemented
724
        self.assertEqual(len(tree.conflicts()), 0)
1624.3.22 by Olaf Conradi
Merge bzr.dev
725
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
726
    def test_format_description(self):
727
        tree = self.make_branch_and_tree('tree')
728
        text = tree._format.get_format_description()
729
        self.failUnless(len(text))
1681.1.1 by Robert Collins
Make WorkingTree.branch a read only property. (Robert Collins)
730
731
    def test_branch_attribute_is_not_settable(self):
732
        # the branch attribute is an aspect of the working tree, not a
733
        # configurable attribute
734
        tree = self.make_branch_and_tree('tree')
735
        def set_branch():
736
            tree.branch = tree.branch
737
        self.assertRaises(AttributeError, set_branch)
738
1713.3.1 by Robert Collins
Smoke tests for tree.list_files and bzr ignored when a versioned file matches an ignore rule.
739
    def test_list_files_versioned_before_ignored(self):
740
        """A versioned file matching an ignore rule should not be ignored."""
741
        tree = self.make_branch_and_tree('.')
742
        self.build_tree(['foo.pyc'])
743
        # ensure that foo.pyc is ignored
744
        self.build_tree_contents([('.bzrignore', 'foo.pyc')])
745
        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.
746
        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.
747
        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.
748
        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.
749
        self.assertEqual((u'.bzrignore', '?', 'file', None), files[0][:-1])
750
        self.assertEqual((u'foo.pyc', 'V', 'file', 'anid'), files[1][:-1])
751
        self.assertEqual(2, len(files))
1711.8.2 by John Arbash Meinel
Test that WorkingTree locks Branch before self, and unlocks self before Branch
752
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
753
    def test_non_normalized_add_accessible(self):
754
        try:
755
            self.build_tree([u'a\u030a'])
756
        except UnicodeError:
757
            raise TestSkipped('Filesystem does not support unicode filenames')
758
        tree = self.make_branch_and_tree('.')
759
        orig = osutils.normalized_filename
760
        osutils.normalized_filename = osutils._accessible_normalized_filename
761
        try:
762
            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.
763
            tree.lock_read()
1907.1.3 by Aaron Bentley
Fixed unicode test cases
764
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
765
                    [(path, ie.kind) for path,ie in
1830.3.17 by John Arbash Meinel
list_files() with wrong normalized_filename code raises exceptions. Fix this
766
                                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.
767
            tree.unlock()
1830.3.7 by John Arbash Meinel
Check that WorkingTree.add does the right thing.
768
        finally:
769
            osutils.normalized_filename = orig
770
771
    def test_non_normalized_add_inaccessible(self):
772
        try:
773
            self.build_tree([u'a\u030a'])
774
        except UnicodeError:
775
            raise TestSkipped('Filesystem does not support unicode filenames')
776
        tree = self.make_branch_and_tree('.')
777
        orig = osutils.normalized_filename
778
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
779
        try:
780
            self.assertRaises(errors.InvalidNormalization,
781
                tree.add, [u'a\u030a'])
782
        finally:
783
            osutils.normalized_filename = orig
2123.3.9 by Steffen Eichenberg
added tests for deprecated API workingtree.move
784
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
785
    def test__write_inventory(self):
786
        # The private interface _write_inventory is currently used by transform.
787
        tree = self.make_branch_and_tree('.')
788
        # if we write write an inventory then do a walkdirs we should get back
789
        # missing entries, and actual, and unknowns as appropriate.
790
        self.build_tree(['present', 'unknown'])
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
791
        inventory = Inventory(tree.get_root_id())
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
792
        inventory.add_path('missing', 'file', 'missing-id')
793
        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.
794
        # there is no point in being able to write an inventory to an unlocked
795
        # tree object - its a low level api not a convenience api.
796
        tree.lock_write()
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
797
        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.
798
        tree.unlock()
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
799
        tree.lock_read()
800
        try:
801
            present_stat = os.lstat('present')
802
            unknown_stat = os.lstat('unknown')
803
            expected_results = [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
804
                (('', tree.get_root_id()),
1852.15.14 by Robert Collins
test that WorkingTree._write_inventory works as expected by the current code.
805
                 [('missing', 'missing', 'unknown', None, 'missing-id', 'file'),
806
                  ('present', 'present', 'file', present_stat, 'present-id', 'file'),
807
                  ('unknown', 'unknown', 'file', unknown_stat, None, None),
808
                 ]
809
                )]
810
            self.assertEqual(expected_results, list(tree.walkdirs()))
811
        finally:
812
            tree.unlock()
2255.7.56 by Robert Collins
Document behaviour of tree.path2id("path/").
813
814
    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.
815
        # smoke test for path2id
2255.7.56 by Robert Collins
Document behaviour of tree.path2id("path/").
816
        tree = self.make_branch_and_tree('.')
817
        self.build_tree(['foo'])
818
        tree.add(['foo'], ['foo-id'])
819
        self.assertEqual('foo-id', tree.path2id('foo'))
820
        # the next assertion is for backwards compatability with WorkingTree3,
821
        # though its probably a bad idea, it makes things work. Perhaps
822
        # it should raise a deprecation warning?
823
        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.
824
825
    def test_filter_unversioned_files(self):
826
        # smoke test for filter_unversioned_files
827
        tree = self.make_branch_and_tree('.')
828
        paths = ['here-and-versioned', 'here-and-not-versioned',
829
            'not-here-and-versioned', 'not-here-and-not-versioned']
830
        tree.add(['here-and-versioned', 'not-here-and-versioned'],
831
            kinds=['file', 'file'])
832
        self.build_tree(['here-and-versioned', 'here-and-not-versioned'])
833
        tree.lock_read()
834
        self.addCleanup(tree.unlock)
835
        self.assertEqual(
836
            set(['not-here-and-not-versioned', 'here-and-not-versioned']),
837
            tree.filter_unversioned_files(paths))
2255.2.200 by Martin Pool
Add simple test for WorkingTree.kind
838
839
    def test_detect_real_kind(self):
840
        # working trees report the real kind of the file on disk, not the kind
841
        # they had when they were first added
842
        # create one file of every interesting type
843
        tree = self.make_branch_and_tree('.')
4100.2.2 by Aaron Bentley
Remove locking decorator
844
        tree.lock_write()
845
        self.addCleanup(tree.unlock)
2255.2.200 by Martin Pool
Add simple test for WorkingTree.kind
846
        self.build_tree(['file', 'directory/'])
847
        names = ['file', 'directory']
848
        if has_symlinks():
849
            os.symlink('target', 'symlink')
850
            names.append('symlink')
851
        tree.add(names, [n + '-id' for n in names])
852
        # now when we first look, we should see everything with the same kind
853
        # with which they were initially added
854
        for n in names:
855
            actual_kind = tree.kind(n + '-id')
856
            self.assertEqual(n, actual_kind)
857
        # move them around so the names no longer correspond to the types
858
        os.rename(names[0], 'tmp')
859
        for i in range(1, len(names)):
860
            os.rename(names[i], names[i-1])
861
        os.rename('tmp', names[-1])
2255.2.202 by Martin Pool
WorkingTree_4.kind should report tree-references if they're
862
        # now look and expect to see the correct types again
863
        for i in range(len(names)):
864
            actual_kind = tree.kind(names[i-1] + '-id')
865
            expected_kind = names[i]
866
            self.assertEqual(expected_kind, actual_kind)
2499.3.1 by Aaron Bentley
Fix Workingtree4.get_file_sha1 on missing files
867
3146.8.4 by Aaron Bentley
Eliminate direct use of inventory from transform application
868
    def test_stored_kind_with_missing(self):
869
        tree = self.make_branch_and_tree('tree')
870
        tree.lock_write()
871
        self.addCleanup(tree.unlock)
872
        self.build_tree(['tree/a', 'tree/b/'])
873
        tree.add(['a', 'b'], ['a-id', 'b-id'])
874
        os.unlink('tree/a')
875
        os.rmdir('tree/b')
876
        self.assertEqual('file', tree.stored_kind('a-id'))
877
        self.assertEqual('directory', tree.stored_kind('b-id'))
878
2499.3.1 by Aaron Bentley
Fix Workingtree4.get_file_sha1 on missing files
879
    def test_missing_file_sha1(self):
880
        """If a file is missing, its sha1 should be reported as None."""
881
        tree = self.make_branch_and_tree('.')
882
        tree.lock_write()
883
        self.addCleanup(tree.unlock)
884
        self.build_tree(['file'])
885
        tree.add('file', 'file-id')
886
        tree.commit('file added')
887
        os.unlink('file')
888
        self.assertIs(None, tree.get_file_sha1('file-id'))
1551.15.56 by Aaron Bentley
Raise NoSuchId when get_file_sha1 is invoked with a baed file id
889
890
    def test_no_file_sha1(self):
891
        """If a file is not present, get_file_sha1 should raise NoSuchId"""
892
        tree = self.make_branch_and_tree('.')
893
        tree.lock_write()
894
        self.addCleanup(tree.unlock)
895
        self.assertRaises(errors.NoSuchId, tree.get_file_sha1, 'file-id')
896
        self.build_tree(['file'])
897
        tree.add('file', 'file-id')
898
        tree.commit('foo')
899
        tree.remove('file')
900
        self.assertRaises(errors.NoSuchId, tree.get_file_sha1, 'file-id')
3034.4.5 by Aaron Bentley
Add workingtree test for case_insensitive var
901
902
    def test_case_sensitive(self):
903
        """If filesystem is case-sensitive, tree should report this.
904
905
        We check case-sensitivity by creating a file with a lowercase name,
906
        then testing whether it exists with an uppercase name.
907
        """
3034.4.9 by Alexander Belchenko
skip test_workingtree.TestWorkingTree.test_case_sensitive for WT2
908
        self.build_tree(['filename'])
3034.4.5 by Aaron Bentley
Add workingtree test for case_insensitive var
909
        if os.path.exists('FILENAME'):
910
            case_sensitive = False
911
        else:
912
            case_sensitive = True
913
        tree = self.make_branch_and_tree('test')
3034.4.9 by Alexander Belchenko
skip test_workingtree.TestWorkingTree.test_case_sensitive for WT2
914
        if tree.__class__ == WorkingTree2:
915
            raise TestSkipped('WorkingTree2 is not supported')
3034.4.5 by Aaron Bentley
Add workingtree test for case_insensitive var
916
        self.assertEqual(case_sensitive, tree.case_sensitive)
3146.8.2 by Aaron Bentley
Introduce iter_all_file_ids, to avoid hitting Inventory for this case
917
3146.8.16 by Aaron Bentley
Updates from review
918
    def test_all_file_ids_with_missing(self):
3146.8.2 by Aaron Bentley
Introduce iter_all_file_ids, to avoid hitting Inventory for this case
919
        tree = self.make_branch_and_tree('tree')
920
        tree.lock_write()
921
        self.addCleanup(tree.unlock)
922
        self.build_tree(['tree/a', 'tree/b'])
923
        tree.add(['a', 'b'], ['a-id', 'b-id'])
924
        os.unlink('tree/a')
925
        self.assertEqual(set(['a-id', 'b-id', tree.get_root_id()]),
3146.8.16 by Aaron Bentley
Updates from review
926
                         tree.all_file_ids())
3146.8.19 by Aaron Bentley
Merge with bzr.dev
927
3136.1.10 by Aaron Bentley
Clean error if filesystem does not support hard-links
928
    def test_sprout_hardlink(self):
3619.6.9 by Mark Hammond
Move check for os.link to the start of the test
929
        real_os_link = getattr(os, 'link', None)
930
        if real_os_link is None:
931
            raise TestNotApplicable("This platform doesn't provide os.link")
3136.1.10 by Aaron Bentley
Clean error if filesystem does not support hard-links
932
        source = self.make_branch_and_tree('source')
933
        self.build_tree(['source/file'])
934
        source.add('file')
935
        source.commit('added file')
936
        def fake_link(source, target):
937
            raise OSError(errno.EPERM, 'Operation not permitted')
938
        os.link = fake_link
939
        try:
940
            # Hard-link support is optional, so supplying hardlink=True may
941
            # or may not raise an exception.  But if it does, it must be
942
            # HardLinkNotSupported
943
            try:
944
                source.bzrdir.sprout('target', accelerator_tree=source,
945
                                     hardlink=True)
946
            except errors.HardLinkNotSupported:
947
                pass
948
        finally:
949
            os.link = real_os_link
3287.20.3 by John Arbash Meinel
Aaron recommended to make this a WT_impl test.
950
951
952
class TestIllegalPaths(TestCaseWithWorkingTree):
953
954
    def test_bad_fs_path(self):
3638.3.14 by Vincent Ladeuil
Make test_bad_fs_path not applicable on OSX.
955
        if osutils.normalizes_filenames():
956
            # You *can't* create an illegal filename on OSX.
957
            raise tests.TestNotApplicable('OSX normalizes filenames')
3287.20.3 by John Arbash Meinel
Aaron recommended to make this a WT_impl test.
958
        self.requireFeature(tests.UTF8Filesystem)
959
        # We require a UTF8 filesystem, because otherwise we would need to get
960
        # tricky to figure out how to create an illegal filename.
961
        # \xb5 is an illegal path because it should be \xc2\xb5 for UTF-8
962
        tree = self.make_branch_and_tree('tree')
963
        self.build_tree(['tree/subdir/'])
964
        tree.add('subdir')
965
966
        f = open('tree/subdir/m\xb5', 'wb')
967
        try:
968
            f.write('trivial\n')
969
        finally:
970
            f.close()
971
972
        tree.lock_read()
973
        self.addCleanup(tree.unlock)
974
        basis = tree.basis_tree()
975
        basis.lock_read()
976
        self.addCleanup(basis.unlock)
977
978
        e = self.assertListRaises(errors.BadFilenameEncoding,
979
                                  tree.iter_changes, tree.basis_tree(),
980
                                                     want_unversioned=True)
981
        # We should display the relative path
982
        self.assertEqual('subdir/m\xb5', e.filename)
983
        self.assertEqual(osutils._fs_enc, e.fs_encoding)