~bzr-pqm/bzr/bzr.dev

3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
16
3389.2.6 by John Arbash Meinel
minor fix to test file comment
17
"""Tests for branch implementations - test check() functionality"""
3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
18
4332.3.3 by Robert Collins
Alter Branch.check to log errors rather than raising.
19
from StringIO import StringIO
20
21
from bzrlib import errors, tests, ui
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
22
from bzrlib.symbol_versioning import deprecated_in
4523.1.1 by Martin Pool
Rename tests.branch_implementations to per_branch
23
from bzrlib.tests.per_branch import TestCaseWithBranch
3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
24
25
26
class TestBranchCheck(TestCaseWithBranch):
27
28
    def test_check_detects_invalid_revhistory(self):
29
        # Different formats have different ways of handling invalid revision
30
        # histories, so the setup portion is customized
31
        tree = self.make_branch_and_tree('test')
32
        r1 = tree.commit('one')
33
        r2 = tree.commit('two')
34
        r3 = tree.commit('three')
35
        r4 = tree.commit('four')
36
        # create an alternate branch
37
        tree.set_parent_ids([r1])
38
        tree.branch.set_last_revision_info(1, r1)
39
        r2b = tree.commit('two-b')
40
41
        # now go back and merge the commit
42
        tree.set_parent_ids([r4, r2b])
43
        tree.branch.set_last_revision_info(4, r4)
44
45
        r5 = tree.commit('five')
46
        # Now, try to set an invalid history
47
        try:
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
48
            self.applyDeprecated(deprecated_in((2, 4, 0)),
49
                tree.branch.set_revision_history, [r1, r2b, r5])
3489.2.4 by Andrew Bennetts
Fix all tests broken by fixing make_branch_and_tree.
50
            if tree.branch.last_revision_info() != (3, r5):
51
                # RemoteBranch silently corrects an impossible revision
52
                # history given to set_revision_history.  It can be tricked
53
                # with set_last_revision_info though.
54
                tree.branch.set_last_revision_info(3, r5)
3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
55
        except errors.NotLefthandHistory:
56
            # Branch5 allows set_revision_history to be wrong
57
            # Branch6 raises NotLefthandHistory, but we can force bogus stuff
58
            # with set_last_revision_info
59
            tree.branch.set_last_revision_info(3, r5)
60
4332.3.7 by Robert Collins
Convert Branch.check to take a refs dict as well.
61
        tree.lock_read()
62
        self.addCleanup(tree.unlock)
63
        refs = self.make_refs(tree.branch)
64
        result = tree.branch.check(refs)
4332.3.3 by Robert Collins
Alter Branch.check to log errors rather than raising.
65
        ui.ui_factory = tests.TestUIFactory(stdout=StringIO())
66
        result.report_results(True)
67
        self.assertContainsRe('revno does not match len',
68
            ui.ui_factory.stdout.getvalue())
3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
69
70
    def test_check_branch_report_results(self):
71
        """Checking a branch produces results which can be printed"""
72
        branch = self.make_branch('.')
4332.3.7 by Robert Collins
Convert Branch.check to take a refs dict as well.
73
        branch.lock_read()
74
        self.addCleanup(branch.unlock)
75
        result = branch.check(self.make_refs(branch))
3389.2.5 by John Arbash Meinel
Fix the line endings on test_check and test_reconcile
76
        # reports results through logging
77
        result.report_results(verbose=True)
78
        result.report_results(verbose=False)
79
4332.3.5 by Robert Collins
Add Branch._get_check_refs.
80
    def test__get_check_refs(self):
81
        tree = self.make_branch_and_tree('.')
82
        revid = tree.commit('foo')
83
        self.assertEqual(
84
            set([('revision-existence', revid), ('lefthand-distance', revid)]),
85
            set(tree.branch._get_check_refs()))
4332.3.7 by Robert Collins
Convert Branch.check to take a refs dict as well.
86
87
    def make_refs(self, branch):
88
        needed_refs = branch._get_check_refs()
89
        refs = {}
90
        distances = set()
91
        existences = set()
92
        for ref in needed_refs:
93
            kind, value = ref
94
            if kind == 'lefthand-distance':
95
                distances.add(value)
96
            elif kind == 'revision-existence':
97
                existences.add(value)
98
            else:
99
                raise AssertionError(
100
                    'unknown ref kind for ref %s' % ref)
101
        node_distances = branch.repository.get_graph().find_lefthand_distances(
102
            distances)
103
        for key, distance in node_distances.iteritems():
104
            refs[('lefthand-distance', key)] = distance
105
            if key in existences and distance > 0:
106
                refs[('revision-existence', key)] = True
107
                existences.remove(key)
108
        parent_map = branch.repository.get_graph().get_parent_map(existences)
109
        for key in parent_map:
110
            refs[('revision-existence', key)] = True
111
            existences.remove(key)
112
        for key in existences:
113
            refs[('revision-existence', key)] = False
114
        return refs