~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revision.py

  • Committer: Matthew Revell
  • Date: 2008-03-05 14:11:35 UTC
  • mto: This revision was merged to the branch mainline in revision 3263.
  • Revision ID: matthew.revell@canonical.com-20080305141135-96jf8j24b9crru6v
Added an admin-guide directory to doc/en and outlined headings for the admin guide in index.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
19
import warnings
20
20
 
21
21
from bzrlib import (
22
 
    bugtracker,
23
22
    revision,
24
 
    symbol_versioning,
25
23
    )
26
24
from bzrlib.branch import Branch
27
 
from bzrlib.errors import (
28
 
    InvalidBugStatus,
29
 
    InvalidLineInBugsProperty,
30
 
    NoSuchRevision,
31
 
    )
 
25
from bzrlib.errors import NoSuchRevision
32
26
from bzrlib.deprecated_graph import Graph
33
 
from bzrlib.revision import (find_present_ancestors,
 
27
from bzrlib.revision import (find_present_ancestors, combined_graph,
 
28
                             common_ancestor,
 
29
                             is_ancestor, MultipleRevisionSources,
34
30
                             NULL_REVISION)
 
31
from bzrlib.symbol_versioning import one_zero
35
32
from bzrlib.tests import TestCase, TestCaseWithTransport
36
33
from bzrlib.trace import mutter
37
34
from bzrlib.workingtree import WorkingTree
51
48
 
52
49
    the object graph is
53
50
    B:     A:
54
 
    a..0   a..0
 
51
    a..0   a..0 
55
52
    a..1   a..1
56
53
    a..2   a..2
57
54
    b..3   a..3 merges b..4
64
61
    """
65
62
    tree1 = self.make_branch_and_tree("branch1", format=format)
66
63
    br1 = tree1.branch
67
 
 
 
64
    
68
65
    tree1.commit("Commit one", rev_id="a@u-0-0")
69
66
    tree1.commit("Commit two", rev_id="a@u-0-1")
70
67
    tree1.commit("Commit three", rev_id="a@u-0-2")
71
68
 
72
 
    tree2 = tree1.bzrdir.sprout("branch2").open_workingtree()
 
69
    tree2 = tree1.bzrdir.clone("branch2").open_workingtree()
73
70
    br2 = tree2.branch
74
71
    tree2.commit("Commit four", rev_id="b@u-0-3")
75
72
    tree2.commit("Commit five", rev_id="b@u-0-4")
76
73
    revisions_2 = br2.revision_history()
77
74
    self.assertEquals(revisions_2[-1], 'b@u-0-4')
78
 
 
 
75
    
79
76
    tree1.merge_from_branch(br2)
80
77
    tree1.commit("Commit six", rev_id="a@u-0-3")
81
78
    tree1.commit("Commit seven", rev_id="a@u-0-4")
82
79
    tree2.commit("Commit eight", rev_id="b@u-0-5")
83
80
    self.assertEquals(br2.revision_history()[-1], 'b@u-0-5')
84
 
 
 
81
    
85
82
    tree1.merge_from_branch(br2)
86
83
    tree1.commit("Commit nine", rev_id="a@u-0-5")
87
84
    # DO NOT MERGE HERE - we WANT a GHOST.
88
85
    tree2.add_parent_tree_id(br1.revision_history()[4])
89
86
    tree2.commit("Commit ten - ghost merge", rev_id="b@u-0-6")
90
 
 
 
87
    
91
88
    return br1, br2
92
89
 
93
90
 
127
124
                       rev_id, branch.repository.get_ancestry(rev_id))
128
125
                result = sorted(branch.repository.get_ancestry(rev_id))
129
126
                self.assertEquals(result, [None] + sorted(anc))
 
127
    
 
128
    
 
129
    def test_is_ancestor(self):
 
130
        """Test checking whether a revision is an ancestor of another revision"""
 
131
        br1, br2 = make_branches(self)
 
132
        revisions = br1.revision_history()
 
133
        revisions_2 = br2.revision_history()
 
134
        sources = br1
 
135
 
 
136
        br1.lock_read()
 
137
        br2.lock_read()
 
138
        self.addCleanup(br1.unlock)
 
139
        br2.lock_read()
 
140
        self.addCleanup(br2.unlock)
 
141
 
 
142
        self.assertTrue(self.applyDeprecated(one_zero,
 
143
                        is_ancestor, revisions[0], revisions[0], br1))
 
144
        self.assertTrue(self.applyDeprecated(one_zero,
 
145
                        is_ancestor, revisions[1], revisions[0], sources))
 
146
        self.assertFalse(self.applyDeprecated(one_zero,
 
147
                         is_ancestor, revisions[0], revisions[1], sources))
 
148
        self.assertTrue(self.applyDeprecated(one_zero,
 
149
                        is_ancestor, revisions_2[3], revisions[0], sources))
 
150
        # disabled mbp 20050914, doesn't seem to happen anymore
 
151
        ## self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
 
152
        ##                  revisions[0], br1)
 
153
        self.assertTrue(self.applyDeprecated(one_zero,
 
154
                        is_ancestor, revisions[3], revisions_2[4], sources))
 
155
        self.assertTrue(self.applyDeprecated(one_zero,
 
156
                        is_ancestor, revisions[3], revisions_2[4], br1))
 
157
        self.assertTrue(self.applyDeprecated(one_zero,
 
158
                        is_ancestor, revisions[3], revisions_2[3], sources))
 
159
        ## self.assert_(not is_ancestor(revisions[3], revisions_2[3], br1))
130
160
 
131
161
 
132
162
class TestIntermediateRevisions(TestCaseWithTransport):
146
176
        wt2.merge_from_branch(self.br1)
147
177
        wt2.commit("Commit fifteen", rev_id="b@u-0-10")
148
178
 
 
179
        from bzrlib.revision import MultipleRevisionSources
 
180
        self.sources = MultipleRevisionSources(self.br1.repository,
 
181
                                               self.br2.repository)
 
182
 
 
183
 
149
184
 
150
185
class MockRevisionSource(object):
151
186
    """A RevisionSource that takes a pregenerated graph.
165
200
class TestCommonAncestor(TestCaseWithTransport):
166
201
    """Test checking whether a revision is an ancestor of another revision"""
167
202
 
 
203
    def test_common_ancestor(self):
 
204
        """Pick a reasonable merge base"""
 
205
        br1, br2 = make_branches(self)
 
206
        revisions = br1.revision_history()
 
207
        revisions_2 = br2.revision_history()
 
208
        sources = MultipleRevisionSources(br1.repository, br2.repository)
 
209
        expected_ancestors_list = {revisions[3]:(0, 0), 
 
210
                                   revisions[2]:(1, 1),
 
211
                                   revisions_2[4]:(2, 1), 
 
212
                                   revisions[1]:(3, 2),
 
213
                                   revisions_2[3]:(4, 2),
 
214
                                   revisions[0]:(5, 3) }
 
215
        ancestors_list = find_present_ancestors(revisions[3], sources)
 
216
        self.assertEquals(len(expected_ancestors_list), len(ancestors_list))
 
217
        for key, value in expected_ancestors_list.iteritems():
 
218
            self.assertEqual(ancestors_list[key], value, 
 
219
                              "key %r, %r != %r" % (key, ancestors_list[key],
 
220
                                                    value))
 
221
        self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
 
222
                          revisions[0])
 
