/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
1
# Copyright (C) 2007 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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Test operations that check the repository for corruption"""
19
20
21
from bzrlib import (
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
22
    errors,
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
23
    inventory,
24
    revision as _mod_revision,
25
    )
26
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
27
from bzrlib.tests.repository_implementations import TestCaseWithRepository
28
from bzrlib.tests import TestNotApplicable
29
30
31
class TestFindInconsistentRevisionParents(TestCaseWithRepository):
32
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
33
    def setUp(self):
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
34
        if not isinstance(self.repository_format, RepositoryFormatKnit):
35
            # XXX: This could happen to weaves too, but they're pretty
36
            # deprecated.
37
            raise TestNotApplicable(
38
                "%s isn't a knit format" % self.repository_format)
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
39
        TestCaseWithRepository.setUp(self)
40
41
    def make_repo_with_extra_ghost_index(self):
42
        """Make a corrupt repository.
43
        
44
        It will contain one revision, 'revision-id'.  The knit index will claim
45
        that it has one parent, 'incorrect-parent', but the revision text will
46
        claim it has no parents.`
47
48
        Note: only the *cache* of the knit index is corrupted.  Thus the
49
        corruption will only last while the repository is locked.  For this
50
        reason, the returned repo is (read) locked.
51
        """
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
52
        repo = self.make_repository('broken')
53
        inv = inventory.Inventory(revision_id='revision-id')
54
        inv.root.revision = 'revision-id'
55
        repo.add_inventory('revision-id', inv, [])
56
        revision = _mod_revision.Revision('revision-id',
57
            committer='jrandom@example.com', timestamp=0,
58
            inventory_sha1='', timezone=0, message='message', parent_ids=[])
59
        repo.add_revision('revision-id',revision, inv)
60
61
        # Change the knit index's record of the parents for 'revision-id' to
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
62
        # claim it has a parent, 'incorrect-parent', that doesn't exist in this
63
        # knit at all.
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
64
        repo.lock_read()
65
        rev_knit = repo._get_revision_vf()
66
        index_cache = rev_knit._index._cache
67
        cached_index_entry = list(index_cache['revision-id'])
68
        cached_index_entry[4] = ['incorrect-parent']
69
        index_cache['revision-id'] = tuple(cached_index_entry)
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
70
        return repo
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
71
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
72
    def test__find_inconsistent_revision_parents(self):
73
        """_find_inconsistent_revision_parents finds revisions with broken
74
        parents.
75
        """
76
        repo = self.make_repo_with_extra_ghost_index()
2819.2.1 by Andrew Bennetts
Implement KnitRepository._find_inconsistent_revision_parents.
77
        self.assertEqual(
78
            [('revision-id', ['incorrect-parent'], [])],
79
            list(repo._find_inconsistent_revision_parents()))
80
        repo.unlock()
81
2819.2.2 by Andrew Bennetts
Implement _check_for_inconsistent_revision_parents.
82
    def test__check_for_inconsistent_revision_parents(self):
83
        """_check_for_inconsistent_revision_parents raises BzrCheckError if
84
        there are any revisions with inconsistent parents.
85
        """
86
        repo = self.make_repo_with_extra_ghost_index()
87
        self.assertRaises(
88
            errors.BzrCheckError,
89
            repo._check_for_inconsistent_revision_parents)
90
        repo.unlock()
91
92
    def test__check_for_inconsistent_revision_parents_on_clean_repo(self):
93
        """_check_for_inconsistent_revision_parents does nothing if there are
94
        no broken revisions.
95
        """
96
        repo = self.make_repository('empty-repo')
97
        repo._check_for_inconsistent_revision_parents()  # nothing happens
98