~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testrevision.py

  • Committer: Martin Pool
  • Date: 2005-09-19 08:04:06 UTC
  • Revision ID: mbp@sourcefrog.net-20050919080406-68df87945871a505
- Fix up one more RevisionReference bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
#
 
1
# (C) 2005 Canonical Ltd
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
18
import os
19
 
import warnings
20
19
 
21
 
from bzrlib import (
22
 
    bugtracker,
23
 
    revision,
24
 
    symbol_versioning,
25
 
    )
 
20
from bzrlib.selftest import TestCaseInTempDir
26
21
from bzrlib.branch import Branch
27
 
from bzrlib.errors import (
28
 
    InvalidBugStatus,
29
 
    InvalidLineInBugsProperty,
30
 
    NoSuchRevision,
31
 
    )
32
 
from bzrlib.deprecated_graph import Graph
33
 
from bzrlib.revision import (find_present_ancestors,
34
 
                             NULL_REVISION)
35
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
22
from bzrlib.commit import commit
 
23
from bzrlib.fetch import fetch
 
24
from bzrlib.revision import (find_present_ancestors, common_ancestor,
 
25
                             is_ancestor)
36
26
from bzrlib.trace import mutter
37
 
from bzrlib.workingtree import WorkingTree
38
 
 
39
 
# We're allowed to test deprecated interfaces
40
 
warnings.filterwarnings('ignore',
41
 
        '.*get_intervening_revisions was deprecated',
42
 
        DeprecationWarning,
43
 
        r'bzrlib\.tests\.test_revision')
44
 
 
45
 
# XXX: Make this a method of a merge base case
46
 
def make_branches(self, format=None):
47
 
    """Create two branches
48
 
 
49
 
    branch 1 has 6 commits, branch 2 has 3 commits
50
 
    commit 10 is a ghosted merge merge from branch 1
51
 
 
52
 
    the object graph is
53
 
    B:     A:
54
 
    a..0   a..0
55
 
    a..1   a..1
56
 
    a..2   a..2
57
 
    b..3   a..3 merges b..4
58
 
    b..4   a..4
59
 
    b..5   a..5 merges b..5
60
 
    b..6 merges a4
61
 
 
62
 
    so A is missing b6 at the start
63
 
    and B is missing a3, a4, a5
64
 
    """
65
 
    tree1 = self.make_branch_and_tree("branch1", format=format)
66
 
    br1 = tree1.branch
67
 
 
68
 
    tree1.commit("Commit one", rev_id="a@u-0-0")
69
 
    tree1.commit("Commit two", rev_id="a@u-0-1")
70
 
    tree1.commit("Commit three", rev_id="a@u-0-2")
71
 
 
72
 
    tree2 = tree1.bzrdir.sprout("branch2").open_workingtree()
73
 
    br2 = tree2.branch
74
 
    tree2.commit("Commit four", rev_id="b@u-0-3")
75
 
    tree2.commit("Commit five", rev_id="b@u-0-4")
 
27
from bzrlib.errors import NoSuchRevision
 
28
 
 
29
def make_branches():
 
30
    os.mkdir("branch1")
 
31
    br1 = Branch("branch1", init=True)
 
32
    
 
33
    commit(br1, "Commit one", rev_id="a@u-0-0")
 
34
    commit(br1, "Commit two", rev_id="a@u-0-1")
 
35
    commit(br1, "Commit three", rev_id="a@u-0-2")
 
36
 
 
37
    os.mkdir("branch2")
 
38
    br2 = Branch("branch2", init=True)
 
39
    br2.update_revisions(br1)
 
40
    commit(br2, "Commit four", rev_id="b@u-0-3")
 
41
    commit(br2, "Commit five", rev_id="b@u-0-4")
76
42
    revisions_2 = br2.revision_history()
77
 
    self.assertEquals(revisions_2[-1], 'b@u-0-4')
78
 
 
79
 
    tree1.merge_from_branch(br2)
80
 
    tree1.commit("Commit six", rev_id="a@u-0-3")
81
 
    tree1.commit("Commit seven", rev_id="a@u-0-4")
82
 
    tree2.commit("Commit eight", rev_id="b@u-0-5")
83
 
    self.assertEquals(br2.revision_history()[-1], 'b@u-0-5')
84
 
 
85
 
    tree1.merge_from_branch(br2)