223
        self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
 
224
                          revisions[1])
 
225
        self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
 
226
                          revisions[1])
 
227
        self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
 
228
                          revisions[2])
 
229
        self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
 
230
                          revisions_2[4])
 
231
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
 
232
                          revisions_2[4])
 
233
        self.assertTrue(common_ancestor(revisions[5], revisions_2[6], sources) in
 
234
                        (revisions[4], revisions_2[5]))
 
235
        self.assertTrue(common_ancestor(revisions_2[6], revisions[5], sources),
 
236
                        (revisions[4], revisions_2[5]))
 
237
        self.assertEqual(None, common_ancestor(None, revisions[5], sources))
 
238
        self.assertEqual(NULL_REVISION,
 
239
            common_ancestor(NULL_REVISION, NULL_REVISION, sources))
 
240
        self.assertEqual(NULL_REVISION,
 
241
            common_ancestor(revisions[0], NULL_REVISION, sources))
 
242
        self.assertEqual(NULL_REVISION,
 
243
            common_ancestor(NULL_REVISION, revisions[0], sources))
 
244
 
 
245
    def test_combined(self):
 
246
        """combined_graph
 
247
        Ensure it's not order-sensitive
 
248
        """
 
249
        br1, br2 = make_branches(self)
 
250
        source = MultipleRevisionSources(br1.repository, br2.repository)
 
