~greatmay12/+junk/test1

« back to all changes in this revision

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

  • Committer: thitipong at ndrsolution
  • Date: 2011-11-14 06:31:02 UTC
  • Revision ID: thitipong@ndrsolution.com-20111114063102-9obte3yfi2azku7d
ndr redirect version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Helper classes for repository implementation tests."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import (
 
22
    inventory,
 
23
    osutils,
 
24
    revision as _mod_revision,
 
25
    )
 
26
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
 
27
from bzrlib.tests.per_repository import TestCaseWithRepository
 
28
from bzrlib.tests import TestNotApplicable
 
29
 
 
30
 
 
31
class TestCaseWithBrokenRevisionIndex(TestCaseWithRepository):
 
32
 
 
33
    def make_repo_with_extra_ghost_index(self):
 
34
        """Make a corrupt repository.
 
35
 
 
36
        It will contain one revision, 'revision-id'.  The knit index will claim
 
37
        that it has one parent, 'incorrect-parent', but the revision text will
 
38
        claim it has no parents.
 
39
 
 
40
        Note: only the *cache* of the knit index is corrupted.  Thus the
 
41
        corruption will only last while the repository is locked.  For this
 
42
        reason, the returned repo is locked.
 
43
        """
 
44
        if not isinstance(self.repository_format, RepositoryFormatKnit):
 
45
            # XXX: Broken revision graphs can happen to weaves too, but they're
 
46
            # pretty deprecated.  Ideally these tests should apply to any repo
 
47
            # where repo.revision_graph_can_have_wrong_parents() is True, but
 
48
            # at the moment we only know how to corrupt knit repos.
 
49
            raise TestNotApplicable(
 
50
                "%s isn't a knit format" % self.repository_format)
 
51
 
 
52
        repo = self.make_repository('broken')
 
53
        repo.lock_write()
 
54
        repo.start_write_group()
 
55
        try:
 
56
            inv = inventory.Inventory(revision_id='revision-id')
 
57
            inv.root.revision = 'revision-id'
 
58
            inv_sha1 = repo.add_inventory('revision-id', inv, [])
 
59
            if repo.supports_rich_root():
 
60
                root_id = inv.root.file_id
 
61
                repo.texts.add_lines((root_id, 'revision-id'), [], [])
 
62
            revision = _mod_revision.Revision('revision-id',
 
63
                committer='jrandom@example.com', timestamp=0,
 
64
                inventory_sha1=inv_sha1, timezone=0, message='message',
 
65
                parent_ids=[])
 
66
            # Manually add the revision text using the RevisionStore API, with
 
67
            # bad parents.
 
68
            rev_text = repo._serializer.write_revision_to_string(revision)
 
69
            repo.revisions.add_lines((revision.revision_id,),
 
70
                [('incorrect-parent',)],
 
71
                osutils.split_lines(rev_text))
 
72
        except:
 
73
            repo.abort_write_group()
 
74
            repo.unlock()
 
75
            raise
 
76
        else:
 
77
            repo.commit_write_group()
 
78
            repo.unlock()
 
79
 
 
80
        repo.lock_write()
 
81
        self.addCleanup(repo.unlock)
 
82
        return repo
 
83