/
tests.py
416 lines (327 loc) · 14 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Copyright (C) 2007-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"Test suite for the bzr bisect plugin."
from cStringIO import StringIO
import os
import stat
import sys
import shutil
import bzrlib
from bzrlib.bzrdir import BzrDir
from bzrlib.plugins import bisect
from bzrlib.plugins.bisect import cmds
from bzrlib.tests import (
KnownFailure,
TestCaseWithTransport,
TestSkipped,
)
class BisectTestCase(TestCaseWithTransport):
"""Test harness specific to the bisect plugin."""
def assertRevno(self, rev):
"""Make sure we're at the right revision."""
rev_contents = {1: "one", 1.1: "one dot one", 1.2: "one dot two",
1.3: "one dot three", 2: "two", 3: "three",
4: "four", 5: "five"}
test_file = open("test_file")
content = test_file.read().strip()
if content != rev_contents[rev]:
rev_ids = dict((rev_contents[k], k) for k in rev_contents.keys())
found_rev = rev_ids[content]
raise AssertionError("expected rev %0.1f, found rev %0.1f"
% (rev, found_rev))
def setUp(self):
"""Set up tests."""
# These tests assume a branch with five revisions, and
# a branch from version 1 containing three revisions
# merged at version 2.
TestCaseWithTransport.setUp(self)
self.tree = self.make_branch_and_tree(".")
test_file = open("test_file", "w")
test_file.write("one")
test_file.close()
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
'test_file')))
test_file_append = open("test_file_append", "a")
test_file_append.write("one\n")
test_file_append.close()
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(),
'test_file_append')))
self.tree.commit(message = "add test files")
BzrDir.open(".").sprout("../temp-clone")
clone_bzrdir = BzrDir.open("../temp-clone")
clone_tree = clone_bzrdir.open_workingtree()
for content in ["one dot one", "one dot two", "one dot three"]:
test_file = open("../temp-clone/test_file", "w")
test_file.write(content)
test_file.close()
test_file_append = open("../temp-clone/test_file_append", "a")
test_file_append.write(content + "\n")
test_file_append.close()
clone_tree.commit(message = "make branch test change")
saved_subtree_revid = clone_tree.branch.last_revision()
self.tree.merge_from_branch(clone_tree.branch)
test_file = open("test_file", "w")
test_file.write("two")
test_file.close()
test_file_append = open("test_file_append", "a")
test_file_append.write("two\n")
test_file_append.close()
self.tree.commit(message = "merge external branch")
shutil.rmtree("../temp-clone")
self.subtree_rev = saved_subtree_revid
file_contents = ["three", "four", "five"]
for content in file_contents:
test_file = open("test_file", "w")
test_file.write(content)
test_file.close()
test_file_append = open("test_file_append", "a")
test_file_append.write(content + "\n")
test_file_append.close()
self.tree.commit(message = "make test change")
class BisectHarnessTests(BisectTestCase):
"""Tests for the harness itself."""
def testLastRev(self):
"""Test that the last revision is correct."""
repo = self.tree.branch.repository
top_revtree = repo.revision_tree(self.tree.last_revision())
top_revtree.lock_read()
top_file = top_revtree.get_file(top_revtree.path2id("test_file"))
test_content = top_file.read().strip()
top_file.close()
top_revtree.unlock()
assert test_content == "five"
def testSubtreeRev(self):
"""Test that the last revision in a subtree is correct."""
repo = self.tree.branch.repository
sub_revtree = repo.revision_tree(self.subtree_rev)
sub_revtree.lock_read()
sub_file = sub_revtree.get_file(sub_revtree.path2id("test_file"))
test_content = sub_file.read().strip()
sub_file.close()
sub_revtree.unlock()
assert test_content == "one dot three"
class BisectMetaTests(BisectTestCase):
"""Test the metadata provided by the package."""
def testVersionPresent(self):
"""Check if version_info is available for the plugin."""
assert bisect.version_info
def testBzrVersioning(self):
"""Check if API is of right version."""
assert bisect.bzr_minimum_api <= bzrlib.api_minimum_version
assert bisect.bzr_minimum_api <= bzrlib.version_info[:3]
class BisectCurrentUnitTests(BisectTestCase):
"""Test the BisectCurrent class."""
def testShowLog(self):
"""Test that the log can be shown."""
# Not a very good test; just makes sure the code doesn't fail,
# not that the output makes any sense.
sio = StringIO()
cmds.BisectCurrent().show_rev_log(out=sio)
def testShowLogSubtree(self):
"""Test that a subtree's log can be shown."""
current = cmds.BisectCurrent()
current.switch(self.subtree_rev)
sio = StringIO()
current.show_rev_log(out=sio)
def testSwitchVersions(self):
"""Test switching versions."""
current = cmds.BisectCurrent()
self.assertRevno(5)
current.switch(4)
self.assertRevno(4)
def testReset(self):
"""Test resetting the working tree to a non-bisected state."""
current = cmds.BisectCurrent()
current.switch(4)
current.reset()
self.assertRevno(5)
assert not os.path.exists(cmds.bisect_rev_path)
def testIsMergePoint(self):
"""Test merge point detection."""
current = cmds.BisectCurrent()
self.assertRevno(5)
assert not current.is_merge_point()
current.switch(2)
assert current.is_merge_point()
class BisectLogUnitTests(BisectTestCase):
"""Test the BisectLog class."""
def testCreateBlank(self):
"""Test creation of new log."""
bisect_log = cmds.BisectLog()
bisect_log.save()
assert os.path.exists(cmds.bisect_info_path)
def testLoad(self):
"""Test loading a log."""
preloaded_log = open(cmds.bisect_info_path, "w")
preloaded_log.write("rev1 yes\nrev2 no\nrev3 yes\n")
preloaded_log.close()
bisect_log = cmds.BisectLog()
assert len(bisect_log._items) == 3
assert bisect_log._items[0] == ("rev1", "yes")
assert bisect_log._items[1] == ("rev2", "no")
assert bisect_log._items[2] == ("rev3", "yes")
def testSave(self):
"""Test saving the log."""
bisect_log = cmds.BisectLog()
bisect_log._items = [("rev1", "yes"), ("rev2", "no"), ("rev3", "yes")]
bisect_log.save()
logfile = open(cmds.bisect_info_path)
assert logfile.read() == "rev1 yes\nrev2 no\nrev3 yes\n"
class BisectFuncTests(BisectTestCase):
"""Functional tests for the bisect plugin."""
def testWorkflow(self):
"""Run through a basic usage scenario."""
# Start up the bisection. When the two ends are set, we should
# end up in the middle.
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.assertRevno(3)
# Mark feature as present in the middle. Should move us
# halfway back between the current middle and the start.
self.run_bzr(['bisect', 'yes'])
self.assertRevno(2)
# Mark feature as not present. Since this is only one
# rev back from the lowest marked revision with the feature,
# the process should end, with the current rev set to the
# rev following.
self.run_bzr(['bisect', 'no'])
self.assertRevno(3)
# Run again. Since we're done, this should do nothing.
self.run_bzr(['bisect', 'no'])
self.assertRevno(3)
def testWorkflowSubtree(self):
"""Run through a usage scenario where the offending change
is in a subtree."""
# Similar to testWorkflow, but make sure the plugin traverses
# subtrees when the "final" revision is a merge point.
# This part is similar to testWorkflow.
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.run_bzr(['bisect', 'yes'])
# Check to make sure we're where we expect to be.
self.assertRevno(2)
# Now, mark the merge point revno, meaning the feature
# appeared at a merge point.
self.run_bzr(['bisect', 'yes'])
self.assertRevno(1.2)
# Continue bisecting along the subtree to the real conclusion.
self.run_bzr(['bisect', 'yes'])
self.assertRevno(1.1)
self.run_bzr(['bisect', 'yes'])
self.assertRevno(1.1)
# Run again. Since we're done, this should do nothing.
self.run_bzr(['bisect', 'yes'])
self.assertRevno(1.1)
def testMove(self):
"""Test manually moving to a different revision during the bisection."""
# Set up a bisection in progress.
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
# Move.
self.run_bzr(['bisect', 'move', '-r', '2'])
self.assertRevno(2)
def testReset(self):
"""Test resetting the tree."""
# Set up a bisection in progress.
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.run_bzr(['bisect', 'yes'])
# Now reset.
self.run_bzr(['bisect', 'reset'])
self.assertRevno(5)
# Check that reset doesn't do anything unless there's a
# bisection in progress.
test_file = open("test_file", "w")
test_file.write("keep me")
test_file.close()
out, err = self.run_bzr(['bisect', 'reset'], retcode=3)
self.assert_("No bisection in progress." in err)
test_file = open("test_file")
content = test_file.read().strip()
test_file.close()
self.failUnless(content == "keep me")
def testLog(self):
"""Test saving the current bisection state, and re-loading it."""
# Set up a bisection in progress.
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.run_bzr(['bisect', 'yes'])
# Now save the log.
self.run_bzr(['bisect', 'log', '-o', 'bisect_log'])
# Reset.
self.run_bzr(['bisect', 'reset'])
# Read it back in.
self.run_bzr(['bisect', 'replay', 'bisect_log'])
self.assertRevno(2)
# Mark another state, and see if the bisect moves in the
# right way.
self.run_bzr(['bisect', 'no'])
self.assertRevno(3)
def testRunScript(self):
"""Make a test script and run it."""
test_script = open("test_script", "w")
test_script.write("#!/bin/sh\n"
"grep -q '^four' test_file_append\n")
test_script.close()
os.chmod("test_script", stat.S_IRWXU)
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.run_bzr(['bisect', 'run', './test_script'])
self.assertRevno(4)
def testRunScriptMergePoint(self):
"""Make a test script and run it."""
if sys.platform == "win32":
raise TestSkipped("Unable to run shell script on windows")
test_script = open("test_script", "w")
test_script.write("#!/bin/sh\n"
"grep -q '^two' test_file_append\n")
test_script.close()
os.chmod("test_script", stat.S_IRWXU)
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.run_bzr(['bisect', 'run', './test_script'])
try:
self.assertRevno(2)
except AssertionError:
raise KnownFailure\
("bisect does not drill down into merge commits: "
"https://bugs.launchpad.net/bzr-bisect/+bug/539937")
def testRunScriptSubtree(self):
"""Make a test script and run it."""
if sys.platform == "win32":
raise TestSkipped("Unable to run shell script on windows")
test_script = open("test_script", "w")
test_script.write("#!/bin/sh\n"
"grep -q '^one dot two' test_file_append\n")
test_script.close()
os.chmod("test_script", stat.S_IRWXU)
self.run_bzr(['bisect', 'start'])
self.run_bzr(['bisect', 'yes'])
self.run_bzr(['bisect', 'no', '-r', '1'])
self.run_bzr(['bisect', 'run', './test_script'])
try:
self.assertRevno(1.2)
except AssertionError:
raise KnownFailure\
("bisect does not drill down into merge commits: "
"https://bugs.launchpad.net/bzr-bisect/+bug/539937")