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,
32
from ...bzr.bzrdir import BzrDir
35
class BisectTestCase(TestCaseWithTransport):
36
"""Test harness specific to the bisect plugin."""
38
def assertRevno(self, rev):
39
"""Make sure we're at the right revision."""
41
rev_contents = {1: "one", 1.1: "one dot one", 1.2: "one dot two",
42
1.3: "one dot three", 2: "two", 3: "three",
45
test_file = open("test_file")
46
content = test_file.read().strip()
47
if content != rev_contents[rev]:
48
rev_ids = dict((rev_contents[k], k) for k in rev_contents.keys())
49
found_rev = rev_ids[content]
50
raise AssertionError("expected rev %0.1f, found rev %0.1f"
56
# These tests assume a branch with five revisions, and
57
# a branch from version 1 containing three revisions
58
# merged at version 2.
60
TestCaseWithTransport.setUp(self)
62
self.tree = self.make_branch_and_tree(".")
64
test_file = open("test_file", "w")
65
test_file.write("one")
67
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
69
test_file_append = open("test_file_append", "a")
70
test_file_append.write("one\n")
71
test_file_append.close()
72
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
74
self.tree.commit(message="add test files")
76
BzrDir.open(".").sprout("../temp-clone")
77
clone_bzrdir = BzrDir.open("../temp-clone")
78
clone_tree = clone_bzrdir.open_workingtree()
79
for content in ["one dot one", "one dot two", "one dot three"]:
80
test_file = open("../temp-clone/test_file", "w")
81
test_file.write(content)
83
test_file_append = open("../temp-clone/test_file_append", "a")
84
test_file_append.write(content + "\n")
85
test_file_append.close()
86
clone_tree.commit(message="make branch test change")
87
saved_subtree_revid = clone_tree.branch.last_revision()
89
self.tree.merge_from_branch(clone_tree.branch)
90
test_file = open("test_file", "w")
91
test_file.write("two")
93
test_file_append = open("test_file_append", "a")
94
test_file_append.write("two\n")
95
test_file_append.close()
96
self.tree.commit(message="merge external branch")
97
shutil.rmtree("../temp-clone")
99
self.subtree_rev = saved_subtree_revid
101
file_contents = ["three", "four", "five"]
102
for content in file_contents:
103
test_file = open("test_file", "w")
104
test_file.write(content)
106
test_file_append = open("test_file_append", "a")
107
test_file_append.write(content + "\n")
108
test_file_append.close()
109
self.tree.commit(message="make test change")
111
def testWorkflow(self):
112
"""Run through a basic usage scenario."""
114
# Start up the bisection. When the two ends are set, we should
115
# end up in the middle.
117
self.run_bzr(['bisect', 'start'])
118
self.run_bzr(['bisect', 'yes'])
119
self.run_bzr(['bisect', 'no', '-r', '1'])
122
# Mark feature as present in the middle. Should move us
123
# halfway back between the current middle and the start.
125
self.run_bzr(['bisect', 'yes'])
128
# Mark feature as not present. Since this is only one
129
# rev back from the lowest marked revision with the feature,
130
# the process should end, with the current rev set to the
133
self.run_bzr(['bisect', 'no'])
136
# Run again. Since we're done, this should do nothing.
138
self.run_bzr(['bisect', 'no'])
141
def testWorkflowSubtree(self):
142
"""Run through a usage scenario where the offending change
145
# Similar to testWorkflow, but make sure the plugin traverses
146
# subtrees when the "final" revision is a merge point.
148
# This part is similar to testWorkflow.
150
self.run_bzr(['bisect', 'start'])
151
self.run_bzr(['bisect', 'yes'])
152
self.run_bzr(['bisect', 'no', '-r', '1'])
153
self.run_bzr(['bisect', 'yes'])
155
# Check to make sure we're where we expect to be.
159
# Now, mark the merge point revno, meaning the feature
160
# appeared at a merge point.
162
self.run_bzr(['bisect', 'yes'])
163
self.assertRevno(1.2)
165
# Continue bisecting along the subtree to the real conclusion.
167
self.run_bzr(['bisect', 'yes'])
168
self.assertRevno(1.1)
169
self.run_bzr(['bisect', 'yes'])
170
self.assertRevno(1.1)
172
# Run again. Since we're done, this should do nothing.
174
self.run_bzr(['bisect', 'yes'])
175
self.assertRevno(1.1)
178
"""Test manually moving to a different revision during the bisection."""
180
# Set up a bisection in progress.
182
self.run_bzr(['bisect', 'start'])
183
self.run_bzr(['bisect', 'yes'])
184
self.run_bzr(['bisect', 'no', '-r', '1'])
188
self.run_bzr(['bisect', 'move', '-r', '2'])
192
"""Test resetting the tree."""
194
# Set up a bisection in progress.
196
self.run_bzr(['bisect', 'start'])
197
self.run_bzr(['bisect', 'yes'])
198
self.run_bzr(['bisect', 'no', '-r', '1'])
199
self.run_bzr(['bisect', 'yes'])
203
self.run_bzr(['bisect', 'reset'])
206
# Check that reset doesn't do anything unless there's a
207
# bisection in progress.
209
test_file = open("test_file", "w")
210
test_file.write("keep me")
213
out, err = self.run_bzr(['bisect', 'reset'], retcode=3)
214
self.assertIn("No bisection in progress.", err)
216
test_file = open("test_file")
217
content = test_file.read().strip()
219
self.assertEqual(content, "keep me")
222
"""Test saving the current bisection state, and re-loading it."""
224
# Set up a bisection in progress.
226
self.run_bzr(['bisect', 'start'])
227
self.run_bzr(['bisect', 'yes'])
228
self.run_bzr(['bisect', 'no', '-r', '1'])
229
self.run_bzr(['bisect', 'yes'])
233
self.run_bzr(['bisect', 'log', '-o', 'bisect_log'])
237
self.run_bzr(['bisect', 'reset'])
241
self.run_bzr(['bisect', 'replay', 'bisect_log'])
244
# Mark another state, and see if the bisect moves in the
247
self.run_bzr(['bisect', 'no'])
250
def testRunScript(self):
251
"""Make a test script and run it."""
252
test_script = open("test_script", "w")
253
test_script.write("#!/bin/sh\n"
254
"grep -q '^four' test_file_append\n")
256
os.chmod("test_script", stat.S_IRWXU)
257
self.run_bzr(['bisect', 'start'])
258
self.run_bzr(['bisect', 'yes'])
259
self.run_bzr(['bisect', 'no', '-r', '1'])
260
self.run_bzr(['bisect', 'run', './test_script'])
263
def testRunScriptMergePoint(self):
264
"""Make a test script and run it."""
265
if sys.platform == "win32":
266
raise TestSkipped("Unable to run shell script on windows")
267
test_script = open("test_script", "w")
268
test_script.write("#!/bin/sh\n"
269
"grep -q '^two' test_file_append\n")
271
os.chmod("test_script", stat.S_IRWXU)
272
self.run_bzr(['bisect', 'start'])
273
self.run_bzr(['bisect', 'yes'])
274
self.run_bzr(['bisect', 'no', '-r', '1'])
275
self.run_bzr(['bisect', 'run', './test_script'])
278
except AssertionError:
279
raise KnownFailure("bisect does not drill down into merge commits: "
280
"https://bugs.launchpad.net/bzr-bisect/+bug/539937")
282
def testRunScriptSubtree(self):
283
"""Make a test script and run it."""
284
if sys.platform == "win32":
285
raise TestSkipped("Unable to run shell script on windows")
286
test_script = open("test_script", "w")
287
test_script.write("#!/bin/sh\n"
288
"grep -q '^one dot two' test_file_append\n")
290
os.chmod("test_script", stat.S_IRWXU)
291
self.run_bzr(['bisect', 'start'])
292
self.run_bzr(['bisect', 'yes'])
293
self.run_bzr(['bisect', 'no', '-r', '1'])
294
self.run_bzr(['bisect', 'run', './test_script'])
296
self.assertRevno(1.2)
297
except AssertionError:
298
raise KnownFailure("bisect does not drill down into merge commits: "
299
"https://bugs.launchpad.net/bzr-bisect/+bug/539937")