86
 
    tree1.commit("Commit nine", rev_id="a@u-0-5")
87
 
    # DO NOT MERGE HERE - we WANT a GHOST.
88
 
    tree2.add_parent_tree_id(br1.revision_history()[4])
89
 
    tree2.commit("Commit ten - ghost merge", rev_id="b@u-0-6")
90
 
 
 
43
    
 
44
    fetch(from_branch=br2, to_branch=br1)
 
45
    br1.add_pending_merge(revisions_2[4])
 
46
    assert revisions_2[4] == 'b@u-0-4'
 
47
    commit(br1, "Commit six", rev_id="a@u-0-3")
 
48
    commit(br1, "Commit seven", rev_id="a@u-0-4")
 
49
    commit(br2, "Commit eight", rev_id="b@u-0-5")
 
50
    
 
51
    fetch(from_branch=br2, to_branch=br1)
 
52
    br1.add_pending_merge(br2.revision_history()[5])
 
53
    commit(br1, "Commit nine", rev_id="a@u-0-5")
 
54
 
 
55
    fetch(from_branch=br1, to_branch=br2)
 
56
    br2.add_pending_merge(br1.revision_history()[4])
 
57
    commit(br2, "Commit ten", rev_id="b@u-0-6")
 
58
 
 
59
    fetch(from_branch=br2, to_branch=br1)
 
60
    
91
61
    return br1, br2
92
62
 
93
63
 
94
 
class TestIsAncestor(TestCaseWithTransport):
95
 
 
 
64
class TestIsAncestor(TestCaseInTempDir):
96
65
    def test_recorded_ancestry(self):
97
66
        """Test that commit records all ancestors"""
98
 
        br1, br2 = make_branches(self)
 
67
        br1, br2 = make_branches()
99
68
        d = [('a@u-0-0', ['a@u-0-0']),
100
69
             ('a@u-0-1', ['a@u-0-0', 'a@u-0-1']),
101
70
             ('a@u-0-2', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2']),
111
80
             ('a@u-0-5', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
112
81
                          'b@u-0-3', 'b@u-0-4',
113
82
                          'b@u-0-5', 'a@u-0-5']),
114
 
             ('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2',
 
83
             ('b@u-0-6', ['a@u-0-0', 'a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4',
115
84
                          'b@u-0-3', 'b@u-0-4',
116
85
                          'b@u-0-5', 'b@u-0-6']),
117
86
             ]
118
 
        br1_only = ('a@u-0-3', 'a@u-0-4', 'a@u-0-5')
119
 
        br2_only = ('b@u-0-6',)
120
87
        for branch in br1, br2:
121
88
            for rev_id, anc in d:
122
 
                if rev_id in br1_only and not branch is br1:
123
 
                    continue
124
 
                if rev_id in br2_only and not branch is br2:
125
 
                    continue
126
89
                mutter('ancestry of {%s}: %r',
127
 
                       rev_id, branch.repository.get_ancestry(rev_id))
128
 
                result = sorted(branch.repository.get_ancestry(rev_id))
129
 
                self.assertEquals(result, [None] + sorted(anc))
130
 
 
131
 
 
132
 
class TestIntermediateRevisions(TestCaseWithTransport):
 
90
                       rev_id, branch.get_ancestry(rev_id))
 
91
                self.assertEquals(sorted(branch.get_ancestry(rev_id)),
 
92
                                  sorted(anc))
 
93
    
 
94
    
 
95
    def test_is_ancestor(self):
 
96
        """Test checking whether a revision is an ancestor of another revision"""
 
97
        br1, br2 = make_branches()
 
98
        revisions = br1.revision_history()
 
99
        revisions_2 = br2.revision_history()
 
100
        sources = br1
 
101
 
 
102
        assert is_ancestor(revisions[0], revisions[0], br1)
 
103
        assert is_ancestor(revisions[1], revisions[0], sources)
 
104
        assert not is_ancestor(revisions[0], revisions[1], sources)
 
105
        assert is_ancestor(revisions_2[3], revisions[0], sources)
 
106
        # disabled mbp 20050914, doesn't seem to happen anymore
 
107
        ## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
 
108
        ##                  revisions[0], br1)        
 
109
        assert is_ancestor(revisions[3], revisions_2[4], sources)
 
110
        assert is_ancestor(revisions[3], revisions_2[4], br1)
 
