1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
# TODO: Perhaps there should be an API to find out if bzr running under the
19
# test suite -- some plugins might want to avoid making intrusive changes if
20
# this is the case. However, we want behaviour under to test to diverge as
21
# little as possible, so this should be used rarely if it's added at all.
22
# (Suggestion from j-a-meinel, 2005-11-24)
24
# NOTE: Some classes in here use camelCaseNaming() rather than
25
# underscore_naming(). That's for consistency with unittest; it's not the
26
# general style of bzrlib. Please continue that consistency when adding e.g.
27
# new assertFoo() methods.
18
30
from cStringIO import StringIO
39
from subprocess import Popen, PIPE
46
from bzrlib import memorytree
48
import bzrlib.bzrdir as bzrdir
29
49
import bzrlib.commands
50
import bzrlib.bundle.serializer
51
import bzrlib.errors as errors
53
import bzrlib.inventory
54
import bzrlib.iterablefile
59
# lsprof not available
61
from bzrlib.merge import merge_inner
64
import bzrlib.osutils as osutils
66
import bzrlib.progress as progress
67
from bzrlib.revision import common_ancestor
69
from bzrlib import symbol_versioning
30
70
import bzrlib.trace
32
from bzrlib.selftest import TestUtil
33
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
71
from bzrlib.transport import get_transport
72
import bzrlib.transport
73
from bzrlib.transport.local import LocalURLServer
74
from bzrlib.transport.memory import MemoryServer
75
from bzrlib.transport.readonly import ReadonlyServer
76
from bzrlib.trace import mutter
77
from bzrlib.tests import TestUtil
78
from bzrlib.tests.HttpServer import HttpServer
79
from bzrlib.tests.TestUtil import (
83
from bzrlib.tests.treeshape import build_tree_contents
84
import bzrlib.urlutils as urlutils
85
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
87
default_transport = LocalURLServer
36
89
MODULES_TO_TEST = []
37
MODULES_TO_DOCTEST = []
39
from logging import debug, warning, error
43
class EarlyStoppingTestResultAdapter(object):
44
"""An adapter for TestResult to stop at the first first failure or error"""
46
def __init__(self, result):
49
def addError(self, test, err):
50
self._result.addError(test, err)
53
def addFailure(self, test, err):
54
self._result.addFailure(test, err)
57
def __getattr__(self, name):
58
return getattr(self._result, name)
60
def __setattr__(self, name, value):
62
object.__setattr__(self, name, value)
63
return setattr(self._result, name, value)
90
MODULES_TO_DOCTEST = [
91
bzrlib.bundle.serializer,
103
def packages_to_test():
104
"""Return a list of packages to test.
106
The packages are not globally imported so that import failures are
107
triggered when running selftest, not when importing the command.
110
import bzrlib.tests.blackbox
111
import bzrlib.tests.branch_implementations
112
import bzrlib.tests.bzrdir_implementations
113
import bzrlib.tests.interrepository_implementations
114
import bzrlib.tests.interversionedfile_implementations
115
import bzrlib.tests.intertree_implementations
116
import bzrlib.tests.repository_implementations
117
import bzrlib.tests.revisionstore_implementations
118
import bzrlib.tests.tree_implementations
119
import bzrlib.tests.workingtree_implementations
122
bzrlib.tests.blackbox,
123
bzrlib.tests.branch_implementations,
124
bzrlib.tests.bzrdir_implementations,
125
bzrlib.tests.interrepository_implementations,
126
bzrlib.tests.interversionedfile_implementations,
127
bzrlib.tests.intertree_implementations,
128
bzrlib.tests.repository_implementations,
129
bzrlib.tests.revisionstore_implementations,
130
bzrlib.tests.tree_implementations,
131
bzrlib.tests.workingtree_implementations,
66
135
class _MyResult(unittest._TextTestResult):
70
No special behaviour for now.
136
"""Custom TestResult.
138
Shows output in a different format, including displaying runtime for tests.
142
def __init__(self, stream, descriptions, verbosity, pb=None,
144
"""Construct new TestResult.
146
:param bench_history: Optionally, a writable file object to accumulate
149
unittest._TextTestResult.__init__(self, stream, descriptions, verbosity)
151
if bench_history is not None:
152
from bzrlib.version import _get_bzr_source_tree
153
src_tree = _get_bzr_source_tree()
156
revision_id = src_tree.get_parent_ids()[0]
158
# XXX: if this is a brand new tree, do the same as if there
162
# XXX: If there's no branch, what should we do?
164
bench_history.write("--date %s %s\n" % (time.time(), revision_id))
165
self._bench_history = bench_history
167
def extractBenchmarkTime(self, testCase):
168
"""Add a benchmark time for the current test case."""
169
self._benchmarkTime = getattr(testCase, "_benchtime", None)
171
def _elapsedTestTimeString(self):
172
"""Return a time string for the overall time the current test has taken."""
173
return self._formatTime(time.time() - self._start_time)
175
def _testTimeString(self):
176
if self._benchmarkTime is not None:
178
self._formatTime(self._benchmarkTime),
179
self._elapsedTestTimeString())
181
return " %s" % self._elapsedTestTimeString()
183
def _formatTime(self, seconds):
184
"""Format seconds as milliseconds with leading spaces."""
185
return "%5dms" % (1000 * seconds)
187
def _ellipsise_unimportant_words(self, a_string, final_width,
189
"""Add ellipses (sp?) for overly long strings.
191
:param keep_start: If true preserve the start of a_string rather
195
if len(a_string) > final_width:
196
result = a_string[:final_width-3] + '...'
200
if len(a_string) > final_width:
201
result = '...' + a_string[3-final_width:]
204
return result.ljust(final_width)
73
206
def startTest(self, test):
74
207
unittest.TestResult.startTest(self, test)
75
# TODO: Maybe show test.shortDescription somewhere?
76
what = test.shortDescription() or test.id()
208
# In a short description, the important words are in
209
# the beginning, but in an id, the important words are
211
SHOW_DESCRIPTIONS = False
213
if not self.showAll and self.dots and self.pb is not None:
216
final_width = osutils.terminal_width()
217
final_width = final_width - 15 - 8
219
if SHOW_DESCRIPTIONS:
220
what = test.shortDescription()
222
what = self._ellipsise_unimportant_words(what, final_width, keep_start=True)
225
if what.startswith('bzrlib.tests.'):
227
what = self._ellipsise_unimportant_words(what, final_width)
78
self.stream.write('%-70.70s' % what)
229
self.stream.write(what)
230
elif self.dots and self.pb is not None:
231
self.pb.update(what, self.testsRun - 1, None)
79
232
self.stream.flush()
233
self._recordTestStartTime()
235
def _recordTestStartTime(self):
236
"""Record that a test has started."""
237
self._start_time = time.time()
81
239
def addError(self, test, err):
82
super(_MyResult, self).addError(test, err)
240
if isinstance(err[1], TestSkipped):
241
return self.addSkipped(test, err)
242
unittest.TestResult.addError(self, test, err)
243
# We can only do this if we have one of our TestCases, not if
245
setKeepLogfile = getattr(test, 'setKeepLogfile', None)
246
if setKeepLogfile is not None:
248
self.extractBenchmarkTime(test)
250
self.stream.writeln("ERROR %s" % self._testTimeString())
251
elif self.dots and self.pb is None:
252
self.stream.write('E')
254
self.pb.update(self._ellipsise_unimportant_words('ERROR', 13), self.testsRun, None)
255
self.pb.note(self._ellipsise_unimportant_words(
256
test.id() + ': ERROR',
257
osutils.terminal_width()))
83
258
self.stream.flush()
85
262
def addFailure(self, test, err):
86
super(_MyResult, self).addFailure(test, err)
263
unittest.TestResult.addFailure(self, test, err)
264
# We can only do this if we have one of our TestCases, not if
266
setKeepLogfile = getattr(test, 'setKeepLogfile', None)
267
if setKeepLogfile is not None:
269
self.extractBenchmarkTime(test)
271
self.stream.writeln(" FAIL %s" % self._testTimeString())
272
elif self.dots and self.pb is None:
273
self.stream.write('F')
275
self.pb.update(self._ellipsise_unimportant_words('FAIL', 13), self.testsRun, None)
276
self.pb.note(self._ellipsise_unimportant_words(
277
test.id() + ': FAIL',
278
osutils.terminal_width()))
87
279
self.stream.flush()
89
283
def addSuccess(self, test):
284
self.extractBenchmarkTime(test)
285
if self._bench_history is not None:
286
if self._benchmarkTime is not None:
287
self._bench_history.write("%s %s\n" % (
288
self._formatTime(self._benchmarkTime),
91
self.stream.writeln('OK')
291
self.stream.writeln(' OK %s' % self._testTimeString())
292
for bench_called, stats in getattr(test, '_benchcalls', []):
293
self.stream.writeln('LSProf output for %s(%s, %s)' % bench_called)
294
stats.pprint(file=self.stream)
295
elif self.dots and self.pb is None:
93
296
self.stream.write('~')
298
self.pb.update(self._ellipsise_unimportant_words('OK', 13), self.testsRun, None)
94
299
self.stream.flush()
95
300
unittest.TestResult.addSuccess(self, test)
302
def addSkipped(self, test, skip_excinfo):
303
self.extractBenchmarkTime(test)
305
print >>self.stream, ' SKIP %s' % self._testTimeString()
306
print >>self.stream, ' %s' % skip_excinfo[1]
307
elif self.dots and self.pb is None:
308
self.stream.write('S')
310
self.pb.update(self._ellipsise_unimportant_words('SKIP', 13), self.testsRun, None)
312
# seems best to treat this as success from point-of-view of unittest
313
# -- it actually does nothing so it barely matters :)
316
except KeyboardInterrupt:
319
self.addError(test, test.__exc_info())
321
unittest.TestResult.addSuccess(self, test)
97
323
def printErrorList(self, flavour, errors):
98
324
for test, err in errors:
99
325
self.stream.writeln(self.separator1)
100
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
101
if hasattr(test, '_get_log'):
102
self.stream.writeln()
103
self.stream.writeln('log from this test:')
326
self.stream.writeln("%s: %s" % (flavour, self.getDescription(test)))
327
if getattr(test, '_get_log', None) is not None:
329
print >>self.stream, \
330
('vvvv[log from %s]' % test.id()).ljust(78,'-')
104
331
print >>self.stream, test._get_log()
332
print >>self.stream, \
333
('^^^^[log from %s]' % test.id()).ljust(78,'-')
105
334
self.stream.writeln(self.separator2)
106
335
self.stream.writeln("%s" % err)
109
class TextTestRunner(unittest.TextTestRunner):
338
class TextTestRunner(object):
339
stop_on_failure = False
348
self.stream = unittest._WritelnDecorator(stream)
349
self.descriptions = descriptions
350
self.verbosity = verbosity
351
self.keep_output = keep_output
353
self._bench_history = bench_history
111
355
def _makeResult(self):
112
result = _MyResult(self.stream, self.descriptions, self.verbosity)
113
return EarlyStoppingTestResultAdapter(result)
356
result = _MyResult(self.stream,
360
bench_history=self._bench_history)
361
result.stop_early = self.stop_on_failure
365
"Run the given test case or test suite."
366
result = self._makeResult()
367
startTime = time.time()
368
if self.pb is not None:
369
self.pb.update('Running tests', 0, test.countTestCases())
371
stopTime = time.time()
372
timeTaken = stopTime - startTime
374
self.stream.writeln(result.separator2)
375
run = result.testsRun
376
self.stream.writeln("Ran %d test%s in %.3fs" %
377
(run, run != 1 and "s" or "", timeTaken))
378
self.stream.writeln()
379
if not result.wasSuccessful():
380
self.stream.write("FAILED (")
381
failed, errored = map(len, (result.failures, result.errors))
383
self.stream.write("failures=%d" % failed)
385
if failed: self.stream.write(", ")
386
self.stream.write("errors=%d" % errored)
387
self.stream.writeln(")")
389
self.stream.writeln("OK")
390
if self.pb is not None:
391
self.pb.update('Cleaning up', 0, 1)
392
# This is still a little bogus,
393
# but only a little. Folk not using our testrunner will
394
# have to delete their temp directories themselves.
395
test_root = TestCaseWithMemoryTransport.TEST_ROOT
396
if result.wasSuccessful() or not self.keep_output:
397
if test_root is not None:
398
# If LANG=C we probably have created some bogus paths
399
# which rmtree(unicode) will fail to delete
400
# so make sure we are using rmtree(str) to delete everything
401
# except on win32, where rmtree(str) will fail
402
# since it doesn't have the property of byte-stream paths
403
# (they are either ascii or mbcs)
404
if sys.platform == 'win32':
405
# make sure we are using the unicode win32 api
406
test_root = unicode(test_root)
408
test_root = test_root.encode(
409
sys.getfilesystemencoding())
410
osutils.rmtree(test_root)
412
if self.pb is not None:
413
self.pb.note("Failed tests working directories are in '%s'\n",
417
"Failed tests working directories are in '%s'\n" %
419
TestCaseWithMemoryTransport.TEST_ROOT = None
420
if self.pb is not None:
116
425
def iter_suite_tests(suite):
143
478
Error and debug log messages are redirected from their usual
144
479
location into a temporary file, the contents of which can be
145
retrieved by _get_log().
480
retrieved by _get_log(). We use a real OS file, not an in-memory object,
481
so that it can also capture file IO. When the test completes this file
482
is read into memory and removed from disk.
147
484
There are also convenience functions to invoke bzr's command-line
148
routine, and to build and check bzr trees."""
485
routine, and to build and check bzr trees.
487
In addition to the usual method of overriding tearDown(), this class also
488
allows subclasses to register functions into the _cleanups list, which is
489
run in order as the object is torn down. It's less likely this will be
490
accidentally overlooked.
493
_log_file_name = None
495
_keep_log_file = False
496
# record lsprof data when performing benchmark calls.
497
_gather_lsprof_in_benchmarks = False
499
def __init__(self, methodName='testMethod'):
500
super(TestCase, self).__init__(methodName)
153
504
unittest.TestCase.setUp(self)
505
self._cleanEnvironment()
154
506
bzrlib.trace.disable_default_logging()
155
self._enable_file_logging()
158
def _enable_file_logging(self):
508
self._benchcalls = []
509
self._benchtime = None
511
def _ndiff_strings(self, a, b):
512
"""Return ndiff between two strings containing lines.
514
A trailing newline is added if missing to make the strings
516
if b and b[-1] != '\n':
518
if a and a[-1] != '\n':
520
difflines = difflib.ndiff(a.splitlines(True),
522
linejunk=lambda x: False,
523
charjunk=lambda x: False)
524
return ''.join(difflines)
526
def assertEqualDiff(self, a, b, message=None):
527
"""Assert two texts are equal, if not raise an exception.
529
This is intended for use with multi-line strings where it can
530
be hard to find the differences by eye.
532
# TODO: perhaps override assertEquals to call this for strings?
536
message = "texts not equal:\n"
537
raise AssertionError(message +
538
self._ndiff_strings(a, b))
540
def assertEqualMode(self, mode, mode_test):
541
self.assertEqual(mode, mode_test,
542
'mode mismatch %o != %o' % (mode, mode_test))
544
def assertStartsWith(self, s, prefix):
545
if not s.startswith(prefix):
546
raise AssertionError('string %r does not start with %r' % (s, prefix))
548
def assertEndsWith(self, s, suffix):
549
"""Asserts that s ends with suffix."""
550
if not s.endswith(suffix):
551
raise AssertionError('string %r does not end with %r' % (s, suffix))
553
def assertContainsRe(self, haystack, needle_re):
554
"""Assert that a contains something matching a regular expression."""
555
if not re.search(needle_re, haystack):
556
raise AssertionError('pattern "%s" not found in "%s"'
557
% (needle_re, haystack))
559
def assertNotContainsRe(self, haystack, needle_re):
560
"""Assert that a does not match a regular expression"""
561
if re.search(needle_re, haystack):
562
raise AssertionError('pattern "%s" found in "%s"'
563
% (needle_re, haystack))
565
def assertSubset(self, sublist, superlist):
566
"""Assert that every entry in sublist is present in superlist."""
568
for entry in sublist:
569
if entry not in superlist:
570
missing.append(entry)
572
raise AssertionError("value(s) %r not present in container %r" %
573
(missing, superlist))
575
def assertListRaises(self, excClass, func, *args, **kwargs):
576
"""Fail unless excClass is raised when the iterator from func is used.
578
Many functions can return generators this makes sure
579
to wrap them in a list() call to make sure the whole generator
580
is run, and that the proper exception is raised.
583
list(func(*args, **kwargs))
587
if getattr(excClass,'__name__', None) is not None:
588
excName = excClass.__name__
590
excName = str(excClass)
591
raise self.failureException, "%s not raised" % excName
593
def assertIs(self, left, right):
594
if not (left is right):
595
raise AssertionError("%r is not %r." % (left, right))
597
def assertTransportMode(self, transport, path, mode):
598
"""Fail if a path does not have mode mode.
600
If modes are not supported on this transport, the assertion is ignored.
602
if not transport._can_roundtrip_unix_modebits():
604
path_stat = transport.stat(path)
605
actual_mode = stat.S_IMODE(path_stat.st_mode)
606
self.assertEqual(mode, actual_mode,
607
'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
609
def assertIsInstance(self, obj, kls):
610
"""Fail if obj is not an instance of kls"""
611
if not isinstance(obj, kls):
612
self.fail("%r is an instance of %s rather than %s" % (
613
obj, obj.__class__, kls))
615
def _capture_warnings(self, a_callable, *args, **kwargs):
616
"""A helper for callDeprecated and applyDeprecated.
618
:param a_callable: A callable to call.
619
:param args: The positional arguments for the callable
620
:param kwargs: The keyword arguments for the callable
621
:return: A tuple (warnings, result). result is the result of calling
622
a_callable(*args, **kwargs).
625
def capture_warnings(msg, cls, stacklevel=None):
626
# we've hooked into a deprecation specific callpath,
627
# only deprecations should getting sent via it.
628
self.assertEqual(cls, DeprecationWarning)
629
local_warnings.append(msg)
630
original_warning_method = symbol_versioning.warn
631
symbol_versioning.set_warning_method(capture_warnings)
633
result = a_callable(*args, **kwargs)
635
symbol_versioning.set_warning_method(original_warning_method)
636
return (local_warnings, result)
638
def applyDeprecated(self, deprecation_format, a_callable, *args, **kwargs):
639
"""Call a deprecated callable without warning the user.
641
:param deprecation_format: The deprecation format that the callable
642
should have been deprecated with. This is the same type as the
643
parameter to deprecated_method/deprecated_function. If the
644
callable is not deprecated with this format, an assertion error
646
:param a_callable: A callable to call. This may be a bound method or
647
a regular function. It will be called with *args and **kwargs.
648
:param args: The positional arguments for the callable
649
:param kwargs: The keyword arguments for the callable
650
:return: The result of a_callable(*args, **kwargs)
652
call_warnings, result = self._capture_warnings(a_callable,
654
expected_first_warning = symbol_versioning.deprecation_string(
655
a_callable, deprecation_format)
656
if len(call_warnings) == 0:
657
self.fail("No assertion generated by call to %s" %
659
self.assertEqual(expected_first_warning, call_warnings[0])
662
def callDeprecated(self, expected, callable, *args, **kwargs):
663
"""Assert that a callable is deprecated in a particular way.
665
This is a very precise test for unusual requirements. The
666
applyDeprecated helper function is probably more suited for most tests
667
as it allows you to simply specify the deprecation format being used
668
and will ensure that that is issued for the function being called.
670
:param expected: a list of the deprecation warnings expected, in order
671
:param callable: The callable to call
672
:param args: The positional arguments for the callable
673
:param kwargs: The keyword arguments for the callable
675
call_warnings, result = self._capture_warnings(callable,
677
self.assertEqual(expected, call_warnings)
680
def _startLogFile(self):
681
"""Send bzr and test log messages to a temporary file.
683
The file is removed as the test is torn down.
159
685
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
161
686
self._log_file = os.fdopen(fileno, 'w+')
163
hdlr = logging.StreamHandler(self._log_file)
164
hdlr.setLevel(logging.DEBUG)
165
hdlr.setFormatter(logging.Formatter('%(levelname)8s %(message)s'))
166
logging.getLogger('').addHandler(hdlr)
167
logging.getLogger('').setLevel(logging.DEBUG)
168
self._log_hdlr = hdlr
169
debug('opened log file %s', name)
687
self._log_nonce = bzrlib.trace.enable_test_log(self._log_file)
171
688
self._log_file_name = name
174
logging.getLogger('').removeHandler(self._log_hdlr)
175
bzrlib.trace.enable_default_logging()
176
logging.debug('%s teardown', self.id())
689
self.addCleanup(self._finishLogFile)
691
def _finishLogFile(self):
692
"""Finished with the log file.
694
Close the file and delete it, unless setKeepLogfile was called.
696
if self._log_file is None:
698
bzrlib.trace.disable_test_log(self._log_nonce)
177
699
self._log_file.close()
700
self._log_file = None
701
if not self._keep_log_file:
702
os.remove(self._log_file_name)
703
self._log_file_name = None
705
def setKeepLogfile(self):
706
"""Make the logfile not be deleted when _finishLogFile is called."""
707
self._keep_log_file = True
709
def addCleanup(self, callable):
710
"""Arrange to run a callable when this case is torn down.
712
Callables are run in the reverse of the order they are registered,
713
ie last-in first-out.
715
if callable in self._cleanups:
716
raise ValueError("cleanup function %r already registered on %s"
718
self._cleanups.append(callable)
720
def _cleanEnvironment(self):
722
'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
724
'APPDATA': os.getcwd(),
726
'BZREMAIL': None, # may still be present in the environment
728
'BZR_PROGRESS_BAR': None,
731
self.addCleanup(self._restoreEnvironment)
732
for name, value in new_env.iteritems():
733
self._captureVar(name, value)
735
def _captureVar(self, name, newvalue):
736
"""Set an environment variable, and reset it when finished."""
737
self.__old_env[name] = osutils.set_or_unset_env(name, newvalue)
739
def _restoreEnvironment(self):
740
for name, value in self.__old_env.iteritems():
741
osutils.set_or_unset_env(name, value)
178
745
unittest.TestCase.tearDown(self)
747
def time(self, callable, *args, **kwargs):
748
"""Run callable and accrue the time it takes to the benchmark time.
750
If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
751
this will cause lsprofile statistics to be gathered and stored in
754
if self._benchtime is None:
758
if not self._gather_lsprof_in_benchmarks:
759
return callable(*args, **kwargs)
761
# record this benchmark
762
ret, stats = bzrlib.lsprof.profile(callable, *args, **kwargs)
764
self._benchcalls.append(((callable, args, kwargs), stats))
767
self._benchtime += time.time() - start
769
def _runCleanups(self):
770
"""Run registered cleanup functions.
772
This should only be called from TestCase.tearDown.
774
# TODO: Perhaps this should keep running cleanups even if
776
for cleanup_fn in reversed(self._cleanups):
180
779
def log(self, *args):
184
"""Return as a string the log for this test"""
185
return open(self._log_file_name).read()
188
def capture(self, cmd):
782
def _get_log(self, keep_log_file=False):
783
"""Return as a string the log for this test. If the file is still
784
on disk and keep_log_file=False, delete the log file and store the
785
content in self._log_contents."""
786
# flush the log file, to get all content
788
bzrlib.trace._trace_file.flush()
789
if self._log_contents:
790
return self._log_contents
791
if self._log_file_name is not None:
792
logfile = open(self._log_file_name)
794
log_contents = logfile.read()
797
if not keep_log_file:
798
self._log_contents = log_contents
799
os.remove(self._log_file_name)
802
return "DELETED log file to reduce memory footprint"
804
def capture(self, cmd, retcode=0):
189
805
"""Shortcut that splits cmd into words, runs, and returns stdout"""
190
return self.run_bzr_captured(cmd.split())[0]
806
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
192
def run_bzr_captured(self, argv, retcode=0):
193
"""Invoke bzr and return (result, stdout, stderr).
808
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
810
"""Invoke bzr and return (stdout, stderr).
195
812
Useful for code that wants to check the contents of the
196
813
output, the way error messages are presented, etc.
243
885
This sends the stdout/stderr results into the test's log,
244
886
where it may be useful for debugging. See also run_captured.
888
:param stdin: A string to be used as stdin for the command.
246
890
retcode = kwargs.pop('retcode', 0)
247
return self.run_bzr_captured(args, retcode)
891
encoding = kwargs.pop('encoding', None)
892
stdin = kwargs.pop('stdin', None)
893
working_dir = kwargs.pop('working_dir', None)
894
return self.run_bzr_captured(args, retcode=retcode, encoding=encoding,
895
stdin=stdin, working_dir=working_dir)
897
def run_bzr_decode(self, *args, **kwargs):
898
if 'encoding' in kwargs:
899
encoding = kwargs['encoding']
901
encoding = bzrlib.user_encoding
902
return self.run_bzr(*args, **kwargs)[0].decode(encoding)
904
def run_bzr_error(self, error_regexes, *args, **kwargs):
905
"""Run bzr, and check that stderr contains the supplied regexes
907
:param error_regexes: Sequence of regular expressions which
908
must each be found in the error output. The relative ordering
910
:param args: command-line arguments for bzr
911
:param kwargs: Keyword arguments which are interpreted by run_bzr
912
This function changes the default value of retcode to be 3,
913
since in most cases this is run when you expect bzr to fail.
914
:return: (out, err) The actual output of running the command (in case you
915
want to do more inspection)
918
# Make sure that commit is failing because there is nothing to do
919
self.run_bzr_error(['no changes to commit'],
920
'commit', '-m', 'my commit comment')
921
# Make sure --strict is handling an unknown file, rather than
922
# giving us the 'nothing to do' error
923
self.build_tree(['unknown'])
924
self.run_bzr_error(['Commit refused because there are unknown files'],
925
'commit', '--strict', '-m', 'my commit comment')
927
kwargs.setdefault('retcode', 3)
928
out, err = self.run_bzr(*args, **kwargs)
929
for regex in error_regexes:
930
self.assertContainsRe(err, regex)
933
def run_bzr_subprocess(self, *args, **kwargs):
934
"""Run bzr in a subprocess for testing.
936
This starts a new Python interpreter and runs bzr in there.
937
This should only be used for tests that have a justifiable need for
938
this isolation: e.g. they are testing startup time, or signal
939
handling, or early startup code, etc. Subprocess code can't be
940
profiled or debugged so easily.
942
:param retcode: The status code that is expected. Defaults to 0. If
943
None is supplied, the status code is not checked.
944
:param env_changes: A dictionary which lists changes to environment
945
variables. A value of None will unset the env variable.
946
The values must be strings. The change will only occur in the
947
child, so you don't need to fix the environment after running.
948
:param universal_newlines: Convert CRLF => LF
949
:param allow_plugins: By default the subprocess is run with
950
--no-plugins to ensure test reproducibility. Also, it is possible
951
for system-wide plugins to create unexpected output on stderr,
952
which can cause unnecessary test failures.
954
env_changes = kwargs.get('env_changes', {})
955
working_dir = kwargs.get('working_dir', None)
956
allow_plugins = kwargs.get('allow_plugins', False)
957
process = self.start_bzr_subprocess(args, env_changes=env_changes,
958
working_dir=working_dir,
959
allow_plugins=allow_plugins)
960
# We distinguish between retcode=None and retcode not passed.
961
supplied_retcode = kwargs.get('retcode', 0)
962
return self.finish_bzr_subprocess(process, retcode=supplied_retcode,
963
universal_newlines=kwargs.get('universal_newlines', False),
966
def start_bzr_subprocess(self, process_args, env_changes=None,
967
skip_if_plan_to_signal=False,
969
allow_plugins=False):
970
"""Start bzr in a subprocess for testing.
972
This starts a new Python interpreter and runs bzr in there.
973
This should only be used for tests that have a justifiable need for
974
this isolation: e.g. they are testing startup time, or signal
975
handling, or early startup code, etc. Subprocess code can't be
976
profiled or debugged so easily.
978
:param process_args: a list of arguments to pass to the bzr executable,
979
for example `['--version']`.
980
:param env_changes: A dictionary which lists changes to environment
981
variables. A value of None will unset the env variable.
982
The values must be strings. The change will only occur in the
983
child, so you don't need to fix the environment after running.
984
:param skip_if_plan_to_signal: raise TestSkipped when true and os.kill
986
:param allow_plugins: If False (default) pass --no-plugins to bzr.
988
:returns: Popen object for the started process.
990
if skip_if_plan_to_signal:
991
if not getattr(os, 'kill', None):
992
raise TestSkipped("os.kill not available.")
994
if env_changes is None:
998
def cleanup_environment():
999
for env_var, value in env_changes.iteritems():
1000
old_env[env_var] = osutils.set_or_unset_env(env_var, value)
1002
def restore_environment():
1003
for env_var, value in old_env.iteritems():
1004
osutils.set_or_unset_env(env_var, value)
1006
bzr_path = self.get_bzr_path()
1009
if working_dir is not None:
1010
cwd = osutils.getcwd()
1011
os.chdir(working_dir)
1014
# win32 subprocess doesn't support preexec_fn
1015
# so we will avoid using it on all platforms, just to
1016
# make sure the code path is used, and we don't break on win32
1017
cleanup_environment()
1018
command = [sys.executable, bzr_path]
1019
if not allow_plugins:
1020
command.append('--no-plugins')
1021
command.extend(process_args)
1022
process = self._popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
1024
restore_environment()
1030
def _popen(self, *args, **kwargs):
1031
"""Place a call to Popen.
1033
Allows tests to override this method to intercept the calls made to
1034
Popen for introspection.
1036
return Popen(*args, **kwargs)
1038
def get_bzr_path(self):
1039
"""Return the path of the 'bzr' executable for this test suite."""
1040
bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
1041
if not os.path.isfile(bzr_path):
1042
# We are probably installed. Assume sys.argv is the right file
1043
bzr_path = sys.argv[0]
1046
def finish_bzr_subprocess(self, process, retcode=0, send_signal=None,
1047
universal_newlines=False, process_args=None):
1048
"""Finish the execution of process.
1050
:param process: the Popen object returned from start_bzr_subprocess.
1051
:param retcode: The status code that is expected. Defaults to 0. If
1052
None is supplied, the status code is not checked.
1053
:param send_signal: an optional signal to send to the process.
1054
:param universal_newlines: Convert CRLF => LF
1055
:returns: (stdout, stderr)
1057
if send_signal is not None:
1058
os.kill(process.pid, send_signal)
1059
out, err = process.communicate()
1061
if universal_newlines:
1062
out = out.replace('\r\n', '\n')
1063
err = err.replace('\r\n', '\n')
1065
if retcode is not None and retcode != process.returncode:
1066
if process_args is None:
1067
process_args = "(unknown args)"
1068
mutter('Output of bzr %s:\n%s', process_args, out)
1069
mutter('Error for bzr %s:\n%s', process_args, err)
1070
self.fail('Command bzr %s failed with retcode %s != %s'
1071
% (process_args, retcode, process.returncode))
249
1074
def check_inventory_shape(self, inv, shape):
250
1075
"""Compare an inventory to a list of expected names.
298
1123
sys.stderr = real_stderr
299
1124
sys.stdin = real_stdin
1126
@symbol_versioning.deprecated_method(symbol_versioning.zero_eleven)
1127
def merge(self, branch_from, wt_to):
1128
"""A helper for tests to do a ui-less merge.
1130
This should move to the main library when someone has time to integrate
1133
# minimal ui-less merge.
1134
wt_to.branch.fetch(branch_from)
1135
base_rev = common_ancestor(branch_from.last_revision(),
1136
wt_to.branch.last_revision(),
1137
wt_to.branch.repository)
1138
merge_inner(wt_to.branch, branch_from.basis_tree(),
1139
wt_to.branch.repository.revision_tree(base_rev),
1141
wt_to.add_parent_tree_id(branch_from.last_revision())
302
1144
BzrTestBase = TestCase
1147
class TestCaseWithMemoryTransport(TestCase):
1148
"""Common test class for tests that do not need disk resources.
1150
Tests that need disk resources should derive from TestCaseWithTransport.
1152
TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
1154
For TestCaseWithMemoryTransport the test_home_dir is set to the name of
1155
a directory which does not exist. This serves to help ensure test isolation
1156
is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
1157
must exist. However, TestCaseWithMemoryTransport does not offer local
1158
file defaults for the transport in tests, nor does it obey the command line
1159
override, so tests that accidentally write to the common directory should
1167
def __init__(self, methodName='runTest'):
1168
# allow test parameterisation after test construction and before test
1169
# execution. Variables that the parameteriser sets need to be
1170
# ones that are not set by setUp, or setUp will trash them.
1171
super(TestCaseWithMemoryTransport, self).__init__(methodName)
1172
self.transport_server = default_transport
1173
self.transport_readonly_server = None
1175
def failUnlessExists(self, path):
1176
"""Fail unless path, which may be abs or relative, exists."""
1177
self.failUnless(osutils.lexists(path))
1179
def failIfExists(self, path):
1180
"""Fail if path, which may be abs or relative, exists."""
1181
self.failIf(osutils.lexists(path))
1183
def get_transport(self):
1184
"""Return a writeable transport for the test scratch space"""
1185
t = get_transport(self.get_url())
1186
self.assertFalse(t.is_readonly())
1189
def get_readonly_transport(self):
1190
"""Return a readonly transport for the test scratch space
1192
This can be used to test that operations which should only need
1193
readonly access in fact do not try to write.
1195
t = get_transport(self.get_readonly_url())
1196
self.assertTrue(t.is_readonly())
1199
def create_transport_readonly_server(self):
1200
"""Create a transport server from class defined at init.
1202
This is mostly a hook for daugter classes.
1204
return self.transport_readonly_server()
1206
def get_readonly_server(self):
1207
"""Get the server instance for the readonly transport
1209
This is useful for some tests with specific servers to do diagnostics.
1211
if self.__readonly_server is None:
1212
if self.transport_readonly_server is None:
1213
# readonly decorator requested
1214
# bring up the server
1216
self.__readonly_server = ReadonlyServer()
1217
self.__readonly_server.setUp(self.__server)
1219
self.__readonly_server = self.create_transport_readonly_server()
1220
self.__readonly_server.setUp()
1221
self.addCleanup(self.__readonly_server.tearDown)
1222
return self.__readonly_server
1224
def get_readonly_url(self, relpath=None):
1225
"""Get a URL for the readonly transport.
1227
This will either be backed by '.' or a decorator to the transport
1228
used by self.get_url()
1229
relpath provides for clients to get a path relative to the base url.
1230
These should only be downwards relative, not upwards.
1232
base = self.get_readonly_server().get_url()
1233
if relpath is not None:
1234
if not base.endswith('/'):
1236
base = base + relpath
1239
def get_server(self):
1240
"""Get the read/write server instance.
1242
This is useful for some tests with specific servers that need
1245
For TestCaseWithMemoryTransport this is always a MemoryServer, and there
1246
is no means to override it.
1248
if self.__server is None:
1249
self.__server = MemoryServer()
1250
self.__server.setUp()
1251
self.addCleanup(self.__server.tearDown)
1252
return self.__server
1254
def get_url(self, relpath=None):
1255
"""Get a URL (or maybe a path) for the readwrite transport.
1257
This will either be backed by '.' or to an equivalent non-file based
1259
relpath provides for clients to get a path relative to the base url.
1260
These should only be downwards relative, not upwards.
1262
base = self.get_server().get_url()
1263
if relpath is not None and relpath != '.':
1264
if not base.endswith('/'):
1266
# XXX: Really base should be a url; we did after all call
1267
# get_url()! But sometimes it's just a path (from
1268
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1269
# to a non-escaped local path.
1270
if base.startswith('./') or base.startswith('/'):
1273
base += urlutils.escape(relpath)
1276
def _make_test_root(self):
1277
if TestCaseWithMemoryTransport.TEST_ROOT is not None:
1281
root = u'test%04d.tmp' % i
1285
if e.errno == errno.EEXIST:
1290
# successfully created
1291
TestCaseWithMemoryTransport.TEST_ROOT = osutils.abspath(root)
1293
# make a fake bzr directory there to prevent any tests propagating
1294
# up onto the source directory's real branch
1295
bzrdir.BzrDir.create_standalone_workingtree(
1296
TestCaseWithMemoryTransport.TEST_ROOT)
1298
def makeAndChdirToTestDir(self):
1299
"""Create a temporary directories for this one test.
1301
This must set self.test_home_dir and self.test_dir and chdir to
1304
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1306
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1307
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1308
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1310
def make_branch(self, relpath, format=None):
1311
"""Create a branch on the transport at relpath."""
1312
repo = self.make_repository(relpath, format=format)
1313
return repo.bzrdir.create_branch()
1315
def make_bzrdir(self, relpath, format=None):
1317
# might be a relative or absolute path
1318
maybe_a_url = self.get_url(relpath)
1319
segments = maybe_a_url.rsplit('/', 1)
1320
t = get_transport(maybe_a_url)
1321
if len(segments) > 1 and segments[-1] not in ('', '.'):
1324
except errors.FileExists:
1327
format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
1328
return format.initialize_on_transport(t)
1329
except errors.UninitializableFormat:
1330
raise TestSkipped("Format %s is not initializable." % format)
1332
def make_repository(self, relpath, shared=False, format=None):
1333
"""Create a repository on our default transport at relpath."""
1334
made_control = self.make_bzrdir(relpath, format=format)
1335
return made_control.create_repository(shared=shared)
1337
def make_branch_and_memory_tree(self, relpath, format=None):
1338
"""Create a branch on the default transport and a MemoryTree for it."""
1339
b = self.make_branch(relpath, format=format)
1340
return memorytree.MemoryTree.create_on_branch(b)
1342
def overrideEnvironmentForTesting(self):
1343
os.environ['HOME'] = self.test_home_dir
1344
os.environ['APPDATA'] = self.test_home_dir
1347
super(TestCaseWithMemoryTransport, self).setUp()
1348
self._make_test_root()
1349
_currentdir = os.getcwdu()
1350
def _leaveDirectory():
1351
os.chdir(_currentdir)
1352
self.addCleanup(_leaveDirectory)
1353
self.makeAndChdirToTestDir()
1354
self.overrideEnvironmentForTesting()
1355
self.__readonly_server = None
1356
self.__server = None
305
class TestCaseInTempDir(TestCase):
1359
class TestCaseInTempDir(TestCaseWithMemoryTransport):
306
1360
"""Derived class that runs a test within a temporary directory.
308
1362
This is useful for tests that need to create a branch, etc.
327
1379
self.log("actually: %r" % contents)
328
1380
self.fail("contents of %s not as expected" % filename)
330
def _make_test_root(self):
331
if TestCaseInTempDir.TEST_ROOT is not None:
1382
def makeAndChdirToTestDir(self):
1383
"""See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
1385
For TestCaseInTempDir we create a temporary directory based on the test
1386
name and then create two subdirs - test and home under it.
1388
# shorten the name, to avoid test failures due to path length
1389
short_id = self.id().replace('bzrlib.tests.', '') \
1390
.replace('__main__.', '')[-100:]
1391
# it's possible the same test class is run several times for
1392
# parameterized tests, so make sure the names don't collide.
335
root = 'test%04d.tmp' % i
339
if e.errno == errno.EEXIST:
344
# successfully created
345
TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
347
# make a fake bzr directory there to prevent any tests propagating
348
# up onto the source directory's real branch
349
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
352
super(TestCaseInTempDir, self).setUp()
353
self._make_test_root()
354
self._currentdir = os.getcwdu()
355
short_id = self.id().replace('bzrlib.selftest.', '') \
356
.replace('__main__.', '')
357
self.test_dir = os.path.join(self.TEST_ROOT, short_id)
358
os.mkdir(self.test_dir)
359
os.chdir(self.test_dir)
362
os.chdir(self._currentdir)
363
super(TestCaseInTempDir, self).tearDown()
365
def build_tree(self, shape):
1396
candidate_dir = '%s/%s.%d' % (self.TEST_ROOT, short_id, i)
1398
candidate_dir = '%s/%s' % (self.TEST_ROOT, short_id)
1399
if os.path.exists(candidate_dir):
1403
os.mkdir(candidate_dir)
1404
self.test_home_dir = candidate_dir + '/home'
1405
os.mkdir(self.test_home_dir)
1406
self.test_dir = candidate_dir + '/work'
1407
os.mkdir(self.test_dir)
1408
os.chdir(self.test_dir)
1411
def build_tree(self, shape, line_endings='native', transport=None):
366
1412
"""Build a test tree according to a pattern.
368
1414
shape is a sequence of file specifications. If the final
369
1415
character is '/', a directory is created.
1417
This assumes that all the elements in the tree being built are new.
371
1419
This doesn't add anything to a branch.
1420
:param line_endings: Either 'binary' or 'native'
1421
in binary mode, exact contents are written
1422
in native mode, the line endings match the
1423
default platform endings.
1425
:param transport: A transport to write to, for building trees on
1426
VFS's. If the transport is readonly or None,
1427
"." is opened automatically.
373
# XXX: It's OK to just create them using forward slashes on windows?
1429
# It's OK to just create them using forward slashes on windows.
1430
if transport is None or transport.is_readonly():
1431
transport = get_transport(".")
374
1432
for name in shape:
375
assert isinstance(name, basestring)
1433
self.assert_(isinstance(name, basestring))
376
1434
if name[-1] == '/':
1435
transport.mkdir(urlutils.escape(name[:-1]))
380
print >>f, "contents of", name
383
def failUnlessExists(self, path):
384
"""Fail unless path, which may be abs or relative, exists."""
385
self.failUnless(os.path.exists(path))
388
class MetaTestLog(TestCase):
389
def test_logging(self):
390
"""Test logs are captured when a test fails."""
391
logging.info('an info message')
392
warning('something looks dodgy...')
393
logging.debug('hello, test is running')
1437
if line_endings == 'binary':
1439
elif line_endings == 'native':
1442
raise errors.BzrError('Invalid line ending request %r' % (line_endings,))
1443
content = "contents of %s%s" % (name.encode('utf-8'), end)
1444
# Technically 'put()' is the right command. However, put
1445
# uses an AtomicFile, which requires an extra rename into place
1446
# As long as the files didn't exist in the past, append() will
1447
# do the same thing as put()
1448
# On jam's machine, make_kernel_like_tree is:
1449
# put: 4.5-7.5s (averaging 6s)
1451
# put_non_atomic: 2.9-4.5s
1452
transport.put_bytes_non_atomic(urlutils.escape(name), content)
1454
def build_tree_contents(self, shape):
1455
build_tree_contents(shape)
1457
def assertFileEqual(self, content, path):
1458
"""Fail if path does not contain 'content'."""
1459
self.failUnless(osutils.lexists(path))
1460
# TODO: jam 20060427 Shouldn't this be 'rb'?
1461
self.assertEqualDiff(content, open(path, 'r').read())
1464
class TestCaseWithTransport(TestCaseInTempDir):
1465
"""A test case that provides get_url and get_readonly_url facilities.
1467
These back onto two transport servers, one for readonly access and one for
1470
If no explicit class is provided for readonly access, a
1471
ReadonlyTransportDecorator is used instead which allows the use of non disk
1472
based read write transports.
1474
If an explicit class is provided for readonly access, that server and the
1475
readwrite one must both define get_url() as resolving to os.getcwd().
1478
def create_transport_server(self):
1479
"""Create a transport server from class defined at init.
1481
This is mostly a hook for daugter classes.
1483
return self.transport_server()
1485
def get_server(self):
1486
"""See TestCaseWithMemoryTransport.
1488
This is useful for some tests with specific servers that need
1491
if self.__server is None:
1492
self.__server = self.create_transport_server()
1493
self.__server.setUp()
1494
self.addCleanup(self.__server.tearDown)
1495
return self.__server
1497
def make_branch_and_tree(self, relpath, format=None):
1498
"""Create a branch on the transport and a tree locally.
1500
If the transport is not a LocalTransport, the Tree can't be created on
1501
the transport. In that case the working tree is created in the local
1502
directory, and the returned tree's branch and repository will also be
1505
This will fail if the original default transport for this test
1506
case wasn't backed by the working directory, as the branch won't
1507
be on disk for us to open it.
1509
:param format: The BzrDirFormat.
1510
:returns: the WorkingTree.
1512
# TODO: always use the local disk path for the working tree,
1513
# this obviously requires a format that supports branch references
1514
# so check for that by checking bzrdir.BzrDirFormat.get_default_format()
1516
b = self.make_branch(relpath, format=format)
1518
return b.bzrdir.create_workingtree()
1519
except errors.NotLocalUrl:
1520
# We can only make working trees locally at the moment. If the
1521
# transport can't support them, then reopen the branch on a local
1522
# transport, and create the working tree there.
1524
# Possibly we should instead keep
1525
# the non-disk-backed branch and create a local checkout?
1526
bd = bzrdir.BzrDir.open(relpath)
1527
return bd.create_workingtree()
1529
def assertIsDirectory(self, relpath, transport):
1530
"""Assert that relpath within transport is a directory.
1532
This may not be possible on all transports; in that case it propagates
1533
a TransportNotPossible.
1536
mode = transport.stat(relpath).st_mode
1537
except errors.NoSuchFile:
1538
self.fail("path %s is not a directory; no such file"
1540
if not stat.S_ISDIR(mode):
1541
self.fail("path %s is not a directory; has mode %#o"
1545
super(TestCaseWithTransport, self).setUp()
1546
self.__server = None
1547
self.transport_server = default_transport
1550
class ChrootedTestCase(TestCaseWithTransport):
1551
"""A support class that provides readonly urls outside the local namespace.
1553
This is done by checking if self.transport_server is a MemoryServer. if it
1554
is then we are chrooted already, if it is not then an HttpServer is used
1557
TODO RBC 20060127: make this an option to TestCaseWithTransport so it can
1558
be used without needed to redo it when a different
1559
subclass is in use ?
1563
super(ChrootedTestCase, self).setUp()
1564
if not self.transport_server == MemoryServer:
1565
self.transport_readonly_server = HttpServer
397
1568
def filter_suite_by_re(suite, pattern):
398
1569
result = TestUtil.TestSuite()
399
1570
filter_re = re.compile(pattern)
400
1571
for test in iter_suite_tests(suite):
401
if filter_re.match(test.id()):
1572
if filter_re.search(test.id()):
402
1573
result.addTest(test)
406
def filter_suite_by_names(suite, wanted_names):
407
"""Return a new suite containing only selected tests.
409
Names are considered to match if any name is a substring of the
410
fully-qualified test id (i.e. the class ."""
412
for test in iter_suite_tests(suite):
414
for p in wanted_names:
415
if this_id.find(p) != -1:
420
def run_suite(suite, name='test', verbose=False, pattern=".*", testnames=None):
421
TestCaseInTempDir._TEST_NAME = name
1577
def run_suite(suite, name='test', verbose=False, pattern=".*",
1578
stop_on_failure=False, keep_output=False,
1579
transport=None, lsprof_timed=None, bench_history=None):
1580
TestCase._gather_lsprof_in_benchmarks = lsprof_timed
1586
pb = progress.ProgressBar()
426
1587
runner = TextTestRunner(stream=sys.stdout,
430
suite = filter_suite_by_names(suite, testnames)
1589
verbosity=verbosity,
1590
keep_output=keep_output,
1592
bench_history=bench_history)
1593
runner.stop_on_failure=stop_on_failure
431
1594
if pattern != '.*':
432
1595
suite = filter_suite_by_re(suite, pattern)
433
1596
result = runner.run(suite)
434
# This is still a little bogus,
435
# but only a little. Folk not using our testrunner will
436
# have to delete their temp directories themselves.
437
if result.wasSuccessful():
438
if TestCaseInTempDir.TEST_ROOT is not None:
439
shutil.rmtree(TestCaseInTempDir.TEST_ROOT)
441
print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
442
1597
return result.wasSuccessful()
445
def selftest(verbose=False, pattern=".*", testnames=None):
1600
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
1603
test_suite_factory=None,
1605
bench_history=None):
446
1606
"""Run the whole test suite under the enhanced runner"""
447
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
1607
# XXX: Very ugly way to do this...
1608
# Disable warning about old formats because we don't want it to disturb
1609
# any blackbox tests.
1610
from bzrlib import repository
1611
repository._deprecation_warning_done = True
1613
global default_transport
1614
if transport is None:
1615
transport = default_transport
1616
old_transport = default_transport
1617
default_transport = transport
1619
if test_suite_factory is None:
1620
suite = test_suite()
1622
suite = test_suite_factory()
1623
return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
1624
stop_on_failure=stop_on_failure, keep_output=keep_output,
1625
transport=transport,
1626
lsprof_timed=lsprof_timed,
1627
bench_history=bench_history)
1629
default_transport = old_transport
451
1632
def test_suite():
452
"""Build and return TestSuite for the whole program."""
453
import bzrlib.store, bzrlib.inventory, bzrlib.branch
454
import bzrlib.osutils, bzrlib.merge3, bzrlib.plugin
455
from doctest import DocTestSuite
457
global MODULES_TO_TEST, MODULES_TO_DOCTEST
460
['bzrlib.selftest.MetaTestLog',
461
'bzrlib.selftest.testinv',
462
'bzrlib.selftest.test_ancestry',
463
'bzrlib.selftest.test_commit',
464
'bzrlib.selftest.test_commit_merge',
465
'bzrlib.selftest.versioning',
466
'bzrlib.selftest.testmerge3',
467
'bzrlib.selftest.testmerge',
468
'bzrlib.selftest.testhashcache',
469
'bzrlib.selftest.teststatus',
470
'bzrlib.selftest.testlog',
471
'bzrlib.selftest.testrevisionnamespaces',
472
'bzrlib.selftest.testbranch',
473
'bzrlib.selftest.testrevision',
474
'bzrlib.selftest.test_revision_info',
475
'bzrlib.selftest.test_merge_core',
476
'bzrlib.selftest.test_smart_add',
477
'bzrlib.selftest.test_bad_files',
478
'bzrlib.selftest.testdiff',
479
'bzrlib.selftest.test_parent',
480
'bzrlib.selftest.test_xml',
481
'bzrlib.selftest.test_weave',
482
'bzrlib.selftest.testfetch',
483
'bzrlib.selftest.whitebox',
484
'bzrlib.selftest.teststore',
485
'bzrlib.selftest.blackbox',
486
'bzrlib.selftest.testsampler',
487
'bzrlib.selftest.testtransport',
488
'bzrlib.selftest.testgraph',
489
'bzrlib.selftest.testworkingtree',
490
'bzrlib.selftest.test_upgrade',
491
'bzrlib.selftest.test_conflicts',
1633
"""Build and return TestSuite for the whole of bzrlib.
1635
This function can be replaced if you need to change the default test
1636
suite on a global basis, but it is not encouraged.
1639
'bzrlib.tests.test_ancestry',
1640
'bzrlib.tests.test_api',
1641
'bzrlib.tests.test_atomicfile',
1642
'bzrlib.tests.test_bad_files',
1643
'bzrlib.tests.test_branch',
1644
'bzrlib.tests.test_bundle',
1645
'bzrlib.tests.test_bzrdir',
1646
'bzrlib.tests.test_cache_utf8',
1647
'bzrlib.tests.test_command',
1648
'bzrlib.tests.test_commit',
1649
'bzrlib.tests.test_commit_merge',
1650
'bzrlib.tests.test_config',
1651
'bzrlib.tests.test_conflicts',
1652
'bzrlib.tests.test_decorators',
1653
'bzrlib.tests.test_diff',
1654
'bzrlib.tests.test_doc_generate',
1655
'bzrlib.tests.test_errors',
1656
'bzrlib.tests.test_escaped_store',
1657
'bzrlib.tests.test_fetch',
1658
'bzrlib.tests.test_ftp_transport',
1659
'bzrlib.tests.test_gpg',
1660
'bzrlib.tests.test_graph',
1661
'bzrlib.tests.test_hashcache',
1662
'bzrlib.tests.test_http',
1663
'bzrlib.tests.test_http_response',
1664
'bzrlib.tests.test_identitymap',
1665
'bzrlib.tests.test_ignores',
1666
'bzrlib.tests.test_inv',
1667
'bzrlib.tests.test_knit',
1668
'bzrlib.tests.test_lazy_import',
1669
'bzrlib.tests.test_lazy_regex',
1670
'bzrlib.tests.test_lockdir',
1671
'bzrlib.tests.test_lockable_files',
1672
'bzrlib.tests.test_log',
1673
'bzrlib.tests.test_memorytree',
1674
'bzrlib.tests.test_merge',
1675
'bzrlib.tests.test_merge3',
1676
'bzrlib.tests.test_merge_core',
1677
'bzrlib.tests.test_missing',
1678
'bzrlib.tests.test_msgeditor',
1679
'bzrlib.tests.test_nonascii',
1680
'bzrlib.tests.test_options',
1681
'bzrlib.tests.test_osutils',
1682
'bzrlib.tests.test_patch',
1683
'bzrlib.tests.test_patches',
1684
'bzrlib.tests.test_permissions',
1685
'bzrlib.tests.test_plugins',
1686
'bzrlib.tests.test_progress',
1687
'bzrlib.tests.test_reconcile',
1688
'bzrlib.tests.test_registry',
1689
'bzrlib.tests.test_repository',
1690
'bzrlib.tests.test_revert',
1691
'bzrlib.tests.test_revision',
1692
'bzrlib.tests.test_revisionnamespaces',
1693
'bzrlib.tests.test_revisiontree',
1694
'bzrlib.tests.test_rio',
1695
'bzrlib.tests.test_sampler',
1696
'bzrlib.tests.test_selftest',
1697
'bzrlib.tests.test_setup',
1698
'bzrlib.tests.test_sftp_transport',
1699
'bzrlib.tests.test_smart_add',
1700
'bzrlib.tests.test_smart_transport',
1701
'bzrlib.tests.test_source',
1702
'bzrlib.tests.test_status',
1703
'bzrlib.tests.test_store',
1704
'bzrlib.tests.test_symbol_versioning',
1705
'bzrlib.tests.test_testament',
1706
'bzrlib.tests.test_textfile',
1707
'bzrlib.tests.test_textmerge',
1708
'bzrlib.tests.test_trace',
1709
'bzrlib.tests.test_transactions',
1710
'bzrlib.tests.test_transform',
1711
'bzrlib.tests.test_transport',
1712
'bzrlib.tests.test_tree',
1713
'bzrlib.tests.test_treebuilder',
1714
'bzrlib.tests.test_tsort',
1715
'bzrlib.tests.test_tuned_gzip',
1716
'bzrlib.tests.test_ui',
1717
'bzrlib.tests.test_upgrade',
1718
'bzrlib.tests.test_urlutils',
1719
'bzrlib.tests.test_versionedfile',
1720
'bzrlib.tests.test_version',
1721
'bzrlib.tests.test_version_info',
1722
'bzrlib.tests.test_weave',
1723
'bzrlib.tests.test_whitebox',
1724
'bzrlib.tests.test_workingtree',
1725
'bzrlib.tests.test_xml',
494
for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
495
bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
496
if m not in MODULES_TO_DOCTEST:
497
MODULES_TO_DOCTEST.append(m)
499
TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
500
print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
503
suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
1727
test_transport_implementations = [
1728
'bzrlib.tests.test_transport_implementations',
1729
'bzrlib.tests.test_read_bundle',
1731
suite = TestUtil.TestSuite()
1732
loader = TestUtil.TestLoader()
1733
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
1734
from bzrlib.transport import TransportTestProviderAdapter
1735
adapter = TransportTestProviderAdapter()
1736
adapt_modules(test_transport_implementations, adapter, loader, suite)
1737
for package in packages_to_test():
1738
suite.addTest(package.test_suite())
504
1739
for m in MODULES_TO_TEST:
505
suite.addTest(TestLoader().loadTestsFromModule(m))
506
for m in (MODULES_TO_DOCTEST):
507
suite.addTest(DocTestSuite(m))
508
for p in bzrlib.plugin.all_plugins:
509
if hasattr(p, 'test_suite'):
510
suite.addTest(p.test_suite())
1740
suite.addTest(loader.loadTestsFromModule(m))
1741
for m in MODULES_TO_DOCTEST:
1743
suite.addTest(doctest.DocTestSuite(m))
1744
except ValueError, e:
1745
print '**failed to get doctest for: %s\n%s' %(m,e)
1747
for name, plugin in bzrlib.plugin.all_plugins().items():
1748
if getattr(plugin, 'test_suite', None) is not None:
1749
suite.addTest(plugin.test_suite())
1753
def adapt_modules(mods_list, adapter, loader, suite):
1754
"""Adapt the modules in mods_list using adapter and add to suite."""
1755
for test in iter_suite_tests(loader.loadTestsFromModuleNames(mods_list)):
1756
suite.addTests(adapter.adapt(test))