~bzr/ubuntu/lucid/bzr/beta-ppa

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_branch/test_revision_history.py

  • Committer: Martin Pool
  • Date: 2010-07-02 07:29:40 UTC
  • mfrom: (129.1.7 packaging-karmic)
  • Revision ID: mbp@sourcefrog.net-20100702072940-hpzq5elg8wjve8rh
* PPA rebuild.
* PPA rebuild for Karmic.
* PPA rebuild for Jaunty.
* PPA rebuild for Hardy.
* From postinst, actually remove the example bash completion scripts.
  (LP: #249452)
* New upstream release.
* New upstream release.
* New upstream release.
* Revert change to Build-depends: Dapper does not have python-central.
  Should be python-support..
* Target ppa..
* Target ppa..
* Target ppa..
* Target ppa..
* New upstream release.
* Switch to dpkg-source 3.0 (quilt) format.
* Bump standards version to 3.8.4.
* Remove embedded copy of python-configobj. Closes: #555336
* Remove embedded copy of python-elementtree. Closes: #555343
* Change section from 'Devel' to 'Vcs'..
* Change section from 'Devel' to 'Vcs'..
* Change section from 'Devel' to 'Vcs'..
* Change section from 'Devel' to 'Vcs'..
* Change section from 'Devel' to 'Vcs'..
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* debian/control: Fix obsolete-relation-form-in-source
  lintian warning. 
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Split out docs into bzr-doc package.
* New upstream release.
* Added John Francesco Ferlito to Uploaders.
* Fix install path to quick-reference guide
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Fix FTBFS due to path changes, again.
* Fix FTBFS due to doc paths changing
* New upstream release.
* Fix FTBFS due to path changes, again.
* Fix FTBFS due to doc paths changing
* New upstream release.
* Fix FTBFS due to path changes, again.
* Fix FTBFS due to doc paths changing
* New upstream release.
* Fix FTBFS due to path changes, again, again.
* Fix FTBFS due to path changes, again.
* Fix FTBFS due to path changes.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Bump standards version to 3.8.3.
* Remove unused patch system.
* New upstream release.
* New upstream release.
* New upstream release.
* Fix copy and paste tab error in .install file
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
 + Fixes compatibility with Python 2.4. Closes: #537708
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream version.
* Bump standards version to 3.8.2.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Add python-pyrex to build-deps to ensure C extensions are always build.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Split documentation into bzr-doc package. ((LP: #385074)
* Multiple packaging changes to make us more linitan clean.
* New upstream release.
* Split documentation into bzr-doc package. ((LP: #385074)
* Multiple packaging changes to make us more linitan clean.
* New upstream release.
* Split documentation into bzr-doc package. ((LP: #385074)
* Multiple packaging changes to make us more linitan clean.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Fix API compatibility version. (Closes: #526233)
* New upstream release.
  + Fixes default format for upgrade command. (Closes: #464688)
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Add missing dependency on zlib development library. (Closes:
  #523595)
* Add zlib build-depends.
* Add zlib build-depends.
* Add zlib build-depends.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Move to section vcs.
* Bump standards version to 3.8.1.
* New upstream release.
* Remove temporary patch for missing .c files from distribution
* New upstream release.
* Remove temporary patch for missing .c files from distribution
* New upstream release.
* Remove temporary patch for missing .c files from distribution
* Add temporary patch for missing .c files from distribution
* Add temporary patch for missing .c files from distribution
* Add temporary patch for missing .c files from distribution
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Recommend ca-certificates. (Closes: #452024)
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Update watch file. bazaar now uses launchpad to host its sources.
* Remove patch for inventory root revision copy, applied upstream.
* New upstream release.
* New upstream release.
* New upstream release
* Force removal of files installed in error to /etc/bash_completion.d/
  (LP: #249452)
* New upstream release.
* New upstream release
* New upstream release.
* Bump standards version.
* Include patch for inventory root revision copy, required for bzr-svn.
* New upstream release.
* Remove unused lintian overrides.
* Correct the package version not to be native.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* New upstream release.
* Final 1.5 release.
* New upstream release.
* New upstream release.
* New upstream release.
* Add myself as a co-maintainer.
* Add a Dm-Upload-Allowed: yes header.
* New upstream bugfix release.
* New upstream release.
* Final 1.3 release.
* New upstream release.
* First release candidate of the upcoming 1.3 release.
* Rebuild to fix the problem caused by a build with a broken python-central.
* New upstream release.
* Rebuild for dapper PPA.
* Apply Lamont's patches to fix build-dependencies on dapper.
  (See: https://bugs.launchpad.net/bzr/+bug/189915)

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
"""Tests for Branch.revision_history and last_revision."""
 
18
 
 
19
from bzrlib import (
 
20
    branch,
 
21
    errors,
 
22
    revision as _mod_revision,
 
23
    )
 
24
from bzrlib.tests.per_branch.test_branch import TestCaseWithBranch
 
25
 
 
26
 
 
27
class TestLastRevision(TestCaseWithBranch):
 
28
    """Tests for the last_revision property of the branch.
 
29
    """
 
30
 
 
31
    def test_set_last_revision_info(self):
 
32
        # based on TestBranch.test_append_revisions, which uses the old
 
33
        # append_revision api
 
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()
 
39
        br.fetch(wt.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:')
 
48
 
 
49
 
 
50
class TestRevisionHistoryCaching(TestCaseWithBranch):
 
51
    """Tests for the caching of branches' revision_history.
 
52
 
 
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
 
56
    unexpectedly.
 
57
 
 
58
    When not locked, obviously the revision_history will need to be regenerated
 
59
    or reread each time.
 
60
 
 
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.
 
64
    """
 
65
 
 
66
    def get_instrumented_branch(self):
 
67
        """Get a branch and monkey patch it to log calls to
 
68
        _gen_revision_history.
 
69
 
 
70
        :returns: a tuple of (the branch, list that calls will be logged to)
 
71
        """
 
72
        branch = self.get_branch()
 
73
        calls = []
 
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
 
79
        return branch, calls
 
80
 
 
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.
 
84
        """
 
85
        branch, calls = self.get_instrumented_branch()
 
86
        # Repeatedly call revision_history.
 
87
        branch.revision_history()
 
88
        branch.revision_history()
 
89
        self.assertEqual(
 
90
            ['_gen_revision_history', '_gen_revision_history'], calls)
 
91
 
 
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.
 
95
        """
 
96
        branch, calls = self.get_instrumented_branch()
 
97
        # Lock the branch, then repeatedly call revision_history.
 
98
        branch.lock_read()
 
99
        try:
 
100
            branch.revision_history()
 
101
            branch.revision_history()
 
102
            self.assertEqual(['_gen_revision_history'], calls)
 
103
        finally:
 
104
            branch.unlock()
 
105
 
 
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.
 
110
        """
 
111
        branch, calls = self.get_instrumented_branch()
 
112
        # Lock the branch, set the revision history, then repeatedly call
 
113
        # revision_history.
 
114
        branch.lock_write()
 
115
        branch.set_revision_history([])
 
116
        try:
 
117
            branch.revision_history()
 
118
            self.assertEqual([], calls)
 
119
        finally:
 
120
            branch.unlock()
 
121
 
 
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.
 
125
        """
 
126
        branch, calls = self.get_instrumented_branch()
 
127
        branch.set_revision_history([])
 
128
        branch.revision_history()
 
129
        self.assertEqual(['_gen_revision_history'], calls)
 
130
 
 
131
    def test_set_last_revision_info_when_locked(self):
 
132
        """When the branch is locked, calling set_last_revision_info should
 
133
        cache the last revision info so that a later call to last_revision_info
 
134
        will not need the revision_history.  Thus the branch will not to call
 
135
        _gen_revision_history in this situation.
 
136
        """
 
137
        a_branch, calls = self.get_instrumented_branch()
 
138
        # Lock the branch, set the last revision info, then call
 
139
        # last_revision_info.
 
140
        a_branch.lock_write()
 
141
        a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
 
142
        del calls[:]
 
143
        try:
 
144
            a_branch.last_revision_info()
 
145
            self.assertEqual([], calls)
 
146
        finally:
 
147
            a_branch.unlock()
 
148
 
 
149
    def test_set_last_revision_info_none(self):
 
150
        """Passing None to revision_info to None sets it to NULL_REVISION."""
 
151
        a_branch = self.get_branch()
 
152
        # Lock the branch, set the last revision info, then call
 
153
        # last_revision_info.
 
154
        a_branch.lock_write()
 
155
        self.addCleanup(a_branch.unlock)
 
156
        self.callDeprecated(['NULL_REVISION should be used for the null'
 
157
            ' revision instead of None, as of bzr 0.91.'],
 
158
            a_branch.set_last_revision_info, 0, None)
 
159
        self.assertEqual((0, _mod_revision.NULL_REVISION),
 
160
                         a_branch.last_revision_info())
 
161
 
 
162
    def test_set_last_revision_info_uncaches_revision_history_for_format6(self):
 
163
        """On format 6 branches, set_last_revision_info invalidates the revision
 
164
        history cache.
 
165
        """
 
166
        if not isinstance(self.branch_format, branch.BzrBranchFormat6):
 
167
            return
 
168
        a_branch, calls = self.get_instrumented_branch()
 
169
        # Lock the branch, cache the revision history.
 
170
        a_branch.lock_write()
 
171
        a_branch.revision_history()
 
172
        # Set the last revision info, clearing the cache.
 
173
        a_branch.set_last_revision_info(0, _mod_revision.NULL_REVISION)
 
174
        del calls[:]
 
175
        try:
 
176
            a_branch.revision_history()
 
177
            self.assertEqual(['_gen_revision_history'], calls)
 
178
        finally:
 
179
            a_branch.unlock()
 
180
 
 
181
    def test_cached_revision_history_not_accidentally_mutable(self):
 
182
        """When there's a cached version of the history, revision_history
 
183
        returns a copy of the cached data so that callers cannot accidentally
 
184
        corrupt the cache.
 
185
        """
 
186
        branch = self.get_branch()
 
187
        # Lock the branch, then repeatedly call revision_history, mutating the
 
188
        # results.
 
189
        branch.lock_read()
 
190
        try:
 
191
            # The first time the data returned will not be in the cache.
 
192
            history = branch.revision_history()
 
193
            history.append('one')
 
194
            # The second time the data comes from the cache.
 
195
            history = branch.revision_history()
 
196
            history.append('two')
 
197
            # The revision_history() should still be unchanged, even though
 
198
            # we've mutated the return values from earlier calls.
 
199
            self.assertEqual([], branch.revision_history())
 
200
        finally:
 
201
            branch.unlock()
 
202
 
 
203
 
 
204
class TestRevisionHistory(TestCaseWithBranch):
 
205
 
 
206
    def test_parent_ghost(self):
 
207
        tree = self.make_branch_and_tree('tree')
 
208
        tree.add_parent_tree_id('ghost-revision',
 
209
                                allow_leftmost_as_ghost=True)
 
210
        tree.commit('first non-ghost commit', rev_id='non-ghost-revision')
 
211
        self.assertEqual(['non-ghost-revision'],
 
212
                         tree.branch.revision_history())