21
21
# TODO: might be nice to create a versionedfile with some type of corruption
22
22
# considered typical and check that it can be detected/corrected.
24
from itertools import chain, izip
25
from StringIO import StringIO
24
from gzip import GzipFile
29
29
graph as _mod_graph,
36
from bzrlib.errors import (
37
from ..errors import (
37
38
RevisionNotPresent,
38
39
RevisionAlreadyPresent,
41
from bzrlib.knit import (
48
from bzrlib.tests import (
46
from ..sixish import (
50
52
TestCaseWithMemoryTransport,
54
split_suite_by_condition,
57
from bzrlib.tests.http_utils import TestCaseWithWebserver
58
from bzrlib.trace import mutter
59
from bzrlib.transport import get_transport
60
from bzrlib.transport.memory import MemoryTransport
61
from bzrlib.tsort import topo_sort
62
from bzrlib.tuned_gzip import GzipFile
63
import bzrlib.versionedfile as versionedfile
64
from bzrlib.versionedfile import (
56
from .http_utils import TestCaseWithWebserver
57
from ..transport.memory import MemoryTransport
58
from .. import versionedfile as versionedfile
59
from ..versionedfile import (
66
61
HashEscapedPrefixMapper,
68
63
VirtualVersionedFiles,
69
64
make_versioned_files_factory,
71
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)
66
from ..weave import WeaveFile
67
from ..weavefile import write_weave
68
from .scenarios import load_tests_apply_scenarios
71
load_tests = load_tests_apply_scenarios
169
74
def get_diamond_vf(f, trailing_eol=True, left_only=False):
314
219
self.assertTrue('r0' in versions)
315
220
self.assertTrue('r1' in versions)
316
221
self.assertTrue('r2' in versions)
317
self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
318
self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
319
self.assertEquals(f.get_lines('r2'), ['c\n', 'd\n'])
222
self.assertEqual(f.get_lines('r0'), ['a\n', 'b\n'])
223
self.assertEqual(f.get_lines('r1'), ['b\n', 'c\n'])
224
self.assertEqual(f.get_lines('r2'), ['c\n', 'd\n'])
320
225
self.assertEqual(3, f.num_versions())
321
226
origins = f.annotate('r1')
322
self.assertEquals(origins[0][0], 'r0')
323
self.assertEquals(origins[1][0], 'r1')
227
self.assertEqual(origins[0][0], 'r0')
228
self.assertEqual(origins[1][0], 'r1')
324
229
origins = f.annotate('r2')
325
self.assertEquals(origins[0][0], 'r1')
326
self.assertEquals(origins[1][0], 'r2')
230
self.assertEqual(origins[0][0], 'r1')
231
self.assertEqual(origins[1][0], 'r2')
329
234
f = self.reopen_file()
844
749
['base', 'a_ghost'],
845
750
['line\n', 'line_b\n', 'line_c\n'])
846
751
origins = vf.annotate('references_ghost')
847
self.assertEquals(('base', 'line\n'), origins[0])
848
self.assertEquals(('base', 'line_b\n'), origins[1])
849
self.assertEquals(('references_ghost', 'line_c\n'), origins[2])
752
self.assertEqual(('base', 'line\n'), origins[0])
753
self.assertEqual(('base', 'line_b\n'), origins[1])
754
self.assertEqual(('references_ghost', 'line_c\n'), origins[2])
851
756
def test_readonly_mode(self):
852
transport = get_transport(self.get_url('.'))
757
t = self.get_transport()
853
758
factory = self.get_factory()
854
vf = factory('id', transport, 0777, create=True, access_mode='w')
855
vf = factory('id', transport, access_mode='r')
759
vf = factory('id', t, 0o777, create=True, access_mode='w')
760
vf = factory('id', t, access_mode='r')
856
761
self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
857
762
self.assertRaises(errors.ReadOnlyError,
858
763
vf.add_lines_with_ghosts,
880
785
class TestWeave(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
882
787
def get_file(self, name='foo'):
883
return WeaveFile(name, get_transport(self.get_url('.')), create=True,
884
get_scope=self.get_transaction)
788
return WeaveFile(name, self.get_transport(),
790
get_scope=self.get_transaction)
886
792
def get_file_corrupted_text(self):
887
w = WeaveFile('foo', get_transport(self.get_url('.')), create=True,
888
get_scope=self.get_transaction)
793
w = WeaveFile('foo', self.get_transport(),
795
get_scope=self.get_transaction)
889
796
w.add_lines('v1', [], ['hello\n'])
890
797
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
999
907
# we should be able to read from http with a versioned file.
1000
908
vf = self.get_file()
1001
909
# try an empty file access
1002
readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
910
readonly_vf = self.get_factory()('foo',
911
transport.get_transport_from_url(self.get_readonly_url('.')))
1003
912
self.assertEqual([], readonly_vf.versions())
914
def test_readonly_http_works_with_feeling(self):
915
# we should be able to read from http with a versioned file.
1004
917
# now with feeling.
1005
918
vf.add_lines('1', [], ['a\n'])
1006
919
vf.add_lines('2', ['1'], ['b\n', 'a\n'])
1007
readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
920
readonly_vf = self.get_factory()('foo',
921
transport.get_transport_from_url(self.get_readonly_url('.')))
1008
922
self.assertEqual(['1', '2'], vf.versions())
923
self.assertEqual(['1', '2'], readonly_vf.versions())
1009
924
for version in readonly_vf.versions():
1010
925
readonly_vf.get_lines(version)
1023
939
class MergeCasesMixin(object):
1025
941
def doMerge(self, base, a, b, mp):
1026
from cStringIO import StringIO
1027
942
from textwrap import dedent
1032
947
w = self.get_file()
1033
w.add_lines('text0', [], map(addcrlf, base))
1034
w.add_lines('text1', ['text0'], map(addcrlf, a))
1035
w.add_lines('text2', ['text0'], map(addcrlf, b))
948
w.add_lines('text0', [], list(map(addcrlf, base)))
949
w.add_lines('text1', ['text0'], list(map(addcrlf, a)))
950
w.add_lines('text2', ['text0'], list(map(addcrlf, b)))
1037
952
self.log_contents(w)
1463
1379
class TestVersionedFiles(TestCaseWithMemoryTransport):
1464
1380
"""Tests for the multiple-file variant of VersionedFile."""
1382
# We want to be sure of behaviour for:
1383
# weaves prefix layout (weave texts)
1384
# individually named weaves (weave inventories)
1385
# annotated knits - prefix|hash|hash-escape layout, we test the third only
1386
# as it is the most complex mapper.
1387
# individually named knits
1388
# individual no-graph knits in packs (signatures)
1389
# individual graph knits in packs (inventories)
1390
# individual graph nocompression knits in packs (revisions)
1391
# plain text knits in packs (texts)
1392
len_one_scenarios = [
1395
'factory':make_versioned_files_factory(WeaveFile,
1396
ConstantMapper('inventory')),
1399
'support_partial_insertion': False,
1403
'factory':make_file_factory(False, ConstantMapper('revisions')),
1406
'support_partial_insertion': False,
1408
('named-nograph-nodelta-knit-pack', {
1409
'cleanup':cleanup_pack_knit,
1410
'factory':make_pack_factory(False, False, 1),
1413
'support_partial_insertion': False,
1415
('named-graph-knit-pack', {
1416
'cleanup':cleanup_pack_knit,
1417
'factory':make_pack_factory(True, True, 1),
1420
'support_partial_insertion': True,
1422
('named-graph-nodelta-knit-pack', {
1423
'cleanup':cleanup_pack_knit,
1424
'factory':make_pack_factory(True, False, 1),
1427
'support_partial_insertion': False,
1429
('groupcompress-nograph', {
1430
'cleanup':groupcompress.cleanup_pack_group,
1431
'factory':groupcompress.make_pack_factory(False, False, 1),
1434
'support_partial_insertion':False,
1437
len_two_scenarios = [
1440
'factory':make_versioned_files_factory(WeaveFile,
1444
'support_partial_insertion': False,
1446
('annotated-knit-escape', {
1448
'factory':make_file_factory(True, HashEscapedPrefixMapper()),
1451
'support_partial_insertion': False,
1453
('plain-knit-pack', {
1454
'cleanup':cleanup_pack_knit,
1455
'factory':make_pack_factory(True, True, 2),
1458
'support_partial_insertion': True,
1461
'cleanup':groupcompress.cleanup_pack_group,
1462
'factory':groupcompress.make_pack_factory(True, False, 1),
1465
'support_partial_insertion':False,
1469
scenarios = len_one_scenarios + len_two_scenarios
1466
1471
def get_versionedfiles(self, relpath='files'):
1467
1472
transport = self.get_transport(relpath)
1468
1473
if relpath != '.':
1480
1485
return ('FileA',) + (suffix,)
1487
def test_add_fallback_implies_without_fallbacks(self):
1488
f = self.get_versionedfiles('files')
1489
if getattr(f, 'add_fallback_versioned_files', None) is None:
1490
raise TestNotApplicable("%s doesn't support fallbacks"
1491
% (f.__class__.__name__,))
1492
g = self.get_versionedfiles('fallback')
1493
key_a = self.get_simple_key('a')
1494
g.add_lines(key_a, [], ['\n'])
1495
f.add_fallback_versioned_files(g)
1496
self.assertTrue(key_a in f.get_parent_map([key_a]))
1497
self.assertFalse(key_a in f.without_fallbacks().get_parent_map([key_a]))
1482
1499
def test_add_lines(self):
1483
1500
f = self.get_versionedfiles()
1484
1501
key0 = self.get_simple_key('r0')
2755
2772
def test_get_sha1s_nonexistent(self):
2756
self.assertEquals({}, self.texts.get_sha1s([("NONEXISTENT",)]))
2773
self.assertEqual({}, self.texts.get_sha1s([("NONEXISTENT",)]))
2758
2775
def test_get_sha1s(self):
2759
2776
self._lines["key"] = ["dataline1", "dataline2"]
2760
self.assertEquals({("key",): osutils.sha_strings(self._lines["key"])},
2777
self.assertEqual({("key",): osutils.sha_strings(self._lines["key"])},
2761
2778
self.texts.get_sha1s([("key",)]))
2763
2780
def test_get_parent_map(self):
2764
2781
self._parent_map = {"G": ("A", "B")}
2765
self.assertEquals({("G",): (("A",),("B",))},
2782
self.assertEqual({("G",): (("A",),("B",))},
2766
2783
self.texts.get_parent_map([("G",), ("L",)]))
2768
2785
def test_get_record_stream(self):
2769
2786
self._lines["A"] = ["FOO", "BAR"]
2770
2787
it = self.texts.get_record_stream([("A",)], "unordered", True)
2772
self.assertEquals("chunked", record.storage_kind)
2773
self.assertEquals("FOOBAR", record.get_bytes_as("fulltext"))
2774
self.assertEquals(["FOO", "BAR"], record.get_bytes_as("chunked"))
2789
self.assertEqual("chunked", record.storage_kind)
2790
self.assertEqual("FOOBAR", record.get_bytes_as("fulltext"))
2791
self.assertEqual(["FOO", "BAR"], record.get_bytes_as("chunked"))
2776
2793
def test_get_record_stream_absent(self):
2777
2794
it = self.texts.get_record_stream([("A",)], "unordered", True)
2779
self.assertEquals("absent", record.storage_kind)
2796
self.assertEqual("absent", record.storage_kind)
2781
2798
def test_iter_lines_added_or_present_in_keys(self):
2782
2799
self._lines["A"] = ["FOO", "BAR"]
2783
2800
self._lines["B"] = ["HEY"]
2784
2801
self._lines["C"] = ["Alberta"]
2785
2802
it = self.texts.iter_lines_added_or_present_in_keys([("A",), ("B",)])
2786
self.assertEquals(sorted([("FOO", "A"), ("BAR", "A"), ("HEY", "B")]),
2803
self.assertEqual(sorted([("FOO", "A"), ("BAR", "A"), ("HEY", "B")]),
2787
2804
sorted(list(it)))