~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_branch/test_check.py

merge 2.0 branch rev 4647

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for branch implementations - test check() functionality"""
18
18
 
19
 
from bzrlib import errors
20
 
from bzrlib.tests.branch_implementations import TestCaseWithBranch
 
19
from StringIO import StringIO
 
20
 
 
21
from bzrlib import errors, tests, ui
 
22
from bzrlib.tests.per_branch import TestCaseWithBranch
21
23
 
22
24
 
23
25
class TestBranchCheck(TestCaseWithBranch):
54
56
            # with set_last_revision_info
55
57
            tree.branch.set_last_revision_info(3, r5)
56
58
 
57
 
        e = self.assertRaises(errors.BzrCheckError,
58
 
                              tree.branch.check)
59
 
        self.assertEqual('Internal check failed:'
60
 
                         ' revno does not match len(mainline) 3 != 5', str(e))
 
59
        tree.lock_read()
 
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())
61
67
 
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()
 
71
        branch.lock_read()
 
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)
69
77
 
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')
 
81
        self.assertEqual(
 
82
            set([('revision-existence', revid), ('lefthand-distance', revid)]),
 
83
            set(tree.branch._get_check_refs()))
77
84
 
 
85
    def make_refs(self, branch):
 
86
        needed_refs = branch._get_check_refs()
 
87
        refs = {}
 
88
        distances = set()
 
89
        existences = set()
 
90
        for ref in needed_refs:
 
91
            kind, value = ref
 
92
            if kind == 'lefthand-distance':
 
93
                distances.add(value)
 
94
            elif kind == 'revision-existence':
 
95
                existences.add(value)
 
96
            else:
 
97
                raise AssertionError(
 
98
                    'unknown ref kind for ref %s' % ref)
 
99
        node_distances = branch.repository.get_graph().find_lefthand_distances(
 
100
            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
 
112
        return refs