~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
 
18
18
"""Test operations that check the repository for corruption"""
19
19
 
 
20
import os
 
21
 
20
22
from bzrlib import (
 
23
    check,
 
24
    config as _mod_config,
 
25
    errors,
 
26
    inventory,
21
27
    revision as _mod_revision,
22
28
    )
 
29
from bzrlib.tests import TestNotApplicable
23
30
from bzrlib.tests.per_repository import TestCaseWithRepository
 
31
from bzrlib.tests.per_repository.helpers import (
 
32
    TestCaseWithBrokenRevisionIndex,
 
33
    )
 
34
 
 
35
 
 
36
class TestNoSpuriousInconsistentAncestors(TestCaseWithRepository):
 
37
 
 
38
    def test_two_files_different_versions_no_inconsistencies_bug_165071(self):
 
39
        """Two files, with different versions can be clean."""
 
40
        tree = self.make_branch_and_tree('.')
 
41
        self.build_tree(['foo'])
 
42
        tree.smart_add(['.'])
 
43
        revid1 = tree.commit('1')
 
44
        self.build_tree(['bar'])
 
45
        tree.smart_add(['.'])
 
46
        revid2 = tree.commit('2')
 
47
        check_object = tree.branch.repository.check([revid1, revid2])
 
48
        check_object.report_results(verbose=True)
 
49
        self.assertContainsRe(self.get_log(), "0 unreferenced text versions")
 
50
 
 
51
 
 
52
class TestFindInconsistentRevisionParents(TestCaseWithBrokenRevisionIndex):
 
53
 
 
54
    def test__find_inconsistent_revision_parents(self):
 
55
        """_find_inconsistent_revision_parents finds revisions with broken
 
56
        parents.
 
57
        """
 
58
        repo = self.make_repo_with_extra_ghost_index()
 
59
        self.assertEqual(
 
60
            [('revision-id', ('incorrect-parent',), ())],
 
61
            list(repo._find_inconsistent_revision_parents()))
 
62
 
 
63
    def test__check_for_inconsistent_revision_parents(self):
 
64
        """_check_for_inconsistent_revision_parents raises BzrCheckError if
 
65
        there are any revisions with inconsistent parents.
 
66
        """
 
67
        repo = self.make_repo_with_extra_ghost_index()
 
68
        self.assertRaises(
 
69
            errors.BzrCheckError,
 
70
            repo._check_for_inconsistent_revision_parents)
 
71
 
 
72
    def test__check_for_inconsistent_revision_parents_on_clean_repo(self):
 
73
        """_check_for_inconsistent_revision_parents does nothing if there are
 
74
        no broken revisions.
 
75
        """
 
76
        repo = self.make_repository('empty-repo')
 
77
        if not repo.revision_graph_can_have_wrong_parents():
 
78
            raise TestNotApplicable(
 
79
                '%r cannot have corrupt revision index.' % repo)
 
80
        repo.lock_read()
 
81
        try:
 
82
            repo._check_for_inconsistent_revision_parents()  # nothing happens
 
83
        finally:
 
84
            repo.unlock()
 
85
 
 
86
    def test_check_reports_bad_ancestor(self):
 
87
        repo = self.make_repo_with_extra_ghost_index()
 
88
        # XXX: check requires a non-empty revision IDs list, but it ignores the
 
89
        # contents of it!
 
90
        check_object = repo.check(['ignored'])
 
91
        check_object.report_results(verbose=False)
 
92
        self.assertContainsRe(self.get_log(),
 
93
            '1 revisions have incorrect parents in the revision index')
 
94
        check_object.report_results(verbose=True)
 
95
        self.assertContainsRe(
 
96
            self.get_log(),
 
97
            "revision-id has wrong parents in index: "
 
98
            r"\('incorrect-parent',\) should be \(\)")
 
99
 
 
100
 
 
101
class TestCallbacks(TestCaseWithRepository):
 
102
 
 
103
    def test_callback_tree_and_branch(self):
 
104
        # use a real tree to get actual refs that will work
 
105
        tree = self.make_branch_and_tree('foo')
 
106
        revid = tree.commit('foo')
 
107
        tree.lock_read()
 
108
        self.addCleanup(tree.unlock)
 
109
        needed_refs = {}
 
110
        for ref in tree._get_check_refs():
 
111
            needed_refs.setdefault(ref, []).append(tree)
 
112
        for ref in tree.branch._get_check_refs():
 
113
            needed_refs.setdefault(ref, []).append(tree.branch)
 
114
        self.tree_check = tree._check
 
115
        self.branch_check = tree.branch.check
 
116
        tree._check = self.tree_callback
 
117
        tree.branch.check = self.branch_callback
 
118
        self.callbacks = []
 
119
        tree.branch.repository.check([revid], callback_refs=needed_refs)
 
120
        self.assertNotEqual([], self.callbacks)
 
121
 
 
122
    def tree_callback(self, refs):
 
123
        self.callbacks.append(('tree', refs))
 
124
        return self.tree_check(refs)
 
125
 
 
126
    def branch_callback(self, refs):
 
127
        self.callbacks.append(('branch', refs))
 
128
        return self.branch_check(refs)
24
129
 
25
130
 
26
131
class TestCleanRepository(TestCaseWithRepository):
27
132
 
28
133
    def test_new_repo(self):
29
 
        branch = self.make_branch('foo')
30
 
        branch.lock_write()
31
 
        self.addCleanup(branch.unlock)
32
 
        self.overrideEnv('BZR_EMAIL', 'foo@sample.com')
33
 
        builder = branch.get_commit_builder([], branch.get_config_stack())
 
134
        repo = self.make_repository('foo')
 
135
        repo.lock_write()
 
136
        self.addCleanup(repo.unlock)
 
137
        config = _mod_config.Config()
 
138
        os.environ['BZR_EMAIL'] = 'foo@sample.com'
 
139
        builder = repo.get_commit_builder(None, [], config)
34
140
        list(builder.record_iter_changes(None, _mod_revision.NULL_REVISION, [
35
141
            ('TREE_ROOT', (None, ''), True, (False, True), (None, None),
36
142
            (None, ''), (None, 'directory'), (None, False))]))
37
143
        builder.finish_inventory()
38
144
        rev_id = builder.commit('first post')
39
 
        result = branch.repository.check(None, check_repo=True)
 
145
        result = repo.check(None, check_repo=True)
40
146
        result.report_results(True)
41
147
        log = self.get_log()
42
148
        self.assertFalse('Missing' in log, "Something was missing in %r" % log)