111
        assert is_ancestor(revisions[3], revisions_2[3], sources)
 
112
        ## assert not is_ancestor(revisions[3], revisions_2[3], br1)
 
113
 
 
114
 
 
115
 
 
116
class TestIntermediateRevisions(TestCaseInTempDir):
133
117
 
134
118
    def setUp(self):
135
 
        TestCaseWithTransport.setUp(self)
136
 
        self.br1, self.br2 = make_branches(self)
137
 
        wt1 = self.br1.bzrdir.open_workingtree()
138
 
        wt2 = self.br2.bzrdir.open_workingtree()
139
 
        wt2.commit("Commit eleven", rev_id="b@u-0-7")
140
 
        wt2.commit("Commit twelve", rev_id="b@u-0-8")
141
 
        wt2.commit("Commit thirtteen", rev_id="b@u-0-9")
142
 
 
143
 
        wt1.merge_from_branch(self.br2)
144
 
        wt1.commit("Commit fourtten", rev_id="a@u-0-6")
145
 
 
146
 
        wt2.merge_from_branch(self.br1)
147
 
        wt2.commit("Commit fifteen", rev_id="b@u-0-10")
148
 
 
149
 
 
150
 
class MockRevisionSource(object):
151
 
    """A RevisionSource that takes a pregenerated graph.
152
 
 
153
 
    This is useful for testing revision graph algorithms where
154
 
    the actual branch existing is irrelevant.
155
 
    """
156
 
 
157
 
    def __init__(self, full_graph):
158
 
        self._full_graph = full_graph
159
 
 
160
 
    def get_revision_graph_with_ghosts(self, revision_ids):
161
 
        # This is mocked out to just return a constant graph.
162
 
        return self._full_graph
163
 
 
164
 
 
165
 
class TestCommonAncestor(TestCaseWithTransport):
 
119
        from bzrlib.commit import commit
 
120
        TestCaseInTempDir.setUp(self)
 
121
        self.br1, self.br2 = make_branches()
 
122
 
 
123
        self.br2.commit("Commit eleven", rev_id="b@u-0-7")
 
124
        self.br2.commit("Commit twelve", rev_id="b@u-0-8")
 
125
        self.br2.commit("Commit thirtteen", rev_id="b@u-0-9")
 
126
 
 
127
        fetch(from_branch=self.br2, to_branch=self.br1)
 
128
        self.br1.add_pending_merge(self.br2.revision_history()[6])
 
129
        self.br1.commit("Commit fourtten", rev_id="a@u-0-6")
 
130
 
 
131
        fetch(from_branch=self.br1, to_branch=self.br2)
 
132
        self.br2.add_pending_merge(self.br1.revision_history()[6])
 
133
        self.br2.commit("Commit fifteen", rev_id="b@u-0-10")
 
134
 
 
135
        from bzrlib.revision import MultipleRevisionSources
 
136
        self.sources = MultipleRevisionSources(self.br1, self.br2)
 
137
 
 
138
    def intervene(self, ancestor, revision, revision_history=None):
 
139
        from bzrlib.revision import get_intervening_revisions
 
140
        return get_intervening_revisions(ancestor,revision, self.sources, 
 
141
                                         revision_history)
 
142
 
 
143
    def test_intervene(self):
 
144
        """Find intermediate revisions, without requiring history"""
 
145
        from bzrlib.errors import NotAncestor, NoSuchRevision
 
146
        assert len(self.intervene('a@u-0-0', 'a@u-0-0')) == 0
 
147
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-1'), ['a@u-0-1'])
 
148
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-2'), 
 
149
                         ['a@u-0-1', 'a@u-0-2'])
 
150
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-3'), 
 
151
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3'])
 
152
        self.assertEqual(self.intervene('b@u-0-3', 'a@u-0-3'), 
 
153
                         ['b@u-0-4', 'a@u-0-3'])
 
154
        self.assertEqual(self.intervene('a@u-0-2', 'a@u-0-3', 
 
155
                                        self.br1.revision_history()), 
 
156
                         ['a@u-0-3'])
 
157
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-5', 
 
158
                                        self.br1.revision_history()), 
 
159
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
160
                          'a@u-0-5'])
 
161
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6', 
 
162
                         self.br1.revision_history()), 
 
163
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
164
                          'b@u-0-6'])
 
165
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'), 
 
