/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6656.2.1 by Jelmer Vernooij
Integrate bisect command into core.
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
18
19
"""Tests of the 'brz bisect' command."""
20
21
from __future__ import absolute_import
22
23
import os
24
import shutil
25
import stat
26
import sys
27
28
29
from .. import (
30
    KnownFailure,
31
    TestCaseWithTransport,
32
    )
6670.4.6 by Jelmer Vernooij
Fix some more imports.
33
from ...bzr.bzrdir import BzrDir
6656.2.1 by Jelmer Vernooij
Integrate bisect command into core.
34
35
36
class BisectTestCase(TestCaseWithTransport):
37
    """Test harness specific to the bisect plugin."""
38
39
    def assertRevno(self, rev):
40
        """Make sure we're at the right revision."""
41
42
        rev_contents = {1: "one", 1.1: "one dot one", 1.2: "one dot two",
43
                        1.3: "one dot three", 2: "two", 3: "three",
44
                        4: "four", 5: "five"}
45
46
        test_file = open("test_file")
47
        content = test_file.read().strip()
48
        if content != rev_contents[rev]:
49
            rev_ids = dict((rev_contents[k], k) for k in rev_contents.keys())
50
            found_rev = rev_ids[content]
51
            raise AssertionError("expected rev %0.1f, found rev %0.1f"
52
                                 % (rev, found_rev))
53
54
    def setUp(self):
55
        """Set up tests."""
56
57
        # These tests assume a branch with five revisions, and
58
        # a branch from version 1 containing three revisions
59
        # merged at version 2.
60
61
        TestCaseWithTransport.setUp(self)
62
63
        self.tree = self.make_branch_and_tree(".")
64
65
        test_file = open("test_file", "w")
66
        test_file.write("one")
67
        test_file.close()
68
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
69
                                                     'test_file')))
70
        test_file_append = open("test_file_append", "a")
71
        test_file_append.write("one\n")
72
        test_file_append.close()
73
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
74
                                                     'test_file_append')))
75
        self.tree.commit(message = "add test files")
76
77
        BzrDir.open(".").sprout("../temp-clone")
78
        clone_bzrdir = BzrDir.open("../temp-clone")
79
        clone_tree = clone_bzrdir.open_workingtree()
80
        for content in ["one dot one", "one dot two", "one dot three"]:
81
            test_file = open("../temp-clone/test_file", "w")
82
            test_file.write(content)
83
            test_file.close()
84
            test_file_append = open("../temp-clone/test_file_append", "a")
85
            test_file_append.write(content + "\n")
86
            test_file_append.close()
87
            clone_tree.commit(message = "make branch test change")
88
            saved_subtree_revid = clone_tree.branch.last_revision()
89
90
        self.tree.merge_from_branch(clone_tree.branch)
91
        test_file = open("test_file", "w")
92
        test_file.write("two")
93
        test_file.close()
94
        test_file_append = open("test_file_append", "a")
95
        test_file_append.write("two\n")
96
        test_file_append.close()
97
        self.tree.commit(message = "merge external branch")
98
        shutil.rmtree("../temp-clone")
99
100
        self.subtree_rev = saved_subtree_revid
101
102
        file_contents = ["three", "four", "five"]
103
        for content in file_contents:
104
            test_file = open("test_file", "w")
105
            test_file.write(content)
106
            test_file.close()
107
            test_file_append = open("test_file_append", "a")
108
            test_file_append.write(content + "\n")
109
            test_file_append.close()
110
            self.tree.commit(message = "make test change")
111
112
    def testWorkflow(self):
113
        """Run through a basic usage scenario."""
114
115
        # Start up the bisection.  When the two ends are set, we should
116
        # end up in the middle.
117
118
        self.run_bzr(['bisect', 'start'])
119
        self.run_bzr(['bisect', 'yes'])
120
        self.run_bzr(['bisect', 'no', '-r', '1'])
121
        self.assertRevno(3)
122
123
        # Mark feature as present in the middle.  Should move us
124
        # halfway back between the current middle and the start.
125
126
        self.run_bzr(['bisect', 'yes'])
127
        self.assertRevno(2)
128
129
        # Mark feature as not present.  Since this is only one
130
        # rev back from the lowest marked revision with the feature,
131
        # the process should end, with the current rev set to the
132
        # rev following.
133
134
        self.run_bzr(['bisect', 'no'])
135
        self.assertRevno(3)
136
137
        # Run again.  Since we're done, this should do nothing.
138
139
        self.run_bzr(['bisect', 'no'])
140
        self.assertRevno(3)
141
142
    def testWorkflowSubtree(self):
143
        """Run through a usage scenario where the offending change
144
        is in a subtree."""
145
146
        # Similar to testWorkflow, but make sure the plugin traverses
147
        # subtrees when the "final" revision is a merge point.
148
149
        # This part is similar to testWorkflow.
150
151
        self.run_bzr(['bisect', 'start'])
152
        self.run_bzr(['bisect', 'yes'])
153
        self.run_bzr(['bisect', 'no', '-r', '1'])
154
        self.run_bzr(['bisect', 'yes'])
155
156
        # Check to make sure we're where we expect to be.
157
158
        self.assertRevno(2)
159
160
        # Now, mark the merge point revno, meaning the feature
161
        # appeared at a merge point.
162
163
        self.run_bzr(['bisect', 'yes'])
