/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testrevision.py

  • Committer: Martin Pool
  • Date: 2005-09-16 04:19:49 UTC
  • Revision ID: mbp@sourcefrog.net-20050916041949-b6a152f4affa4d78
- notes on conversion of existing history to weaves

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
import os
 
19
 
 
20
from bzrlib.selftest import TestCaseInTempDir
 
21
from bzrlib.branch import Branch
 
22
from bzrlib.commit import commit
 
23
from bzrlib.fetch import fetch
 
24
 
 
25
def make_branches():
 
26
    os.mkdir("branch1")
 
27
    br1 = Branch("branch1", init=True)
 
28
    
 
29
    commit(br1, "Commit one", rev_id="a@u-0-0")
 
30
    commit(br1, "Commit two", rev_id="a@u-0-1")
 
31
    commit(br1, "Commit three", rev_id="a@u-0-2")
 
32
 
 
33
    os.mkdir("branch2")
 
34
    br2 = Branch("branch2", init=True)
 
35
    br2.update_revisions(br1)
 
36
    commit(br2, "Commit four", rev_id="b@u-0-3")
 
37
    commit(br2, "Commit five", rev_id="b@u-0-4")
 
38
    revisions_2 = br2.revision_history()
 
39
    
 
40
    fetch(from_branch=br2, to_branch=br1)
 
41
    br1.add_pending_merge(revisions_2[4])
 
42
    commit(br1, "Commit six", rev_id="a@u-0-3")
 
43
    commit(br1, "Commit seven", rev_id="a@u-0-4")
 
44
    commit(br2, "Commit eight", rev_id="b@u-0-5")
 
45
    
 
46
    fetch(from_branch=br2, to_branch=br1)
 
47
    br1.add_pending_merge(br2.revision_history()[5])
 
48
    commit(br1, "Commit nine", rev_id="a@u-0-5")
 
49
 
 
50
    fetch(from_branch=br1, to_branch=br2)
 
51
    br2.add_pending_merge(br1.revision_history()[4])
 
52
    commit(br2, "Commit ten", rev_id="b@u-0-6")
 
53
    
 
54
    return br1, br2
 
55
 
 
56
 
 
57
class TestIsAncestor(TestCaseInTempDir):
 
58
    def test_is_ancestor(self):
 
59
        """Test checking whether a revision is an ancestor of another revision"""
 
60
        from bzrlib.revision import is_ancestor, MultipleRevisionSources
 
61
        from bzrlib.errors import NoSuchRevision
 
62
        br1, br2 = make_branches()
 
63
        revisions = br1.revision_history()
 
64
        revisions_2 = br2.revision_history()
 
65
        sources = MultipleRevisionSources(br1, br2)
 
66
 
 
67
        assert is_ancestor(revisions[0], revisions[0], sources)
 
68
        assert is_ancestor(revisions[1], revisions[0], sources)
 
69
        assert not is_ancestor(revisions[0], revisions[1], sources)
 
70
        assert is_ancestor(revisions_2[3], revisions[0], sources)
 
71
        self.assertRaises(NoSuchRevision, is_ancestor, revisions_2[3],
 
72
                          revisions[0], br1)        
 
73
        assert is_ancestor(revisions[3], revisions_2[4], sources)
 
74
        assert is_ancestor(revisions[3], revisions_2[4], br1)
 
75
        assert is_ancestor(revisions[3], revisions_2[3], sources)
 
76
        assert not is_ancestor(revisions[3], revisions_2[3], br1)
 
77
 
 
78
class TestIntermediateRevisions(TestCaseInTempDir):
 
79
 
 
80
    def setUp(self):
 
81
        from bzrlib.commit import commit
 
82
        TestCaseInTempDir.setUp(self)
 
83
        self.br1, self.br2 = make_branches()
 
84
        commit(self.br2, "Commit eleven", rev_id="b@u-0-7")
 
85
        commit(self.br2, "Commit twelve", rev_id="b@u-0-8")
 
86
        commit(self.br2, "Commit thirtteen", rev_id="b@u-0-9")
 
87
        self.br1.add_pending_merge(self.br2.revision_history()[6])
 
88
        commit(self.br1, "Commit fourtten", rev_id="a@u-0-6")
 
89
        self.br2.add_pending_merge(self.br1.revision_history()[6])
 
90
        commit(self.br2, "Commit fifteen", rev_id="b@u-0-10")
 
91
 
 
92
        from bzrlib.revision import MultipleRevisionSources
 
93
        self.sources = MultipleRevisionSources(self.br1, self.br2)
 
94
 
 
95
    def intervene(self, ancestor, revision, revision_history=None):
 
96
        from bzrlib.revision import get_intervening_revisions
 
97
        return get_intervening_revisions(ancestor,revision, self.sources, 
 
98
                                         revision_history)
 
99
 
 
100
    def test_intervene(self):
 
101
        """Find intermediate revisions, without requiring history"""
 
102
        from bzrlib.errors import NotAncestor, NoSuchRevision
 
