1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for Branch.revision_history and last_revision."""
22
revision as _mod_revision,
24
from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
27
class TestLastRevision(TestCaseWithBranch):
28
"""Tests for the last_revision property of the branch.
31
def test_set_last_revision_info(self):
32
# based on TestBranch.test_append_revisions, which uses the old
34
wt = self.make_branch_and_tree('tree')
35
wt.commit('f', rev_id='rev1')
36
wt.commit('f', rev_id='rev2')
37
wt.commit('f', rev_id='rev3')
38
br = self.get_branch()
40
br.set_last_revision_info(1, 'rev1')
41
self.assertEquals(br.revision_history(), ["rev1",])
42
br.set_last_revision_info(3, 'rev3')
43
self.assertEquals(br.revision_history(), ["rev1", "rev2", "rev3"])
44
# append_revision specifically prohibits some ids;
45
# set_last_revision_info currently does not
46
## self.assertRaises(errors.ReservedId,
47
## br.set_last_revision_info, 4, 'current:')
50
class TestRevisionHistoryCaching(TestCaseWithBranch):
51
"""Tests for the caching of branches' revision_history.
53
When locked, branches should avoid regenerating or rereading
54
revision_history by caching the last value of it. This is safe because
55
the branch is locked, so nothing can change the revision_history
58
When not locked, obviously the revision_history will need to be regenerated
61
We test if revision_history is using the cache by instrumenting the branch's
62
_gen_revision_history method, which is called by Branch.revision_history if
63
the branch does not have a cache of the revision history.
66
def get_instrumented_branch(self):
67
"""Get a branch and monkey patch it to log calls to
68
_gen_revision_history.
70
:returns: a tuple of (the branch, list that calls will be logged to)
72
branch = self.get_branch()
74
real_gen_revision_history = branch._gen_revision_history
75
def fake_gen_revision_history():
76
calls.append('_gen_revision_history')
77
return real_gen_revision_history()
78
branch._gen_revision_history = fake_gen_revision_history
81
def test_revision_history_when_unlocked(self):
82
"""Repeated calls to revision history will call _gen_revision_history
83
each time when the branch is not locked.
85
branch, calls = self.get_instrumented_branch()
86
# Repeatedly call revision_history.
87
branch.revision_history()
88
branch.revision_history()
90
['_gen_revision_history', '_gen_revision_history'], calls)
92
def test_revision_history_when_locked(self):
93
"""Repeated calls to revision history will only call
94
_gen_revision_history once while the branch is locked.
96
branch, calls = self.get_instrumented_branch()
97
# Lock the branch, then repeatedly call revision_history.
100
branch.revision_history()
101
branch.revision_history()
102
self.assertEqual(['_gen_revision_history'], calls)
106
def test_set_revision_history_when_locked(self):
107
"""When the branch is locked, calling set_revision_history should cache
108
the revision history so that a later call to revision_history will not
109
need to call _gen_revision_history.
111
branch, calls = self.get_instrumented_branch()
112
# Lock the branch, set the revision history, then repeatedly call
115
branch.set_revision_history([])
117
branch.revision_history()
118
self.assertEqual([], calls)
122
def test_set_revision_history_when_unlocked(self):
123
"""When the branch is not locked, calling set_revision_history will not
124
cause the revision history to be cached.
126
branch, calls = self.get_instrumented_branch()
127
# Lock the branch, set the revision history, then repeatedly call
129
branch.set_revision_history([])
130
branch.revision_history()
131
self.assertEqual(['_gen_revision_history'], calls)
133
def test_set_last_revision_info_when_locked(self):
134
"""When the branch is locked, calling set_last_revision_info should
135
cache the last revision info so that a later call to last_revision_info
136
will not need the revision_history. Thus the branch will not to call
137
_gen_revision_history in this situation.
139
a_branch, calls = self.get_instrumented_branch()
140
# Lock the branch, set the last revision info, then call
141
# last_revision_info.
142
a_branch.lock_write()
143
a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
146
a_branch.last_revision_info()
147
self.assertEqual([], calls)
151
def test_set_last_revision_info_none(self):
152
"""Passing None to revision_info to None sets it to NULL_REVISION."""
153
a_branch = self.get_branch()
154
# Lock the branch, set the last revision info, then call
155
# last_revision_info.
156
a_branch.lock_write()
157
self.addCleanup(a_branch.unlock)
158
self.callDeprecated(['NULL_REVISION should be used for the null'
159
' revision instead of None, as of bzr 0.91.'],
160
a_branch.set_last_revision_info, 0, None)
161
self.assertEqual((0, _mod_revision.NULL_REVISION),
162
a_branch.last_revision_info())
164
def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
165
"""On format 6 branches, set_last_revision_info invalidates the revision
168
if not isinstance(self.branch_format, branch.BzrBranchFormat6):
170
a_branch, calls = self.get_instrumented_branch()
171
# Lock the branch, cache the revision history.
172
a_branch.lock_write()
173
a_branch.revision_history()
174
# Set the last revision info, clearing the cache.
175
a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
178
a_branch.revision_history()
179
self.assertEqual(['_gen_revision_history'], calls)
183
def test_cached_revision_history_not_accidentally_mutable(self):
184
"""When there's a cached version of the history, revision_history
185
returns a copy of the cached data so that callers cannot accidentally
188
branch = self.get_branch()
189
# Lock the branch, then repeatedly call revision_history, mutating the
193
# The first time the data returned will not be in the cache.
194
history = branch.revision_history()
195
history.append('one')
196
# The second time the data comes from the cache.
197
history = branch.revision_history()
198
history.append('two')
199
# The revision_history() should still be unchanged, even though
200
# we've mutated the return values from earlier calls.
201
self.assertEqual([], branch.revision_history())
206
class TestRevisionHistory(TestCaseWithBranch):
208
def test_parent_ghost(self):
209
tree = self.make_branch_and_tree('tree')
210
tree.add_parent_tree_id('ghost-revision',
211
allow_leftmost_as_ghost=True)
212
tree.commit('first non-ghost commit', rev_id='non-ghost-revision')
213
self.assertEqual(['non-ghost-revision'],
214
tree.branch.revision_history())