1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests that use BrokenRepoScenario objects.
That is, tests for reconcile and check.
"""
import sha
from bzrlib.inventory import Inventory, InventoryFile
from bzrlib.revision import Revision
from bzrlib.tests import TestNotApplicable
from bzrlib.tests.repository_implementations import TestCaseWithRepository
class TestFileParentReconciliation(TestCaseWithRepository):
"""Tests for how reconcile corrects errors in parents of file versions."""
def make_populated_repository(self, factory):
"""Create a new repository populated by the given factory."""
repo = self.make_repository('broken-repo')
repo.lock_write()
try:
repo.start_write_group()
try:
factory(repo)
repo.commit_write_group()
return repo
except:
repo.abort_write_group()
raise
finally:
repo.unlock()
def add_revision(self, repo, revision_id, inv, parent_ids):
"""Add a revision with a given inventory and parents to a repository.
:param repo: a repository.
:param revision_id: the revision ID for the new revision.
:param inv: an inventory (such as created by
`make_one_file_inventory`).
:param parent_ids: the parents for the new revision.
"""
inv.revision_id = revision_id
inv.root.revision = revision_id
repo.add_inventory(revision_id, inv, parent_ids)
revision = Revision(revision_id, committer='jrandom@example.com',
timestamp=0, inventory_sha1='', timezone=0, message='foo',
parent_ids=parent_ids)
repo.add_revision(revision_id,revision, inv)
def make_one_file_inventory(self, repo, revision, parents,
inv_revision=None, root_revision=None):
"""Make an inventory containing a version of a file with ID 'a-file'.
The file's ID will be 'a-file', and its filename will be 'a file name',
stored at the tree root.
:param repo: a repository to add the new file version to.
:param revision: the revision ID of the new inventory.
:param parents: the parents for this revision of 'a-file'.
:param inv_revision: if not None, the revision ID to store in the
inventory entry. Otherwise, this defaults to revision.
:param root_revision: if not None, the inventory's root.revision will
be set to this.
"""
inv = Inventory(revision_id=revision)
if root_revision is not None:
inv.root.revision = root_revision
file_id = 'a-file-id'
entry = InventoryFile(file_id, 'a file name', 'TREE_ROOT')
if inv_revision is not None:
entry.revision = inv_revision
else:
entry.revision = revision
entry.text_size = 0
file_contents = '%sline\n' % entry.revision
entry.text_sha1 = sha.sha(file_contents).hexdigest()
inv.add(entry)
vf = repo.weave_store.get_weave_or_empty(file_id,
repo.get_transaction())
vf.add_lines(revision, parents, [file_contents])
return inv
def require_repo_suffers_text_parent_corruption(self, repo):
if not repo._reconcile_fixes_text_parents:
raise TestNotApplicable(
"Format does not support text parent reconciliation")
def file_parents(self, repo, revision_id):
return repo.weave_store.get_weave('a-file-id',
repo.get_transaction()).get_parents(revision_id)
def assertParentsMatch(self, expected_parents_for_versions, repo,
when_description):
for expected_parents, version in expected_parents_for_versions:
found_parents = self.file_parents(repo, version)
self.assertEqual(expected_parents, found_parents,
"Expected version %s of a-file-id to have parents %s %s "
"reconcile, but it has %s instead."
% (version, expected_parents, when_description, found_parents))
def shas_for_versions_of_file(self, repo, versions):
"""Get the SHA-1 hashes of the versions of 'a-file' in the repository.
:param repo: the repository to get the hashes from.
:param versions: a list of versions to get hashes for.
:returns: A dict of `{version: hash}`.
"""
vf = repo.weave_store.get_weave('a-file-id', repo.get_transaction())
return dict((v, vf.get_sha1(v)) for v in versions)
def test_reconcile_behaviour(self):
"""Populate a repository and reconcile it, verifying the state before
and after.
"""
scenario = self.scenario_class(self)
repo = self.make_populated_repository(scenario.populate_repository)
self.require_repo_suffers_text_parent_corruption(repo)
self.assertParentsMatch(scenario.populated_parents(), repo, 'before')
vf_shas = self.shas_for_versions_of_file(repo, scenario.all_versions())
result = repo.reconcile(thorough=True)
self.assertParentsMatch(scenario.corrected_parents(), repo, 'after')
# The contents of the versions in the versionedfile should be the same
# after the reconcile.
self.assertEqual(
vf_shas,
self.shas_for_versions_of_file(repo, scenario.all_versions()))
def test_check_behaviour(self):
"""Populate a repository and check it, and verify the output."""
scenario = self.scenario_class(self)
repo = self.make_populated_repository(scenario.populate_repository)
self.require_repo_suffers_text_parent_corruption(repo)
check_result = repo.check()
check_result.report_results(verbose=True)
for pattern in scenario.check_regexes():
self.assertContainsRe(
self._get_log(keep_log_file=True),
pattern)
|