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.index import GraphIndex, InMemoryGraphIndex
36
from bzrlib.repository import RepositoryFormat
37
from bzrlib.smart import server
38
from bzrlib.tests import (
40
TestCaseWithTransport,
43
from bzrlib.transport import get_transport
44
from bzrlib.transport.memory import MemoryServer
45
from bzrlib.util import bencode
51
revision as _mod_revision,
56
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
59
class TestDefaultFormat(TestCase):
61
def test_get_set_default_format(self):
62
old_default = bzrdir.format_registry.get('default')
63
private_default = old_default().repository_format.__class__
64
old_format = repository.RepositoryFormat.get_default_format()
65
self.assertTrue(isinstance(old_format, private_default))
66
def make_sample_bzrdir():
67
my_bzrdir = bzrdir.BzrDirMetaFormat1()
68
my_bzrdir.repository_format = SampleRepositoryFormat()
70
bzrdir.format_registry.remove('default')
71
bzrdir.format_registry.register('sample', make_sample_bzrdir, '')
72
bzrdir.format_registry.set_default('sample')
73
# creating a repository should now create an instrumented dir.
75
# the default branch format is used by the meta dir format
76
# which is not the default bzrdir format at this point
77
dir = bzrdir.BzrDirMetaFormat1().initialize('memory:///')
78
result = dir.create_repository()
79
self.assertEqual(result, 'A bzr repository dir')
81
bzrdir.format_registry.remove('default')
82
bzrdir.format_registry.remove('sample')
83
bzrdir.format_registry.register('default', old_default, '')
84
self.assertIsInstance(repository.RepositoryFormat.get_default_format(),
88
class SampleRepositoryFormat(repository.RepositoryFormat):
91
this format is initializable, unsupported to aid in testing the
92
open and open(unsupported=True) routines.
95
def get_format_string(self):
96
"""See RepositoryFormat.get_format_string()."""
97
return "Sample .bzr repository format."
99
def initialize(self, a_bzrdir, shared=False):
100
"""Initialize a repository in a BzrDir"""
101
t = a_bzrdir.get_repository_transport(self)
102
t.put_bytes('format', self.get_format_string())
103
return 'A bzr repository dir'
105
def is_supported(self):
108
def open(self, a_bzrdir, _found=False):
109
return "opened repository."
112
class TestRepositoryFormat(TestCaseWithTransport):
113
"""Tests for the Repository format detection used by the bzr meta dir facility.BzrBranchFormat facility."""
115
def test_find_format(self):
116
# is the right format object found for a repository?
117
# create a branch with a few known format objects.
118
# this is not quite the same as
119
self.build_tree(["foo/", "bar/"])
120
def check_format(format, url):
121
dir = format._matchingbzrdir.initialize(url)
122
format.initialize(dir)
123
t = get_transport(url)
124
found_format = repository.RepositoryFormat.find_format(dir)
125
self.failUnless(isinstance(found_format, format.__class__))
126
check_format(weaverepo.RepositoryFormat7(), "bar")
128
def test_find_format_no_repository(self):
129
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
130
self.assertRaises(errors.NoRepositoryPresent,
131
repository.RepositoryFormat.find_format,
134
def test_find_format_unknown_format(self):
135
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
136
SampleRepositoryFormat().initialize(dir)
137
self.assertRaises(UnknownFormatError,
138
repository.RepositoryFormat.find_format,
141
def test_register_unregister_format(self):
142
format = SampleRepositoryFormat()
144
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
146
format.initialize(dir)
147
# register a format for it.
148
repository.RepositoryFormat.register_format(format)
149
# which repository.Open will refuse (not supported)
150
self.assertRaises(UnsupportedFormatError, repository.Repository.open, self.get_url())
151
# but open(unsupported) will work
152
self.assertEqual(format.open(dir), "opened repository.")
153
# unregister the format
154
repository.RepositoryFormat.unregister_format(format)
157
class TestFormat6(TestCaseWithTransport):
159
def test_no_ancestry_weave(self):
160
control = bzrdir.BzrDirFormat6().initialize(self.get_url())
161
repo = weaverepo.RepositoryFormat6().initialize(control)
162
# We no longer need to create the ancestry.weave file
163
# since it is *never* used.
164
self.assertRaises(NoSuchFile,
165
control.transport.get,
168
def test_exposed_versioned_files_are_marked_dirty(self):
169
control = bzrdir.BzrDirFormat6().initialize(self.get_url())
170
repo = weaverepo.RepositoryFormat6().initialize(control)
172
inv = repo.get_inventory_weave()
174
self.assertRaises(errors.OutSideTransaction,
175
inv.add_lines, 'foo', [], [])
178
class TestFormat7(TestCaseWithTransport):
180
def test_disk_layout(self):
181
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
182
repo = weaverepo.RepositoryFormat7().initialize(control)
183
# in case of side effects of locking.
187
# format 'Bazaar-NG Repository format 7'
189
# inventory.weave == empty_weave
190
# empty revision-store directory
191
# empty weaves directory
192
t = control.get_repository_transport(None)
193
self.assertEqualDiff('Bazaar-NG Repository format 7',
194
t.get('format').read())
195
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
196
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
197
self.assertEqualDiff('# bzr weave file v5\n'
200
t.get('inventory.weave').read())
202
def test_shared_disk_layout(self):
203
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
204
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
206
# format 'Bazaar-NG Repository format 7'
207
# inventory.weave == empty_weave
208
# empty revision-store directory
209
# empty weaves directory
210
# a 'shared-storage' marker file.
211
# lock is not present when unlocked
212
t = control.get_repository_transport(None)
213
self.assertEqualDiff('Bazaar-NG Repository format 7',
214
t.get('format').read())
215
self.assertEqualDiff('', t.get('shared-storage').read())
216
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
217
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
218
self.assertEqualDiff('# bzr weave file v5\n'
221
t.get('inventory.weave').read())
222
self.assertFalse(t.has('branch-lock'))
224
def test_creates_lockdir(self):
225
"""Make sure it appears to be controlled by a LockDir existence"""
226
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
227
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
228
t = control.get_repository_transport(None)
229
# TODO: Should check there is a 'lock' toplevel directory,
230
# regardless of contents
231
self.assertFalse(t.has('lock/held/info'))
234
self.assertTrue(t.has('lock/held/info'))
236
# unlock so we don't get a warning about failing to do so
239
def test_uses_lockdir(self):
240
"""repo format 7 actually locks on lockdir"""
241
base_url = self.get_url()
242
control = bzrdir.BzrDirMetaFormat1().initialize(base_url)
243
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
244
t = control.get_repository_transport(None)
248
# make sure the same lock is created by opening it
249
repo = repository.Repository.open(base_url)
251
self.assertTrue(t.has('lock/held/info'))
253
self.assertFalse(t.has('lock/held/info'))
255
def test_shared_no_tree_disk_layout(self):
256
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
257
repo = weaverepo.RepositoryFormat7().initialize(control, shared=True)
258
repo.set_make_working_trees(False)
260
# format 'Bazaar-NG Repository format 7'
262
# inventory.weave == empty_weave
263
# empty revision-store directory
264
# empty weaves directory
265
# a 'shared-storage' marker file.
266
t = control.get_repository_transport(None)
267
self.assertEqualDiff('Bazaar-NG Repository format 7',
268
t.get('format').read())
269
## self.assertEqualDiff('', t.get('lock').read())
270
self.assertEqualDiff('', t.get('shared-storage').read())
271
self.assertEqualDiff('', t.get('no-working-trees').read())
272
repo.set_make_working_trees(True)
273
self.assertFalse(t.has('no-working-trees'))
274
self.assertTrue(S_ISDIR(t.stat('revision-store').st_mode))
275
self.assertTrue(S_ISDIR(t.stat('weaves').st_mode))
276
self.assertEqualDiff('# bzr weave file v5\n'
279
t.get('inventory.weave').read())
281
def test_exposed_versioned_files_are_marked_dirty(self):
282
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
283
repo = weaverepo.RepositoryFormat7().initialize(control)
285
inv = repo.get_inventory_weave()
287
self.assertRaises(errors.OutSideTransaction,
288
inv.add_lines, 'foo', [], [])
291
class TestFormatKnit1(TestCaseWithTransport):
293
def test_disk_layout(self):
294
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
295
repo = knitrepo.RepositoryFormatKnit1().initialize(control)
296
# in case of side effects of locking.
300
# format 'Bazaar-NG Knit Repository Format 1'
301
# lock: is a directory
302
# inventory.weave == empty_weave
303
# empty revision-store directory
304
# empty weaves directory
305
t = control.get_repository_transport(None)
306
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
307
t.get('format').read())
308
# XXX: no locks left when unlocked at the moment
309
# self.assertEqualDiff('', t.get('lock').read())
310
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
313
def assertHasKnit(self, t, knit_name):
314
"""Assert that knit_name exists on t."""
315
self.assertEqualDiff('# bzr knit index 8\n',
316
t.get(knit_name + '.kndx').read())
318
self.assertTrue(t.has(knit_name + '.knit'))
320
def check_knits(self, t):
321
"""check knit content for a repository."""
322
self.assertHasKnit(t, 'inventory')
323
self.assertHasKnit(t, 'revisions')
324
self.assertHasKnit(t, 'signatures')
326
def test_shared_disk_layout(self):
327
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
328
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
330
# format 'Bazaar-NG Knit Repository Format 1'
331
# lock: is a directory
332
# inventory.weave == empty_weave
333
# empty revision-store directory
334
# empty weaves directory
335
# a 'shared-storage' marker file.
336
t = control.get_repository_transport(None)
337
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
338
t.get('format').read())
339
# XXX: no locks left when unlocked at the moment
340
# self.assertEqualDiff('', t.get('lock').read())
341
self.assertEqualDiff('', t.get('shared-storage').read())
342
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
345
def test_shared_no_tree_disk_layout(self):
346
control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
347
repo = knitrepo.RepositoryFormatKnit1().initialize(control, shared=True)
348
repo.set_make_working_trees(False)
350
# format 'Bazaar-NG Knit Repository Format 1'
352
# inventory.weave == empty_weave
353
# empty revision-store directory
354
# empty weaves directory
355
# a 'shared-storage' marker file.
356
t = control.get_repository_transport(None)
357
self.assertEqualDiff('Bazaar-NG Knit Repository Format 1',
358
t.get('format').read())
359
# XXX: no locks left when unlocked at the moment
360
# self.assertEqualDiff('', t.get('lock').read())
361
self.assertEqualDiff('', t.get('shared-storage').read())
362
self.assertEqualDiff('', t.get('no-working-trees').read())
363
repo.set_make_working_trees(True)
364
self.assertFalse(t.has('no-working-trees'))
365
self.assertTrue(S_ISDIR(t.stat('knits').st_mode))
368
def test_exposed_versioned_files_are_marked_dirty(self):
369
format = bzrdir.BzrDirMetaFormat1()
370
format.repository_format = knitrepo.RepositoryFormatKnit1()
371
repo = self.make_repository('.', format=format)
373
inv = repo.get_inventory_weave()
375
self.assertRaises(errors.OutSideTransaction,
376
inv.add_lines, 'foo', [], [])
378
def test_deserialise_sets_root_revision(self):
379
"""We must have a inventory.root.revision
381
Old versions of the XML5 serializer did not set the revision_id for
382
the whole inventory. So we grab the one from the expected text. Which
383
is valid when the api is not being abused.
385
repo = self.make_repository('.',
386
format=bzrdir.format_registry.get('knit')())
387
inv_xml = '<inventory format="5">\n</inventory>\n'
388
inv = repo.deserialise_inventory('test-rev-id', inv_xml)
389
self.assertEqual('test-rev-id', inv.root.revision)
391
def test_deserialise_uses_global_revision_id(self):
392
"""If it is set, then we re-use the global revision id"""
393
repo = self.make_repository('.',
394
format=bzrdir.format_registry.get('knit')())
395
inv_xml = ('<inventory format="5" revision_id="other-rev-id">\n'
397
# Arguably, the deserialise_inventory should detect a mismatch, and
398
# raise an error, rather than silently using one revision_id over the
400
inv = repo.deserialise_inventory('test-rev-id', inv_xml)
401
self.assertEqual('other-rev-id', inv.root.revision)
404
class KnitRepositoryStreamTests(test_knit.KnitTests):
405
"""Tests for knitrepo._get_stream_as_bytes."""
407
def test_get_stream_as_bytes(self):
409
k1 = self.make_test_knit()
410
k1.add_lines('text-a', [], test_knit.split_lines(test_knit.TEXT_1))
412
# Serialise it, check the output.
413
bytes = knitrepo._get_stream_as_bytes(k1, ['text-a'])
414
data = bencode.bdecode(bytes)
415
format, record = data
416
self.assertEqual('knit-plain', format)
417
self.assertEqual(['text-a', ['fulltext'], []], record[:3])
418
self.assertRecordContentEqual(k1, 'text-a', record[3])
420
def test_get_stream_as_bytes_all(self):
421
"""Get a serialised data stream for all the records in a knit.
423
Much like test_get_stream_all, except for get_stream_as_bytes.
425
k1 = self.make_test_knit()
426
# Insert the same data as BasicKnitTests.test_knit_join, as they seem
427
# to cover a range of cases (no parents, one parent, multiple parents).
429
('text-a', [], test_knit.TEXT_1),
430
('text-b', ['text-a'], test_knit.TEXT_1),
431
('text-c', [], test_knit.TEXT_1),
432
('text-d', ['text-c'], test_knit.TEXT_1),
433
('text-m', ['text-b', 'text-d'], test_knit.TEXT_1),
435
expected_data_list = [
436
# version, options, parents
437
('text-a', ['fulltext'], []),
438
('text-b', ['line-delta'], ['text-a']),
439
('text-c', ['fulltext'], []),
440
('text-d', ['line-delta'], ['text-c']),
441
('text-m', ['line-delta'], ['text-b', 'text-d']),
443
for version_id, parents, lines in test_data:
444
k1.add_lines(version_id, parents, test_knit.split_lines(lines))
446
bytes = knitrepo._get_stream_as_bytes(
447
k1, ['text-a', 'text-b', 'text-c', 'text-d', 'text-m'])
449
data = bencode.bdecode(bytes)
451
self.assertEqual('knit-plain', format)
453
for expected, actual in zip(expected_data_list, data):
454
expected_version = expected[0]
455
expected_options = expected[1]
456
expected_parents = expected[2]
457
version, options, parents, bytes = actual
458
self.assertEqual(expected_version, version)
459
self.assertEqual(expected_options, options)
460
self.assertEqual(expected_parents, parents)
461
self.assertRecordContentEqual(k1, version, bytes)
464
class DummyRepository(object):
465
"""A dummy repository for testing."""
469
def supports_rich_root(self):
473
class InterDummy(repository.InterRepository):
474
"""An inter-repository optimised code path for DummyRepository.
476
This is for use during testing where we use DummyRepository as repositories
477
so that none of the default regsitered inter-repository classes will
482
def is_compatible(repo_source, repo_target):
483
"""InterDummy is compatible with DummyRepository."""
484
return (isinstance(repo_source, DummyRepository) and
485
isinstance(repo_target, DummyRepository))
488
class TestInterRepository(TestCaseWithTransport):
490
def test_get_default_inter_repository(self):
491
# test that the InterRepository.get(repo_a, repo_b) probes
492
# for a inter_repo class where is_compatible(repo_a, repo_b) returns
493
# true and returns a default inter_repo otherwise.
494
# This also tests that the default registered optimised interrepository
495
# classes do not barf inappropriately when a surprising repository type
497
dummy_a = DummyRepository()
498
dummy_b = DummyRepository()
499
self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
501
def assertGetsDefaultInterRepository(self, repo_a, repo_b):
502
"""Asserts that InterRepository.get(repo_a, repo_b) -> the default.
504
The effective default is now InterSameDataRepository because there is
505
no actual sane default in the presence of incompatible data models.
507
inter_repo = repository.InterRepository.get(repo_a, repo_b)
508
self.assertEqual(repository.InterSameDataRepository,
509
inter_repo.__class__)
510
self.assertEqual(repo_a, inter_repo.source)
511
self.assertEqual(repo_b, inter_repo.target)
513
def test_register_inter_repository_class(self):
514
# test that a optimised code path provider - a
515
# InterRepository subclass can be registered and unregistered
516
# and that it is correctly selected when given a repository
517
# pair that it returns true on for the is_compatible static method
519
dummy_a = DummyRepository()
520
dummy_b = DummyRepository()
521
repo = self.make_repository('.')
522
# hack dummies to look like repo somewhat.
523
dummy_a._serializer = repo._serializer
524
dummy_b._serializer = repo._serializer
525
repository.InterRepository.register_optimiser(InterDummy)
527
# we should get the default for something InterDummy returns False
529
self.assertFalse(InterDummy.is_compatible(dummy_a, repo))
530
self.assertGetsDefaultInterRepository(dummy_a, repo)
531
# and we should get an InterDummy for a pair it 'likes'
532
self.assertTrue(InterDummy.is_compatible(dummy_a, dummy_b))
533
inter_repo = repository.InterRepository.get(dummy_a, dummy_b)
534
self.assertEqual(InterDummy, inter_repo.__class__)
535
self.assertEqual(dummy_a, inter_repo.source)
536
self.assertEqual(dummy_b, inter_repo.target)
538
repository.InterRepository.unregister_optimiser(InterDummy)
539
# now we should get the default InterRepository object again.
540
self.assertGetsDefaultInterRepository(dummy_a, dummy_b)
543
class TestInterWeaveRepo(TestCaseWithTransport):
545
def test_is_compatible_and_registered(self):
546
# InterWeaveRepo is compatible when either side
547
# is a format 5/6/7 branch
548
from bzrlib.repofmt import knitrepo, weaverepo
549
formats = [weaverepo.RepositoryFormat5(),
550
weaverepo.RepositoryFormat6(),
551
weaverepo.RepositoryFormat7()]
552
incompatible_formats = [weaverepo.RepositoryFormat4(),
553
knitrepo.RepositoryFormatKnit1(),
555
repo_a = self.make_repository('a')
556
repo_b = self.make_repository('b')
557
is_compatible = repository.InterWeaveRepo.is_compatible
558
for source in incompatible_formats:
559
# force incompatible left then right
560
repo_a._format = source
561
repo_b._format = formats[0]
562
self.assertFalse(is_compatible(repo_a, repo_b))
563
self.assertFalse(is_compatible(repo_b, repo_a))
564
for source in formats:
565
repo_a._format = source
566
for target in formats:
567
repo_b._format = target
568
self.assertTrue(is_compatible(repo_a, repo_b))
569
self.assertEqual(repository.InterWeaveRepo,
570
repository.InterRepository.get(repo_a,
574
class TestInterRemoteToOther(TestCaseWithTransport):
576
def make_remote_repository(self, path, backing_format=None):
577
"""Make a RemoteRepository object backed by a real repository that will
578
be created at the given path."""
579
self.make_repository(path, format=backing_format)
580
smart_server = server.SmartTCPServer_for_testing()
582
remote_transport = get_transport(smart_server.get_url()).clone(path)
583
self.addCleanup(smart_server.tearDown)
584
remote_bzrdir = bzrdir.BzrDir.open_from_transport(remote_transport)
585
remote_repo = remote_bzrdir.open_repository()
588
def test_is_compatible_same_format(self):
589
"""InterRemoteToOther is compatible with a remote repository and a
590
second repository that have the same format."""
591
local_repo = self.make_repository('local')
592
remote_repo = self.make_remote_repository('remote')
593
is_compatible = repository.InterRemoteToOther.is_compatible
595
is_compatible(remote_repo, local_repo),
596
"InterRemoteToOther(%r, %r) is false" % (remote_repo, local_repo))
598
def test_is_incompatible_different_format(self):
599
local_repo = self.make_repository('local', 'dirstate')
600
remote_repo = self.make_remote_repository('a', 'dirstate-with-subtree')
601
is_compatible = repository.InterRemoteToOther.is_compatible
603
is_compatible(remote_repo, local_repo),
604
"InterRemoteToOther(%r, %r) is true" % (local_repo, remote_repo))
606
def test_is_incompatible_different_format_both_remote(self):
607
remote_repo_a = self.make_remote_repository(
608
'a', 'dirstate-with-subtree')
609
remote_repo_b = self.make_remote_repository('b', 'dirstate')
610
is_compatible = repository.InterRemoteToOther.is_compatible
612
is_compatible(remote_repo_a, remote_repo_b),
613
"InterRemoteToOther(%r, %r) is true"
614
% (remote_repo_a, remote_repo_b))
617
class TestRepositoryConverter(TestCaseWithTransport):
619
def test_convert_empty(self):
620
t = get_transport(self.get_url('.'))
621
t.mkdir('repository')
622
repo_dir = bzrdir.BzrDirMetaFormat1().initialize('repository')
623
repo = weaverepo.RepositoryFormat7().initialize(repo_dir)
624
target_format = knitrepo.RepositoryFormatKnit1()
625
converter = repository.CopyConverter(target_format)
626
pb = bzrlib.ui.ui_factory.nested_progress_bar()
628
converter.convert(repo, pb)
631
repo = repo_dir.open_repository()
632
self.assertTrue(isinstance(target_format, repo._format.__class__))
635
class TestMisc(TestCase):
637
def test_unescape_xml(self):
638
"""We get some kind of error when malformed entities are passed"""
639
self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;')
642
class TestRepositoryFormatKnit3(TestCaseWithTransport):
644
def test_convert(self):
645
"""Ensure the upgrade adds weaves for roots"""
646
format = bzrdir.BzrDirMetaFormat1()
647
format.repository_format = knitrepo.RepositoryFormatKnit1()
648
tree = self.make_branch_and_tree('.', format)
649
tree.commit("Dull commit", rev_id="dull")
650
revision_tree = tree.branch.repository.revision_tree('dull')
651
self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
652
revision_tree.inventory.root.file_id)
653
format = bzrdir.BzrDirMetaFormat1()
654
format.repository_format = knitrepo.RepositoryFormatKnit3()
655
upgrade.Convert('.', format)
656
tree = workingtree.WorkingTree.open('.')
657
revision_tree = tree.branch.repository.revision_tree('dull')
658
revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
659
tree.commit("Another dull commit", rev_id='dull2')
660
revision_tree = tree.branch.repository.revision_tree('dull2')
661
self.assertEqual('dull', revision_tree.inventory.root.revision)
663
def test_exposed_versioned_files_are_marked_dirty(self):
664
format = bzrdir.BzrDirMetaFormat1()
665
format.repository_format = knitrepo.RepositoryFormatKnit3()
666
repo = self.make_repository('.', format=format)
668
inv = repo.get_inventory_weave()
670
self.assertRaises(errors.OutSideTransaction,
671
inv.add_lines, 'foo', [], [])
674
class TestWithBrokenRepo(TestCaseWithTransport):
675
"""These tests seem to be more appropriate as interface tests?"""
677
def make_broken_repository(self):
678
# XXX: This function is borrowed from Aaron's "Reconcile can fix bad
679
# parent references" branch which is due to land in bzr.dev soon. Once
680
# it does, this duplication should be removed.
681
repo = self.make_repository('broken-repo')
685
cleanups.append(repo.unlock)
686
repo.start_write_group()
687
cleanups.append(repo.commit_write_group)
688
# make rev1a: A well-formed revision, containing 'file1'
689
inv = inventory.Inventory(revision_id='rev1a')
690
inv.root.revision = 'rev1a'
691
self.add_file(repo, inv, 'file1', 'rev1a', [])
692
repo.add_inventory('rev1a', inv, [])
693
revision = _mod_revision.Revision('rev1a',
694
committer='jrandom@example.com', timestamp=0,
695
inventory_sha1='', timezone=0, message='foo', parent_ids=[])
696
repo.add_revision('rev1a',revision, inv)
698
# make rev1b, which has no Revision, but has an Inventory, and
700
inv = inventory.Inventory(revision_id='rev1b')
701
inv.root.revision = 'rev1b'
702
self.add_file(repo, inv, 'file1', 'rev1b', [])
703
repo.add_inventory('rev1b', inv, [])
705
# make rev2, with file1 and file2
707
# file1 has 'rev1b' as an ancestor, even though this is not
708
# mentioned by 'rev1a', making it an unreferenced ancestor
709
inv = inventory.Inventory()
710
self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
711
self.add_file(repo, inv, 'file2', 'rev2', [])
712
self.add_revision(repo, 'rev2', inv, ['rev1a'])
714
# make ghost revision rev1c
715
inv = inventory.Inventory()
716
self.add_file(repo, inv, 'file2', 'rev1c', [])
718
# make rev3 with file2
719
# file2 refers to 'rev1c', which is a ghost in this repository, so
720
# file2 cannot have rev1c as its ancestor.
721
inv = inventory.Inventory()
722
self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
723
self.add_revision(repo, 'rev3', inv, ['rev1c'])
726
for cleanup in reversed(cleanups):
729
def add_revision(self, repo, revision_id, inv, parent_ids):
730
inv.revision_id = revision_id
731
inv.root.revision = revision_id
732
repo.add_inventory(revision_id, inv, parent_ids)
733
revision = _mod_revision.Revision(revision_id,
734
committer='jrandom@example.com', timestamp=0, inventory_sha1='',
735
timezone=0, message='foo', parent_ids=parent_ids)
736
repo.add_revision(revision_id,revision, inv)
738
def add_file(self, repo, inv, filename, revision, parents):
739
file_id = filename + '-id'
740
entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
741
entry.revision = revision
744
vf = repo.weave_store.get_weave_or_empty(file_id,
745
repo.get_transaction())
746
vf.add_lines(revision, parents, ['line\n'])
748
def test_insert_from_broken_repo(self):
749
"""Inserting a data stream from a broken repository won't silently
750
corrupt the target repository.
752
broken_repo = self.make_broken_repository()
753
empty_repo = self.make_repository('empty-repo')
754
stream = broken_repo.get_data_stream(['rev1a', 'rev2', 'rev3'])
755
empty_repo.lock_write()
756
self.addCleanup(empty_repo.unlock)
757
empty_repo.start_write_group()
760
errors.KnitCorrupt, empty_repo.insert_data_stream, stream)
762
empty_repo.abort_write_group()
765
class TestKnitPackNoSubtrees(TestCaseWithTransport):
767
def get_format(self):
768
return bzrdir.format_registry.make_bzrdir('knitpack-experimental')
770
def test_disk_layout(self):
771
format = self.get_format()
772
repo = self.make_repository('.', format=format)
773
# in case of side effects of locking.
776
t = repo.bzrdir.get_repository_transport(None)
778
# XXX: no locks left when unlocked at the moment
779
# self.assertEqualDiff('', t.get('lock').read())
780
self.check_databases(t)
782
def check_format(self, t):
783
self.assertEqualDiff(
784
"Bazaar pack repository format 1 (needs bzr 0.92)\n",
785
t.get('format').read())
787
def assertHasKndx(self, t, knit_name):
788
"""Assert that knit_name exists on t."""
789
self.assertEqualDiff('# bzr knit index 8\n',
790
t.get(knit_name + '.kndx').read())
792
def assertHasNoKndx(self, t, knit_name):
793
"""Assert that knit_name has no index on t."""
794
self.assertFalse(t.has(knit_name + '.kndx'))
796
def assertHasNoKnit(self, t, knit_name):
797
"""Assert that knit_name exists on t."""
799
self.assertFalse(t.has(knit_name + '.knit'))
801
def check_databases(self, t):
802
"""check knit content for a repository."""
803
# check conversion worked
804
self.assertHasNoKndx(t, 'inventory')
805
self.assertHasNoKnit(t, 'inventory')
806
self.assertHasNoKndx(t, 'revisions')
807
self.assertHasNoKnit(t, 'revisions')
808
self.assertHasNoKndx(t, 'signatures')
809
self.assertHasNoKnit(t, 'signatures')
810
self.assertFalse(t.has('knits'))
811
# revision-indexes file-container directory
813
list(GraphIndex(t, 'pack-names', None).iter_all_entries()))
814
self.assertTrue(S_ISDIR(t.stat('packs').st_mode))
815
self.assertTrue(S_ISDIR(t.stat('upload').st_mode))
816
self.assertTrue(S_ISDIR(t.stat('indices').st_mode))
817
self.assertTrue(S_ISDIR(t.stat('obsolete_packs').st_mode))
819
def test_shared_disk_layout(self):
820
format = self.get_format()
821
repo = self.make_repository('.', shared=True, format=format)
823
t = repo.bzrdir.get_repository_transport(None)
825
# XXX: no locks left when unlocked at the moment
826
# self.assertEqualDiff('', t.get('lock').read())
827
# We should have a 'shared-storage' marker file.
828
self.assertEqualDiff('', t.get('shared-storage').read())
829
self.check_databases(t)
831
def test_shared_no_tree_disk_layout(self):
832
format = self.get_format()
833
repo = self.make_repository('.', shared=True, format=format)
834
repo.set_make_working_trees(False)
836
t = repo.bzrdir.get_repository_transport(None)
838
# XXX: no locks left when unlocked at the moment
839
# self.assertEqualDiff('', t.get('lock').read())
840
# We should have a 'shared-storage' marker file.
841
self.assertEqualDiff('', t.get('shared-storage').read())
842
# We should have a marker for the no-working-trees flag.
843
self.assertEqualDiff('', t.get('no-working-trees').read())
844
# The marker should go when we toggle the setting.
845
repo.set_make_working_trees(True)
846
self.assertFalse(t.has('no-working-trees'))
847
self.check_databases(t)
849
def test_adding_revision_creates_pack_indices(self):
850
format = self.get_format()
851
tree = self.make_branch_and_tree('.', format=format)
852
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
854
list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
855
tree.commit('foobarbaz')
856
index = GraphIndex(trans, 'pack-names', None)
857
index_nodes = list(index.iter_all_entries())
858
self.assertEqual(1, len(index_nodes))
859
node = index_nodes[0]
861
# the pack sizes should be listed in the index
863
sizes = [int(digits) for digits in pack_value.split(' ')]
864
for size, suffix in zip(sizes, ['.rix', '.iix', '.tix', '.six']):
865
stat = trans.stat('indices/%s%s' % (name, suffix))
866
self.assertEqual(size, stat.st_size)
868
def test_pulling_nothing_leads_to_no_new_names(self):
869
format = self.get_format()
870
tree1 = self.make_branch_and_tree('1', format=format)
871
tree2 = self.make_branch_and_tree('2', format=format)
872
tree1.branch.repository.fetch(tree2.branch.repository)
873
trans = tree1.branch.repository.bzrdir.get_repository_transport(None)
875
list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
877
def test_commit_across_pack_shape_boundary_autopacks(self):
878
format = self.get_format()
879
tree = self.make_branch_and_tree('.', format=format)
880
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
881
# This test could be a little cheaper by replacing the packs
882
# attribute on the repository to allow a different pack distribution
883
# and max packs policy - so we are checking the policy is honoured
884
# in the test. But for now 11 commits is not a big deal in a single
887
tree.commit('commit %s' % x)
888
# there should be 9 packs:
889
index = GraphIndex(trans, 'pack-names', None)
890
self.assertEqual(9, len(list(index.iter_all_entries())))
891
# committing one more should coalesce to 1 of 10.
892
tree.commit('commit triggering pack')
893
index = GraphIndex(trans, 'pack-names', None)
894
self.assertEqual(1, len(list(index.iter_all_entries())))
895
# packing should not damage data
896
tree = tree.bzrdir.open_workingtree()
897
check_result = tree.branch.repository.check(
898
[tree.branch.last_revision()])
899
# XXX: Todo check packs obsoleted correctly - old packs and indices
900
# in the obsolete_packs directory.
901
large_pack_name = list(index.iter_all_entries())[0][1][0]
902
# finally, committing again should not touch the large pack.
903
tree.commit('commit not triggering pack')
904
index = GraphIndex(trans, 'pack-names', None)
905
self.assertEqual(2, len(list(index.iter_all_entries())))
906
pack_names = [node[1][0] for node in index.iter_all_entries()]
907
self.assertTrue(large_pack_name in pack_names)
909
def test_pack_after_two_commits_packs_everything(self):
910
format = self.get_format()
911
tree = self.make_branch_and_tree('.', format=format)
912
trans = tree.branch.repository.bzrdir.get_repository_transport(None)
914
tree.commit('more work')
915
tree.branch.repository.pack()
916
# there should be 1 pack:
917
index = GraphIndex(trans, 'pack-names', None)
918
self.assertEqual(1, len(list(index.iter_all_entries())))
919
self.assertEqual(2, len(tree.branch.repository.all_revision_ids()))
921
def test_pack_repositories_support_multiple_write_locks(self):
922
format = self.get_format()
923
self.make_repository('.', shared=True, format=format)
924
r1 = repository.Repository.open('.')
925
r2 = repository.Repository.open('.')
927
self.addCleanup(r1.unlock)
931
def _add_text(self, repo, fileid):
932
"""Add a text to the repository within a write group."""
933
vf =repo.weave_store.get_weave(fileid, repo.get_transaction())
934
vf.add_lines('samplerev+' + fileid, [], [])
936
def test_concurrent_writers_merge_new_packs(self):
937
format = self.get_format()
938
self.make_repository('.', shared=True, format=format)
939
r1 = repository.Repository.open('.')
940
r2 = repository.Repository.open('.')
943
# access enough data to load the names list
944
list(r1.all_revision_ids())
947
# access enough data to load the names list
948
list(r2.all_revision_ids())
949
r1.start_write_group()
951
r2.start_write_group()
953
self._add_text(r1, 'fileidr1')
954
self._add_text(r2, 'fileidr2')
956
r2.abort_write_group()
959
r1.abort_write_group()
961
# both r1 and r2 have open write groups with data in them
962
# created while the other's write group was open.
963
# Commit both which requires a merge to the pack-names.
965
r1.commit_write_group()
967
r1.abort_write_group()
968
r2.abort_write_group()
970
r2.commit_write_group()
971
# tell r1 to reload from disk
972
r1._pack_collection.reset()
973
# Now both repositories should know about both names
974
r1._pack_collection.ensure_loaded()
975
r2._pack_collection.ensure_loaded()
976
self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
977
self.assertEqual(2, len(r1._pack_collection.names()))
983
def test_concurrent_writer_second_preserves_dropping_a_pack(self):
984
format = self.get_format()
985
self.make_repository('.', shared=True, format=format)
986
r1 = repository.Repository.open('.')
987
r2 = repository.Repository.open('.')
991
r1.start_write_group()
993
self._add_text(r1, 'fileidr1')
995
r1.abort_write_group()
998
r1.commit_write_group()
999
r1._pack_collection.ensure_loaded()
1000
name_to_drop = r1._pack_collection.all_packs()[0].name
1005
# access enough data to load the names list
1006
list(r1.all_revision_ids())
1009
# access enough data to load the names list
1010
list(r2.all_revision_ids())
1011
r1._pack_collection.ensure_loaded()
1013
r2.start_write_group()
1015
# in r1, drop the pack
1016
r1._pack_collection._remove_pack_from_memory(
1017
r1._pack_collection.get_pack_by_name(name_to_drop))
1019
self._add_text(r2, 'fileidr2')
1021
r2.abort_write_group()
1024
r1._pack_collection.reset()
1026
# r1 has a changed names list, and r2 an open write groups with
1028
# save r1, and then commit the r2 write group, which requires a
1029
# merge to the pack-names, which should not reinstate
1032
r1._pack_collection._save_pack_names()
1033
r1._pack_collection.reset()
1035
r2.abort_write_group()
1038
r2.commit_write_group()
1040
r2.abort_write_group()
1042
# Now both repositories should now about just one name.
1043
r1._pack_collection.ensure_loaded()
1044
r2._pack_collection.ensure_loaded()
1045
self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
1046
self.assertEqual(1, len(r1._pack_collection.names()))
1047
self.assertFalse(name_to_drop in r1._pack_collection.names())
1053
def test_lock_write_does_not_physically_lock(self):
1054
repo = self.make_repository('.', format=self.get_format())
1056
self.addCleanup(repo.unlock)
1057
self.assertFalse(repo.get_physical_lock_status())
1059
def prepare_for_break_lock(self):
1060
# Setup the global ui factory state so that a break-lock method call
1061
# will find usable input in the input stream.
1062
old_factory = bzrlib.ui.ui_factory
1063
def restoreFactory():
1064
bzrlib.ui.ui_factory = old_factory
1065
self.addCleanup(restoreFactory)
1066
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1067
bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1069
def test_break_lock_breaks_physical_lock(self):
1070
repo = self.make_repository('.', format=self.get_format())
1071
repo._pack_collection.lock_names()
1072
repo2 = repository.Repository.open('.')
1073
self.assertTrue(repo.get_physical_lock_status())
1074
self.prepare_for_break_lock()
1076
self.assertFalse(repo.get_physical_lock_status())
1078
def test_broken_physical_locks_error_on__unlock_names_lock(self):
1079
repo = self.make_repository('.', format=self.get_format())
1080
repo._pack_collection.lock_names()
1081
self.assertTrue(repo.get_physical_lock_status())
1082
repo2 = repository.Repository.open('.')
1083
self.prepare_for_break_lock()
1085
self.assertRaises(errors.LockBroken, repo._pack_collection._unlock_names)
1087
def test_fetch_without_find_ghosts_ignores_ghosts(self):
1088
# we want two repositories at this point:
1089
# one with a revision that is a ghost in the other
1091
# 'ghost' is present in has_ghost, 'ghost' is absent in 'missing_ghost'.
1092
# 'references' is present in both repositories, and 'tip' is present
1093
# just in has_ghost.
1094
# has_ghost missing_ghost
1095
#------------------------------
1097
# 'references' 'references'
1099
# In this test we fetch 'tip' which should not fetch 'ghost'
1100
has_ghost = self.make_repository('has_ghost', format=self.get_format())
1101
missing_ghost = self.make_repository('missing_ghost',
1102
format=self.get_format())
1104
def add_commit(repo, revision_id, parent_ids):
1106
repo.start_write_group()
1107
inv = inventory.Inventory(revision_id=revision_id)
1108
inv.root.revision = revision_id
1109
root_id = inv.root.file_id
1110
sha1 = repo.add_inventory(revision_id, inv, [])
1111
vf = repo.weave_store.get_weave_or_empty(root_id,
1112
repo.get_transaction())
1113
vf.add_lines(revision_id, [], [])
1114
rev = bzrlib.revision.Revision(timestamp=0,
1116
committer="Foo Bar <foo@example.com>",
1118
inventory_sha1=sha1,
1119
revision_id=revision_id)
1120
rev.parent_ids = parent_ids
1121
repo.add_revision(revision_id, rev)
1122
repo.commit_write_group()
1124
add_commit(has_ghost, 'ghost', [])
1125
add_commit(has_ghost, 'references', ['ghost'])
1126
add_commit(missing_ghost, 'references', ['ghost'])
1127
add_commit(has_ghost, 'tip', ['references'])
1128
missing_ghost.fetch(has_ghost, 'tip')
1129
# missing ghost now has tip and not ghost.
1130
rev = missing_ghost.get_revision('tip')
1131
inv = missing_ghost.get_inventory('tip')
1132
self.assertRaises(errors.NoSuchRevision,
1133
missing_ghost.get_revision, 'ghost')
1134
self.assertRaises(errors.RevisionNotPresent,
1135
missing_ghost.get_inventory, 'ghost')
1138
class TestKnitPackSubtrees(TestKnitPackNoSubtrees):
1140
def get_format(self):
1141
return bzrdir.format_registry.make_bzrdir(
1142
'knitpack-subtree-experimental')
1144
def check_format(self, t):
1145
self.assertEqualDiff(
1146
"Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n",
1147
t.get('format').read())
1150
class TestRepositoryPackCollection(TestCaseWithTransport):
1152
def get_format(self):
1153
return bzrdir.format_registry.make_bzrdir('knitpack-experimental')
1155
def test__max_pack_count(self):
1156
"""The maximum pack count is a function of the number of revisions."""
1157
format = self.get_format()
1158
repo = self.make_repository('.', format=format)
1159
packs = repo._pack_collection
1160
# no revisions - one pack, so that we can have a revision free repo
1161
# without it blowing up
1162
self.assertEqual(1, packs._max_pack_count(0))
1163
# after that the sum of the digits, - check the first 1-9
1164
self.assertEqual(1, packs._max_pack_count(1))
1165
self.assertEqual(2, packs._max_pack_count(2))
1166
self.assertEqual(3, packs._max_pack_count(3))
1167
self.assertEqual(4, packs._max_pack_count(4))
1168
self.assertEqual(5, packs._max_pack_count(5))
1169
self.assertEqual(6, packs._max_pack_count(6))
1170
self.assertEqual(7, packs._max_pack_count(7))
1171
self.assertEqual(8, packs._max_pack_count(8))
1172
self.assertEqual(9, packs._max_pack_count(9))
1173
# check the boundary cases with two digits for the next decade
1174
self.assertEqual(1, packs._max_pack_count(10))
1175
self.assertEqual(2, packs._max_pack_count(11))
1176
self.assertEqual(10, packs._max_pack_count(19))
1177
self.assertEqual(2, packs._max_pack_count(20))
1178
self.assertEqual(3, packs._max_pack_count(21))
1179
# check some arbitrary big numbers
1180
self.assertEqual(25, packs._max_pack_count(112894))
1182
def test_pack_distribution_zero(self):
1183
format = self.get_format()
1184
repo = self.make_repository('.', format=format)
1185
packs = repo._pack_collection
1186
self.assertEqual([0], packs.pack_distribution(0))
1188
def test_pack_distribution_one_to_nine(self):
1189
format = self.get_format()
1190
repo = self.make_repository('.', format=format)
1191
packs = repo._pack_collection
1192
self.assertEqual([1],
1193
packs.pack_distribution(1))
1194
self.assertEqual([1, 1],
1195
packs.pack_distribution(2))
1196
self.assertEqual([1, 1, 1],
1197
packs.pack_distribution(3))
1198
self.assertEqual([1, 1, 1, 1],
1199
packs.pack_distribution(4))
1200
self.assertEqual([1, 1, 1, 1, 1],
1201
packs.pack_distribution(5))
1202
self.assertEqual([1, 1, 1, 1, 1, 1],
1203
packs.pack_distribution(6))
1204
self.assertEqual([1, 1, 1, 1, 1, 1, 1],
1205
packs.pack_distribution(7))
1206
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
1207
packs.pack_distribution(8))
1208
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
1209
packs.pack_distribution(9))
1211
def test_pack_distribution_stable_at_boundaries(self):
1212
"""When there are multi-rev packs the counts are stable."""
1213
format = self.get_format()
1214
repo = self.make_repository('.', format=format)
1215
packs = repo._pack_collection
1217
self.assertEqual([10], packs.pack_distribution(10))
1218
self.assertEqual([10, 1], packs.pack_distribution(11))
1219
self.assertEqual([10, 10], packs.pack_distribution(20))
1220
self.assertEqual([10, 10, 1], packs.pack_distribution(21))
1222
self.assertEqual([100], packs.pack_distribution(100))
1223
self.assertEqual([100, 1], packs.pack_distribution(101))
1224
self.assertEqual([100, 10, 1], packs.pack_distribution(111))
1225
self.assertEqual([100, 100], packs.pack_distribution(200))
1226
self.assertEqual([100, 100, 1], packs.pack_distribution(201))
1227
self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
1229
def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
1230
format = self.get_format()
1231
repo = self.make_repository('.', format=format)
1232
packs = repo._pack_collection
1233
existing_packs = [(2000, "big"), (9, "medium")]
1234
# rev count - 2009 -> 2x1000 + 9x1
1235
pack_operations = packs.plan_autopack_combinations(
1236
existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1237
self.assertEqual([], pack_operations)
1239
def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
1240
format = self.get_format()
1241
repo = self.make_repository('.', format=format)
1242
packs = repo._pack_collection
1243
existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
1244
# rev count - 2010 -> 2x1000 + 1x10
1245
pack_operations = packs.plan_autopack_combinations(
1246
existing_packs, [1000, 1000, 10])
1247
self.assertEqual([], pack_operations)
1249
def test_plan_pack_operations_2010_combines_smallest_two(self):
1250
format = self.get_format()
1251
repo = self.make_repository('.', format=format)
1252
packs = repo._pack_collection
1253
existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
1255
# rev count - 2010 -> 2x1000 + 1x10 (3)
1256
pack_operations = packs.plan_autopack_combinations(
1257
existing_packs, [1000, 1000, 10])
1258
self.assertEqual([[2, ["single2", "single1"]], [0, []]], pack_operations)
1260
def test_all_packs_none(self):
1261
format = self.get_format()
1262
tree = self.make_branch_and_tree('.', format=format)
1264
self.addCleanup(tree.unlock)
1265
packs = tree.branch.repository._pack_collection
1266
packs.ensure_loaded()
1267
self.assertEqual([], packs.all_packs())
1269
def test_all_packs_one(self):
1270
format = self.get_format()
1271
tree = self.make_branch_and_tree('.', format=format)
1272
tree.commit('start')
1274
self.addCleanup(tree.unlock)
1275
packs = tree.branch.repository._pack_collection
1276
packs.ensure_loaded()
1278
packs.get_pack_by_name(packs.names()[0])],
1281
def test_all_packs_two(self):
1282
format = self.get_format()
1283
tree = self.make_branch_and_tree('.', format=format)
1284
tree.commit('start')
1285
tree.commit('continue')
1287
self.addCleanup(tree.unlock)
1288
packs = tree.branch.repository._pack_collection
1289
packs.ensure_loaded()
1291
packs.get_pack_by_name(packs.names()[0]),
1292
packs.get_pack_by_name(packs.names()[1]),
1293
], packs.all_packs())
1295
def test_get_pack_by_name(self):
1296
format = self.get_format()
1297
tree = self.make_branch_and_tree('.', format=format)
1298
tree.commit('start')
1300
self.addCleanup(tree.unlock)
1301
packs = tree.branch.repository._pack_collection
1302
packs.ensure_loaded()
1303
name = packs.names()[0]
1304
pack_1 = packs.get_pack_by_name(name)
1305
# the pack should be correctly initialised
1306
rev_index = GraphIndex(packs._index_transport, name + '.rix',
1307
packs._names[name][0])
1308
inv_index = GraphIndex(packs._index_transport, name + '.iix',
1309
packs._names[name][1])
1310
txt_index = GraphIndex(packs._index_transport, name + '.tix',
1311
packs._names[name][2])
1312
sig_index = GraphIndex(packs._index_transport, name + '.six',
1313
packs._names[name][3])
1314
self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
1315
name, rev_index, inv_index, txt_index, sig_index), pack_1)
1316
# and the same instance should be returned on successive calls.
1317
self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1320
class TestPack(TestCaseWithTransport):
1321
"""Tests for the Pack object."""
1323
def assertCurrentlyEqual(self, left, right):
1324
self.assertTrue(left == right)
1325
self.assertTrue(right == left)
1326
self.assertFalse(left != right)
1327
self.assertFalse(right != left)
1329
def assertCurrentlyNotEqual(self, left, right):
1330
self.assertFalse(left == right)
1331
self.assertFalse(right == left)
1332
self.assertTrue(left != right)
1333
self.assertTrue(right != left)
1335
def test___eq____ne__(self):
1336
left = pack_repo.ExistingPack('', '', '', '', '', '')
1337
right = pack_repo.ExistingPack('', '', '', '', '', '')
1338
self.assertCurrentlyEqual(left, right)
1339
# change all attributes and ensure equality changes as we do.
1340
left.revision_index = 'a'
1341
self.assertCurrentlyNotEqual(left, right)
1342
right.revision_index = 'a'
1343
self.assertCurrentlyEqual(left, right)
1344
left.inventory_index = 'a'
1345
self.assertCurrentlyNotEqual(left, right)
1346
right.inventory_index = 'a'
1347
self.assertCurrentlyEqual(left, right)
1348
left.text_index = 'a'
1349
self.assertCurrentlyNotEqual(left, right)
1350
right.text_index = 'a'
1351
self.assertCurrentlyEqual(left, right)
1352
left.signature_index = 'a'
1353
self.assertCurrentlyNotEqual(left, right)
1354
right.signature_index = 'a'
1355
self.assertCurrentlyEqual(left, right)
1357
self.assertCurrentlyNotEqual(left, right)
1359
self.assertCurrentlyEqual(left, right)
1360
left.transport = 'a'
1361
self.assertCurrentlyNotEqual(left, right)
1362
right.transport = 'a'
1363
self.assertCurrentlyEqual(left, right)
1365
def test_file_name(self):
1366
pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
1367
self.assertEqual('a_name.pack', pack.file_name())
1370
class TestNewPack(TestCaseWithTransport):
1371
"""Tests for pack_repo.NewPack."""
1373
def test_new_instance_attributes(self):
1374
upload_transport = self.get_transport('upload')
1375
pack_transport = self.get_transport('pack')
1376
index_transport = self.get_transport('index')
1377
upload_transport.mkdir('.')
1378
pack = pack_repo.NewPack(upload_transport, index_transport,
1380
self.assertIsInstance(pack.revision_index, InMemoryGraphIndex)
1381
self.assertIsInstance(pack.inventory_index, InMemoryGraphIndex)
1382
self.assertIsInstance(pack._hash, type(md5.new()))
1383
self.assertTrue(pack.upload_transport is upload_transport)
1384
self.assertTrue(pack.index_transport is index_transport)
1385
self.assertTrue(pack.pack_transport is pack_transport)
1386
self.assertEqual(None, pack.index_sizes)
1387
self.assertEqual(20, len(pack.random_name))
1388
self.assertIsInstance(pack.random_name, str)
1389
self.assertIsInstance(pack.start_time, float)