17
17
"""Tests for branch implementations - test check() functionality"""
19
from bzrlib import errors
20
from bzrlib.tests.branch_implementations import TestCaseWithBranch
19
from StringIO import StringIO
21
from bzrlib import errors, tests, ui
22
from bzrlib.tests.per_branch import TestCaseWithBranch
23
25
class TestBranchCheck(TestCaseWithBranch):
54
56
# with set_last_revision_info
55
57
tree.branch.set_last_revision_info(3, r5)
57
e = self.assertRaises(errors.BzrCheckError,
59
self.assertEqual('Internal check failed:'
60
' revno does not match len(mainline) 3 != 5', str(e))
60
self.addCleanup(tree.unlock)
61
refs = self.make_refs(tree.branch)
62
result = tree.branch.check(refs)
63
ui.ui_factory = tests.TestUIFactory(stdout=StringIO())
64
result.report_results(True)
65
self.assertContainsRe('revno does not match len',
66
ui.ui_factory.stdout.getvalue())
62
68
def test_check_branch_report_results(self):
63
69
"""Checking a branch produces results which can be printed"""
64
70
branch = self.make_branch('.')
65
result = branch.check()
72
self.addCleanup(branch.unlock)
73
result = branch.check(self.make_refs(branch))
66
74
# reports results through logging
67
75
result.report_results(verbose=True)
68
76
result.report_results(verbose=False)
70
def test_check_detects_ghosts_in_mainline(self):
71
tree = self.make_branch_and_tree('test')
72
tree.set_parent_ids(['thisisaghost'], allow_leftmost_as_ghost=True)
73
r1 = tree.commit('one')
74
r2 = tree.commit('two')
75
result = tree.branch.check()
76
self.assertEquals(True, result.ghosts_in_mainline)
78
def test__get_check_refs(self):
79
tree = self.make_branch_and_tree('.')
80
revid = tree.commit('foo')
82
set([('revision-existence', revid), ('lefthand-distance', revid)]),
83
set(tree.branch._get_check_refs()))
85
def make_refs(self, branch):
86
needed_refs = branch._get_check_refs()
90
for ref in needed_refs:
92
if kind == 'lefthand-distance':
94
elif kind == 'revision-existence':
98
'unknown ref kind for ref %s' % ref)
99
node_distances = branch.repository.get_graph().find_lefthand_distances(
101
for key, distance in node_distances.iteritems():
102
refs[('lefthand-distance', key)] = distance
103
if key in existences and distance > 0:
104
refs[('revision-existence', key)] = True
105
existences.remove(key)
106
parent_map = branch.repository.get_graph().get_parent_map(existences)
107
for key in parent_map:
108
refs[('revision-existence', key)] = True
109
existences.remove(key)
110
for key in existences:
111
refs[('revision-existence', key)] = False