1
# Copyright (C) 2007-2010 Canonical Ltd
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.
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.
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
17
"""Tests for Branch.revision_history and last_revision."""
22
revision as _mod_revision,
24
from bzrlib.symbol_versioning import deprecated_in
25
from bzrlib.tests import (
31
class TestLastRevision(per_branch.TestCaseWithBranch):
32
"""Tests for the last_revision property of the branch.
35
def test_set_last_revision_info(self):
36
# based on TestBranch.test_append_revisions, which uses the old
38
wt = self.make_branch_and_tree('tree')
39
wt.commit('f', rev_id='rev1')
40
wt.commit('f', rev_id='rev2')
41
wt.commit('f', rev_id='rev3')
42
br = self.get_branch()
44
br.set_last_revision_info(1, 'rev1')
46
self.applyDeprecated(deprecated_in((2, 5, 0)), br.revision_history),
48
br.set_last_revision_info(3, 'rev3')
50
self.applyDeprecated(deprecated_in((2, 5, 0)), br.revision_history),
51
["rev1", "rev2", "rev3"])
52
# append_revision specifically prohibits some ids;
53
# set_last_revision_info currently does not
54
## self.assertRaises(errors.ReservedId,
55
## br.set_last_revision_info, 4, 'current:')
58
class TestRevisionHistoryCaching(per_branch.TestCaseWithBranch):
59
"""Tests for the caching of branches' revision_history.
61
When locked, branches should avoid regenerating or rereading
62
revision_history by caching the last value of it. This is safe because
63
the branch is locked, so nothing can change the revision_history
66
When not locked, obviously the revision_history will need to be regenerated
69
We test if revision_history is using the cache by instrumenting the branch's
70
_gen_revision_history method, which is called by Branch.revision_history if
71
the branch does not have a cache of the revision history.
74
def get_instrumented_branch(self):
75
"""Get a branch and monkey patch it to log calls to
76
_gen_revision_history.
78
:returns: a tuple of (the branch, list that calls will be logged to)
80
branch = self.get_branch()
82
real_gen_revision_history = branch._gen_revision_history
83
def fake_gen_revision_history():
84
calls.append('_gen_revision_history')
85
return real_gen_revision_history()
86
branch._gen_revision_history = fake_gen_revision_history
89
def test_revision_history_when_unlocked(self):
90
"""Repeated calls to revision history will call _gen_revision_history
91
each time when the branch is not locked.
93
branch, calls = self.get_instrumented_branch()
94
# Repeatedly call revision_history.
95
self.applyDeprecated(deprecated_in((2, 5, 0)), branch.revision_history)
96
self.applyDeprecated(deprecated_in((2, 5, 0)), branch.revision_history)
98
['_gen_revision_history', '_gen_revision_history'], calls)
100
def test_revision_history_when_locked(self):
101
"""Repeated calls to revision history will only call
102
_gen_revision_history once while the branch is locked.
104
branch, calls = self.get_instrumented_branch()
105
# Lock the branch, then repeatedly call revision_history.
108
self.applyDeprecated(deprecated_in((2, 5, 0)),
109
branch.revision_history)
110
self.applyDeprecated(deprecated_in((2, 5, 0)),
111
branch.revision_history)
112
self.assertEqual(['_gen_revision_history'], calls)
116
def test_set_revision_history_when_locked(self):
117
"""When the branch is locked, calling set_revision_history should cache
118
the revision history so that a later call to revision_history will not
119
need to call _gen_revision_history.
121
branch, calls = self.get_instrumented_branch()
122
# Lock the branch, set the revision history, then repeatedly call
125
self.applyDeprecated(deprecated_in((2, 4, 0)),
126
branch.set_revision_history, [])
128
self.applyDeprecated(deprecated_in((2, 5, 0)),
129
branch.revision_history)
130
self.assertEqual([], calls)
134
def test_set_revision_history_when_unlocked(self):
135
"""When the branch is not locked, calling set_revision_history will not
136
cause the revision history to be cached.
138
branch, calls = self.get_instrumented_branch()
139
self.applyDeprecated(deprecated_in((2, 4, 0)),
140
branch.set_revision_history, [])
141
self.applyDeprecated(deprecated_in((2, 5, 0)),
142
branch.revision_history)
143
self.assertEqual(['_gen_revision_history'], calls)
145
def test_set_last_revision_info_when_locked(self):
146
"""When the branch is locked, calling set_last_revision_info should
147
cache the last revision info so that a later call to last_revision_info
148
will not need the revision_history. Thus the branch will not to call
149
_gen_revision_history in this situation.
151
a_branch, calls = self.get_instrumented_branch()
152
# Lock the branch, set the last revision info, then call
153
# last_revision_info.
154
a_branch.lock_write()
155
a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
158
a_branch.last_revision_info()
159
self.assertEqual([], calls)
163
def test_set_last_revision_info_none(self):
164
"""Passing None to set_last_revision_info raises an exception."""
165
a_branch = self.get_branch()
166
# Lock the branch, set the last revision info, then call
167
# last_revision_info.
168
a_branch.lock_write()
169
self.addCleanup(a_branch.unlock)
170
self.assertRaises(errors.InvalidRevisionId,
171
a_branch.set_last_revision_info, 0, None)
173
def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
174
"""On format 6 branches, set_last_revision_info invalidates the revision
177
if not isinstance(self.branch_format, branch.BzrBranchFormat6):
179
a_branch, calls = self.get_instrumented_branch()
180
# Lock the branch, cache the revision history.
181
a_branch.lock_write()
182
self.applyDeprecated(deprecated_in((2, 5, 0)),
183
a_branch.revision_history)
184
# Set the last revision info, clearing the cache.
185
a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
188
self.applyDeprecated(deprecated_in((2, 5, 0)),
189
a_branch.revision_history)
190
self.assertEqual(['_gen_revision_history'], calls)
194
def test_cached_revision_history_not_accidentally_mutable(self):
195
"""When there's a cached version of the history, revision_history
196
returns a copy of the cached data so that callers cannot accidentally
199
branch = self.get_branch()
200
# Lock the branch, then repeatedly call revision_history, mutating the
204
# The first time the data returned will not be in the cache.
205
history = self.applyDeprecated(deprecated_in((2, 5, 0)),
206
branch.revision_history)
207
history.append('one')
208
# The second time the data comes from the cache.
209
history = self.applyDeprecated(deprecated_in((2, 5, 0)),
210
branch.revision_history)
211
history.append('two')
212
# The revision_history() should still be unchanged, even though
213
# we've mutated the return values from earlier calls.
214
self.assertEqual([], self.applyDeprecated(
215
deprecated_in((2, 5, 0)), branch.revision_history))
220
class TestRevisionHistory(per_branch.TestCaseWithBranch):
222
def test_parent_ghost(self):
223
tree = self.make_branch_and_tree('tree')
224
if not tree.branch.repository._format.supports_ghosts:
225
raise TestNotApplicable("repository format does not support ghosts")
226
tree.add_parent_tree_id('ghost-revision',
227
allow_leftmost_as_ghost=True)
228
tree.commit('first non-ghost commit', rev_id='non-ghost-revision')
229
self.assertEqual(['non-ghost-revision'],
230
self.applyDeprecated(deprecated_in((2, 5, 0)),
231
tree.branch.revision_history))