1
# (C) 2006 Canonical Ltd
1
# Copyright (C) 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
34
UnknownFormatError,
34
35
UnsupportedFormatError,
36
import bzrlib.repository as repository
37
from bzrlib.repository import RepositoryFormat
37
38
from bzrlib.tests import TestCase, TestCaseWithTransport
38
39
from bzrlib.transport import get_transport
39
from bzrlib.transport.http import HttpServer
40
40
from bzrlib.transport.memory import MemoryServer
46
from bzrlib.repofmt import knitrepo, weaverepo
43
49
class TestDefaultFormat(TestCase):
45
51
def test_get_set_default_format(self):
52
old_default = bzrdir.format_registry.get('default')
53
private_default = old_default().repository_format.__class__
46
54
old_format = repository.RepositoryFormat.get_default_format()
47
self.assertTrue(isinstance(old_format, repository.RepositoryFormatKnit1))
48
repository.RepositoryFormat.set_default_format(SampleRepositoryFormat())
55
self.assertTrue(isinstance(old_format, private_default))
56
def make_sample_bzrdir():
57
my_bzrdir = bzrdir.BzrDirMetaFormat1()
58
my_bzrdir.repository_format = SampleRepositoryFormat()
60
bzrdir.format_registry.remove('default')
61
bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
62
bzrdir.format_registry.set_default('sample')
49
63
# creating a repository should now create an instrumented dir.
51
65
# the default branch format is used by the meta dir format
52
66
# which is not the default bzrdir format at this point
53
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:/')
67
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
54
68
result = dir.create_repository()
55
69
self.assertEqual(result, 'A bzr repository dir')
57
repository.RepositoryFormat.set_default_format(old_format)
58
self.assertEqual(old_format, repository.RepositoryFormat.get_default_format())
71
bzrdir.format_registry.remove('default')
72
bzrdir.format_registry.register('default', old_default, '')
73
self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
61
77
class SampleRepositoryFormat(repository.RepositoryFormat):
72
88
def initialize(self, a_bzrdir, shared=False):
73
89
"""Initialize a repository in a BzrDir"""
74
90
t = a_bzrdir.get_repository_transport(self)
75
t.put('format', StringIO(self.get_format_string()))
91
t.put_bytes('format', self.get_format_string())
76
92
return 'A bzr repository dir'
78
94
def is_supported(self):
96
112
t = get_transport(url)
97
113
found_format = repository.RepositoryFormat.find_format(dir)
98
114
self.failUnless(isinstance(found_format, format.__class__))
99
check_format(repository.RepositoryFormat7(), "bar")
115
check_format(weaverepo.RepositoryFormat7(), "bar")
101
117
def test_find_format_no_repository(self):
102
118
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
132
148
def test_no_ancestry_weave(self):
133
149
control = bzrdir.BzrDirFormat6().initialize(self.get_url())
134
repo = repository.RepositoryFormat6().initialize(control)
150
repo = weaverepo.RepositoryFormat6().initialize(control)
135
151
# We no longer need to create the ancestry.weave file
136
152
# since it is *never* used.
137
153
self.assertRaises(NoSuchFile,
144
160
def test_disk_layout(self):
145
161
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
146
repo = repository.RepositoryFormat7().initialize(control)
162
repo = weaverepo.RepositoryFormat7().initialize(control)
147
163
# in case of side effects of locking.
148
164
repo.lock_write()
166
182
def test_shared_disk_layout(self):
167
183
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
168
repo = repository.RepositoryFormat7().initialize(control, shared=True)
184
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
170
186
# format 'Bazaar-NG Repository format 7'
171
187
# inventory.weave == empty_weave
188
204
def test_creates_lockdir(self):
189
205
"""Make sure it appears to be controlled by a LockDir existence"""
190
206
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
191
repo = repository.RepositoryFormat7().initialize(control, shared=True)
207
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
192
208
t = control.get_repository_transport(None)
193
209
# TODO: Should check there is a 'lock' toplevel directory,
194
210
# regardless of contents
204
220
"""repo format 7 actually locks on lockdir"""
205
221
base_url = self.get_url()
206
222
control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
207
repo = repository.RepositoryFormat7().initialize(control, shared=True)
223
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
208
224
t = control.get_repository_transport(None)
209
225
repo.lock_write()
219
235
def test_shared_no_tree_disk_layout(self):
220
236
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
221
repo = repository.RepositoryFormat7().initialize(control, shared=True)
237
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
222
238
repo.set_make_working_trees(False)
224
240
# format 'Bazaar-NG Repository format 7'
248
264
def test_disk_layout(self):
249
265
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
250
repo = repository.RepositoryFormatKnit1().initialize(control)
266
repo = knitrepo.RepositoryFormatKnit1().initialize(control)
251
267
# in case of side effects of locking.
252
268
repo.lock_write()
281
297
def test_shared_disk_layout(self):
282
298
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
283
repo = repository.RepositoryFormatKnit1().initialize(control, shared=True)
299
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
285
301
# format 'Bazaar-NG Knit Repository Format 1'
286
302
# lock: is a directory
300
316
def test_shared_no_tree_disk_layout(self):
301
317
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
302
repo = repository.RepositoryFormatKnit1().initialize(control, shared=True)
318
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
303
319
repo.set_make_working_trees(False)
305
321
# format 'Bazaar-NG Knit Repository Format 1'
387
402
def test_is_compatible_and_registered(self):
388
403
# InterWeaveRepo is compatible when either side
389
404
# is a format 5/6/7 branch
390
formats = [repository.RepositoryFormat5(),
391
repository.RepositoryFormat6(),
392
repository.RepositoryFormat7()]
393
incompatible_formats = [repository.RepositoryFormat4(),
394
repository.RepositoryFormatKnit1(),
405
from bzrlib.repofmt import knitrepo, weaverepo
406
formats = [weaverepo.RepositoryFormat5(),
407
weaverepo.RepositoryFormat6(),
408
weaverepo.RepositoryFormat7()]
409
incompatible_formats = [weaverepo.RepositoryFormat4(),
410
knitrepo.RepositoryFormatKnit1(),
396
412
repo_a = self.make_repository('a')
397
413
repo_b = self.make_repository('b')
418
434
t = get_transport(self.get_url('.'))
419
435
t.mkdir('repository')
420
436
repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
421
repo = repository.RepositoryFormat7().initialize(repo_dir)
422
target_format = repository.RepositoryFormatKnit1()
437
repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
438
target_format = knitrepo.RepositoryFormatKnit1()
423
439
converter = repository.CopyConverter(target_format)
424
440
pb = bzrlib.ui.ui_factory.nested_progress_bar()
429
445
repo = repo_dir.open_repository()
430
446
self.assertTrue(isinstance(target_format, repo._format.__class__))
449
class TestMisc(TestCase):
451
def test_unescape_xml(self):
452
"""We get some kind of error when malformed entities are passed"""
453
self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;')
456
class TestRepositoryFormatKnit3(TestCaseWithTransport):
458
def test_convert(self):
459
"""Ensure the upgrade adds weaves for roots"""
460
format = bzrdir.BzrDirMetaFormat1()
461
format.repository_format = knitrepo.RepositoryFormatKnit1()
462
tree = self.make_branch_and_tree('.', format)
463
tree.commit("Dull commit", rev_id="dull")
464
revision_tree = tree.branch.repository.revision_tree('dull')
465
self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
466
revision_tree.inventory.root.file_id)
467
format = bzrdir.BzrDirMetaFormat1()
468
format.repository_format = knitrepo.RepositoryFormatKnit3()
469
upgrade.Convert('.', format)
470
tree = workingtree.WorkingTree.open('.')
471
revision_tree = tree.branch.repository.revision_tree('dull')
472
revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
473
tree.commit("Another dull commit", rev_id='dull2')
474
revision_tree = tree.branch.repository.revision_tree('dull2')
475
self.assertEqual('dull', revision_tree.inventory.root.revision)