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
18
from cStringIO import StringIO
18
21
from bzrlib import (
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):
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()
66
70
self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
67
workingtree.format_registry.set_default(SampleTreeFormat())
71
workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
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')
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())
80
84
def test_open(self):
81
85
tree = self.make_branch_and_tree('.')
122
126
return "opened tree."
125
class SampleExtraTreeFormat(workingtree.WorkingTreeFormat):
126
"""A sample format that does not support use in a metadir.
130
def get_format_string(self):
131
# Not usable in a metadir, so no format string
134
def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
135
accelerator_tree=None, hardlink=False):
136
raise NotImplementedError(self.initialize)
138
def is_supported(self):
141
def open(self, transport, _found=False):
142
raise NotImplementedError(self.open)
145
129
class TestWorkingTreeFormat(TestCaseWithTransport):
146
130
"""Tests for the WorkingTreeFormat facility."""
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))
203
class TestWorkingTreeFormatRegistry(TestCase):
206
super(TestWorkingTreeFormatRegistry, self).setUp()
207
self.registry = workingtree.WorkingTreeFormatRegistry()
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.")
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())
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())
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)
237
179
class TestWorkingTreeFormat3(TestCaseWithTransport):
297
239
self.assertEqual([], tree.get_parent_ids())
242
class TestFormat2WorkingTree(TestCaseWithTransport):
243
"""Tests that are specific to format 2 trees."""
245
def create_format2_tree(self, url):
246
return self.make_branch_and_tree(
247
url, format=bzrdir.BzrDirFormat6())
249
def test_conflicts(self):
250
# test backwards compatability
251
tree = self.create_format2_tree('.')
252
self.assertRaises(errors.UnsupportedOperation, tree.set_conflicts,
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])
300
272
class InstrumentedTree(object):
301
273
"""A instrumented tree to check the needs_tree_write_lock decorator."""
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)