23
23
from bzrlib.errors import KnitError, RevisionAlreadyPresent, NoSuchFile
24
24
from bzrlib.knit import (
27
28
KnitAnnotateFactory,
29
30
from bzrlib.osutils import split_lines
30
from bzrlib.tests import TestCaseWithTransport
31
from bzrlib.tests import TestCase, TestCaseWithTransport
31
32
from bzrlib.transport import TransportLogger, get_transport
32
33
from bzrlib.transport.memory import MemoryTransport
33
34
from bzrlib.weave import Weave
37
class KnitContentTests(TestCase):
39
def test_constructor(self):
40
content = KnitContent([])
43
content = KnitContent([])
44
self.assertEqual(content.text(), [])
46
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
47
self.assertEqual(content.text(), ["text1", "text2"])
49
def test_annotate(self):
50
content = KnitContent([])
51
self.assertEqual(content.annotate(), [])
53
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
54
self.assertEqual(content.annotate(),
55
[("origin1", "text1"), ("origin2", "text2")])
57
def test_annotate_iter(self):
58
content = KnitContent([])
59
it = content.annotate_iter()
60
self.assertRaises(StopIteration, it.next)
62
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
63
it = content.annotate_iter()
64
self.assertEqual(it.next(), ("origin1", "text1"))
65
self.assertEqual(it.next(), ("origin2", "text2"))
66
self.assertRaises(StopIteration, it.next)
69
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
71
self.assertIsInstance(copy, KnitContent)
72
self.assertEqual(copy.annotate(),
73
[("origin1", "text1"), ("origin2", "text2")])
75
def test_line_delta(self):
76
content1 = KnitContent([("", "a"), ("", "b")])
77
content2 = KnitContent([("", "a"), ("", "a"), ("", "c")])
78
self.assertEqual(content1.line_delta(content2),
79
[(1, 2, 2, [("", "a"), ("", "c")])])
81
def test_line_delta_iter(self):
82
content1 = KnitContent([("", "a"), ("", "b")])
83
content2 = KnitContent([("", "a"), ("", "a"), ("", "c")])
84
it = content1.line_delta_iter(content2)
85
self.assertEqual(it.next(), (1, 2, 2, [("", "a"), ("", "c")]))
86
self.assertRaises(StopIteration, it.next)
36
88
class KnitTests(TestCaseWithTransport):
37
89
"""Class containing knit test helper routines."""
630
682
text = k.get_text('text-1')
631
683
self.assertEqual(TEXT_1, text)
632
684
self.assertEqual({}, k._data._cache)
687
class TestKnitIndex(KnitTests):
689
def test_add_versions_dictionary_compresses(self):
690
"""Adding versions to the index should update the lookup dict"""
691
knit = self.make_test_knit()
693
idx.add_version('a-1', ['fulltext'], 0, 0, [])
694
self.check_file_contents('test.kndx',
695
'# bzr knit index 8\n'
699
idx.add_versions([('a-2', ['fulltext'], 0, 0, ['a-1']),
700
('a-3', ['fulltext'], 0, 0, ['a-2']),
702
self.check_file_contents('test.kndx',
703
'# bzr knit index 8\n'
705
'a-1 fulltext 0 0 :\n'
706
'a-2 fulltext 0 0 0 :\n'
707
'a-3 fulltext 0 0 1 :'
709
self.assertEqual(['a-1', 'a-2', 'a-3'], idx._history)
710
self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, [], 0),
711
'a-2':('a-2', ['fulltext'], 0, 0, ['a-1'], 1),
712
'a-3':('a-3', ['fulltext'], 0, 0, ['a-2'], 2),
715
def test_add_versions_fails_clean(self):
716
"""If add_versions fails in the middle, it restores a pristine state.
718
Any modifications that are made to the index are reset if all versions
721
# This cheats a little bit by passing in a generator which will
722
# raise an exception before the processing finishes
723
# Other possibilities would be to have an version with the wrong number
724
# of entries, or to make the backing transport unable to write any
727
knit = self.make_test_knit()
729
idx.add_version('a-1', ['fulltext'], 0, 0, [])
731
class StopEarly(Exception):
734
def generate_failure():
735
"""Add some entries and then raise an exception"""
736
yield ('a-2', ['fulltext'], 0, 0, ['a-1'])
737
yield ('a-3', ['fulltext'], 0, 0, ['a-2'])
740
# Assert the pre-condition
741
self.assertEqual(['a-1'], idx._history)
742
self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, [], 0)}, idx._cache)
744
self.assertRaises(StopEarly, idx.add_versions, generate_failure())
746
# And it shouldn't be modified
747
self.assertEqual(['a-1'], idx._history)
748
self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, [], 0)}, idx._cache)