~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

  • Committer: Andrew Bennetts
  • Date: 2011-03-15 07:54:39 UTC
  • mfrom: (0.38.5 trunk)
  • mto: This revision was merged to the branch mainline in revision 5726.
  • Revision ID: andrew.bennetts@canonical.com-20110315075439-nzm293joz143cx0k
Merge bzr-changelog-merge plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
 
 
21
18
from bzrlib import (
22
19
    bzrdir,
23
20
    conflicts,
24
21
    errors,
 
22
    symbol_versioning,
25
23
    transport,
26
24
    workingtree,
27
25
    )
28
 
from bzrlib.branch import Branch
29
 
from bzrlib.bzrdir import BzrDir
30
26
from bzrlib.lockdir import LockDir
31
27
from bzrlib.mutabletree import needs_tree_write_lock
32
28
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
65
61
class TestDefaultFormat(TestCaseWithTransport):
66
62
 
67
63
    def test_get_set_default_format(self):
68
 
        old_format = workingtree.WorkingTreeFormat.get_default_format()
 
64
        old_format = workingtree.format_registry.get_default()
69
65
        # default is 3
70
66
        self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
71
 
        workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
 
67
        workingtree.format_registry.set_default(SampleTreeFormat())
72
68
        try:
73
69
            # the default branch format is used by the meta dir format
74
70
            # which is not the default bzrdir format at this point
78
74
            result = dir.create_workingtree()
79
75
            self.assertEqual(result, 'A tree')
80
76
        finally:
81
 
            workingtree.WorkingTreeFormat.set_default_format(old_format)
82
 
        self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
 
77
            workingtree.format_registry.set_default(old_format)
 
78
        self.assertEqual(old_format, workingtree.format_registry.get_default())
83
79
 
84
80
    def test_open(self):
85
81
        tree = self.make_branch_and_tree('.')
126
122
        return "opened tree."
127
123
 
128
124
 
 
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
 
129
145
class TestWorkingTreeFormat(TestCaseWithTransport):
130
146
    """Tests for the WorkingTreeFormat facility."""
131
147
 
167
183
        # make a branch
168
184
        format.initialize(dir)
169
185
        # register a format for it.
170
 
        workingtree.WorkingTreeFormat.register_format(format)
 
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))
171
191
        # which branch.Open will refuse (not supported)
172
192
        self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
173
193
        # but open_downlevel will work
174
194
        self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
175
195
        # unregister the format
176
 
        workingtree.WorkingTreeFormat.unregister_format(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)
177
235
 
178
236
 
179
237
class TestWorkingTreeFormat3(TestCaseWithTransport):
239
297
        self.assertEqual([], tree.get_parent_ids())
240
298
 
241
299
 
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
 
 
272
300
class InstrumentedTree(object):
273
301
    """A instrumented tree to check the needs_tree_write_lock decorator."""
274
302
 
349
377
        self.build_tree_contents([('this/hello', 'Hello World')])
350
378
        this.commit('Add World')
351
379
        this.merge_from_branch(other.branch)
352
 
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
380
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
353
381
                         this.conflicts())
354
382
        this.auto_resolve()
355
 
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
383
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
356
384
                         this.conflicts())
357
385
        self.build_tree_contents([('this/hello', '<<<<<<<')])
358
386
        this.auto_resolve()
359
 
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
387
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
360
388
                         this.conflicts())
361
389
        self.build_tree_contents([('this/hello', '=======')])
362
390
        this.auto_resolve()
363
 
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
391
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
364
392
                         this.conflicts())
365
393
        self.build_tree_contents([('this/hello', '\n>>>>>>>')])
366
394
        remaining, resolved = this.auto_resolve()
367
 
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
395
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
368
396
                         this.conflicts())
369
397
        self.assertEqual([], resolved)
370
398
        self.build_tree_contents([('this/hello', 'hELLO wORLD')])
371
399
        remaining, resolved = this.auto_resolve()
372
400
        self.assertEqual([], this.conflicts())
373
 
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
401
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
374
402
                         resolved)
375
403
        self.failIfExists('this/hello.BASE')
376
404
 
378
406
        tree = self.make_branch_and_tree('tree')
379
407
        self.build_tree(['tree/hello/'])
380
408
        tree.add('hello', 'hello-id')
381
 
        file_conflict = conflicts.TextConflict('file', None, 'hello-id')
 
409
        file_conflict = conflicts.TextConflict('file', 'hello-id')
382
410
        tree.set_conflicts(conflicts.ConflictList([file_conflict]))
383
411
        tree.auto_resolve()
384
412