103
        assert len(self.intervene('a@u-0-0', 'a@u-0-0')) == 0
 
104
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-1'), ['a@u-0-1'])
 
105
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-2'), 
 
106
                         ['a@u-0-1', 'a@u-0-2'])
 
107
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-3'), 
 
108
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3'])
 
109
        self.assertEqual(self.intervene('b@u-0-3', 'a@u-0-3'), 
 
110
                         ['b@u-0-4', 'a@u-0-3'])
 
111
        self.assertEqual(self.intervene('a@u-0-2', 'a@u-0-3', 
 
112
                                        self.br1.revision_history()), 
 
113
                         ['a@u-0-3'])
 
114
        self.assertEqual(self.intervene('a@u-0-0', 'a@u-0-5', 
 
115
                                        self.br1.revision_history()), 
 
116
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
117
                          'a@u-0-5'])
 
118
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-6', 
 
119
                         self.br1.revision_history()), 
 
120
                         ['a@u-0-1', 'a@u-0-2', 'a@u-0-3', 'a@u-0-4', 
 
121
                          'b@u-0-6'])
 
122
        self.assertEqual(self.intervene('a@u-0-0', 'b@u-0-5'), 
 
123
                         ['a@u-0-1', 'a@u-0-2', 'b@u-0-3', 'b@u-0-4', 
 
124
                          'b@u-0-5'])
 
125
        self.assertEqual(self.intervene('b@u-0-3', 'b@u-0-6', 
 
126
                         self.br2.revision_history()), 
 
127
                         ['b@u-0-4', 'b@u-0-5', 'b@u-0-6'])
 
128
        self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10'), 
 
129
                         ['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
 
130
        self.assertEqual(self.intervene('b@u-0-6', 'b@u-0-10', 
 
131
                                        self.br2.revision_history()), 
 
132
                         ['b@u-0-7', 'b@u-0-8', 'b@u-0-9', 'b@u-0-10'])
 
133
        self.assertRaises(NotAncestor, self.intervene, 'b@u-0-10', 'b@u-0-6', 
 
134
                          self.br2.revision_history())
 
135
        self.assertRaises(NoSuchRevision, self.intervene, 'c@u-0-10', 
 
136
                          'b@u-0-6', self.br2.revision_history())
 
137
        self.assertRaises(NoSuchRevision, self.intervene, 'b@u-0-10', 
 
138
                          'c@u-0-6', self.br2.revision_history())
 
139
 
 
140
 
 
141
class TestCommonAncestor(TestCaseInTempDir):
 
142
    """Test checking whether a revision is an ancestor of another revision"""
 
143
 
 
144
    def test_common_ancestor(self):
 
145
        from bzrlib.revision import find_present_ancestors, common_ancestor
 
146
        from bzrlib.revision import MultipleRevisionSources
 
147
        br1, br2 = make_branches()
 
148
        revisions = br1.revision_history()
 
149
        revisions_2 = br2.revision_history()
 
150
        sources = MultipleRevisionSources(br1, br2)
 
151
 
 
152
        expected_ancestors_list = {revisions[3]:(0, 0), 
 
153
                                   revisions[2]:(1, 1),
 
154
                                   revisions_2[4]:(2, 1), 
 
155
                                   revisions[1]:(3, 2),
 
156
                                   revisions_2[3]:(4, 2),
 
157
                                   revisions[0]:(5, 3) }
 
158
        ancestors_list = find_present_ancestors(revisions[3], sources)
 
159
        assert len(expected_ancestors_list) == len(ancestors_list)
 
160
        for key, value in expected_ancestors_list.iteritems():
 
161
            self.assertEqual(ancestors_list[key], value, 
 
162
                              "key %r, %r != %r" % (key, ancestors_list[key],
 
163
                                                    value))
 
164
 
 
165
        self.assertEqual(common_ancestor(revisions[0], revisions[0], sources),
 
166
                          revisions[0])
 
167
        self.assertEqual(common_ancestor(revisions[1], revisions[2], sources),
 
168
                          revisions[1])
 
169
        self.assertEqual(common_ancestor(revisions[1], revisions[1], sources),
 
170
                          revisions[1])
 
171
        self.assertEqual(common_ancestor(revisions[2], revisions_2[4], sources),
 
172
                          revisions[2])
 
173
        self.assertEqual(common_ancestor(revisions[3], revisions_2[4], sources),
 
174
                          revisions_2[4])
 
175
        self.assertEqual(common_ancestor(revisions[4], revisions_2[5], sources),
 
176
                          revisions_2[4])
 
177
        self.assertEqual(common_ancestor(revisions[5], revisions_2[6], sources),
 
178
                          revisions[4])
 
179
        self.assertEqual(common_ancestor(revisions_2[6], revisions[5], sources),
 
180
                          revisions_2[5])
 
181
 
 
182
 
 
183
if __name__ == '__main__':
 
184
    import unittest, sys
 
185
    unittest.main()
 
186