1
# Copyright (C) 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the Repository facility that are not interface tests.
19
For interface tests see tests/repository_implementations/*.py.
21
For concrete class tests see this file, and for storage formats tests
25
from stat import S_ISDIR
26
from StringIO import StringIO
28
from bzrlib import symbol_versioning
30
import bzrlib.bzrdir as bzrdir
31
import bzrlib.errors as errors
32
from bzrlib.errors import (NotBranchError,
35
UnsupportedFormatError,
37
from bzrlib.repository import RepositoryFormat
38
from bzrlib.tests import TestCase, TestCaseWithTransport
39
from bzrlib.transport import get_transport
40
from bzrlib.transport.memory import MemoryServer
46
from bzrlib.repofmt import knitrepo, weaverepo
49
class TestDefaultFormat(TestCase):
51
def test_get_set_default_format(self):
52
old_default = bzrdir.format_registry.get('default')
53
private_default = old_default().repository_format.__class__
54
old_format = repository.RepositoryFormat.get_default_format()
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')
63
# creating a repository should now create an instrumented dir.
65
# the default branch format is used by the meta dir format
66
# which is not the default bzrdir format at this point
67
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
68
result = dir.create_repository()
69
self.assertEqual(result, 'A bzr repository dir')
71
bzrdir.format_registry.remove('default')
72
bzrdir.format_registry.register('default', old_default, '')
73
self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
77
class SampleRepositoryFormat(repository.RepositoryFormat):
80
this format is initializable, unsupported to aid in testing the
81
open and open(unsupported=True) routines.
84
def get_format_string(self):
85
"""See RepositoryFormat.get_format_string()."""
86
return "Sample .bzr repository format."
88
def initialize(self, a_bzrdir, shared=False):
89
"""Initialize a repository in a BzrDir"""
90
t = a_bzrdir.get_repository_transport(self)
91
t.put_bytes('format', self.get_format_string())
92
return 'A bzr repository dir'
94
def is_supported(self):
97
def open(self, a_bzrdir, _found=False):
98
return "opened repository."
101
class TestRepositoryFormat(TestCaseWithTransport):
102
"""Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
104
def test_find_format(self):
105
# is the right format object found for a repository?
106
# create a branch with a few known format objects.
107
# this is not quite the same as
108
self.build_tree(["foo/", "bar/"])
109
def check_format(format, url):
110
dir = format._matchingbzrdir.initialize(url)
111
format.initialize(dir)
112
t = get_transport(url)
113
found_format = repository.RepositoryFormat.find_format(dir)
114
self.failUnless(isinstance(found_format, format.__class__))
115
check_format(weaverepo.RepositoryFormat7(), "bar")
117
def test_find_format_no_repository(self):
118
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
119
self.assertRaises(errors.NoRepositoryPresent,
120
repository.RepositoryFormat.find_format,
123
def test_find_format_unknown_format(self):
124
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
125
SampleRepositoryFormat().initialize(dir)
126
self.assertRaises(UnknownFormatError,
127
repository.RepositoryFormat.find_format,
130
def test_register_unregister_format(self):
131
format = SampleRepositoryFormat()
133
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
135
format.initialize(dir)
136
# register a format for it.
137
repository.RepositoryFormat.register_format(format)
138
# which repository.Open will refuse (not supported)
139
self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
140
# but open(unsupported) will work
141
self.assertEqual(format.open(dir), "opened repository.")
142
# unregister the format
143
repository.RepositoryFormat.unregister_format(format)
146
class TestFormat6(TestCaseWithTransport):
148
def test_no_ancestry_weave(self):
149
control = bzrdir.BzrDirFormat6().initialize(self.get_url())
150
repo = weaverepo.RepositoryFormat6().initialize(control)
151
# We no longer need to create the ancestry.weave file
152
# since it is *never* used.
153
self.assertRaises(NoSuchFile,
154
control.transport.get,
158
class TestFormat7(TestCaseWithTransport):
160
def test_disk_layout(self):
161
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
162
repo = weaverepo.RepositoryFormat7().initialize(control)
163
# in case of side effects of locking.
167
# format 'Bazaar-NG Repository format 7'
169
# inventory.weave == empty_weave
170
# empty revision-store directory
171
# empty weaves directory
172
t = control.get_repository_transport(None)
173
self.assertEqualDiff('Bazaar-NG Repository format 7',
174
t.get('format').read())
175
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
176
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
177
self.assertEqualDiff('# bzr weave file v5\n'
180
t.get('inventory.weave').read())
182
def test_shared_disk_layout(self):
183
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
184
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
186
# format 'Bazaar-NG Repository format 7'
187
# inventory.weave == empty_weave
188
# empty revision-store directory
189
# empty weaves directory
190
# a 'shared-storage' marker file.
191
# lock is not present when unlocked
192
t = control.get_repository_transport(None)
193
self.assertEqualDiff('Bazaar-NG Repository format 7',
194
t.get('format').read())
195
self.assertEqualDiff('', t.get('shared-storage').read())
196
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
197
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
198
self.assertEqualDiff('# bzr weave file v5\n'
201
t.get('inventory.weave').read())
202
self.assertFalse(t.has('branch-lock'))
204
def test_creates_lockdir(self):
205
"""Make sure it appears to be controlled by a LockDir existence"""
206
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
207
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
208
t = control.get_repository_transport(None)
209
# TODO: Should check there is a 'lock' toplevel directory,
210
# regardless of contents
211
self.assertFalse(t.has('lock/held/info'))
214
self.assertTrue(t.has('lock/held/info'))
216
# unlock so we don't get a warning about failing to do so
219
def test_uses_lockdir(self):
220
"""repo format 7 actually locks on lockdir"""
221
base_url = self.get_url()
222
control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
223
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
224
t = control.get_repository_transport(None)
228
# make sure the same lock is created by opening it
229
repo = repository.Repository.open(base_url)
231
self.assertTrue(t.has('lock/held/info'))
233
self.assertFalse(t.has('lock/held/info'))
235
def test_shared_no_tree_disk_layout(self):
236
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
237
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
238
repo.set_make_working_trees(False)
240
# format 'Bazaar-NG Repository format 7'
242
# inventory.weave == empty_weave
243
# empty revision-store directory
244
# empty weaves directory
245
# a 'shared-storage' marker file.
246
t = control.get_repository_transport(None)
247
self.assertEqualDiff('Bazaar-NG Repository format 7',
248
t.get('format').read())
249
## self.assertEqualDiff('', t.get('lock').read())
250
self.assertEqualDiff('', t.get('shared-storage').read())
251
self.assertEqualDiff('', t.get('no-working-trees').read())
252
repo.set_make_working_trees(True)
253
self.assertFalse(t.has('no-working-trees'))
254
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
255
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
256
self.assertEqualDiff('# bzr weave file v5\n'
259
t.get('inventory.weave').read())
262
class TestFormatKnit1(TestCaseWithTransport):
264
def test_disk_layout(self):
265
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
266
repo = knitrepo.RepositoryFormatKnit1().initialize(control)
267
# in case of side effects of locking.
271
# format 'Bazaar-NG Knit Repository Format 1'
272
# lock: is a directory
273
# inventory.weave == empty_weave
274
# empty revision-store directory
275
# empty weaves directory
276
t = control.get_repository_transport(None)
277
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
278
t.get('format').read())
279
# XXX: no locks left when unlocked at the moment
280
# self.assertEqualDiff('', t.get('lock').read())
281
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
284
def assertHasKnit(self, t, knit_name):
285
"""Assert that knit_name exists on t."""
286
self.assertEqualDiff('# bzr knit index 8\n',
287
t.get(knit_name + '.kndx').read())
289
self.assertTrue(t.has(knit_name + '.knit'))
291
def check_knits(self, t):
292
"""check knit content for a repository."""
293
self.assertHasKnit(t, 'inventory')
294
self.assertHasKnit(t, 'revisions')
295
self.assertHasKnit(t, 'signatures')
297
def test_shared_disk_layout(self):
298
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
299
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
301
# format 'Bazaar-NG Knit Repository Format 1'
302
# lock: is a directory
303
# inventory.weave == empty_weave
304
# empty revision-store directory
305
# empty weaves directory
306
# a 'shared-storage' marker file.
307
t = control.get_repository_transport(None)
308
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
309
t.get('format').read())
310
# XXX: no locks left when unlocked at the moment
311
# self.assertEqualDiff('', t.get('lock').read())
312
self.assertEqualDiff('', t.get('shared-storage').read())
313
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
316
def test_shared_no_tree_disk_layout(self):
317
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
318
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
319
repo.set_make_working_trees(False)
321
# format 'Bazaar-NG Knit Repository Format 1'
323
# inventory.weave == empty_weave
324
# empty revision-store directory
325
# empty weaves directory
326
# a 'shared-storage' marker file.
327
t = control.get_repository_transport(None)
328
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
329
t.get('format').read())
330
# XXX: no locks left when unlocked at the moment
331
# self.assertEqualDiff('', t.get('lock').read())
332
self.assertEqualDiff('', t.get('shared-storage').read())
333
self.assertEqualDiff('', t.get('no-working-trees').read())
334
repo.set_make_working_trees(True)
335
self.assertFalse(t.has('no-working-trees'))
336
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
339
class InterString(repository.InterRepository):
340
"""An inter-repository optimised code path for strings.
342
This is for use during testing where we use strings as repositories
343
so that none of the default regsitered inter-repository classes will
348
def is_compatible(repo_source, repo_target):
349
"""InterString is compatible with strings-as-repos."""
350
return isinstance(repo_source, str) and isinstance(repo_target, str)
353
class TestInterRepository(TestCaseWithTransport):
355
def test_get_default_inter_repository(self):
356
# test that the InterRepository.get(repo_a, repo_b) probes
357
# for a inter_repo class where is_compatible(repo_a, repo_b) returns
358
# true and returns a default inter_repo otherwise.
359
# This also tests that the default registered optimised interrepository
360
# classes do not barf inappropriately when a surprising repository type
362
dummy_a = "Repository 1."
363
dummy_b = "Repository 2."
364
self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
366
def assertGetsDefaultInterRepository(self, repo_a, repo_b):
367
"""Asserts that InterRepository.get(repo_a, repo_b) -> the default."""
368
inter_repo = repository.InterRepository.get(repo_a, repo_b)
369
self.assertEqual(repository.InterRepository,
370
inter_repo.__class__)
371
self.assertEqual(repo_a, inter_repo.source)
372
self.assertEqual(repo_b, inter_repo.target)
374
def test_register_inter_repository_class(self):
375
# test that a optimised code path provider - a
376
# InterRepository subclass can be registered and unregistered
377
# and that it is correctly selected when given a repository
378
# pair that it returns true on for the is_compatible static method
380
dummy_a = "Repository 1."
381
dummy_b = "Repository 2."
382
repository.InterRepository.register_optimiser(InterString)
384
# we should get the default for something InterString returns False
386
self.assertFalse(InterString.is_compatible(dummy_a, None))
387
self.assertGetsDefaultInterRepository(dummy_a, None)
388
# and we should get an InterString for a pair it 'likes'
389
self.assertTrue(InterString.is_compatible(dummy_a, dummy_b))
390
inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
391
self.assertEqual(InterString, inter_repo.__class__)
392
self.assertEqual(dummy_a, inter_repo.source)
393
self.assertEqual(dummy_b, inter_repo.target)
395
repository.InterRepository.unregister_optimiser(InterString)
396
# now we should get the default InterRepository object again.
397
self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
400
class TestInterWeaveRepo(TestCaseWithTransport):
402
def test_is_compatible_and_registered(self):
403
# InterWeaveRepo is compatible when either side
404
# is a format 5/6/7 branch
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(),
412
repo_a = self.make_repository('a')
413
repo_b = self.make_repository('b')
414
is_compatible = repository.InterWeaveRepo.is_compatible
415
for source in incompatible_formats:
416
# force incompatible left then right
417
repo_a._format = source
418
repo_b._format = formats[0]
419
self.assertFalse(is_compatible(repo_a, repo_b))
420
self.assertFalse(is_compatible(repo_b, repo_a))
421
for source in formats:
422
repo_a._format = source
423
for target in formats:
424
repo_b._format = target
425
self.assertTrue(is_compatible(repo_a, repo_b))
426
self.assertEqual(repository.InterWeaveRepo,
427
repository.InterRepository.get(repo_a,
431
class TestRepositoryConverter(TestCaseWithTransport):
433
def test_convert_empty(self):
434
t = get_transport(self.get_url('.'))
435
t.mkdir('repository')
436
repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
437
repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
438
target_format = knitrepo.RepositoryFormatKnit1()
439
converter = repository.CopyConverter(target_format)
440
pb = bzrlib.ui.ui_factory.nested_progress_bar()
442
converter.convert(repo, pb)
445
repo = repo_dir.open_repository()
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)