/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 breezy/tests/test_bisect.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 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 suite for the bzr bisect plugin."
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from ..sixish import StringIO
 
22
import os
 
23
import shutil
 
24
 
 
25
from ..controldir import ControlDir
 
26
from .. import bisect
 
27
from . import (
 
28
    TestCaseWithTransport,
 
29
    TestSkipped,
 
30
    )
 
31
 
 
32
 
 
33
class BisectTestCase(TestCaseWithTransport):
 
34
    """Test harness specific to the bisect plugin."""
 
35
 
 
36
    def assertRevno(self, rev):
 
37
        """Make sure we're at the right revision."""
 
38
 
 
39
        rev_contents = {1: "one", 1.1: "one dot one", 1.2: "one dot two",
 
40
                        1.3: "one dot three", 2: "two", 3: "three",
 
41
                        4: "four", 5: "five"}
 
42
 
 
43
        with open("test_file") as f:
 
44
            content = f.read().strip()
 
45
        if content != rev_contents[rev]:
 
46
            rev_ids = dict((rev_contents[k], k) for k in rev_contents)
 
47
            found_rev = rev_ids[content]
 
48
            raise AssertionError("expected rev %0.1f, found rev %0.1f"
 
49
                                 % (rev, found_rev))
 
50
 
 
51
    def setUp(self):
 
52
        """Set up tests."""
 
53
 
 
54
        # These tests assume a branch with five revisions, and
 
55
        # a branch from version 1 containing three revisions
 
56
        # merged at version 2.
 
57
 
 
58
        TestCaseWithTransport.setUp(self)
 
59
 
 
60
        self.tree = self.make_branch_and_tree(".")
 
61
 
 
62
        test_file = open("test_file", "w")
 
63
        test_file.write("one")
 
64
        test_file.close()
 
65
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
 
66
                                                     'test_file')))
 
67
        test_file_append = open("test_file_append", "a")
 
68
        test_file_append.write("one\n")
 
69
        test_file_append.close()
 
70
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
 
71
                                                     'test_file_append')))
 
72
        self.tree.commit(message = "add test files")
 
73
 
 
74
        ControlDir.open(".").sprout("../temp-clone")
 
75
        clone_bzrdir = ControlDir.open("../temp-clone")
 
76
        clone_tree = clone_bzrdir.open_workingtree()
 
77
        for content in ["one dot one", "one dot two", "one dot three"]:
 
78
            test_file = open("../temp-clone/test_file", "w")
 
79
            test_file.write(content)
 
80
            test_file.close()
 
81
            test_file_append = open("../temp-clone/test_file_append", "a")
 
82
            test_file_append.write(content + "\n")
 
83
            test_file_append.close()
 
84
            clone_tree.commit(message = "make branch test change")
 
85
            saved_subtree_revid = clone_tree.branch.last_revision()
 
86
 
 
87
        self.tree.merge_from_branch(clone_tree.branch)
 
88
        test_file = open("test_file", "w")
 
89
        test_file.write("two")
 
90
        test_file.close()
 
91
        test_file_append = open("test_file_append", "a")
 
92
        test_file_append.write("two\n")
 
93
        test_file_append.close()
 
94
        self.tree.commit(message = "merge external branch")
 
95
        shutil.rmtree("../temp-clone")
 
96
 
 
97
        self.subtree_rev = saved_subtree_revid
 
98
 
 
99
        file_contents = ["three", "four", "five"]
 
100
        for content in file_contents:
 
101
            test_file = open("test_file", "w")
 
102
            test_file.write(content)
 
103
            test_file.close()
 
104
            test_file_append = open("test_file_append", "a")
 
105
            test_file_append.write(content + "\n")
 
106
            test_file_append.close()
 
107
            self.tree.commit(message = "make test change")
 
108
 
 
109
 
 
110
class BisectHarnessTests(BisectTestCase):
 
111
    """Tests for the harness itself."""
 
112
 
 
113
    def testLastRev(self):
 
114
        """Test that the last revision is correct."""
 
115
        repo = self.tree.branch.repository
 
116
        top_revtree = repo.revision_tree(self.tree.last_revision())
 
117
        top_revtree.lock_read()
 
118
        top_file = top_revtree.get_file("test_file")
 
119
        test_content = top_file.read().strip()
 
120
        top_file.close()
 
