/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
1
# Copyright (C) 2005, 2006 Canonical Ltd
1399.1.12 by Robert Collins
add new test script
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
18
from cStringIO import StringIO
1399.1.12 by Robert Collins
add new test script
19
import os
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
20
1836.1.15 by John Arbash Meinel
Updated WorkingTree to use the new user-level ignores.
21
from bzrlib import ignores
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
22
import bzrlib
1399.1.12 by Robert Collins
add new test script
23
from bzrlib.branch import Branch
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
24
from bzrlib import bzrdir, conflicts, errors, workingtree
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
25
from bzrlib.bzrdir import BzrDir
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
26
from bzrlib.errors import NotBranchError, NotVersionedError
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
27
from bzrlib.lockdir import LockDir
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
28
from bzrlib.mutabletree import needs_tree_write_lock
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
29
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
30
from bzrlib.symbol_versioning import zero_thirteen
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
31
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
1399.1.12 by Robert Collins
add new test script
32
from bzrlib.trace import mutter
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
33
from bzrlib.transport import get_transport
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
34
from bzrlib.workingtree import (
35
    TreeEntry,
36
    TreeDirectory,
37
    TreeFile,
38
    TreeLink,
39
    WorkingTree,
40
    )
1399.1.12 by Robert Collins
add new test script
41
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
42
class TestTreeDirectory(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
43
44
    def test_kind_character(self):
45
        self.assertEqual(TreeDirectory().kind_character(), '/')
46
47
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
48
class TestTreeEntry(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
49
50
    def test_kind_character(self):
51
        self.assertEqual(TreeEntry().kind_character(), '???')
52
53
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
54
class TestTreeFile(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
55
56
    def test_kind_character(self):
57
        self.assertEqual(TreeFile().kind_character(), '')
58
59
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
60
class TestTreeLink(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
61
62
    def test_kind_character(self):
63
        self.assertEqual(TreeLink().kind_character(), '')
64
65
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
66
files=['a','b/','b/c']
67
class TestRemove(TestCaseWithTransport):
68
    """Tests WorkingTree.remove"""
69
70
    def test_remove(self):
71
        tree = self.make_branch_and_tree('.')
72
        self.build_tree(files)
73
        tree.add(files)
74
        self.assertInWorkingTree(files)
75
76
        tree.remove(files,delete_files=True)
77
78
        self.assertNotInWorkingTree(files)
79
        self.failIfExists(files)
80
81
        tree.remove([''],delete_files=True)
82
        tree.remove(['b'],delete_files=True)
83
84
    def test_unversion(self):
85
        tree = self.make_branch_and_tree('.')
86
        self.build_tree(files)
87
        tree.add(files)
88
        self.assertInWorkingTree(files)
89
90
        tree.remove(files)
91
92
        self.assertNotInWorkingTree(files)
93
        self.failUnlessExists(files)
94
95
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
96
class TestDefaultFormat(TestCaseWithTransport):
97
98
    def test_get_set_default_format(self):
99
        old_format = workingtree.WorkingTreeFormat.get_default_format()
100
        # default is 3
101
        self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
102
        workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
103
        try:
104
            # the default branch format is used by the meta dir format
105
            # which is not the default bzrdir format at this point
106
            dir = bzrdir.BzrDirMetaFormat1().initialize('.')
107
            dir.create_repository()
108
            dir.create_branch()
109
            result = dir.create_workingtree()
110
            self.assertEqual(result, 'A tree')
111
        finally:
112
            workingtree.WorkingTreeFormat.set_default_format(old_format)
113
        self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
114
115
116
class SampleTreeFormat(workingtree.WorkingTreeFormat):
117
    """A sample format
118
119
    this format is initializable, unsupported to aid in testing the 
120
    open and open_downlevel routines.
121
    """
122
123
    def get_format_string(self):
124
        """See WorkingTreeFormat.get_format_string()."""
125
        return "Sample tree format."
126
1508.1.24 by Robert Collins
Add update command for use with checkouts.
127
    def initialize(self, a_bzrdir, revision_id=None):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
128
        """Sample branches cannot be created."""
129
        t = a_bzrdir.get_workingtree_transport(self)
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
130
        t.put_bytes('format', self.get_format_string())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
131
        return 'A tree'
132
133
    def is_supported(self):
134
        return False
135
136
    def open(self, transport, _found=False):
137
        return "opened tree."
138
139
140
class TestWorkingTreeFormat(TestCaseWithTransport):
141
    """Tests for the WorkingTreeFormat facility."""
142
143
    def test_find_format(self):
144
        # is the right format object found for a working tree?
145
        # create a branch with a few known format objects.
146
        self.build_tree(["foo/", "bar/"])
147
        def check_format(format, url):
148
            dir = format._matchingbzrdir.initialize(url)
149
            dir.create_repository()
150
            dir.create_branch()
151
            format.initialize(dir)
152
            t = get_transport(url)
153
            found_format = workingtree.WorkingTreeFormat.find_format(dir)
154
            self.failUnless(isinstance(found_format, format.__class__))
155
        check_format(workingtree.WorkingTreeFormat3(), "bar")
156
        
157
    def test_find_format_no_tree(self):
158
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
159
        self.assertRaises(errors.NoWorkingTree,
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
160
                          workingtree.WorkingTreeFormat.find_format,
161
                          dir)
162
163
    def test_find_format_unknown_format(self):
164
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
165
        dir.create_repository()
166
        dir.create_branch()
167
        SampleTreeFormat().initialize(dir)
168
        self.assertRaises(errors.UnknownFormatError,
169
                          workingtree.WorkingTreeFormat.find_format,
170
                          dir)
171
172
    def test_register_unregister_format(self):
173
        format = SampleTreeFormat()
174
        # make a control dir
175
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
176
        dir.create_repository()
177
        dir.create_branch()
178
        # make a branch
179
        format.initialize(dir)
180
        # register a format for it.
181
        workingtree.WorkingTreeFormat.register_format(format)
182
        # which branch.Open will refuse (not supported)
183
        self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
184
        # but open_downlevel will work
185
        self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
186
        # unregister the format
187
        workingtree.WorkingTreeFormat.unregister_format(format)
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
188
189
190
class TestWorkingTreeFormat3(TestCaseWithTransport):
191
    """Tests specific to WorkingTreeFormat3."""
192
193
    def test_disk_layout(self):
194
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
195
        control.create_repository()
196
        control.create_branch()
197
        tree = workingtree.WorkingTreeFormat3().initialize(control)
198
        # we want:
199
        # format 'Bazaar-NG Working Tree format 3'
200
        # inventory = blank inventory
201
        # pending-merges = ''
202
        # stat-cache = ??
203
        # no inventory.basis yet
204
        t = control.get_workingtree_transport(None)
1553.5.81 by Martin Pool
Revert change to WorkingTreeFormat3 format string; too many things want it the old way
205
        self.assertEqualDiff('Bazaar-NG Working Tree format 3',
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
206
                             t.get('format').read())
2084.1.4 by John Arbash Meinel
Fix the test that assumed unique ids were generated
207
        # self.assertContainsRe(t.get('inventory').read(), 
208
        #                       '<inventory file_id="[^"]*" format="5">\n'
209
        #                       '</inventory>\n',
210
        #                      )
211
        # WorkingTreeFormat3 doesn't default to creating a unique root id,
212
        # because it is incompatible with older bzr versions
213
        self.assertContainsRe(t.get('inventory').read(),
214
                              '<inventory format="5">\n'
1731.1.33 by Aaron Bentley
Revert no-special-root changes
215
                              '</inventory>\n',
216
                             )
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
217
        self.assertEqualDiff('### bzr hashcache v5\n',
218
                             t.get('stat-cache').read())
219
        self.assertFalse(t.has('inventory.basis'))
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
220
        # no last-revision file means 'None' or 'NULLREVISION'
221
        self.assertFalse(t.has('last-revision'))
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
222
        # TODO RBC 20060210 do a commit, check the inventory.basis is created 
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
223
        # correctly and last-revision file becomes present.
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
224
225
    def test_uses_lockdir(self):
226
        """WorkingTreeFormat3 uses its own LockDir:
227
            
228
            - lock is a directory
229
            - when the WorkingTree is locked, LockDir can see that
230
        """
231
        t = self.get_transport()
232
        url = self.get_url()
233
        dir = bzrdir.BzrDirMetaFormat1().initialize(url)
234
        repo = dir.create_repository()
235
        branch = dir.create_branch()
1558.10.1 by Aaron Bentley
Handle lockdirs over NFS properly
236
        try:
237
            tree = workingtree.WorkingTreeFormat3().initialize(dir)
238
        except errors.NotLocalUrl:
239
            raise TestSkipped('Not a local URL')
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
240
        self.assertIsDirectory('.bzr', t)
241
        self.assertIsDirectory('.bzr/checkout', t)
242
        self.assertIsDirectory('.bzr/checkout/lock', t)
243
        our_lock = LockDir(t, '.bzr/checkout/lock')
244
        self.assertEquals(our_lock.peek(), None)
1553.5.75 by Martin Pool
Additional WorkingTree LockDir test
245
        tree.lock_write()
246
        self.assertTrue(our_lock.peek())
247
        tree.unlock()
248
        self.assertEquals(our_lock.peek(), None)
1534.10.6 by Aaron Bentley
Conflict serialization working for WorkingTree3
249
1815.2.2 by Jelmer Vernooij
Move missing_pending_merges test to WorkingTreeFormat3-specific tests.
250
    def test_missing_pending_merges(self):
251
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
252
        control.create_repository()
253
        control.create_branch()
254
        tree = workingtree.WorkingTreeFormat3().initialize(control)
255
        tree._control_files._transport.delete("pending-merges")
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
256
        self.assertEqual([], tree.get_parent_ids())
1815.2.2 by Jelmer Vernooij
Move missing_pending_merges test to WorkingTreeFormat3-specific tests.
257
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
258
259
class TestFormat2WorkingTree(TestCaseWithTransport):
260
    """Tests that are specific to format 2 trees."""
261
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
262
    def create_format2_tree(self, url):
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
263
        return self.make_branch_and_tree(
264
            url, format=bzrlib.bzrdir.BzrDirFormat6())
1534.10.6 by Aaron Bentley
Conflict serialization working for WorkingTree3
265
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
266
    def test_conflicts(self):
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
267
        # test backwards compatability
268
        tree = self.create_format2_tree('.')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
269
        self.assertRaises(errors.UnsupportedOperation, tree.set_conflicts,
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
270
                          None)
271
        file('lala.BASE', 'wb').write('labase')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
272
        expected = conflicts.ContentsConflict('lala')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
273
        self.assertEqual(list(tree.conflicts()), [expected])
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
274
        file('lala', 'wb').write('la')
275
        tree.add('lala', 'lala-id')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
276
        expected = conflicts.ContentsConflict('lala', file_id='lala-id')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
277
        self.assertEqual(list(tree.conflicts()), [expected])
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
278
        file('lala.THIS', 'wb').write('lathis')
279
        file('lala.OTHER', 'wb').write('laother')
280
        # When "text conflict"s happen, stem, THIS and OTHER are text
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
281
        expected = conflicts.TextConflict('lala', file_id='lala-id')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
282
        self.assertEqual(list(tree.conflicts()), [expected])
1534.10.8 by Aaron Bentley
Implemented conflict_lines in terms of old system on WorkingTree
283
        os.unlink('lala.OTHER')
284
        os.mkdir('lala.OTHER')
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
285
        expected = conflicts.ContentsConflict('lala', file_id='lala-id')
1534.10.22 by Aaron Bentley
Got ConflictList implemented
286
        self.assertEqual(list(tree.conflicts()), [expected])
1713.2.3 by Robert Collins
Combine ignore rules into a single regex preventing pathological behaviour during add.
287
288
289
class TestNonFormatSpecificCode(TestCaseWithTransport):
290
    """This class contains tests of workingtree that are not format specific."""
291
1713.1.6 by Robert Collins
Move file id random data selection out of the inner loop for 'bzr add'.
292
    def test_gen_file_id(self):
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
293
        file_id = self.applyDeprecated(zero_thirteen, workingtree.gen_file_id,
294
                                      'filename')
295
        self.assertStartsWith(file_id, 'filename-')
296
297
    def test_gen_root_id(self):
298
        file_id = self.applyDeprecated(zero_thirteen, workingtree.gen_root_id)
299
        self.assertStartsWith(file_id, 'tree_root-')
1864.4.1 by John Arbash Meinel
Fix bug #43801 by squashing file ids a little bit more.
300
        
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
301
302
class InstrumentedTree(object):
303
    """A instrumented tree to check the needs_tree_write_lock decorator."""
304
305
    def __init__(self):
306
        self._locks = []
307
308
    def lock_tree_write(self):
309
        self._locks.append('t')
310
311
    @needs_tree_write_lock
312
    def method_with_tree_write_lock(self, *args, **kwargs):
313
        """A lock_tree_write decorated method that returns its arguments."""
314
        return args, kwargs
315
316
    @needs_tree_write_lock
317
    def method_that_raises(self):
318
        """This method causes an exception when called with parameters.
319
        
320
        This allows the decorator code to be checked - it should still call
321
        unlock.
322
        """
323
324
    def unlock(self):
325
        self._locks.append('u')
326
327
328
class TestInstrumentedTree(TestCase):
329
330
    def test_needs_tree_write_lock(self):
331
        """@needs_tree_write_lock should be semantically transparent."""
332
        tree = InstrumentedTree()
333
        self.assertEqual(
334
            'method_with_tree_write_lock',
335
            tree.method_with_tree_write_lock.__name__)
336
        self.assertEqual(
337
            "A lock_tree_write decorated method that returns its arguments.",
338
            tree.method_with_tree_write_lock.__doc__)
339
        args = (1, 2, 3)
340
        kwargs = {'a':'b'}
341
        result = tree.method_with_tree_write_lock(1,2,3, a='b')
342
        self.assertEqual((args, kwargs), result)
343
        self.assertEqual(['t', 'u'], tree._locks)
344
        self.assertRaises(TypeError, tree.method_that_raises, 'foo')
345
        self.assertEqual(['t', 'u', 't', 'u'], tree._locks)