36
38
from bzrlib.errors import (
37
39
RevisionNotPresent,
38
40
RevisionAlreadyPresent,
41
42
from bzrlib.knit import (
48
47
from bzrlib.tests import (
50
49
TestCaseWithMemoryTransport,
54
split_suite_by_condition,
57
53
from bzrlib.tests.http_utils import TestCaseWithWebserver
58
from bzrlib.trace import mutter
59
from bzrlib.transport import get_transport
60
54
from bzrlib.transport.memory import MemoryTransport
61
from bzrlib.tsort import topo_sort
62
from bzrlib.tuned_gzip import GzipFile
63
55
import bzrlib.versionedfile as versionedfile
64
56
from bzrlib.versionedfile import (
69
61
make_versioned_files_factory,
71
63
from bzrlib.weave import WeaveFile
72
from bzrlib.weavefile import read_weave, write_weave
75
def load_tests(standard_tests, module, loader):
76
"""Parameterize VersionedFiles tests for different implementations."""
77
to_adapt, result = split_suite_by_condition(
78
standard_tests, condition_isinstance(TestVersionedFiles))
79
# We want to be sure of behaviour for:
80
# weaves prefix layout (weave texts)
81
# individually named weaves (weave inventories)
82
# annotated knits - prefix|hash|hash-escape layout, we test the third only
83
# as it is the most complex mapper.
84
# individually named knits
85
# individual no-graph knits in packs (signatures)
86
# individual graph knits in packs (inventories)
87
# individual graph nocompression knits in packs (revisions)
88
# plain text knits in packs (texts)
92
'factory':make_versioned_files_factory(WeaveFile,
93
ConstantMapper('inventory')),
96
'support_partial_insertion': False,
100
'factory':make_file_factory(False, ConstantMapper('revisions')),
103
'support_partial_insertion': False,
105
('named-nograph-nodelta-knit-pack', {
106
'cleanup':cleanup_pack_knit,
107
'factory':make_pack_factory(False, False, 1),
110
'support_partial_insertion': False,
112
('named-graph-knit-pack', {
113
'cleanup':cleanup_pack_knit,
114
'factory':make_pack_factory(True, True, 1),
117
'support_partial_insertion': True,
119
('named-graph-nodelta-knit-pack', {
120
'cleanup':cleanup_pack_knit,
121
'factory':make_pack_factory(True, False, 1),
124
'support_partial_insertion': False,
126
('groupcompress-nograph', {
127
'cleanup':groupcompress.cleanup_pack_group,
128
'factory':groupcompress.make_pack_factory(False, False, 1),
131
'support_partial_insertion':False,
134
len_two_scenarios = [
137
'factory':make_versioned_files_factory(WeaveFile,
141
'support_partial_insertion': False,
143
('annotated-knit-escape', {
145
'factory':make_file_factory(True, HashEscapedPrefixMapper()),
148
'support_partial_insertion': False,
150
('plain-knit-pack', {
151
'cleanup':cleanup_pack_knit,
152
'factory':make_pack_factory(True, True, 2),
155
'support_partial_insertion': True,
158
'cleanup':groupcompress.cleanup_pack_group,
159
'factory':groupcompress.make_pack_factory(True, False, 1),
162
'support_partial_insertion':False,
165
scenarios = len_one_scenarios + len_two_scenarios
166
return multiply_tests(to_adapt, scenarios, result)
64
from bzrlib.weavefile import write_weave
65
from bzrlib.tests.scenarios import load_tests_apply_scenarios
68
load_tests = load_tests_apply_scenarios
169
71
def get_diamond_vf(f, trailing_eol=True, left_only=False):
850
751
self.assertEquals(('references_ghost', 'line_c\n'), origins[2])
852
753
def test_readonly_mode(self):
853
transport = get_transport(self.get_url('.'))
754
t = self.get_transport()
854
755
factory = self.get_factory()
855
vf = factory('id', transport, 0777, create=True, access_mode='w')
856
vf = factory('id', transport, access_mode='r')
756
vf = factory('id', t, 0777, create=True, access_mode='w')
757
vf = factory('id', t, access_mode='r')
857
758
self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
858
759
self.assertRaises(errors.ReadOnlyError,
859
760
vf.add_lines_with_ghosts,
881
782
class TestWeave(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
883
784
def get_file(self, name='foo'):
884
return WeaveFile(name, get_transport(self.get_url('.')), create=True,
885
get_scope=self.get_transaction)
785
return WeaveFile(name, self.get_transport(),
787
get_scope=self.get_transaction)
887
789
def get_file_corrupted_text(self):
888
w = WeaveFile('foo', get_transport(self.get_url('.')), create=True,
889
get_scope=self.get_transaction)
790
w = WeaveFile('foo', self.get_transport(),
792
get_scope=self.get_transaction)
890
793
w.add_lines('v1', [], ['hello\n'])
891
794
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
922
825
def reopen_file(self, name='foo', create=False):
923
return WeaveFile(name, get_transport(self.get_url('.')), create=create,
924
get_scope=self.get_transaction)
826
return WeaveFile(name, self.get_transport(),
828
get_scope=self.get_transaction)
926
830
def test_no_implicit_create(self):
927
831
self.assertRaises(errors.NoSuchFile,
930
get_transport(self.get_url('.')),
834
self.get_transport(),
931
835
get_scope=self.get_transaction)
933
837
def get_factory(self):
1000
904
# we should be able to read from http with a versioned file.
1001
905
vf = self.get_file()
1002
906
# try an empty file access
1003
readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
907
readonly_vf = self.get_factory()('foo',
908
transport.get_transport_from_url(self.get_readonly_url('.')))
1004
909
self.assertEqual([], readonly_vf.versions())
911
def test_readonly_http_works_with_feeling(self):
912
# we should be able to read from http with a versioned file.
1005
914
# now with feeling.
1006
915
vf.add_lines('1', [], ['a\n'])
1007
916
vf.add_lines('2', ['1'], ['b\n', 'a\n'])
1008
readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
917
readonly_vf = self.get_factory()('foo',
918
transport.get_transport_from_url(self.get_readonly_url('.')))
1009
919
self.assertEqual(['1', '2'], vf.versions())
920
self.assertEqual(['1', '2'], readonly_vf.versions())
1010
921
for version in readonly_vf.versions():
1011
922
readonly_vf.get_lines(version)
1464
1377
class TestVersionedFiles(TestCaseWithMemoryTransport):
1465
1378
"""Tests for the multiple-file variant of VersionedFile."""
1380
# We want to be sure of behaviour for:
1381
# weaves prefix layout (weave texts)
1382
# individually named weaves (weave inventories)
1383
# annotated knits - prefix|hash|hash-escape layout, we test the third only
1384
# as it is the most complex mapper.
1385
# individually named knits
1386
# individual no-graph knits in packs (signatures)
1387
# individual graph knits in packs (inventories)
1388
# individual graph nocompression knits in packs (revisions)
1389
# plain text knits in packs (texts)
1390
len_one_scenarios = [
1393
'factory':make_versioned_files_factory(WeaveFile,
1394
ConstantMapper('inventory')),
1397
'support_partial_insertion': False,
1401
'factory':make_file_factory(False, ConstantMapper('revisions')),
1404
'support_partial_insertion': False,
1406
('named-nograph-nodelta-knit-pack', {
1407
'cleanup':cleanup_pack_knit,
1408
'factory':make_pack_factory(False, False, 1),
1411
'support_partial_insertion': False,
1413
('named-graph-knit-pack', {
1414
'cleanup':cleanup_pack_knit,
1415
'factory':make_pack_factory(True, True, 1),
1418
'support_partial_insertion': True,
1420
('named-graph-nodelta-knit-pack', {
1421
'cleanup':cleanup_pack_knit,
1422
'factory':make_pack_factory(True, False, 1),
1425
'support_partial_insertion': False,
1427
('groupcompress-nograph', {
1428
'cleanup':groupcompress.cleanup_pack_group,
1429
'factory':groupcompress.make_pack_factory(False, False, 1),
1432
'support_partial_insertion':False,
1435
len_two_scenarios = [
1438
'factory':make_versioned_files_factory(WeaveFile,
1442
'support_partial_insertion': False,
1444
('annotated-knit-escape', {
1446
'factory':make_file_factory(True, HashEscapedPrefixMapper()),
1449
'support_partial_insertion': False,
1451
('plain-knit-pack', {
1452
'cleanup':cleanup_pack_knit,
1453
'factory':make_pack_factory(True, True, 2),
1456
'support_partial_insertion': True,
1459
'cleanup':groupcompress.cleanup_pack_group,
1460
'factory':groupcompress.make_pack_factory(True, False, 1),
1463
'support_partial_insertion':False,
1467
scenarios = len_one_scenarios + len_two_scenarios
1467
1469
def get_versionedfiles(self, relpath='files'):
1468
1470
transport = self.get_transport(relpath)
1469
1471
if relpath != '.':
1470
1472
transport.mkdir('.')
1471
1473
files = self.factory(transport)
1472
1474
if self.cleanup is not None:
1473
self.addCleanup(lambda:self.cleanup(files))
1475
self.addCleanup(self.cleanup, files)
1476
1478
def get_simple_key(self, suffix):
1481
1483
return ('FileA',) + (suffix,)
1485
def test_add_fallback_implies_without_fallbacks(self):
1486
f = self.get_versionedfiles('files')
1487
if getattr(f, 'add_fallback_versioned_files', None) is None:
1488
raise TestNotApplicable("%s doesn't support fallbacks"
1489
% (f.__class__.__name__,))
1490
g = self.get_versionedfiles('fallback')
1491
key_a = self.get_simple_key('a')
1492
g.add_lines(key_a, [], ['\n'])
1493
f.add_fallback_versioned_files(g)
1494
self.assertTrue(key_a in f.get_parent_map([key_a]))
1495
self.assertFalse(key_a in f.without_fallbacks().get_parent_map([key_a]))
1483
1497
def test_add_lines(self):
1484
1498
f = self.get_versionedfiles()
1485
1499
key0 = self.get_simple_key('r0')
1757
1775
self.assertIsInstance(kg, _mod_graph.KnownGraph)
1758
1776
self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1778
def test_known_graph_with_fallbacks(self):
1779
f = self.get_versionedfiles('files')
1781
raise TestNotApplicable('ancestry info only relevant with graph.')
1782
if getattr(f, 'add_fallback_versioned_files', None) is None:
1783
raise TestNotApplicable("%s doesn't support fallbacks"
1784
% (f.__class__.__name__,))
1785
key_a = self.get_simple_key('a')
1786
key_b = self.get_simple_key('b')
1787
key_c = self.get_simple_key('c')
1788
# A only in fallback
1793
g = self.get_versionedfiles('fallback')
1794
g.add_lines(key_a, [], ['\n'])
1795
f.add_fallback_versioned_files(g)
1796
f.add_lines(key_b, [key_a], ['\n'])
1797
f.add_lines(key_c, [key_a, key_b], ['\n'])
1798
kg = f.get_known_graph_ancestry([key_c])
1799
self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1760
1801
def test_get_record_stream_empty(self):
1761
1802
"""An empty stream can be requested without error."""
1762
1803
f = self.get_versionedfiles()
2412
2453
self.assertIdenticalVersionedFile(source, files)
2455
def test_insert_record_stream_long_parent_chain_out_of_order(self):
2456
"""An out of order stream can either error or work."""
2458
raise TestNotApplicable('ancestry info only relevant with graph.')
2459
# Create a reasonably long chain of records based on each other, where
2460
# most will be deltas.
2461
source = self.get_versionedfiles('source')
2464
content = [('same same %d\n' % n) for n in range(500)]
2465
for letter in 'abcdefghijklmnopqrstuvwxyz':
2466
key = ('key-' + letter,)
2467
if self.key_length == 2:
2468
key = ('prefix',) + key
2469
content.append('content for ' + letter + '\n')
2470
source.add_lines(key, parents, content)
2473
# Create a stream of these records, excluding the first record that the
2474
# rest ultimately depend upon, and insert it into a new vf.
2476
for key in reversed(keys):
2477
streams.append(source.get_record_stream([key], 'unordered', False))
2478
deltas = chain(*streams[:-1])
2479
files = self.get_versionedfiles()
2481
files.insert_record_stream(deltas)
2482
except RevisionNotPresent:
2483
# Must not have corrupted the file.
2486
# Must only report either just the first key as a missing parent,
2487
# no key as missing (for nodelta scenarios).
2488
missing = set(files.get_missing_compression_parent_keys())
2489
missing.discard(keys[0])
2490
self.assertEqual(set(), missing)
2414
2492
def get_knit_delta_source(self):
2415
2493
"""Get a source that can produce a stream with knit delta records,
2416
2494
regardless of this test's scenario.