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
26
from stat import S_ISDIR
27
from StringIO import StringIO
30
from bzrlib.errors import (NotBranchError,
33
UnsupportedFormatError,
35
from bzrlib import graph
36
from bzrlib.index import GraphIndex, InMemoryGraphIndex
37
from bzrlib.repository import RepositoryFormat
38
from bzrlib.smart import server
39
from bzrlib.tests import (
41
TestCaseWithTransport,
45
from bzrlib.transport import (
49
from bzrlib.transport.memory import MemoryServer
50
from bzrlib.util import bencode
57
revision as _mod_revision,
62
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
65
class TestDefaultFormat(TestCase):
67
def test_get_set_default_format(self):
68
old_default = bzrdir.format_registry.get('default')
69
private_default = old_default().repository_format.__class__
70
old_format = repository.RepositoryFormat.get_default_format()
71
self.assertTrue(isinstance(old_format, private_default))
72
def make_sample_bzrdir():
73
my_bzrdir = bzrdir.BzrDirMetaFormat1()
74
my_bzrdir.repository_format = SampleRepositoryFormat()
76
bzrdir.format_registry.remove('default')
77
bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
78
bzrdir.format_registry.set_default('sample')
79
# creating a repository should now create an instrumented dir.
81
# the default branch format is used by the meta dir format
82
# which is not the default bzrdir format at this point
83
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
84
result = dir.create_repository()
85
self.assertEqual(result, 'A bzr repository dir')
87
bzrdir.format_registry.remove('default')
88
bzrdir.format_registry.remove('sample')
89
bzrdir.format_registry.register('default', old_default, '')
90
self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
94
class SampleRepositoryFormat(repository.RepositoryFormat):
97
this format is initializable, unsupported to aid in testing the
98
open and open(unsupported=True) routines.
101
def get_format_string(self):
102
"""See RepositoryFormat.get_format_string()."""
103
return "Sample .bzr repository format."
105
def initialize(self, a_bzrdir, shared=False):
106
"""Initialize a repository in a BzrDir"""
107
t = a_bzrdir.get_repository_transport(self)
108
t.put_bytes('format', self.get_format_string())
109
return 'A bzr repository dir'
111
def is_supported(self):
114
def open(self, a_bzrdir, _found=False):
115
return "opened repository."
118
class TestRepositoryFormat(TestCaseWithTransport):
119
"""Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
121
def test_find_format(self):
122
# is the right format object found for a repository?
123
# create a branch with a few known format objects.
124
# this is not quite the same as
125
self.build_tree(["foo/", "bar/"])
126
def check_format(format, url):
127
dir = format._matchingbzrdir.initialize(url)
128
format.initialize(dir)
129
t = get_transport(url)
130
found_format = repository.RepositoryFormat.find_format(dir)
131
self.failUnless(isinstance(found_format, format.__class__))
132
check_format(weaverepo.RepositoryFormat7(), "bar")
134
def test_find_format_no_repository(self):
135
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
136
self.assertRaises(errors.NoRepositoryPresent,
137
repository.RepositoryFormat.find_format,
140
def test_find_format_unknown_format(self):
141
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
142
SampleRepositoryFormat().initialize(dir)
143
self.assertRaises(UnknownFormatError,
144
repository.RepositoryFormat.find_format,
147
def test_register_unregister_format(self):
148
format = SampleRepositoryFormat()
150
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
152
format.initialize(dir)
153
# register a format for it.
154
repository.RepositoryFormat.register_format(format)
155
# which repository.Open will refuse (not supported)
156
self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
157
# but open(unsupported) will work
158
self.assertEqual(format.open(dir), "opened repository.")
159
# unregister the format
160
repository.RepositoryFormat.unregister_format(format)
163
class TestFormat6(TestCaseWithTransport):
165
def test_no_ancestry_weave(self):
166
control = bzrdir.BzrDirFormat6().initialize(self.get_url())
167
repo = weaverepo.RepositoryFormat6().initialize(control)
168
# We no longer need to create the ancestry.weave file
169
# since it is *never* used.
170
self.assertRaises(NoSuchFile,
171
control.transport.get,
174
def test_supports_external_lookups(self):
175
control = bzrdir.BzrDirFormat6().initialize(self.get_url())
176
repo = weaverepo.RepositoryFormat6().initialize(control)
177
self.assertFalse(repo._format.supports_external_lookups)
180
class TestFormat7(TestCaseWithTransport):
182
def test_disk_layout(self):
183
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
184
repo = weaverepo.RepositoryFormat7().initialize(control)
185
# in case of side effects of locking.
189
# format 'Bazaar-NG Repository format 7'
191
# inventory.weave == empty_weave
192
# empty revision-store directory
193
# empty weaves directory
194
t = control.get_repository_transport(None)
195
self.assertEqualDiff('Bazaar-NG Repository format 7',
196
t.get('format').read())
197
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
198
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
199
self.assertEqualDiff('# bzr weave file v5\n'
202
t.get('inventory.weave').read())
203
# Creating a file with id Foo:Bar results in a non-escaped file name on
205
control.create_branch()
206
tree = control.create_workingtree()
207
tree.add(['foo'], ['Foo:Bar'], ['file'])
208
tree.put_file_bytes_non_atomic('Foo:Bar', 'content\n')
209
tree.commit('first post', rev_id='first')
210
self.assertEqualDiff(
211
'# bzr weave file v5\n'
213
'1 7fe70820e08a1aac0ef224d9c66ab66831cc4ab1\n'
221
t.get('weaves/74/Foo%3ABar.weave').read())
223
def test_shared_disk_layout(self):
224
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
225
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
227
# format 'Bazaar-NG Repository format 7'
228
# inventory.weave == empty_weave
229
# empty revision-store directory
230
# empty weaves directory
231
# a 'shared-storage' marker file.
232
# lock is not present when unlocked
233
t = control.get_repository_transport(None)
234
self.assertEqualDiff('Bazaar-NG Repository format 7',
235
t.get('format').read())
236
self.assertEqualDiff('', t.get('shared-storage').read())
237
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
238
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
239
self.assertEqualDiff('# bzr weave file v5\n'
242
t.get('inventory.weave').read())
243
self.assertFalse(t.has('branch-lock'))
245
def test_creates_lockdir(self):
246
"""Make sure it appears to be controlled by a LockDir existence"""
247
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
248
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
249
t = control.get_repository_transport(None)
250
# TODO: Should check there is a 'lock' toplevel directory,
251
# regardless of contents
252
self.assertFalse(t.has('lock/held/info'))
255
self.assertTrue(t.has('lock/held/info'))
257
# unlock so we don't get a warning about failing to do so
260
def test_uses_lockdir(self):
261
"""repo format 7 actually locks on lockdir"""
262
base_url = self.get_url()
263
control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
264
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
265
t = control.get_repository_transport(None)
269
# make sure the same lock is created by opening it
270
repo = repository.Repository.open(base_url)
272
self.assertTrue(t.has('lock/held/info'))
274
self.assertFalse(t.has('lock/held/info'))
276
def test_shared_no_tree_disk_layout(self):
277
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
278
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
279
repo.set_make_working_trees(False)
281
# format 'Bazaar-NG Repository format 7'
283
# inventory.weave == empty_weave
284
# empty revision-store directory
285
# empty weaves directory
286
# a 'shared-storage' marker file.
287
t = control.get_repository_transport(None)
288
self.assertEqualDiff('Bazaar-NG Repository format 7',
289
t.get('format').read())
290
## self.assertEqualDiff('', t.get('lock').read())
291
self.assertEqualDiff('', t.get('shared-storage').read())
292
self.assertEqualDiff('', t.get('no-working-trees').read())
293
repo.set_make_working_trees(True)
294
self.assertFalse(t.has('no-working-trees'))
295
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
296
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
297
self.assertEqualDiff('# bzr weave file v5\n'
300
t.get('inventory.weave').read())
302
def test_supports_external_lookups(self):
303
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
304
repo = weaverepo.RepositoryFormat7().initialize(control)
305
self.assertFalse(repo._format.supports_external_lookups)
308
class TestFormatKnit1(TestCaseWithTransport):
310
def test_disk_layout(self):
311
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
312
repo = knitrepo.RepositoryFormatKnit1().initialize(control)
313
# in case of side effects of locking.
317
# format 'Bazaar-NG Knit Repository Format 1'
318
# lock: is a directory
319
# inventory.weave == empty_weave
320
# empty revision-store directory
321
# empty weaves directory
322
t = control.get_repository_transport(None)
323
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
324
t.get('format').read())
325
# XXX: no locks left when unlocked at the moment
326
# self.assertEqualDiff('', t.get('lock').read())
327
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
329
# Check per-file knits.
330
branch = control.create_branch()
331
tree = control.create_workingtree()
332
tree.add(['foo'], ['Nasty-IdC:'], ['file'])
333
tree.put_file_bytes_non_atomic('Nasty-IdC:', '')
334
tree.commit('1st post', rev_id='foo')
335
self.assertHasKnit(t, 'knits/e8/%254easty-%2549d%2543%253a',
336
'\nfoo fulltext 0 81 :')
338
def assertHasKnit(self, t, knit_name, extra_content=''):
339
"""Assert that knit_name exists on t."""
340
self.assertEqualDiff('# bzr knit index 8\n' + extra_content,
341
t.get(knit_name + '.kndx').read())
343
def check_knits(self, t):
344
"""check knit content for a repository."""
345
self.assertHasKnit(t, 'inventory')
346
self.assertHasKnit(t, 'revisions')
347
self.assertHasKnit(t, 'signatures')
349
def test_shared_disk_layout(self):
350
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
351
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
353
# format 'Bazaar-NG Knit Repository Format 1'
354
# lock: is a directory
355
# inventory.weave == empty_weave
356
# empty revision-store directory
357
# empty weaves directory
358
# a 'shared-storage' marker file.
359
t = control.get_repository_transport(None)
360
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
361
t.get('format').read())
362
# XXX: no locks left when unlocked at the moment
363
# self.assertEqualDiff('', t.get('lock').read())
364
self.assertEqualDiff('', t.get('shared-storage').read())
365
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
368
def test_shared_no_tree_disk_layout(self):
369
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
370
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
371
repo.set_make_working_trees(False)
373
# format 'Bazaar-NG Knit Repository Format 1'
375
# inventory.weave == empty_weave
376
# empty revision-store directory
377
# empty weaves directory
378
# a 'shared-storage' marker file.
379
t = control.get_repository_transport(None)
380
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
381
t.get('format').read())
382
# XXX: no locks left when unlocked at the moment
383
# self.assertEqualDiff('', t.get('lock').read())
384
self.assertEqualDiff('', t.get('shared-storage').read())
385
self.assertEqualDiff('', t.get('no-working-trees').read())
386
repo.set_make_working_trees(True)
387
self.assertFalse(t.has('no-working-trees'))
388
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
391
def test_deserialise_sets_root_revision(self):
392
"""We must have a inventory.root.revision
394
Old versions of the XML5 serializer did not set the revision_id for
395
the whole inventory. So we grab the one from the expected text. Which
396
is valid when the api is not being abused.
398
repo = self.make_repository('.',
399
format=bzrdir.format_registry.get('knit')())
400
inv_xml = '<inventory format="5">\n</inventory>\n'
401
inv = repo.deserialise_inventory('test-rev-id', inv_xml)
402
self.assertEqual('test-rev-id', inv.root.revision)
404
def test_deserialise_uses_global_revision_id(self):
405
"""If it is set, then we re-use the global revision id"""
406
repo = self.make_repository('.',
407
format=bzrdir.format_registry.get('knit')())
408
inv_xml = ('<inventory format="5" revision_id="other-rev-id">\n'
410
# Arguably, the deserialise_inventory should detect a mismatch, and
411
# raise an error, rather than silently using one revision_id over the
413
self.assertRaises(AssertionError, repo.deserialise_inventory,
414
'test-rev-id', inv_xml)
415
inv = repo.deserialise_inventory('other-rev-id', inv_xml)
416
self.assertEqual('other-rev-id', inv.root.revision)
418
def test_supports_external_lookups(self):
419
repo = self.make_repository('.',
420
format=bzrdir.format_registry.get('knit')())
421
self.assertFalse(repo._format.supports_external_lookups)
424
class DummyRepository(object):
425
"""A dummy repository for testing."""
429
def supports_rich_root(self):
433
class InterDummy(repository.InterRepository):
434
"""An inter-repository optimised code path for DummyRepository.
436
This is for use during testing where we use DummyRepository as repositories
437
so that none of the default regsitered inter-repository classes will
442
def is_compatible(repo_source, repo_target):
443
"""InterDummy is compatible with DummyRepository."""
444
return (isinstance(repo_source, DummyRepository) and
445
isinstance(repo_target, DummyRepository))
448
class TestInterRepository(TestCaseWithTransport):
450
def test_get_default_inter_repository(self):
451
# test that the InterRepository.get(repo_a, repo_b) probes
452
# for a inter_repo class where is_compatible(repo_a, repo_b) returns
453
# true and returns a default inter_repo otherwise.
454
# This also tests that the default registered optimised interrepository
455
# classes do not barf inappropriately when a surprising repository type
457
dummy_a = DummyRepository()
458
dummy_b = DummyRepository()
459
self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
461
def assertGetsDefaultInterRepository(self, repo_a, repo_b):
462
"""Asserts that InterRepository.get(repo_a, repo_b) -> the default.
464
The effective default is now InterSameDataRepository because there is
465
no actual sane default in the presence of incompatible data models.
467
inter_repo = repository.InterRepository.get(repo_a, repo_b)
468
self.assertEqual(repository.InterSameDataRepository,
469
inter_repo.__class__)
470
self.assertEqual(repo_a, inter_repo.source)
471
self.assertEqual(repo_b, inter_repo.target)
473
def test_register_inter_repository_class(self):
474
# test that a optimised code path provider - a
475
# InterRepository subclass can be registered and unregistered
476
# and that it is correctly selected when given a repository
477
# pair that it returns true on for the is_compatible static method
479
dummy_a = DummyRepository()
480
dummy_b = DummyRepository()
481
repo = self.make_repository('.')
482
# hack dummies to look like repo somewhat.
483
dummy_a._serializer = repo._serializer
484
dummy_b._serializer = repo._serializer
485
repository.InterRepository.register_optimiser(InterDummy)
487
# we should get the default for something InterDummy returns False
489
self.assertFalse(InterDummy.is_compatible(dummy_a, repo))
490
self.assertGetsDefaultInterRepository(dummy_a, repo)
491
# and we should get an InterDummy for a pair it 'likes'
492
self.assertTrue(InterDummy.is_compatible(dummy_a, dummy_b))
493
inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
494
self.assertEqual(InterDummy, inter_repo.__class__)
495
self.assertEqual(dummy_a, inter_repo.source)
496
self.assertEqual(dummy_b, inter_repo.target)
498
repository.InterRepository.unregister_optimiser(InterDummy)
499
# now we should get the default InterRepository object again.
500
self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
503
class TestInterWeaveRepo(TestCaseWithTransport):
505
def test_is_compatible_and_registered(self):
506
# InterWeaveRepo is compatible when either side
507
# is a format 5/6/7 branch
508
from bzrlib.repofmt import knitrepo, weaverepo
509
formats = [weaverepo.RepositoryFormat5(),
510
weaverepo.RepositoryFormat6(),
511
weaverepo.RepositoryFormat7()]
512
incompatible_formats = [weaverepo.RepositoryFormat4(),
513
knitrepo.RepositoryFormatKnit1(),
515
repo_a = self.make_repository('a')
516
repo_b = self.make_repository('b')
517
is_compatible = repository.InterWeaveRepo.is_compatible
518
for source in incompatible_formats:
519
# force incompatible left then right
520
repo_a._format = source
521
repo_b._format = formats[0]
522
self.assertFalse(is_compatible(repo_a, repo_b))
523
self.assertFalse(is_compatible(repo_b, repo_a))
524
for source in formats:
525
repo_a._format = source
526
for target in formats:
527
repo_b._format = target
528
self.assertTrue(is_compatible(repo_a, repo_b))
529
self.assertEqual(repository.InterWeaveRepo,
530
repository.InterRepository.get(repo_a,
534
class TestRepositoryConverter(TestCaseWithTransport):
536
def test_convert_empty(self):
537
t = get_transport(self.get_url('.'))
538
t.mkdir('repository')
539
repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
540
repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
541
target_format = knitrepo.RepositoryFormatKnit1()
542
converter = repository.CopyConverter(target_format)
543
pb = bzrlib.ui.ui_factory.nested_progress_bar()
545
converter.convert(repo, pb)
548
repo = repo_dir.open_repository()
549
self.assertTrue(isinstance(target_format, repo._format.__class__))
552
class TestMisc(TestCase):
554
def test_unescape_xml(self):
555
"""We get some kind of error when malformed entities are passed"""
556
self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;')
559
class TestRepositoryFormatKnit3(TestCaseWithTransport):
561
def test_convert(self):
562
"""Ensure the upgrade adds weaves for roots"""
563
format = bzrdir.BzrDirMetaFormat1()
564
format.repository_format = knitrepo.RepositoryFormatKnit1()
565
tree = self.make_branch_and_tree('.', format)
566
tree.commit("Dull commit", rev_id="dull")
567
revision_tree = tree.branch.repository.revision_tree('dull')
568
revision_tree.lock_read()
570
self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
571
revision_tree.inventory.root.file_id)
573
revision_tree.unlock()
574
format = bzrdir.BzrDirMetaFormat1()
575
format.repository_format = knitrepo.RepositoryFormatKnit3()
576
upgrade.Convert('.', format)
577
tree = workingtree.WorkingTree.open('.')
578
revision_tree = tree.branch.repository.revision_tree('dull')
579
revision_tree.lock_read()
581
revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
583
revision_tree.unlock()
584
tree.commit("Another dull commit", rev_id='dull2')
585
revision_tree = tree.branch.repository.revision_tree('dull2')
586
revision_tree.lock_read()
587
self.addCleanup(revision_tree.unlock)
588
self.assertEqual('dull', revision_tree.inventory.root.revision)
590
def test_supports_external_lookups(self):
591
format = bzrdir.BzrDirMetaFormat1()
592
format.repository_format = knitrepo.RepositoryFormatKnit3()
593
repo = self.make_repository('.', format=format)
594
self.assertFalse(repo._format.supports_external_lookups)
597
class TestWithBrokenRepo(TestCaseWithTransport):
598
"""These tests seem to be more appropriate as interface tests?"""
600
def make_broken_repository(self):
601
# XXX: This function is borrowed from Aaron's "Reconcile can fix bad
602
# parent references" branch which is due to land in bzr.dev soon. Once
603
# it does, this duplication should be removed.
604
repo = self.make_repository('broken-repo')
608
cleanups.append(repo.unlock)
609
repo.start_write_group()
610
cleanups.append(repo.commit_write_group)
611
# make rev1a: A well-formed revision, containing 'file1'
612
inv = inventory.Inventory(revision_id='rev1a')
613
inv.root.revision = 'rev1a'
614
self.add_file(repo, inv, 'file1', 'rev1a', [])
615
repo.add_inventory('rev1a', inv, [])
616
revision = _mod_revision.Revision('rev1a',
617
committer='jrandom@example.com', timestamp=0,
618
inventory_sha1='', timezone=0, message='foo', parent_ids=[])
619
repo.add_revision('rev1a',revision, inv)
621
# make rev1b, which has no Revision, but has an Inventory, and
623
inv = inventory.Inventory(revision_id='rev1b')
624
inv.root.revision = 'rev1b'
625
self.add_file(repo, inv, 'file1', 'rev1b', [])
626
repo.add_inventory('rev1b', inv, [])
628
# make rev2, with file1 and file2
630
# file1 has 'rev1b' as an ancestor, even though this is not
631
# mentioned by 'rev1a', making it an unreferenced ancestor
632
inv = inventory.Inventory()
633
self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
634
self.add_file(repo, inv, 'file2', 'rev2', [])
635
self.add_revision(repo, 'rev2', inv, ['rev1a'])
637
# make ghost revision rev1c
638
inv = inventory.Inventory()
639
self.add_file(repo, inv, 'file2', 'rev1c', [])
641
# make rev3 with file2
642
# file2 refers to 'rev1c', which is a ghost in this repository, so
643
# file2 cannot have rev1c as its ancestor.
644
inv = inventory.Inventory()
645
self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
646
self.add_revision(repo, 'rev3', inv, ['rev1c'])
649
for cleanup in reversed(cleanups):
652
def add_revision(self, repo, revision_id, inv, parent_ids):
653
inv.revision_id = revision_id
654
inv.root.revision = revision_id
655
repo.add_inventory(revision_id, inv, parent_ids)
656
revision = _mod_revision.Revision(revision_id,
657
committer='jrandom@example.com', timestamp=0, inventory_sha1='',
658
timezone=0, message='foo', parent_ids=parent_ids)
659
repo.add_revision(revision_id,revision, inv)
661
def add_file(self, repo, inv, filename, revision, parents):
662
file_id = filename + '-id'
663
entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
664
entry.revision = revision
667
text_key = (file_id, revision)
668
parent_keys = [(file_id, parent) for parent in parents]
669
repo.texts.add_lines(text_key, parent_keys, ['line\n'])
671
def test_insert_from_broken_repo(self):
672
"""Inserting a data stream from a broken repository won't silently
673
corrupt the target repository.
675
broken_repo = self.make_broken_repository()
676
empty_repo = self.make_repository('empty-repo')
677
self.assertRaises(errors.RevisionNotPresent, empty_repo.fetch, broken_repo)
680
class TestKnitPackNoSubtrees(TestCaseWithTransport):
682
def get_format(self):
683
return bzrdir.format_registry.make_bzrdir('pack-0.92')
685
def test_disk_layout(self):
686
format = self.get_format()
687
repo = self.make_repository('.', format=format)
688
# in case of side effects of locking.
691
t = repo.bzrdir.get_repository_transport(None)
693
# XXX: no locks left when unlocked at the moment
694
# self.assertEqualDiff('', t.get('lock').read())
695
self.check_databases(t)
697
def check_format(self, t):
698
self.assertEqualDiff(
699
"Bazaar pack repository format 1 (needs bzr 0.92)\n",
700
t.get('format').read())
702
def assertHasNoKndx(self, t, knit_name):
703
"""Assert that knit_name has no index on t."""
704
self.assertFalse(t.has(knit_name + '.kndx'))
706
def assertHasNoKnit(self, t, knit_name):
707
"""Assert that knit_name exists on t."""
709
self.assertFalse(t.has(knit_name + '.knit'))
711
def check_databases(self, t):
712
"""check knit content for a repository."""
713
# check conversion worked
714
self.assertHasNoKndx(t, 'inventory')
715
self.assertHasNoKnit(t, 'inventory')
716
self.assertHasNoKndx(t, 'revisions')
717
self.assertHasNoKnit(t, 'revisions')
718
self.assertHasNoKndx(t, 'signatures')
719
self.assertHasNoKnit(t, 'signatures')
720
self.assertFalse(t.has('knits'))
721
# revision-indexes file-container directory
723
list(GraphIndex(t, 'pack-names', None).iter_all_entries()))
724
self.assertTrue(S_ISDIR(t.stat('packs').st_mode))
725
self.assertTrue(S_ISDIR(t.stat('upload').st_mode))
726
self.assertTrue(S_ISDIR(t.stat('indices').st_mode))
727
self.assertTrue(S_ISDIR(t.stat('obsolete_packs').st_mode))
729
def test_shared_disk_layout(self):
730
format = self.get_format()
731
repo = self.make_repository('.', shared=True, format=format)
733
t = repo.bzrdir.get_repository_transport(None)
735
# XXX: no locks left when unlocked at the moment
736
# self.assertEqualDiff('', t.get('lock').read())
737
# We should have a 'shared-storage' marker file.
738
self.assertEqualDiff('', t.get('shared-storage').read())
739
self.check_databases(t)
741
def test_shared_no_tree_disk_layout(self):
742
format = self.get_format()
743
repo = self.make_repository('.', shared=True, format=format)
744
repo.set_make_working_trees(False)
746
t = repo.bzrdir.get_repository_transport(None)
748
# XXX: no locks left when unlocked at the moment
749
# self.assertEqualDiff('', t.get('lock').read())
750
# We should have a 'shared-storage' marker file.
751
self.assertEqualDiff('', t.get('shared-storage').read())
752
# We should have a marker for the no-working-trees flag.
753
self.assertEqualDiff('', t.get('no-working-trees').read())
754
# The marker should go when we toggle the setting.
755
repo.set_make_working_trees(True)
756
self.assertFalse(t.has('no-working-trees'))
757
self.check_databases(t)
759
def test_adding_revision_creates_pack_indices(self):
760
format = self.get_format()
761
tree = self.make_branch_and_tree('.', format=format)
762
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
764
list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
765
tree.commit('foobarbaz')
766
index = GraphIndex(trans, 'pack-names', None)
767
index_nodes = list(index.iter_all_entries())
768
self.assertEqual(1, len(index_nodes))
769
node = index_nodes[0]
771
# the pack sizes should be listed in the index
773
sizes = [int(digits) for digits in pack_value.split(' ')]
774
for size, suffix in zip(sizes, ['.rix', '.iix', '.tix', '.six']):
775
stat = trans.stat('indices/%s%s' % (name, suffix))
776
self.assertEqual(size, stat.st_size)
778
def test_pulling_nothing_leads_to_no_new_names(self):
779
format = self.get_format()
780
tree1 = self.make_branch_and_tree('1', format=format)
781
tree2 = self.make_branch_and_tree('2', format=format)
782
tree1.branch.repository.fetch(tree2.branch.repository)
783
trans = tree1.branch.repository.bzrdir.get_repository_transport(None)
785
list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
787
def test_commit_across_pack_shape_boundary_autopacks(self):
788
format = self.get_format()
789
tree = self.make_branch_and_tree('.', format=format)
790
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
791
# This test could be a little cheaper by replacing the packs
792
# attribute on the repository to allow a different pack distribution
793
# and max packs policy - so we are checking the policy is honoured
794
# in the test. But for now 11 commits is not a big deal in a single
797
tree.commit('commit %s' % x)
798
# there should be 9 packs:
799
index = GraphIndex(trans, 'pack-names', None)
800
self.assertEqual(9, len(list(index.iter_all_entries())))
801
# insert some files in obsolete_packs which should be removed by pack.
802
trans.put_bytes('obsolete_packs/foo', '123')
803
trans.put_bytes('obsolete_packs/bar', '321')
804
# committing one more should coalesce to 1 of 10.
805
tree.commit('commit triggering pack')
806
index = GraphIndex(trans, 'pack-names', None)
807
self.assertEqual(1, len(list(index.iter_all_entries())))
808
# packing should not damage data
809
tree = tree.bzrdir.open_workingtree()
810
check_result = tree.branch.repository.check(
811
[tree.branch.last_revision()])
812
# We should have 50 (10x5) files in the obsolete_packs directory.
813
obsolete_files = list(trans.list_dir('obsolete_packs'))
814
self.assertFalse('foo' in obsolete_files)
815
self.assertFalse('bar' in obsolete_files)
816
self.assertEqual(50, len(obsolete_files))
817
# XXX: Todo check packs obsoleted correctly - old packs and indices
818
# in the obsolete_packs directory.
819
large_pack_name = list(index.iter_all_entries())[0][1][0]
820
# finally, committing again should not touch the large pack.
821
tree.commit('commit not triggering pack')
822
index = GraphIndex(trans, 'pack-names', None)
823
self.assertEqual(2, len(list(index.iter_all_entries())))
824
pack_names = [node[1][0] for node in index.iter_all_entries()]
825
self.assertTrue(large_pack_name in pack_names)
827
def test_fail_obsolete_deletion(self):
828
# failing to delete obsolete packs is not fatal
829
format = self.get_format()
830
server = fakenfs.FakeNFSServer()
832
self.addCleanup(server.tearDown)
833
transport = get_transport(server.get_url())
834
bzrdir = self.get_format().initialize_on_transport(transport)
835
repo = bzrdir.create_repository()
836
repo_transport = bzrdir.get_repository_transport(None)
837
self.assertTrue(repo_transport.has('obsolete_packs'))
838
# these files are in use by another client and typically can't be deleted
839
repo_transport.put_bytes('obsolete_packs/.nfsblahblah', 'contents')
840
repo._pack_collection._clear_obsolete_packs()
841
self.assertTrue(repo_transport.has('obsolete_packs/.nfsblahblah'))
843
def test_pack_after_two_commits_packs_everything(self):
844
format = self.get_format()
845
tree = self.make_branch_and_tree('.', format=format)
846
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
848
tree.commit('more work')
849
tree.branch.repository.pack()
850
# there should be 1 pack:
851
index = GraphIndex(trans, 'pack-names', None)
852
self.assertEqual(1, len(list(index.iter_all_entries())))
853
self.assertEqual(2, len(tree.branch.repository.all_revision_ids()))
855
def test_pack_layout(self):
856
format = self.get_format()
857
tree = self.make_branch_and_tree('.', format=format)
858
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
859
tree.commit('start', rev_id='1')
860
tree.commit('more work', rev_id='2')
861
tree.branch.repository.pack()
863
self.addCleanup(tree.unlock)
864
pack = tree.branch.repository._pack_collection.get_pack_by_name(
865
tree.branch.repository._pack_collection.names()[0])
866
# revision access tends to be tip->ancestor, so ordering that way on
867
# disk is a good idea.
868
for _1, key, val, refs in pack.revision_index.iter_all_entries():
870
pos_1 = int(val[1:].split()[0])
872
pos_2 = int(val[1:].split()[0])
873
self.assertTrue(pos_2 < pos_1)
875
def test_pack_repositories_support_multiple_write_locks(self):
876
format = self.get_format()
877
self.make_repository('.', shared=True, format=format)
878
r1 = repository.Repository.open('.')
879
r2 = repository.Repository.open('.')
881
self.addCleanup(r1.unlock)
885
def _add_text(self, repo, fileid):
886
"""Add a text to the repository within a write group."""
887
repo.texts.add_lines((fileid, 'samplerev+'+fileid), [], [])
889
def test_concurrent_writers_merge_new_packs(self):
890
format = self.get_format()
891
self.make_repository('.', shared=True, format=format)
892
r1 = repository.Repository.open('.')
893
r2 = repository.Repository.open('.')
896
# access enough data to load the names list
897
list(r1.all_revision_ids())
900
# access enough data to load the names list
901
list(r2.all_revision_ids())
902
r1.start_write_group()
904
r2.start_write_group()
906
self._add_text(r1, 'fileidr1')
907
self._add_text(r2, 'fileidr2')
909
r2.abort_write_group()
912
r1.abort_write_group()
914
# both r1 and r2 have open write groups with data in them
915
# created while the other's write group was open.
916
# Commit both which requires a merge to the pack-names.
918
r1.commit_write_group()
920
r1.abort_write_group()
921
r2.abort_write_group()
923
r2.commit_write_group()
924
# tell r1 to reload from disk
925
r1._pack_collection.reset()
926
# Now both repositories should know about both names
927
r1._pack_collection.ensure_loaded()
928
r2._pack_collection.ensure_loaded()
929
self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
930
self.assertEqual(2, len(r1._pack_collection.names()))
936
def test_concurrent_writer_second_preserves_dropping_a_pack(self):
937
format = self.get_format()
938
self.make_repository('.', shared=True, format=format)
939
r1 = repository.Repository.open('.')
940
r2 = repository.Repository.open('.')
944
r1.start_write_group()
946
self._add_text(r1, 'fileidr1')
948
r1.abort_write_group()
951
r1.commit_write_group()
952
r1._pack_collection.ensure_loaded()
953
name_to_drop = r1._pack_collection.all_packs()[0].name
958
# access enough data to load the names list
959
list(r1.all_revision_ids())
962
# access enough data to load the names list
963
list(r2.all_revision_ids())
964
r1._pack_collection.ensure_loaded()
966
r2.start_write_group()
968
# in r1, drop the pack
969
r1._pack_collection._remove_pack_from_memory(
970
r1._pack_collection.get_pack_by_name(name_to_drop))
972
self._add_text(r2, 'fileidr2')
974
r2.abort_write_group()
977
r1._pack_collection.reset()
979
# r1 has a changed names list, and r2 an open write groups with
981
# save r1, and then commit the r2 write group, which requires a
982
# merge to the pack-names, which should not reinstate
985
r1._pack_collection._save_pack_names()
986
r1._pack_collection.reset()
988
r2.abort_write_group()
991
r2.commit_write_group()
993
r2.abort_write_group()
995
# Now both repositories should now about just one name.
996
r1._pack_collection.ensure_loaded()
997
r2._pack_collection.ensure_loaded()
998
self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
999
self.assertEqual(1, len(r1._pack_collection.names()))
1000
self.assertFalse(name_to_drop in r1._pack_collection.names())
1006
def test_lock_write_does_not_physically_lock(self):
1007
repo = self.make_repository('.', format=self.get_format())
1009
self.addCleanup(repo.unlock)
1010
self.assertFalse(repo.get_physical_lock_status())
1012
def prepare_for_break_lock(self):
1013
# Setup the global ui factory state so that a break-lock method call
1014
# will find usable input in the input stream.
1015
old_factory = bzrlib.ui.ui_factory
1016
def restoreFactory():
1017
bzrlib.ui.ui_factory = old_factory
1018
self.addCleanup(restoreFactory)
1019
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1020
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1022
def test_break_lock_breaks_physical_lock(self):
1023
repo = self.make_repository('.', format=self.get_format())
1024
repo._pack_collection.lock_names()
1025
repo2 = repository.Repository.open('.')
1026
self.assertTrue(repo.get_physical_lock_status())
1027
self.prepare_for_break_lock()
1029
self.assertFalse(repo.get_physical_lock_status())
1031
def test_broken_physical_locks_error_on__unlock_names_lock(self):
1032
repo = self.make_repository('.', format=self.get_format())
1033
repo._pack_collection.lock_names()
1034
self.assertTrue(repo.get_physical_lock_status())
1035
repo2 = repository.Repository.open('.')
1036
self.prepare_for_break_lock()
1038
self.assertRaises(errors.LockBroken, repo._pack_collection._unlock_names)
1040
def test_fetch_without_find_ghosts_ignores_ghosts(self):
1041
# we want two repositories at this point:
1042
# one with a revision that is a ghost in the other
1044
# 'ghost' is present in has_ghost, 'ghost' is absent in 'missing_ghost'.
1045
# 'references' is present in both repositories, and 'tip' is present
1046
# just in has_ghost.
1047
# has_ghost missing_ghost
1048
#------------------------------
1050
# 'references' 'references'
1052
# In this test we fetch 'tip' which should not fetch 'ghost'
1053
has_ghost = self.make_repository('has_ghost', format=self.get_format())
1054
missing_ghost = self.make_repository('missing_ghost',
1055
format=self.get_format())
1057
def add_commit(repo, revision_id, parent_ids):
1059
repo.start_write_group()
1060
inv = inventory.Inventory(revision_id=revision_id)
1061
inv.root.revision = revision_id
1062
root_id = inv.root.file_id
1063
sha1 = repo.add_inventory(revision_id, inv, [])
1064
repo.texts.add_lines((root_id, revision_id), [], [])
1065
rev = bzrlib.revision.Revision(timestamp=0,
1067
committer="Foo Bar <foo@example.com>",
1069
inventory_sha1=sha1,
1070
revision_id=revision_id)
1071
rev.parent_ids = parent_ids
1072
repo.add_revision(revision_id, rev)
1073
repo.commit_write_group()
1075
add_commit(has_ghost, 'ghost', [])
1076
add_commit(has_ghost, 'references', ['ghost'])
1077
add_commit(missing_ghost, 'references', ['ghost'])
1078
add_commit(has_ghost, 'tip', ['references'])
1079
missing_ghost.fetch(has_ghost, 'tip')
1080
# missing ghost now has tip and not ghost.
1081
rev = missing_ghost.get_revision('tip')
1082
inv = missing_ghost.get_inventory('tip')
1083
self.assertRaises(errors.NoSuchRevision,
1084
missing_ghost.get_revision, 'ghost')
1085
self.assertRaises(errors.NoSuchRevision,
1086
missing_ghost.get_inventory, 'ghost')
1088
def test_supports_external_lookups(self):
1089
repo = self.make_repository('.', format=self.get_format())
1090
self.assertFalse(repo._format.supports_external_lookups)
1093
class TestKnitPackSubtrees(TestKnitPackNoSubtrees):
1095
def get_format(self):
1096
return bzrdir.format_registry.make_bzrdir(
1097
'pack-0.92-subtree')
1099
def check_format(self, t):
1100
self.assertEqualDiff(
1101
"Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n",
1102
t.get('format').read())
1105
class TestDevelopment0(TestKnitPackNoSubtrees):
1107
def get_format(self):
1108
return bzrdir.format_registry.make_bzrdir(
1111
def check_format(self, t):
1112
self.assertEqualDiff(
1113
"Bazaar development format 0 (needs bzr.dev from before 1.3)\n",
1114
t.get('format').read())
1117
class TestDevelopment0Subtree(TestKnitPackNoSubtrees):
1119
def get_format(self):
1120
return bzrdir.format_registry.make_bzrdir(
1121
'development-subtree')
1123
def check_format(self, t):
1124
self.assertEqualDiff(
1125
"Bazaar development format 0 with subtree support "
1126
"(needs bzr.dev from before 1.3)\n",
1127
t.get('format').read())
1130
class TestRepositoryPackCollection(TestCaseWithTransport):
1132
def get_format(self):
1133
return bzrdir.format_registry.make_bzrdir('pack-0.92')
1135
def test__max_pack_count(self):
1136
"""The maximum pack count is a function of the number of revisions."""
1137
format = self.get_format()
1138
repo = self.make_repository('.', format=format)
1139
packs = repo._pack_collection
1140
# no revisions - one pack, so that we can have a revision free repo
1141
# without it blowing up
1142
self.assertEqual(1, packs._max_pack_count(0))
1143
# after that the sum of the digits, - check the first 1-9
1144
self.assertEqual(1, packs._max_pack_count(1))
1145
self.assertEqual(2, packs._max_pack_count(2))
1146
self.assertEqual(3, packs._max_pack_count(3))
1147
self.assertEqual(4, packs._max_pack_count(4))
1148
self.assertEqual(5, packs._max_pack_count(5))
1149
self.assertEqual(6, packs._max_pack_count(6))
1150
self.assertEqual(7, packs._max_pack_count(7))
1151
self.assertEqual(8, packs._max_pack_count(8))
1152
self.assertEqual(9, packs._max_pack_count(9))
1153
# check the boundary cases with two digits for the next decade
1154
self.assertEqual(1, packs._max_pack_count(10))
1155
self.assertEqual(2, packs._max_pack_count(11))
1156
self.assertEqual(10, packs._max_pack_count(19))
1157
self.assertEqual(2, packs._max_pack_count(20))
1158
self.assertEqual(3, packs._max_pack_count(21))
1159
# check some arbitrary big numbers
1160
self.assertEqual(25, packs._max_pack_count(112894))
1162
def test_pack_distribution_zero(self):
1163
format = self.get_format()
1164
repo = self.make_repository('.', format=format)
1165
packs = repo._pack_collection
1166
self.assertEqual([0], packs.pack_distribution(0))
1168
def test_ensure_loaded_unlocked(self):
1169
format = self.get_format()
1170
repo = self.make_repository('.', format=format)
1171
self.assertRaises(errors.ObjectNotLocked,
1172
repo._pack_collection.ensure_loaded)
1174
def test_pack_distribution_one_to_nine(self):
1175
format = self.get_format()
1176
repo = self.make_repository('.', format=format)
1177
packs = repo._pack_collection
1178
self.assertEqual([1],
1179
packs.pack_distribution(1))
1180
self.assertEqual([1, 1],
1181
packs.pack_distribution(2))
1182
self.assertEqual([1, 1, 1],
1183
packs.pack_distribution(3))
1184
self.assertEqual([1, 1, 1, 1],
1185
packs.pack_distribution(4))
1186
self.assertEqual([1, 1, 1, 1, 1],
1187
packs.pack_distribution(5))
1188
self.assertEqual([1, 1, 1, 1, 1, 1],
1189
packs.pack_distribution(6))
1190
self.assertEqual([1, 1, 1, 1, 1, 1, 1],
1191
packs.pack_distribution(7))
1192
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
1193
packs.pack_distribution(8))
1194
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
1195
packs.pack_distribution(9))
1197
def test_pack_distribution_stable_at_boundaries(self):
1198
"""When there are multi-rev packs the counts are stable."""
1199
format = self.get_format()
1200
repo = self.make_repository('.', format=format)
1201
packs = repo._pack_collection
1203
self.assertEqual([10], packs.pack_distribution(10))
1204
self.assertEqual([10, 1], packs.pack_distribution(11))
1205
self.assertEqual([10, 10], packs.pack_distribution(20))
1206
self.assertEqual([10, 10, 1], packs.pack_distribution(21))
1208
self.assertEqual([100], packs.pack_distribution(100))
1209
self.assertEqual([100, 1], packs.pack_distribution(101))
1210
self.assertEqual([100, 10, 1], packs.pack_distribution(111))
1211
self.assertEqual([100, 100], packs.pack_distribution(200))
1212
self.assertEqual([100, 100, 1], packs.pack_distribution(201))
1213
self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
1215
def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
1216
format = self.get_format()
1217
repo = self.make_repository('.', format=format)
1218
packs = repo._pack_collection
1219
existing_packs = [(2000, "big"), (9, "medium")]
1220
# rev count - 2009 -> 2x1000 + 9x1
1221
pack_operations = packs.plan_autopack_combinations(
1222
existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1223
self.assertEqual([], pack_operations)
1225
def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
1226
format = self.get_format()
1227
repo = self.make_repository('.', format=format)
1228
packs = repo._pack_collection
1229
existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
1230
# rev count - 2010 -> 2x1000 + 1x10
1231
pack_operations = packs.plan_autopack_combinations(
1232
existing_packs, [1000, 1000, 10])
1233
self.assertEqual([], pack_operations)
1235
def test_plan_pack_operations_2010_combines_smallest_two(self):
1236
format = self.get_format()
1237
repo = self.make_repository('.', format=format)
1238
packs = repo._pack_collection
1239
existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
1241
# rev count - 2010 -> 2x1000 + 1x10 (3)
1242
pack_operations = packs.plan_autopack_combinations(
1243
existing_packs, [1000, 1000, 10])
1244
self.assertEqual([[2, ["single2", "single1"]], [0, []]], pack_operations)
1246
def test_all_packs_none(self):
1247
format = self.get_format()
1248
tree = self.make_branch_and_tree('.', format=format)
1250
self.addCleanup(tree.unlock)
1251
packs = tree.branch.repository._pack_collection
1252
packs.ensure_loaded()
1253
self.assertEqual([], packs.all_packs())
1255
def test_all_packs_one(self):
1256
format = self.get_format()
1257
tree = self.make_branch_and_tree('.', format=format)
1258
tree.commit('start')
1260
self.addCleanup(tree.unlock)
1261
packs = tree.branch.repository._pack_collection
1262
packs.ensure_loaded()
1264
packs.get_pack_by_name(packs.names()[0])],
1267
def test_all_packs_two(self):
1268
format = self.get_format()
1269
tree = self.make_branch_and_tree('.', format=format)
1270
tree.commit('start')
1271
tree.commit('continue')
1273
self.addCleanup(tree.unlock)
1274
packs = tree.branch.repository._pack_collection
1275
packs.ensure_loaded()
1277
packs.get_pack_by_name(packs.names()[0]),
1278
packs.get_pack_by_name(packs.names()[1]),
1279
], packs.all_packs())
1281
def test_get_pack_by_name(self):
1282
format = self.get_format()
1283
tree = self.make_branch_and_tree('.', format=format)
1284
tree.commit('start')
1286
self.addCleanup(tree.unlock)
1287
packs = tree.branch.repository._pack_collection
1288
packs.ensure_loaded()
1289
name = packs.names()[0]
1290
pack_1 = packs.get_pack_by_name(name)
1291
# the pack should be correctly initialised
1292
rev_index = GraphIndex(packs._index_transport, name + '.rix',
1293
packs._names[name][0])
1294
inv_index = GraphIndex(packs._index_transport, name + '.iix',
1295
packs._names[name][1])
1296
txt_index = GraphIndex(packs._index_transport, name + '.tix',
1297
packs._names[name][2])
1298
sig_index = GraphIndex(packs._index_transport, name + '.six',
1299
packs._names[name][3])
1300
self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
1301
name, rev_index, inv_index, txt_index, sig_index), pack_1)
1302
# and the same instance should be returned on successive calls.
1303
self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1306
class TestPack(TestCaseWithTransport):
1307
"""Tests for the Pack object."""
1309
def assertCurrentlyEqual(self, left, right):
1310
self.assertTrue(left == right)
1311
self.assertTrue(right == left)
1312
self.assertFalse(left != right)
1313
self.assertFalse(right != left)
1315
def assertCurrentlyNotEqual(self, left, right):
1316
self.assertFalse(left == right)
1317
self.assertFalse(right == left)
1318
self.assertTrue(left != right)
1319
self.assertTrue(right != left)
1321
def test___eq____ne__(self):
1322
left = pack_repo.ExistingPack('', '', '', '', '', '')
1323
right = pack_repo.ExistingPack('', '', '', '', '', '')
1324
self.assertCurrentlyEqual(left, right)
1325
# change all attributes and ensure equality changes as we do.
1326
left.revision_index = 'a'
1327
self.assertCurrentlyNotEqual(left, right)
1328
right.revision_index = 'a'
1329
self.assertCurrentlyEqual(left, right)
1330
left.inventory_index = 'a'
1331
self.assertCurrentlyNotEqual(left, right)
1332
right.inventory_index = 'a'
1333
self.assertCurrentlyEqual(left, right)
1334
left.text_index = 'a'
1335
self.assertCurrentlyNotEqual(left, right)
1336
right.text_index = 'a'
1337
self.assertCurrentlyEqual(left, right)
1338
left.signature_index = 'a'
1339
self.assertCurrentlyNotEqual(left, right)
1340
right.signature_index = 'a'
1341
self.assertCurrentlyEqual(left, right)
1343
self.assertCurrentlyNotEqual(left, right)
1345
self.assertCurrentlyEqual(left, right)
1346
left.transport = 'a'
1347
self.assertCurrentlyNotEqual(left, right)
1348
right.transport = 'a'
1349
self.assertCurrentlyEqual(left, right)
1351
def test_file_name(self):
1352
pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
1353
self.assertEqual('a_name.pack', pack.file_name())
1356
class TestNewPack(TestCaseWithTransport):
1357
"""Tests for pack_repo.NewPack."""
1359
def test_new_instance_attributes(self):
1360
upload_transport = self.get_transport('upload')
1361
pack_transport = self.get_transport('pack')
1362
index_transport = self.get_transport('index')
1363
upload_transport.mkdir('.')
1364
pack = pack_repo.NewPack(upload_transport, index_transport,
1366
self.assertIsInstance(pack.revision_index, InMemoryGraphIndex)
1367
self.assertIsInstance(pack.inventory_index, InMemoryGraphIndex)
1368
self.assertIsInstance(pack._hash, type(md5.new()))
1369
self.assertTrue(pack.upload_transport is upload_transport)
1370
self.assertTrue(pack.index_transport is index_transport)
1371
self.assertTrue(pack.pack_transport is pack_transport)
1372
self.assertEqual(None, pack.index_sizes)
1373
self.assertEqual(20, len(pack.random_name))
1374
self.assertIsInstance(pack.random_name, str)
1375
self.assertIsInstance(pack.start_time, float)
1378
class TestPacker(TestCaseWithTransport):
1379
"""Tests for the packs repository Packer class."""
1381
# To date, this class has been factored out and nothing new added to it;
1382
# thus there are not yet any tests.
1385
class TestInterDifferingSerializer(TestCaseWithTransport):
1387
def test_progress_bar(self):
1388
tree = self.make_branch_and_tree('tree')
1389
tree.commit('rev1', rev_id='rev-1')
1390
tree.commit('rev2', rev_id='rev-2')
1391
tree.commit('rev3', rev_id='rev-3')
1392
repo = self.make_repository('repo')
1393
inter_repo = repository.InterDifferingSerializer(
1394
tree.branch.repository, repo)
1395
pb = progress.InstrumentedProgress(to_file=StringIO())
1396
pb.never_throttle = True
1397
inter_repo.fetch('rev-1', pb)
1398
self.assertEqual('Transferring revisions', pb.last_msg)
1399
self.assertEqual(1, pb.last_cnt)
1400
self.assertEqual(1, pb.last_total)
1401
inter_repo.fetch('rev-3', pb)
1402
self.assertEqual(2, pb.last_cnt)
1403
self.assertEqual(2, pb.last_total)