~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

(gz) Change minimum required testtools version for selftest to 0.9.5 for
 unicode fixes (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
 
18
from cStringIO import StringIO
 
19
import os
 
20
 
18
21
from bzrlib import (
19
22
    bzrdir,
20
23
    conflicts,
21
24
    errors,
22
 
    symbol_versioning,
23
25
    transport,
24
26
    workingtree,
25
27
    )
 
28
from bzrlib.branch import Branch
 
29
from bzrlib.bzrdir import BzrDir
26
30
from bzrlib.lockdir import LockDir
27
31
from bzrlib.mutabletree import needs_tree_write_lock
28
32
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
61
65
class TestDefaultFormat(TestCaseWithTransport):
62
66
 
63
67
    def test_get_set_default_format(self):
64
 
        old_format = workingtree.format_registry.get_default()
 
68
        old_format = workingtree.WorkingTreeFormat.get_default_format()
65
69
        # default is 3
66
70
        self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
67
 
        workingtree.format_registry.set_default(SampleTreeFormat())
 
71
        workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
68
72
        try:
69
73
            # the default branch format is used by the meta dir format
70
74
            # which is not the default bzrdir format at this point
74
78
            result = dir.create_workingtree()
75
79
            self.assertEqual(result, 'A tree')
76
80
        finally:
77
 
            workingtree.format_registry.set_default(old_format)
78
 
        self.assertEqual(old_format, workingtree.format_registry.get_default())
 
81
            workingtree.WorkingTreeFormat.set_default_format(old_format)
 
82
        self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
79
83
 
80
84
    def test_open(self):
81
85
        tree = self.make_branch_and_tree('.')
122
126
        return "opened tree."
123
127
 
124
128
 
125
 
class SampleExtraTreeFormat(workingtree.WorkingTreeFormat):
126
 
    """A sample format that does not support use in a metadir.
127
 
 
128
 
    """
129
 
 
130
 
    def get_format_string(self):
131
 
        # Not usable in a metadir, so no format string
132
 
        return None
133
 
 
134
 
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
135
 
                   accelerator_tree=None, hardlink=False):
136
 
        raise NotImplementedError(self.initialize)
137
 
 
138
 
    def is_supported(self):
139
 
        return False
140
 
 
141
 
    def open(self, transport, _found=False):
142
 
        raise NotImplementedError(self.open)
143
 
 
144
 
 
145
129
class TestWorkingTreeFormat(TestCaseWithTransport):
146
130
    """Tests for the WorkingTreeFormat facility."""
147
131
 
156
140
            format.initialize(dir)
157
141
            t = transport.get_transport(url)
158
142
            found_format = workingtree.WorkingTreeFormat.find_format(dir)
159
 
            self.assertIsInstance(found_format, format.__class__)
 
143
            self.failUnless(isinstance(found_format, format.__class__))
160
144
        check_format(workingtree.WorkingTreeFormat3(), "bar")
161
145
 
162
146
    def test_find_format_no_tree(self):
183
167
        # make a branch
184
168
        format.initialize(dir)
185
169
        # register a format for it.
186
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
187
 
            workingtree.WorkingTreeFormat.register_format, format)
188
 
        self.assertTrue(format in 
189
 
            self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
190
 
                workingtree.WorkingTreeFormat.get_formats))
 
170
        workingtree.WorkingTreeFormat.register_format(format)
191
171
        # which branch.Open will refuse (not supported)
192
172
        self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
193
173
        # but open_downlevel will work
194
174
        self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
195
175
        # unregister the format
196
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
197
 
            workingtree.WorkingTreeFormat.unregister_format, format)
198
 
        self.assertFalse(format in
199
 
            self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
200
 
                workingtree.WorkingTreeFormat.get_formats))
201
 
 
202
 
 
203
 
class TestWorkingTreeFormatRegistry(TestCase):
204
 
 
205
 
    def setUp(self):
206
 
        super(TestWorkingTreeFormatRegistry, self).setUp()
207
 
        self.registry = workingtree.WorkingTreeFormatRegistry()
208
 
 
209
 
    def test_register_unregister_format(self):
210
 
        format = SampleTreeFormat()