251
        combined_1 = combined_graph(br1.last_revision(),
 
252
                                    br2.last_revision(), source)
 
253
        combined_2 = combined_graph(br2.last_revision(),
 
254
                                    br1.last_revision(), source)
 
255
        self.assertEquals(combined_1[1], combined_2[1])
 
256
        self.assertEquals(combined_1[2], combined_2[2])
 
257
        self.assertEquals(combined_1[3], combined_2[3])
 
258
        self.assertEquals(combined_1, combined_2)
 
259
 
168
260
    def test_get_history(self):
169
261
        # TODO: test ghosts on the left hand branch's impact
170
262
        # TODO: test ghosts on all parents, we should get some
184
276
        history = rev.get_history(tree.branch.repository)
185
277
        self.assertEqual([None, '1', '2' ,'3'], history)
186
278
 
 
279
    def test_common_ancestor_rootless_graph(self):
 
280
        # common_ancestor on a graph with no reachable roots - only
 
281
        # ghosts - should still return a useful value.
 
282
        graph = Graph()
 
283
        # add a ghost node which would be a root if it wasn't a ghost.
 
284
        graph.add_ghost('a_ghost')
 
285
        # add a normal commit on top of that
 
286
        graph.add_node('rev1', ['a_ghost'])
 
287
        # add a left-branch revision
 
288
        graph.add_node('left', ['rev1'])
 
289
        # add a right-branch revision
 
290
        graph.add_node('right', ['rev1'])
 
291
        source = MockRevisionSource(graph)
 
292
        self.assertEqual('rev1', common_ancestor('left', 'right', source))
 
293
 
 
294
 
 
295
class TestMultipleRevisionSources(TestCaseWithTransport):
 
296
    """Tests for the MultipleRevisionSources adapter."""
 
297
 
 
298
    def test_get_revision_graph_merges_ghosts(self):
 
299
        # when we ask for the revision graph for B, which
 
300
        # is in repo 1 with a ghost of A, and which is not
 
301
        # in repo 2, which has A, the revision_graph()
 
302
        # should return A and B both.
 
303
        tree_1 = self.make_branch_and_tree('1')
 
304
        tree_1.set_parent_ids(['A'], allow_leftmost_as_ghost=True)
 
305
        tree_1.commit('foo', rev_id='B', allow_pointless=True)
 
306
        tree_2 = self.make_branch_and_tree('2')
 
307
        tree_2.commit('bar', rev_id='A', allow_pointless=True)
 
308
        source = MultipleRevisionSources(tree_1.branch.repository,
 
309
                                         tree_2.branch.repository)
 
310
        self.assertEqual({'B':['A'],
 
311
                          'A':[]},
 
312
                         source.get_revision_graph('B'))
 
313
 
187
314
 
188
315
class TestReservedId(TestCase):
189
316
 
208
335
        self.assertEqual('a', r.get_summary())
209
336
        r.message = '\na\nb'
210
337
        self.assertEqual('a', r.get_summary())
211
 
        r.message = None
212
 
        self.assertEqual('', r.get_summary())
213
338
 
214
339
    def test_get_apparent_author(self):
215
340
        r = revision.Revision('1')
216
341
        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())
 
342
        self.assertEqual('A', r.get_apparent_author())
 
343
        r.properties['author'] = 'B'
 
344
        self.assertEqual('B', r.get_apparent_author())