1
# Copyright (C) 2007-2010 Canonical Ltd
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.
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.
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
18
"""Tests of the 'brz bisect' command."""
20
from __future__ import absolute_import
30
TestCaseWithTransport,
33
from ...bzr.bzrdir import BzrDir
36
class BisectTestCase(TestCaseWithTransport):
37
"""Test harness specific to the bisect plugin."""
39
def assertRevno(self, rev):
40
"""Make sure we're at the right revision."""
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",
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"
57
# These tests assume a branch with five revisions, and
58
# a branch from version 1 containing three revisions
59
# merged at version 2.
61
TestCaseWithTransport.setUp(self)
63
self.tree = self.make_branch_and_tree(".")
65
test_file = open("test_file", "w")
66
test_file.write("one")
68
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
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(),
75
self.tree.commit(message="add test files")
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)
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()
90
self.tree.merge_from_branch(clone_tree.branch)
91
test_file = open("test_file", "w")
92
test_file.write("two")
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")
100
self.subtree_rev = saved_subtree_revid
102
file_contents = ["three", "four", "five"]
103
for content in file_contents:
104
test_file = open("test_file", "w")
105
test_file.write(content)
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")
112
def testWorkflow(self):
113
"""Run through a basic usage scenario."""
115
# Start up the bisection. When the two ends are set, we should
116
# end up in the middle.
118
self.run_bzr(['bisect', 'start'])
119
self.run_bzr(['bisect', 'yes'])
120
self.run_bzr(['bisect', 'no', '-r', '1'])
123
# Mark feature as present in the middle. Should move us
124
# halfway back between the current middle and the start.
126
self.run_bzr(['bisect', 'yes'])
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
134
self.run_bzr(['bisect', 'no'])
137
# Run again. Since we're done, this should do nothing.
139
self.run_bzr(['bisect', 'no'])
142
def testWorkflowSubtree(self):
143
"""Run through a usage scenario where the offending change
146
# Similar to testWorkflow, but make sure the plugin traverses
147
# subtrees when the "final" revision is a merge point.
149
# This part is similar to testWorkflow.
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'])
156
# Check to make sure we're where we expect to be.
160
# Now, mark the merge point revno, meaning the feature
161
# appeared at a merge point.
163
self.run_bzr(['bisect', 'yes'])
164
self.assertRevno(1.2)
166
# Continue bisecting along the subtree to the real conclusion.
168
self.run_bzr(['bisect', 'yes'])
169
self.assertRevno(1.1)
170
self.run_bzr(['bisect', 'yes'])
171
self.assertRevno(1.1)
173
# Run again. Since we're done, this should do nothing.
175
self.run_bzr(['bisect', 'yes'])
176
self.assertRevno(1.1)
179
"""Test manually moving to a different revision during the bisection."""
181
# Set up a bisection in progress.
183
self.run_bzr(['bisect', 'start'])
184
self.run_bzr(['bisect', 'yes'])
185
self.run_bzr(['bisect', 'no', '-r', '1'])
189
self.run_bzr(['bisect', 'move', '-r', '2'])
193
"""Test resetting the tree."""
195
# Set up a bisection in progress.
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'])
204
self.run_bzr(['bisect', 'reset'])
207
# Check that reset doesn't do anything unless there's a
208
# bisection in progress.
210
test_file = open("test_file", "w")
211
test_file.write("keep me")
214
out, err = self.run_bzr(['bisect', 'reset'], retcode=3)
215
self.assertIn("No bisection in progress.", err)
217
test_file = open("test_file")
218
content = test_file.read().strip()
220
self.assertEqual(content, "keep me")
223
"""Test saving the current bisection state, and re-loading it."""
225
# Set up a bisection in progress.
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'])
234
self.run_bzr(['bisect', 'log', '-o', 'bisect_log'])
238
self.run_bzr(['bisect', 'reset'])
242
self.run_bzr(['bisect', 'replay', 'bisect_log'])
245
# Mark another state, and see if the bisect moves in the
248
self.run_bzr(['bisect', 'no'])
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")
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'])
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")
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'])
279
except AssertionError:
280
raise KnownFailure("bisect does not drill down into merge commits: "
281
"https://bugs.launchpad.net/bzr-bisect/+bug/539937")
283
def testRunScriptSubtree(self):
284
"""Make a test script and run it."""
285
if sys.platform == "win32":
286
raise TestSkipped("Unable to run shell script on windows")
287
test_script = open("test_script", "w")
288
test_script.write("#!/bin/sh\n"
289
"grep -q '^one dot two' test_file_append\n")
291
os.chmod("test_script", stat.S_IRWXU)
292
self.run_bzr(['bisect', 'start'])
293
self.run_bzr(['bisect', 'yes'])
294
self.run_bzr(['bisect', 'no', '-r', '1'])
295
self.run_bzr(['bisect', 'run', './test_script'])
297
self.assertRevno(1.2)
298
except AssertionError:
299
raise KnownFailure("bisect does not drill down into merge commits: "
300
"https://bugs.launchpad.net/bzr-bisect/+bug/539937")