164
        self.assertRevno(1.2)
165
166
        # Continue bisecting along the subtree to the real conclusion.
167
168
        self.run_bzr(['bisect', 'yes'])
169
        self.assertRevno(1.1)
170
        self.run_bzr(['bisect', 'yes'])
171
        self.assertRevno(1.1)
172
173
        # Run again.  Since we're done, this should do nothing.
174
175
        self.run_bzr(['bisect', 'yes'])
176
        self.assertRevno(1.1)
177
178
    def testMove(self):
179
        """Test manually moving to a different revision during the bisection."""
180
181
        # Set up a bisection in progress.
182
183
        self.run_bzr(['bisect', 'start'])
184
        self.run_bzr(['bisect', 'yes'])
185
        self.run_bzr(['bisect', 'no', '-r', '1'])
186
187
        # Move.
188
189
        self.run_bzr(['bisect', 'move', '-r', '2'])
190
        self.assertRevno(2)
191
192
    def testReset(self):
193
        """Test resetting the tree."""
194
195
        # Set up a bisection in progress.
196
197
        self.run_bzr(['bisect', 'start'])
198
        self.run_bzr(['bisect', 'yes'])
199
        self.run_bzr(['bisect', 'no', '-r', '1'])
200
        self.run_bzr(['bisect', 'yes'])
201
202
        # Now reset.
203
204
        self.run_bzr(['bisect', 'reset'])
205
        self.assertRevno(5)
206
207
        # Check that reset doesn't do anything unless there's a
208
        # bisection in progress.
209
210
        test_file = open("test_file", "w")
211
        test_file.write("keep me")
212
        test_file.close()
213
214
        out, err = self.run_bzr(['bisect', 'reset'], retcode=3)
215
        self.assertIn("No bisection in progress.", err)
216
217
        test_file = open("test_file")
218
        content = test_file.read().strip()
219
        test_file.close()
220
        self.assertEqual(content, "keep me")
221
222
    def testLog(self):
223
        """Test saving the current bisection state, and re-loading it."""
224
225
        # Set up a bisection in progress.
226
227
        self.run_bzr(['bisect', 'start'])
228
        self.run_bzr(['bisect', 'yes'])
229
        self.run_bzr(['bisect', 'no', '-r', '1'])
230
        self.run_bzr(['bisect', 'yes'])
231
232
        # Now save the log.
233
234
        self.run_bzr(['bisect', 'log', '-o', 'bisect_log'])
235
236
        # Reset.
237
238
        self.run_bzr(['bisect', 'reset'])
239
240
        # Read it back in.
241
242
        self.run_bzr(['bisect', 'replay', 'bisect_log'])
243
        self.assertRevno(2)
244
245
        # Mark another state, and see if the bisect moves in the
246
        # right way.
247
248
        self.run_bzr(['bisect', 'no'])
249
        self.assertRevno(3)
250
251
    def testRunScript(self):
252
        """Make a test script and run it."""
253
        test_script = open("test_script", "w")
254
        test_script.write("#!/bin/sh\n"
255
                          "grep -q '^four' test_file_append\n")
256
        test_script.close()
257
        os.chmod("test_script", stat.S_IRWXU)
258
        self.run_bzr(['bisect', 'start'])
259
        self.run_bzr(['bisect', 'yes'])
260
        self.run_bzr(['bisect', 'no', '-r', '1'])
261
        self.run_bzr(['bisect', 'run', './test_script'])
262
        self.assertRevno(4)
263
264
    def testRunScriptMergePoint(self):
265
        """Make a test script and run it."""
266
        if sys.platform == "win32":
267
            raise TestSkipped("Unable to run shell script on windows")
268
        test_script = open("test_script", "w")
269
        test_script.write("#!/bin/sh\n"
270
                          "grep -q '^two' test_file_append\n")
271
        test_script.close()
272
        os.chmod("test_script", stat.S_IRWXU)
273
        self.run_bzr(['bisect', 'start'])
274
        self.run_bzr(['bisect', 'yes'])
275
        self.run_bzr(['bisect', 'no', '-r', '1'])
276
        self.run_bzr(['bisect', 'run', './test_script'])
277
        try:
278
            self.assertRevno(2)
279
        except AssertionError:
280
            raise KnownFailure\
281
                ("bisect does not drill down into merge commits: "
282
                 "https://bugs.launchpad.net/bzr-bisect/+bug/539937")
283
284
    def testRunScriptSubtree(self):
285
        """Make a test script and run it."""
286
        if sys.platform == "win32":
287
            raise TestSkipped("Unable to run shell script on windows")
288
        test_script = open("test_script", "w")
289
        test_script.write("#!/bin/sh\n"
290
                          "grep -q '^one dot two' test_file_append\n")
291
        test_script.close()
292
        os.chmod("test_script", stat.S_IRWXU)
293
        self.run_bzr(['bisect', 'start'])
294
        self.run_bzr(['bisect', 'yes'])
295
        self.run_bzr(['bisect', 'no', '-r', '1'])
296
        self.run_bzr(['bisect', 'run', './test_script'])
297
        try:
298
            self.assertRevno(1.2)
299
        except AssertionError:
300
            raise KnownFailure\
301
                ("bisect does not drill down into merge commits: "
302
                 "https://bugs.launchpad.net/bzr-bisect/+bug/539937")