121
        top_revtree.unlock()
 
122
        self.assertEqual(test_content, "five")
 
123
 
 
124
    def testSubtreeRev(self):
 
125
        """Test that the last revision in a subtree is correct."""
 
126
        repo = self.tree.branch.repository
 
127
        sub_revtree = repo.revision_tree(self.subtree_rev)
 
128
        sub_revtree.lock_read()
 
129
        sub_file = sub_revtree.get_file("test_file")
 
130
        test_content = sub_file.read().strip()
 
131
        sub_file.close()
 
132
        sub_revtree.unlock()
 
133
        self.assertEqual(test_content, "one dot three")
 
134
 
 
135
 
 
136
class BisectCurrentUnitTests(BisectTestCase):
 
137
    """Test the BisectCurrent class."""
 
138
 
 
139
    def testShowLog(self):
 
140
        """Test that the log can be shown."""
 
141
        # Not a very good test; just makes sure the code doesn't fail,
 
142
        # not that the output makes any sense.
 
143
        sio = StringIO()
 
144
        bisect.BisectCurrent(self.tree.controldir).show_rev_log(outf=sio)
 
145
 
 
146
    def testShowLogSubtree(self):
 
147
        """Test that a subtree's log can be shown."""
 
148
        current = bisect.BisectCurrent(self.tree.controldir)
 
149
        current.switch(self.subtree_rev)
 
150
        sio = StringIO()
 
151
        current.show_rev_log(outf=sio)
 
152
 
 
153
    def testSwitchVersions(self):
 
154
        """Test switching versions."""
 
155
        current = bisect.BisectCurrent(self.tree.controldir)
 
156
        self.assertRevno(5)
 
157
        current.switch(4)
 
158
        self.assertRevno(4)
 
159
 
 
160
    def testReset(self):
 
161
        """Test resetting the working tree to a non-bisected state."""
 
162
        current = bisect.BisectCurrent(self.tree.controldir)
 
163
        current.switch(4)
 
164
        current.reset()
 
165
        self.assertRevno(5)
 
166
        self.assertFalse(os.path.exists(
 
167
            os.path.join('.bzr', bisect.BISECT_REV_PATH)))
 
168
 
 
169
    def testIsMergePoint(self):
 
170
        """Test merge point detection."""
 
171
        current = bisect.BisectCurrent(self.tree.controldir)
 
172
        self.assertRevno(5)
 
173
        self.assertFalse(current.is_merge_point())
 
174
        current.switch(2)
 
175
        self.assertTrue(current.is_merge_point())
 
176
 
 
177
 
 
178
class BisectLogUnitTests(BisectTestCase):
 
179
    """Test the BisectLog class."""
 
180
 
 
181
    def testCreateBlank(self):
 
182
        """Test creation of new log."""
 
183
        bisect_log = bisect.BisectLog(self.tree.controldir)
 
184
        bisect_log.save()
 
185
        self.assertTrue(
 
186
            os.path.exists(os.path.join('.bzr', bisect.BISECT_INFO_PATH)))
 
187
 
 
188
    def testLoad(self):
 
189
        """Test loading a log."""
 
190
        preloaded_log = open(os.path.join('.bzr', bisect.BISECT_INFO_PATH), "w")
 
191
        preloaded_log.write("rev1 yes\nrev2 no\nrev3 yes\n")
 
192
        preloaded_log.close()
 
193
 
 
194
        bisect_log = bisect.BisectLog(self.tree.controldir)
 
195
        self.assertEqual(len(bisect_log._items), 3)
 
196
        self.assertEqual(bisect_log._items[0], ("rev1", "yes"))
 
197
        self.assertEqual(bisect_log._items[1], ("rev2", "no"))
 
198
        self.assertEqual(bisect_log._items[2], ("rev3", "yes"))
 
199
 
 
200
    def testSave(self):
 
201
        """Test saving the log."""
 
202
        bisect_log = bisect.BisectLog(self.tree.controldir)
 
203
        bisect_log._items = [("rev1", "yes"), ("rev2", "no"), ("rev3", "yes")]
 
204
        bisect_log.save()
 
205
 
 
206
        with open(os.path.join('.bzr', bisect.BISECT_INFO_PATH)) as logfile:
 
207
            self.assertEqual(logfile.read(), "rev1 yes\nrev2 no\nrev3 yes\n")