/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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