~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_repository/test_iter_reverse_revision_history.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:56:05 UTC
  • mfrom: (6615.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201195605-o7rl92wf6uyum3fk
(vila) Open trunk again as 2.8b1 (Vincent Ladeuil)

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
 
"""Test iter_reverse_revision_history."""
18
 
 
19
 
from bzrlib import (
20
 
    errors,
21
 
    osutils,
22
 
    tests,
23
 
    )
24
 
from bzrlib.tests.per_repository import TestCaseWithRepository
25
 
 
26
 
 
27
 
class TestIterReverseRevisionHistory(TestCaseWithRepository):
28
 
 
29
 
    def create_linear_history(self):
30
 
        tree = self.make_branch_and_memory_tree('tree')
31
 
        tree.lock_write()
32
 
        try:
33
 
            tree.add('')
34
 
            tree.commit('1', rev_id='rev1')
35
 
            tree.commit('2', rev_id='rev2')
36
 
            tree.commit('3', rev_id='rev3')
37
 
            tree.commit('4', rev_id='rev4')
38
 
        finally:
39
 
            tree.unlock()
40
 
        return tree
41
 
 
42
 
    def create_linear_history_with_utf8(self):
43
 
        tree = self.make_branch_and_memory_tree('tree')
44
 
        tree.lock_write()
45
 
        try:
46
 
            tree.add('') # needed for MemoryTree
47
 
            try:
48
 
                tree.commit(u'F\xb5', rev_id=u'rev-\xb5'.encode('utf8'))
49
 
            except errors.NonAsciiRevisionId:
50
 
                raise tests.TestSkipped("%s doesn't support non-ascii"
51
 
                                        " revision ids."
52
 
                                        % self.repository_format)
53
 
            tree.commit(u'B\xe5r', rev_id=u'rev-\xe5'.encode('utf8'))
54
 
        finally:
55
 
            tree.unlock()
56
 
        return tree
57
 
 
58
 
    def create_merged_history(self):
59
 
        # TODO: jam 20070216 MutableTree doesn't yet have the pull() or
60
 
        #       merge_from_branch() apis. So we have to use real trees for
61
 
        #       this.
62
 
        tree1 = self.make_branch_and_tree('tree1')
63
 
        tree2 = self.make_branch_and_tree('tree2')
64
 
        tree1.lock_write()
65
 
        tree2.lock_write()
66
 
        try:
67
 
            tree1.add('')
68
 
            tree1.commit('rev-1-1', rev_id='rev-1-1')
69
 
            tree2.pull(tree1.branch)
70
 
            tree2.commit('rev-2-2', rev_id='rev-2-2')
71
 
            tree2.commit('rev-2-3', rev_id='rev-2-3')
72
 
            tree2.commit('rev-2-4', rev_id='rev-2-4')
73
 
 
74
 
            tree1.commit('rev-1-2', rev_id='rev-1-2')
75
 
            tree1.flush() # workaround merge using _write_inventory
76
 
            tree1.merge_from_branch(tree2.branch)
77
 
            tree1.commit('rev-1-3', rev_id='rev-1-3')
78
 
 
79
 
            tree2.commit('rev-2-5', rev_id='rev-2-5')
80
 
            # Make sure both repositories have all revisions
81
 
            tree1.branch.repository.fetch(tree2.branch.repository,
82
 
                                          revision_id='rev-2-5')
83
 
            tree2.branch.repository.fetch(tree1.branch.repository,
84
 
                                          revision_id='rev-1-3')
85
 
        finally:
86
 
            tree2.unlock()
87
 
            tree1.unlock()
88
 
        return tree1, tree2
89
 
 
90
 
    def test_is_generator(self):
91
 
        tree = self.create_linear_history()
92
 
        repo = tree.branch.repository
93
 
        repo.lock_read()
94
 
        self.addCleanup(repo.unlock)
95
 
        rev_history = repo.iter_reverse_revision_history('rev4')
96
 
        self.assertEqual('rev4', rev_history.next())
97
 
        self.assertEqual('rev3', rev_history.next())
98
 
        self.assertEqual('rev2', rev_history.next())
99
 
        self.assertEqual('rev1', rev_history.next())
100
 
        self.assertRaises(StopIteration, rev_history.next)
101
 
 
102
 
    def assertRevHistoryList(self, expected, repo, revision_id):
103
 
        """Assert the return values of iter_reverse_revision_history."""
104
 
        repo.lock_read()
105
 
        try:
106
 
            actual = list(repo.iter_reverse_revision_history(revision_id))
107
 
        finally:
108
 
            repo.unlock()
109
 
        self.assertEqual(expected, actual)
110
 
 
111
 
    def test_linear_history(self):
112
 
        tree = self.create_linear_history()
113
 
        repo = tree.branch.repository
114
 
 
115
 
        self.assertRevHistoryList(['rev4', 'rev3', 'rev2', 'rev1'],
116
 
                                  repo, 'rev4')
117
 
 
118
 
    def test_partial_history(self):
119
 
        tree = self.create_linear_history()
120
 
        repo = tree.branch.repository
121
 
 
122
 
        self.assertRevHistoryList(['rev3', 'rev2', 'rev1'], repo, 'rev3')
123
 
 
124
 
    def test_revision_ids_are_utf8(self):
125
 
        tree = self.create_linear_history_with_utf8()
126
 
        repo = tree.branch.repository
127
 
        self.assertRevHistoryList(['rev-\xc3\xa5', 'rev-\xc2\xb5'],
128
 
                                  repo, 'rev-\xc3\xa5')
129
 
 
130
 
    def test_merged_history(self):
131
 
        tree1, tree2 = self.create_merged_history()
132
 
        repo = tree1.branch.repository
133
 
 
134
 
        self.assertRevHistoryList(['rev-1-1'],
135
 
                                  repo, 'rev-1-1')
136
 
        self.assertRevHistoryList(['rev-1-2', 'rev-1-1'],
137
 
                                  repo, 'rev-1-2')
138
 
        self.assertRevHistoryList(['rev-1-3', 'rev-1-2', 'rev-1-1'],
139
 
                                  repo, 'rev-1-3')
140
 
        self.assertRevHistoryList(['rev-2-2', 'rev-1-1'],
141
 
                                  repo, 'rev-2-2')
142
 
        self.assertRevHistoryList(['rev-2-3', 'rev-2-2', 'rev-1-1'],
143
 
                                  repo, 'rev-2-3')
144
 
        self.assertRevHistoryList(['rev-2-4', 'rev-2-3', 'rev-2-2', 'rev-1-1'],
145
 
                                  repo, 'rev-2-4')
146
 
        self.assertRevHistoryList(['rev-2-5', 'rev-2-4', 'rev-2-3', 'rev-2-2',
147
 
                                   'rev-1-1'], repo, 'rev-2-5')
148
 
 
149
 
    def test_ghost(self):
150
 
        tree = self.make_branch_and_memory_tree('tree')
151
 
        tree.lock_write()
152
 
        try:
153
 
            tree.add('')
154
 
            tree.set_parent_ids(['spooky'], allow_leftmost_as_ghost=True)
155
 
            tree.commit('1', rev_id='rev1')
156
 
            tree.commit('2', rev_id='rev2')
157
 
        finally:
158
 
            tree.unlock()
159
 
        iter = tree.branch.repository.iter_reverse_revision_history('rev2')
160
 
        tree.branch.repository.lock_read()
161
 
        try:
162
 
            self.assertEquals('rev2', iter.next())
163
 
            self.assertEquals('rev1', iter.next())
164
 
            self.assertRaises(errors.RevisionNotPresent, iter.next)
165
 
        finally:
166
 
            tree.branch.repository.unlock()
167