166
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4', 
 
167
                          'b@u-0-5'])
 
168
        self.assertEqual(self.intervene('b@u-0-3', 'b@u-0-6', 
 
169
                         self.br2.revision_history()), 
 
170
                         ['b@u-0-4', 'b@u-0-5', 'b@u-0-6'])
 
171
        self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10'), 
 
172
                         ['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
 
173
        self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10', 
 
174
                                        self.br2.revision_history()), 
 
175
                         ['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
 
176
        self.assertRaises(NotAncestor, self.intervene, 'b@u-0-10', 'b@u-0-6', 
 
177
                          self.br2.revision_history())
 
178
        self.assertRaises(NoSuchRevision, self.intervene, 'c@u-0-10', 
 
179
                          'b@u-0-6', self.br2.revision_history())
 
180
        self.assertRaises(NoSuchRevision, self.intervene, 'b@u-0-10', 
 
181
                          'c@u-0-6', self.br2.revision_history())
 
182
 
 
183
 
 
184
class TestCommonAncestor(TestCaseInTempDir):
166
185
    """Test checking whether a revision is an ancestor of another revision"""
167
186
 
168
 
    def test_get_history(self):
169
 
        # TODO: test ghosts on the left hand branch's impact
170
 
        # TODO: test ghosts on all parents, we should get some
171
 
        # indicator. i.e. NULL_REVISION
172
 
        # RBC 20060608
173
 
        tree = self.make_branch_and_tree('.')
174
 
        tree.commit('1', rev_id = '1', allow_pointless=True)
175
 
        tree.commit('2', rev_id = '2', allow_pointless=True)
176
 
        tree.commit('3', rev_id = '3', allow_pointless=True)
177
 
        rev = tree.branch.repository.get_revision('1')
178
 
        history = rev.get_history(tree.branch.repository)
179
 
        self.assertEqual([None, '1'], history)
180
 
        rev = tree.branch.repository.get_revision('2')
181
 
        history = rev.get_history(tree.branch.repository)
182
 
        self.assertEqual([None, '1', '2'], history)
183
 
        rev = tree.branch.repository.get_revision('3')
184
 
        history = rev.get_history(tree.branch.repository)
185
 
        self.assertEqual([None, '1', '2' ,'3'], history)
186
 
 
187
 
 
188
 
class TestReservedId(TestCase):
189
 
 
190
 
    def test_is_reserved_id(self):
191
 
        self.assertEqual(True, revision.is_reserved_id(NULL_REVISION))
192
 
        self.assertEqual(True, revision.is_reserved_id(
193
 
            revision.CURRENT_REVISION))
194
 
        self.assertEqual(True, revision.is_reserved_id('arch:'))
195
 
        self.assertEqual(False, revision.is_reserved_id('null'))
196
 
        self.assertEqual(False, revision.is_reserved_id(
197
 
            'arch:a@example.com/c--b--v--r'))
198
 
        self.assertEqual(False, revision.is_reserved_id(None))
199
 
 
200
 
 
201
 
class TestRevisionMethods(TestCase):
202
 
 
203
 
    def test_get_summary(self):
204
 
        r = revision.Revision('1')
205
 
        r.message = 'a'
206
 
        self.assertEqual('a', r.get_summary())
207
 
        r.message = 'a\nb'
208
 
        self.assertEqual('a', r.get_summary())
209
 
        r.message = '\na\nb'
210
 
        self.assertEqual('a', r.get_summary())
211
 
        r.message = None
212
 
        self.assertEqual('', r.get_summary())
213
 
 
214
 
    def test_get_apparent_author(self):
215
 
        r = revision.Revision('1')
216
 
        r.committer = 'A'
217
 
        author = self.applyDeprecated(
218
 
                symbol_versioning.deprecated_in((1, 13, 0)),
219
 
                r.get_apparent_author)
220
 
        self.assertEqual('A', author)
221
 
        r.properties['author'] = 'B'
222
 
        author = self.applyDeprecated(
223
 
                symbol_versioning.deprecated_in((1, 13, 0)),
224
 
                r.get_apparent_author)
225
 
        self.assertEqual('B', author)
226
 
        r.properties['authors'] = 'C\nD'
227
 
        author = self.applyDeprecated(
228
 
                symbol_versioning.deprecated_in((1, 13, 0)),
229
 
                r.get_apparent_author)
230
 
        self.assertEqual('C', author)
231
 
 
232
 
    def test_get_apparent_author_none(self):
233
 
        r = revision.Revision('1')
234
 
        author = self.applyDeprecated(
235
 
                symbol_versioning.deprecated_in((1, 13, 0)),
236
 
                r.get_apparent_author)
237
 
        self.assertEqual(None, author)
238
 
 
239
 
    def test_get_apparent_authors(self):
240
 
        r = revision.Revision('1')
241
 
        r.committer = 'A'
242
 
        self.assertEqual(['A'], r.get_apparent_authors())
243
 
        r.properties['author'] = 'B'
244
 
        self.assertEqual(['B'], r.get_apparent_authors())
245
 
        r.properties['authors'] = 'C\nD'
246
 
        self.assertEqual(['C', 'D'], r.get_apparent_authors())
247
 
 
248
 
    def test_get_apparent_authors_no_committer(self):
249
 
        r = revision.Revision('1')
250
 
        self.assertEqual([], r.get_apparent_authors())
251
 
 
252
 
 
253
 
class TestRevisionBugs(TestCase):
254
 
    """Tests for getting the bugs that a revision is linked to."""
255
 
 
256
 
    def test_no_bugs(self):
257
 
        r = revision.Revision('1')
258
 
        self.assertEqual([], list(r.iter_bugs()))
259
 
 
260
 
    def test_some_bugs(self):
261
 
        r = revision.Revision(
262
 
            '1', properties={
263
 
                'bugs': bugtracker.encode_fixes_bug_urls(
264
 
                    ['http://example.com/bugs/1',
265
 
                     'http://launchpad.net/bugs/1234'])})
266
 
        self.assertEqual(
267
 
            [('http://example.com/bugs/1', bugtracker.FIXED),
268
 
             ('http://launchpad.net/bugs/1234', bugtracker.FIXED)],
269
 
            list(r.iter_bugs()))
270
 
 
271
 
    def test_no_status(self):
272
 
        r = revision.Revision(
273
 
            '1', properties={'bugs': 'http://example.com/bugs/1'})
274
 
        self.assertRaises(InvalidLineInBugsProperty, list, r.iter_bugs())
275
 
 
276
 
    def test_too_much_information(self):
277
 
        r = revision.Revision(
278
 
            '1', properties={'bugs': 'http://example.com/bugs/1 fixed bar'})
279
 
        self.assertRaises(InvalidLineInBugsProperty, list, r.iter_bugs())
280
 
 
281
 
    def test_invalid_status(self):
282
 
        r = revision.Revision(
283
 
            '1', properties={'bugs': 'http://example.com/bugs/1 faxed'})
284
 
        self.assertRaises(InvalidBugStatus, list, r.iter_bugs())
 
187
    def test_common_ancestor(self):
 
188
        br1, br2 = make_branches()
 
189
        revisions = br1.revision_history()
 
190
        revisions_2 = br2.revision_history()
 
191
        sources = br1
 
192
 
 
193
        expected_ancestors_list = {revisions[3]:(0, 0), 
 
194
                                   revisions[2]:(1, 1),
 
195
                                   revisions_2[4]:(2, 1), 
 
196
                                   revisions[1]:(3, 2),
 
197
                                   revisions_2[3]:(4, 2),
 
198
                                   revisions[0]:(5, 3) }
 
199
        ancestors_list = find_present_ancestors(revisions[3], sources)
 
200
        assert len(expected_ancestors_list) == len(ancestors_list)
 
201
        for key, value in expected_ancestors_list.iteritems():
 
202
            self.assertEqual(ancestors_list[key], value, 
 
203
                              "key %r, %r != %r" % (key, ancestors_list[key],
 
204
                                                    value))
 
205
 
 
206
        self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
 
207
                          revisions[0])
 
208
        self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
 
209
                          revisions[1])
 
210
        self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
 
211
                          revisions[1])
 
212
        self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
 
213
                          revisions[2])
 
214
        self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
 
215
                          revisions_2[4])
 
216
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
 
217
                          revisions_2[4])
 
218
        self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
 
219
                          revisions[4])
 
220
        self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
 
221
                          revisions_2[5])
 
222
 
 
223
 
 
224
if __name__ == '__main__':
 
225
    import unittest, sys
 
226
    unittest.main()
 
227