211
 
        self.registry.register(format)
212
 
        self.assertEquals(format, self.registry.get("Sample tree format."))
213
 
        self.registry.remove(format)
214
 
        self.assertRaises(KeyError, self.registry.get, "Sample tree format.")
215
 
 
216
 
    def test_get_all(self):
217
 
        format = SampleTreeFormat()
218
 
        self.assertEquals([], self.registry._get_all())
219
 
        self.registry.register(format)
220
 
        self.assertEquals([format], self.registry._get_all())
221
 
 
222
 
    def test_register_extra(self):
223
 
        format = SampleExtraTreeFormat()
224
 
        self.assertEquals([], self.registry._get_all())
225
 
        self.registry.register_extra(format)
226
 
        self.assertEquals([format], self.registry._get_all())
227
 
 
228
 
    def test_register_extra_lazy(self):
229
 
        self.assertEquals([], self.registry._get_all())
230
 
        self.registry.register_extra_lazy("bzrlib.tests.test_workingtree",
231
 
            "SampleExtraTreeFormat")
232
 
        formats = self.registry._get_all()
233
 
        self.assertEquals(1, len(formats))
234
 
        self.assertIsInstance(formats[0], SampleExtraTreeFormat)
 
176
        workingtree.WorkingTreeFormat.unregister_format(format)
235
177
 
236
178
 
237
179
class TestWorkingTreeFormat3(TestCaseWithTransport):
297
239
        self.assertEqual([], tree.get_parent_ids())
298
240
 
299
241
 
 
242
class TestFormat2WorkingTree(TestCaseWithTransport):
 
243
    """Tests that are specific to format 2 trees."""
 
244
 
 
245
    def create_format2_tree(self, url):
 
246
        return self.make_branch_and_tree(
 
247
            url, format=bzrdir.BzrDirFormat6())
 
248
 
 
249
    def test_conflicts(self):
 
250
        # test backwards compatability
 
251
        tree = self.create_format2_tree('.')
 
252
        self.assertRaises(errors.UnsupportedOperation, tree.set_conflicts,
 
253
                          None)
 
254
        file('lala.BASE', 'wb').write('labase')
 
255
        expected = conflicts.ContentsConflict('lala')
 
256
        self.assertEqual(list(tree.conflicts()), [expected])
 
257
        file('lala', 'wb').write('la')
 
258
        tree.add('lala', 'lala-id')
 
259
        expected = conflicts.ContentsConflict('lala', file_id='lala-id')
 
260
        self.assertEqual(list(tree.conflicts()), [expected])
 
261
        file('lala.THIS', 'wb').write('lathis')
 
262
        file('lala.OTHER', 'wb').write('laother')
 
263
        # When "text conflict"s happen, stem, THIS and OTHER are text
 
264
        expected = conflicts.TextConflict('lala', file_id='lala-id')
 
265
        self.assertEqual(list(tree.conflicts()), [expected])
 
266
        os.unlink('lala.OTHER')
 
267
        os.mkdir('lala.OTHER')
 
268
        expected = conflicts.ContentsConflict('lala', file_id='lala-id')
 
269
        self.assertEqual(list(tree.conflicts()), [expected])
 
270
 
 
271
 
300
272
class InstrumentedTree(object):
301
273
    """A instrumented tree to check the needs_tree_write_lock decorator."""
302
274
 
373
345
        self.build_tree_contents([('other/hello', 'hELLO')])
374
346
        other.commit('Case switch')
375
347
        this = base.bzrdir.sprout('this').open_workingtree()
376
 
        self.assertPathExists('this/hello')
 
348
        self.failUnlessExists('this/hello')
377
349
        self.build_tree_contents([('this/hello', 'Hello World')])
378
350
        this.commit('Add World')
379
351
        this.merge_from_branch(other.branch)
400
372
        self.assertEqual([], this.conflicts())
401
373
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
402
374
                         resolved)
403
 
        self.assertPathDoesNotExist('this/hello.BASE')
 
375
        self.failIfExists('this/hello.BASE')
404
376
 
405
377
    def test_auto_resolve_dir(self):
406
378
        tree = self.make_branch_and_tree('tree')