1
# Copyright (C) 2005, 2006, 2007, 2008 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., 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.
31
from cStringIO import StringIO
37
from pprint import pformat
42
from subprocess import Popen, PIPE
62
import bzrlib.commands
63
import bzrlib.timestamp
65
import bzrlib.inventory
66
import bzrlib.iterablefile
71
# lsprof not available
73
from bzrlib.merge import merge_inner
76
from bzrlib.revision import common_ancestor
78
from bzrlib import symbol_versioning
79
from bzrlib.symbol_versioning import (
86
from bzrlib.transport import get_transport
87
import bzrlib.transport
88
from bzrlib.transport.local import LocalURLServer
89
from bzrlib.transport.memory import MemoryServer
90
from bzrlib.transport.readonly import ReadonlyServer
91
from bzrlib.trace import mutter, note
92
from bzrlib.tests import TestUtil
93
from bzrlib.tests.http_server import HttpServer
94
from bzrlib.tests.TestUtil import (
98
from bzrlib.tests.treeshape import build_tree_contents
99
import bzrlib.version_info_formats.format_custom
100
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
102
# Mark this python module as being part of the implementation
103
# of unittest: this gives us better tracebacks where the last
104
# shown frame is the test code, not our assertXYZ.
107
default_transport = LocalURLServer
110
def packages_to_test():
111
"""Return a list of packages to test.
113
The packages are not globally imported so that import failures are
114
triggered when running selftest, not when importing the command.
117
import bzrlib.tests.blackbox
118
import bzrlib.tests.branch_implementations
119
import bzrlib.tests.bzrdir_implementations
120
import bzrlib.tests.commands
121
import bzrlib.tests.interrepository_implementations
122
import bzrlib.tests.interversionedfile_implementations
123
import bzrlib.tests.intertree_implementations
124
import bzrlib.tests.inventory_implementations
125
import bzrlib.tests.per_lock
126
import bzrlib.tests.repository_implementations
127
import bzrlib.tests.revisionstore_implementations
128
import bzrlib.tests.tree_implementations
129
import bzrlib.tests.workingtree_implementations
132
bzrlib.tests.blackbox,
133
bzrlib.tests.branch_implementations,
134
bzrlib.tests.bzrdir_implementations,
135
bzrlib.tests.commands,
136
bzrlib.tests.interrepository_implementations,
137
bzrlib.tests.interversionedfile_implementations,
138
bzrlib.tests.intertree_implementations,
139
bzrlib.tests.inventory_implementations,
140
bzrlib.tests.per_lock,
141
bzrlib.tests.repository_implementations,
142
bzrlib.tests.revisionstore_implementations,
143
bzrlib.tests.tree_implementations,
144
bzrlib.tests.workingtree_implementations,
148
class ExtendedTestResult(unittest._TextTestResult):
149
"""Accepts, reports and accumulates the results of running tests.
151
Compared to the unittest version this class adds support for
152
profiling, benchmarking, stopping as soon as a test fails, and
153
skipping tests. There are further-specialized subclasses for
154
different types of display.
156
When a test finishes, in whatever way, it calls one of the addSuccess,
157
addFailure or addError classes. These in turn may redirect to a more
158
specific case for the special test results supported by our extended
161
Note that just one of these objects is fed the results from many tests.
166
def __init__(self, stream, descriptions, verbosity,
170
"""Construct new TestResult.
172
:param bench_history: Optionally, a writable file object to accumulate
175
unittest._TextTestResult.__init__(self, stream, descriptions, verbosity)
176
if bench_history is not None:
177
from bzrlib.version import _get_bzr_source_tree
178
src_tree = _get_bzr_source_tree()
181
revision_id = src_tree.get_parent_ids()[0]
183
# XXX: if this is a brand new tree, do the same as if there
187
# XXX: If there's no branch, what should we do?
189
bench_history.write("--date %s %s\n" % (time.time(), revision_id))
190
self._bench_history = bench_history
191
self.ui = ui.ui_factory
192
self.num_tests = num_tests
194
self.failure_count = 0
195
self.known_failure_count = 0
197
self.not_applicable_count = 0
198
self.unsupported = {}
200
self._overall_start_time = time.time()
202
def _extractBenchmarkTime(self, testCase):
203
"""Add a benchmark time for the current test case."""
204
return getattr(testCase, "_benchtime", None)
206
def _elapsedTestTimeString(self):
207
"""Return a time string for the overall time the current test has taken."""
208
return self._formatTime(time.time() - self._start_time)
210
def _testTimeString(self, testCase):
211
benchmark_time = self._extractBenchmarkTime(testCase)
212
if benchmark_time is not None:
214
self._formatTime(benchmark_time),
215
self._elapsedTestTimeString())
217
return " %s" % self._elapsedTestTimeString()
219
def _formatTime(self, seconds):
220
"""Format seconds as milliseconds with leading spaces."""
221
# some benchmarks can take thousands of seconds to run, so we need 8
223
return "%8dms" % (1000 * seconds)
225
def _shortened_test_description(self, test):
227
what = re.sub(r'^bzrlib\.(tests|benchmarks)\.', '', what)
230
def startTest(self, test):
231
unittest.TestResult.startTest(self, test)
232
self.report_test_start(test)
233
test.number = self.count
234
self._recordTestStartTime()
236
def _recordTestStartTime(self):
237
"""Record that a test has started."""
238
self._start_time = time.time()
240
def _cleanupLogFile(self, test):
241
# We can only do this if we have one of our TestCases, not if
243
setKeepLogfile = getattr(test, 'setKeepLogfile', None)
244
if setKeepLogfile is not None:
247
def addError(self, test, err):
248
"""Tell result that test finished with an error.
250
Called from the TestCase run() method when the test
251
fails with an unexpected error.
253
self._testConcluded(test)
254
if isinstance(err[1], TestSkipped):
255
return self._addSkipped(test, err)
256
elif isinstance(err[1], UnavailableFeature):
257
return self.addNotSupported(test, err[1].args[0])
259
self._cleanupLogFile(test)
260
unittest.TestResult.addError(self, test, err)
261
self.error_count += 1
262
self.report_error(test, err)
266
def addFailure(self, test, err):
267
"""Tell result that test failed.
269
Called from the TestCase run() method when the test
270
fails because e.g. an assert() method failed.
272
self._testConcluded(test)
273
if isinstance(err[1], KnownFailure):
274
return self._addKnownFailure(test, err)
276
self._cleanupLogFile(test)
277
unittest.TestResult.addFailure(self, test, err)
278
self.failure_count += 1
279
self.report_failure(test, err)
283
def addSuccess(self, test):
284
"""Tell result that test completed successfully.
286
Called from the TestCase run()
288
self._testConcluded(test)
289
if self._bench_history is not None:
290
benchmark_time = self._extractBenchmarkTime(test)
291
if benchmark_time is not None:
292
self._bench_history.write("%s %s\n" % (
293
self._formatTime(benchmark_time),
295
self.report_success(test)
296
self._cleanupLogFile(test)
297
unittest.TestResult.addSuccess(self, test)
298
test._log_contents = ''
300
def _testConcluded(self, test):
301
"""Common code when a test has finished.
303
Called regardless of whether it succeded, failed, etc.
307
def _addKnownFailure(self, test, err):
308
self.known_failure_count += 1
309
self.report_known_failure(test, err)
311
def addNotSupported(self, test, feature):
312
"""The test will not be run because of a missing feature.
314
# this can be called in two different ways: it may be that the
315
# test started running, and then raised (through addError)
316
# UnavailableFeature. Alternatively this method can be called
317
# while probing for features before running the tests; in that
318
# case we will see startTest and stopTest, but the test will never
320
self.unsupported.setdefault(str(feature), 0)
321
self.unsupported[str(feature)] += 1
322
self.report_unsupported(test, feature)
324
def _addSkipped(self, test, skip_excinfo):
325
if isinstance(skip_excinfo[1], TestNotApplicable):
326
self.not_applicable_count += 1
327
self.report_not_applicable(test, skip_excinfo)
330
self.report_skip(test, skip_excinfo)
333
except KeyboardInterrupt:
336
self.addError(test, test._exc_info())
338
# seems best to treat this as success from point-of-view of unittest
339
# -- it actually does nothing so it barely matters :)
340
unittest.TestResult.addSuccess(self, test)
341
test._log_contents = ''
343
def printErrorList(self, flavour, errors):
344
for test, err in errors:
345
self.stream.writeln(self.separator1)
346
self.stream.write("%s: " % flavour)
347
self.stream.writeln(self.getDescription(test))
348
if getattr(test, '_get_log', None) is not None:
349
self.stream.write('\n')
351
('vvvv[log from %s]' % test.id()).ljust(78,'-'))
352
self.stream.write('\n')
353
self.stream.write(test._get_log())
354
self.stream.write('\n')
356
('^^^^[log from %s]' % test.id()).ljust(78,'-'))
357
self.stream.write('\n')
358
self.stream.writeln(self.separator2)
359
self.stream.writeln("%s" % err)
364
def report_cleaning_up(self):
367
def report_success(self, test):
370
def wasStrictlySuccessful(self):
371
if self.unsupported or self.known_failure_count:
373
return self.wasSuccessful()
376
class TextTestResult(ExtendedTestResult):
377
"""Displays progress and results of tests in text form"""
379
def __init__(self, stream, descriptions, verbosity,
384
ExtendedTestResult.__init__(self, stream, descriptions, verbosity,
385
bench_history, num_tests)
387
self.pb = self.ui.nested_progress_bar()
388
self._supplied_pb = False
391
self._supplied_pb = True
392
self.pb.show_pct = False
393
self.pb.show_spinner = False
394
self.pb.show_eta = False,
395
self.pb.show_count = False
396
self.pb.show_bar = False
398
def report_starting(self):
399
self.pb.update('[test 0/%d] starting...' % (self.num_tests))
401
def _progress_prefix_text(self):
402
# the longer this text, the less space we have to show the test
404
a = '[%d' % self.count # total that have been run
405
# tests skipped as known not to be relevant are not important enough
407
## if self.skip_count:
408
## a += ', %d skip' % self.skip_count
409
## if self.known_failure_count:
410
## a += '+%dX' % self.known_failure_count
411
if self.num_tests is not None:
412
a +='/%d' % self.num_tests
414
runtime = time.time() - self._overall_start_time
416
a += '%dm%ds' % (runtime / 60, runtime % 60)
420
a += ', %d err' % self.error_count
421
if self.failure_count:
422
a += ', %d fail' % self.failure_count
424
a += ', %d missing' % len(self.unsupported)
428
def report_test_start(self, test):
431
self._progress_prefix_text()
433
+ self._shortened_test_description(test))
435
def _test_description(self, test):
436
return self._shortened_test_description(test)
438
def report_error(self, test, err):
439
self.pb.note('ERROR: %s\n %s\n',
440
self._test_description(test),
444
def report_failure(self, test, err):
445
self.pb.note('FAIL: %s\n %s\n',
446
self._test_description(test),
450
def report_known_failure(self, test, err):
451
self.pb.note('XFAIL: %s\n%s\n',
452
self._test_description(test), err[1])
454
def report_skip(self, test, skip_excinfo):
457
def report_not_applicable(self, test, skip_excinfo):
460
def report_unsupported(self, test, feature):
461
"""test cannot be run because feature is missing."""
463
def report_cleaning_up(self):
464
self.pb.update('cleaning up...')
467
if not self._supplied_pb:
471
class VerboseTestResult(ExtendedTestResult):
472
"""Produce long output, with one line per test run plus times"""
474
def _ellipsize_to_right(self, a_string, final_width):
475
"""Truncate and pad a string, keeping the right hand side"""
476
if len(a_string) > final_width:
477
result = '...' + a_string[3-final_width:]
480
return result.ljust(final_width)
482
def report_starting(self):
483
self.stream.write('running %d tests...\n' % self.num_tests)
485
def report_test_start(self, test):
487
name = self._shortened_test_description(test)
488
# width needs space for 6 char status, plus 1 for slash, plus 2 10-char
489
# numbers, plus a trailing blank
490
# when NUMBERED_DIRS: plus 5 chars on test number, plus 1 char on space
491
self.stream.write(self._ellipsize_to_right(name,
492
osutils.terminal_width()-30))
495
def _error_summary(self, err):
497
return '%s%s' % (indent, err[1])
499
def report_error(self, test, err):
500
self.stream.writeln('ERROR %s\n%s'
501
% (self._testTimeString(test),
502
self._error_summary(err)))
504
def report_failure(self, test, err):
505
self.stream.writeln(' FAIL %s\n%s'
506
% (self._testTimeString(test),
507
self._error_summary(err)))
509
def report_known_failure(self, test, err):
510
self.stream.writeln('XFAIL %s\n%s'
511
% (self._testTimeString(test),
512
self._error_summary(err)))
514
def report_success(self, test):
515
self.stream.writeln(' OK %s' % self._testTimeString(test))
516
for bench_called, stats in getattr(test, '_benchcalls', []):
517
self.stream.writeln('LSProf output for %s(%s, %s)' % bench_called)
518
stats.pprint(file=self.stream)
519
# flush the stream so that we get smooth output. This verbose mode is
520
# used to show the output in PQM.
523
def report_skip(self, test, skip_excinfo):
524
self.stream.writeln(' SKIP %s\n%s'
525
% (self._testTimeString(test),
526
self._error_summary(skip_excinfo)))
528
def report_not_applicable(self, test, skip_excinfo):
529
self.stream.writeln(' N/A %s\n%s'
530
% (self._testTimeString(test),
531
self._error_summary(skip_excinfo)))
533
def report_unsupported(self, test, feature):
534
"""test cannot be run because feature is missing."""
535
self.stream.writeln("NODEP %s\n The feature '%s' is not available."
536
%(self._testTimeString(test), feature))
539
class TextTestRunner(object):
540
stop_on_failure = False
549
self.stream = unittest._WritelnDecorator(stream)
550
self.descriptions = descriptions
551
self.verbosity = verbosity
552
self._bench_history = bench_history
553
self.list_only = list_only
556
"Run the given test case or test suite."
557
startTime = time.time()
558
if self.verbosity == 1:
559
result_class = TextTestResult
560
elif self.verbosity >= 2:
561
result_class = VerboseTestResult
562
result = result_class(self.stream,
565
bench_history=self._bench_history,
566
num_tests=test.countTestCases(),
568
result.stop_early = self.stop_on_failure
569
result.report_starting()
571
if self.verbosity >= 2:
572
self.stream.writeln("Listing tests only ...\n")
574
for t in iter_suite_tests(test):
575
self.stream.writeln("%s" % (t.id()))
577
actionTaken = "Listed"
580
run = result.testsRun
582
stopTime = time.time()
583
timeTaken = stopTime - startTime
585
self.stream.writeln(result.separator2)
586
self.stream.writeln("%s %d test%s in %.3fs" % (actionTaken,
587
run, run != 1 and "s" or "", timeTaken))
588
self.stream.writeln()
589
if not result.wasSuccessful():
590
self.stream.write("FAILED (")
591
failed, errored = map(len, (result.failures, result.errors))
593
self.stream.write("failures=%d" % failed)
595
if failed: self.stream.write(", ")
596
self.stream.write("errors=%d" % errored)
597
if result.known_failure_count:
598
if failed or errored: self.stream.write(", ")
599
self.stream.write("known_failure_count=%d" %
600
result.known_failure_count)
601
self.stream.writeln(")")
603
if result.known_failure_count:
604
self.stream.writeln("OK (known_failures=%d)" %
605
result.known_failure_count)
607
self.stream.writeln("OK")
608
if result.skip_count > 0:
609
skipped = result.skip_count
610
self.stream.writeln('%d test%s skipped' %
611
(skipped, skipped != 1 and "s" or ""))
612
if result.unsupported:
613
for feature, count in sorted(result.unsupported.items()):
614
self.stream.writeln("Missing feature '%s' skipped %d tests." %
620
def iter_suite_tests(suite):
621
"""Return all tests in a suite, recursing through nested suites"""
622
for item in suite._tests:
623
if isinstance(item, unittest.TestCase):
625
elif isinstance(item, unittest.TestSuite):
626
for r in iter_suite_tests(item):
629
raise Exception('unknown object %r inside test suite %r'
633
class TestSkipped(Exception):
634
"""Indicates that a test was intentionally skipped, rather than failing."""
637
class TestNotApplicable(TestSkipped):
638
"""A test is not applicable to the situation where it was run.
640
This is only normally raised by parameterized tests, if they find that
641
the instance they're constructed upon does not support one aspect
646
class KnownFailure(AssertionError):
647
"""Indicates that a test failed in a precisely expected manner.
649
Such failures dont block the whole test suite from passing because they are
650
indicators of partially completed code or of future work. We have an
651
explicit error for them so that we can ensure that they are always visible:
652
KnownFailures are always shown in the output of bzr selftest.
656
class UnavailableFeature(Exception):
657
"""A feature required for this test was not available.
659
The feature should be used to construct the exception.
663
class CommandFailed(Exception):
667
class StringIOWrapper(object):
668
"""A wrapper around cStringIO which just adds an encoding attribute.
670
Internally we can check sys.stdout to see what the output encoding
671
should be. However, cStringIO has no encoding attribute that we can
672
set. So we wrap it instead.
677
def __init__(self, s=None):
679
self.__dict__['_cstring'] = StringIO(s)
681
self.__dict__['_cstring'] = StringIO()
683
def __getattr__(self, name, getattr=getattr):
684
return getattr(self.__dict__['_cstring'], name)
686
def __setattr__(self, name, val):
687
if name == 'encoding':
688
self.__dict__['encoding'] = val
690
return setattr(self._cstring, name, val)
693
class TestUIFactory(ui.CLIUIFactory):
694
"""A UI Factory for testing.
696
Hide the progress bar but emit note()s.
698
Allows get_password to be tested without real tty attached.
705
super(TestUIFactory, self).__init__()
706
if stdin is not None:
707
# We use a StringIOWrapper to be able to test various
708
# encodings, but the user is still responsible to
709
# encode the string and to set the encoding attribute
710
# of StringIOWrapper.
711
self.stdin = StringIOWrapper(stdin)
713
self.stdout = sys.stdout
717
self.stderr = sys.stderr
722
"""See progress.ProgressBar.clear()."""
724
def clear_term(self):
725
"""See progress.ProgressBar.clear_term()."""
727
def clear_term(self):
728
"""See progress.ProgressBar.clear_term()."""
731
"""See progress.ProgressBar.finished()."""
733
def note(self, fmt_string, *args, **kwargs):
734
"""See progress.ProgressBar.note()."""
735
self.stdout.write((fmt_string + "\n") % args)
737
def progress_bar(self):
740
def nested_progress_bar(self):
743
def update(self, message, count=None, total=None):
744
"""See progress.ProgressBar.update()."""
746
def get_non_echoed_password(self, prompt):
747
"""Get password from stdin without trying to handle the echo mode"""
749
self.stdout.write(prompt.encode(self.stdout.encoding, 'replace'))
750
password = self.stdin.readline()
753
if password[-1] == '\n':
754
password = password[:-1]
758
class TestCase(unittest.TestCase):
759
"""Base class for bzr unit tests.
761
Tests that need access to disk resources should subclass
762
TestCaseInTempDir not TestCase.
764
Error and debug log messages are redirected from their usual
765
location into a temporary file, the contents of which can be
766
retrieved by _get_log(). We use a real OS file, not an in-memory object,
767
so that it can also capture file IO. When the test completes this file
768
is read into memory and removed from disk.
770
There are also convenience functions to invoke bzr's command-line
771
routine, and to build and check bzr trees.
773
In addition to the usual method of overriding tearDown(), this class also
774
allows subclasses to register functions into the _cleanups list, which is
775
run in order as the object is torn down. It's less likely this will be
776
accidentally overlooked.
779
_log_file_name = None
781
_keep_log_file = False
782
# record lsprof data when performing benchmark calls.
783
_gather_lsprof_in_benchmarks = False
784
attrs_to_keep = ('id', '_testMethodName', '_testMethodDoc',
785
'_log_contents', '_log_file_name', '_benchtime',
786
'_TestCase__testMethodName')
788
def __init__(self, methodName='testMethod'):
789
super(TestCase, self).__init__(methodName)
793
unittest.TestCase.setUp(self)
794
self._cleanEnvironment()
797
self._benchcalls = []
798
self._benchtime = None
800
self._clear_debug_flags()
802
def _clear_debug_flags(self):
803
"""Prevent externally set debug flags affecting tests.
805
Tests that want to use debug flags can just set them in the
806
debug_flags set during setup/teardown.
808
if 'selftest_debug' not in debug.debug_flags:
809
self._preserved_debug_flags = set(debug.debug_flags)
810
debug.debug_flags.clear()
811
self.addCleanup(self._restore_debug_flags)
813
def _clear_hooks(self):
814
# prevent hooks affecting tests
816
import bzrlib.smart.server
817
self._preserved_hooks = {
818
bzrlib.branch.Branch: bzrlib.branch.Branch.hooks,
819
bzrlib.mutabletree.MutableTree: bzrlib.mutabletree.MutableTree.hooks,
820
bzrlib.smart.server.SmartTCPServer: bzrlib.smart.server.SmartTCPServer.hooks,
822
self.addCleanup(self._restoreHooks)
823
# reset all hooks to an empty instance of the appropriate type
824
bzrlib.branch.Branch.hooks = bzrlib.branch.BranchHooks()
825
bzrlib.smart.server.SmartTCPServer.hooks = bzrlib.smart.server.SmartServerHooks()
827
def _silenceUI(self):
828
"""Turn off UI for duration of test"""
829
# by default the UI is off; tests can turn it on if they want it.
830
saved = ui.ui_factory
832
ui.ui_factory = saved
833
ui.ui_factory = ui.SilentUIFactory()
834
self.addCleanup(_restore)
836
def _ndiff_strings(self, a, b):
837
"""Return ndiff between two strings containing lines.
839
A trailing newline is added if missing to make the strings
841
if b and b[-1] != '\n':
843
if a and a[-1] != '\n':
845
difflines = difflib.ndiff(a.splitlines(True),
847
linejunk=lambda x: False,
848
charjunk=lambda x: False)
849
return ''.join(difflines)
851
def assertEqual(self, a, b, message=''):
855
except UnicodeError, e:
856
# If we can't compare without getting a UnicodeError, then
857
# obviously they are different
858
mutter('UnicodeError: %s', e)
861
raise AssertionError("%snot equal:\na = %s\nb = %s\n"
863
pformat(a), pformat(b)))
865
assertEquals = assertEqual
867
def assertEqualDiff(self, a, b, message=None):
868
"""Assert two texts are equal, if not raise an exception.
870
This is intended for use with multi-line strings where it can
871
be hard to find the differences by eye.
873
# TODO: perhaps override assertEquals to call this for strings?
877
message = "texts not equal:\n"
878
raise AssertionError(message +
879
self._ndiff_strings(a, b))
881
def assertEqualMode(self, mode, mode_test):
882
self.assertEqual(mode, mode_test,
883
'mode mismatch %o != %o' % (mode, mode_test))
885
def assertPositive(self, val):
886
"""Assert that val is greater than 0."""
887
self.assertTrue(val > 0, 'expected a positive value, but got %s' % val)
889
def assertNegative(self, val):
890
"""Assert that val is less than 0."""
891
self.assertTrue(val < 0, 'expected a negative value, but got %s' % val)
893
def assertStartsWith(self, s, prefix):
894
if not s.startswith(prefix):
895
raise AssertionError('string %r does not start with %r' % (s, prefix))
897
def assertEndsWith(self, s, suffix):
898
"""Asserts that s ends with suffix."""
899
if not s.endswith(suffix):
900
raise AssertionError('string %r does not end with %r' % (s, suffix))
902
def assertContainsRe(self, haystack, needle_re):
903
"""Assert that a contains something matching a regular expression."""
904
if not re.search(needle_re, haystack):
905
if '\n' in haystack or len(haystack) > 60:
906
# a long string, format it in a more readable way
907
raise AssertionError(
908
'pattern "%s" not found in\n"""\\\n%s"""\n'
909
% (needle_re, haystack))
911
raise AssertionError('pattern "%s" not found in "%s"'
912
% (needle_re, haystack))
914
def assertNotContainsRe(self, haystack, needle_re):
915
"""Assert that a does not match a regular expression"""
916
if re.search(needle_re, haystack):
917
raise AssertionError('pattern "%s" found in "%s"'
918
% (needle_re, haystack))
920
def assertSubset(self, sublist, superlist):
921
"""Assert that every entry in sublist is present in superlist."""
922
missing = set(sublist) - set(superlist)
924
raise AssertionError("value(s) %r not present in container %r" %
925
(missing, superlist))
927
def assertListRaises(self, excClass, func, *args, **kwargs):
928
"""Fail unless excClass is raised when the iterator from func is used.
930
Many functions can return generators this makes sure
931
to wrap them in a list() call to make sure the whole generator
932
is run, and that the proper exception is raised.
935
list(func(*args, **kwargs))
939
if getattr(excClass,'__name__', None) is not None:
940
excName = excClass.__name__
942
excName = str(excClass)
943
raise self.failureException, "%s not raised" % excName
945
def assertRaises(self, excClass, callableObj, *args, **kwargs):
946
"""Assert that a callable raises a particular exception.
948
:param excClass: As for the except statement, this may be either an
949
exception class, or a tuple of classes.
950
:param callableObj: A callable, will be passed ``*args`` and
953
Returns the exception so that you can examine it.
956
callableObj(*args, **kwargs)
960
if getattr(excClass,'__name__', None) is not None:
961
excName = excClass.__name__
964
excName = str(excClass)
965
raise self.failureException, "%s not raised" % excName
967
def assertIs(self, left, right, message=None):
968
if not (left is right):
969
if message is not None:
970
raise AssertionError(message)
972
raise AssertionError("%r is not %r." % (left, right))
974
def assertIsNot(self, left, right, message=None):
976
if message is not None:
977
raise AssertionError(message)
979
raise AssertionError("%r is %r." % (left, right))
981
def assertTransportMode(self, transport, path, mode):
982
"""Fail if a path does not have mode mode.
984
If modes are not supported on this transport, the assertion is ignored.
986
if not transport._can_roundtrip_unix_modebits():
988
path_stat = transport.stat(path)
989
actual_mode = stat.S_IMODE(path_stat.st_mode)
990
self.assertEqual(mode, actual_mode,
991
'mode of %r incorrect (%o != %o)' % (path, mode, actual_mode))
993
def assertIsSameRealPath(self, path1, path2):
994
"""Fail if path1 and path2 points to different files"""
995
self.assertEqual(osutils.realpath(path1),
996
osutils.realpath(path2),
997
"apparent paths:\na = %s\nb = %s\n," % (path1, path2))
999
def assertIsInstance(self, obj, kls):
1000
"""Fail if obj is not an instance of kls"""
1001
if not isinstance(obj, kls):
1002
self.fail("%r is an instance of %s rather than %s" % (
1003
obj, obj.__class__, kls))
1005
def expectFailure(self, reason, assertion, *args, **kwargs):
1006
"""Invoke a test, expecting it to fail for the given reason.
1008
This is for assertions that ought to succeed, but currently fail.
1009
(The failure is *expected* but not *wanted*.) Please be very precise
1010
about the failure you're expecting. If a new bug is introduced,
1011
AssertionError should be raised, not KnownFailure.
1013
Frequently, expectFailure should be followed by an opposite assertion.
1016
Intended to be used with a callable that raises AssertionError as the
1017
'assertion' parameter. args and kwargs are passed to the 'assertion'.
1019
Raises KnownFailure if the test fails. Raises AssertionError if the
1024
self.expectFailure('Math is broken', self.assertNotEqual, 54,
1026
self.assertEqual(42, dynamic_val)
1028
This means that a dynamic_val of 54 will cause the test to raise
1029
a KnownFailure. Once math is fixed and the expectFailure is removed,
1030
only a dynamic_val of 42 will allow the test to pass. Anything other
1031
than 54 or 42 will cause an AssertionError.
1034
assertion(*args, **kwargs)
1035
except AssertionError:
1036
raise KnownFailure(reason)
1038
self.fail('Unexpected success. Should have failed: %s' % reason)
1040
def assertFileEqual(self, content, path):
1041
"""Fail if path does not contain 'content'."""
1042
self.failUnlessExists(path)
1043
f = file(path, 'rb')
1048
self.assertEqualDiff(content, s)
1050
def failUnlessExists(self, path):
1051
"""Fail unless path or paths, which may be abs or relative, exist."""
1052
if not isinstance(path, basestring):
1054
self.failUnlessExists(p)
1056
self.failUnless(osutils.lexists(path),path+" does not exist")
1058
def failIfExists(self, path):
1059
"""Fail if path or paths, which may be abs or relative, exist."""
1060
if not isinstance(path, basestring):
1062
self.failIfExists(p)
1064
self.failIf(osutils.lexists(path),path+" exists")
1066
def _capture_deprecation_warnings(self, a_callable, *args, **kwargs):
1067
"""A helper for callDeprecated and applyDeprecated.
1069
:param a_callable: A callable to call.
1070
:param args: The positional arguments for the callable
1071
:param kwargs: The keyword arguments for the callable
1072
:return: A tuple (warnings, result). result is the result of calling
1073
a_callable(``*args``, ``**kwargs``).
1076
def capture_warnings(msg, cls=None, stacklevel=None):
1077
# we've hooked into a deprecation specific callpath,
1078
# only deprecations should getting sent via it.
1079
self.assertEqual(cls, DeprecationWarning)
1080
local_warnings.append(msg)
1081
original_warning_method = symbol_versioning.warn
1082
symbol_versioning.set_warning_method(capture_warnings)
1084
result = a_callable(*args, **kwargs)
1086
symbol_versioning.set_warning_method(original_warning_method)
1087
return (local_warnings, result)
1089
def applyDeprecated(self, deprecation_format, a_callable, *args, **kwargs):
1090
"""Call a deprecated callable without warning the user.
1092
Note that this only captures warnings raised by symbol_versioning.warn,
1093
not other callers that go direct to the warning module.
1095
To test that a deprecated method raises an error, do something like
1098
self.assertRaises(errors.ReservedId,
1099
self.applyDeprecated,
1100
deprecated_in((1, 5, 0)),
1104
:param deprecation_format: The deprecation format that the callable
1105
should have been deprecated with. This is the same type as the
1106
parameter to deprecated_method/deprecated_function. If the
1107
callable is not deprecated with this format, an assertion error
1109
:param a_callable: A callable to call. This may be a bound method or
1110
a regular function. It will be called with ``*args`` and
1112
:param args: The positional arguments for the callable
1113
:param kwargs: The keyword arguments for the callable
1114
:return: The result of a_callable(``*args``, ``**kwargs``)
1116
call_warnings, result = self._capture_deprecation_warnings(a_callable,
1118
expected_first_warning = symbol_versioning.deprecation_string(
1119
a_callable, deprecation_format)
1120
if len(call_warnings) == 0:
1121
self.fail("No deprecation warning generated by call to %s" %
1123
self.assertEqual(expected_first_warning, call_warnings[0])
1126
def callCatchWarnings(self, fn, *args, **kw):
1127
"""Call a callable that raises python warnings.
1129
The caller's responsible for examining the returned warnings.
1131
If the callable raises an exception, the exception is not
1132
caught and propagates up to the caller. In that case, the list
1133
of warnings is not available.
1135
:returns: ([warning_object, ...], fn_result)
1137
# XXX: This is not perfect, because it completely overrides the
1138
# warnings filters, and some code may depend on suppressing particular
1139
# warnings. It's the easiest way to insulate ourselves from -Werror,
1140
# though. -- Andrew, 20071062
1142
def _catcher(message, category, filename, lineno, file=None):
1143
# despite the name, 'message' is normally(?) a Warning subclass
1145
wlist.append(message)
1146
saved_showwarning = warnings.showwarning
1147
saved_filters = warnings.filters
1149
warnings.showwarning = _catcher
1150
warnings.filters = []
1151
result = fn(*args, **kw)
1153
warnings.showwarning = saved_showwarning
1154
warnings.filters = saved_filters
1155
return wlist, result
1157
def callDeprecated(self, expected, callable, *args, **kwargs):
1158
"""Assert that a callable is deprecated in a particular way.
1160
This is a very precise test for unusual requirements. The
1161
applyDeprecated helper function is probably more suited for most tests
1162
as it allows you to simply specify the deprecation format being used
1163
and will ensure that that is issued for the function being called.
1165
Note that this only captures warnings raised by symbol_versioning.warn,
1166
not other callers that go direct to the warning module. To catch
1167
general warnings, use callCatchWarnings.
1169
:param expected: a list of the deprecation warnings expected, in order
1170
:param callable: The callable to call
1171
:param args: The positional arguments for the callable
1172
:param kwargs: The keyword arguments for the callable
1174
call_warnings, result = self._capture_deprecation_warnings(callable,
1176
self.assertEqual(expected, call_warnings)
1179
def _startLogFile(self):
1180
"""Send bzr and test log messages to a temporary file.
1182
The file is removed as the test is torn down.
1184
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
1185
self._log_file = os.fdopen(fileno, 'w+')
1186
self._log_memento = bzrlib.trace.push_log_file(self._log_file)
1187
self._log_file_name = name
1188
self.addCleanup(self._finishLogFile)
1190
def _finishLogFile(self):
1191
"""Finished with the log file.
1193
Close the file and delete it, unless setKeepLogfile was called.
1195
if self._log_file is None:
1197
bzrlib.trace.pop_log_file(self._log_memento)
1198
self._log_file.close()
1199
self._log_file = None
1200
if not self._keep_log_file:
1201
os.remove(self._log_file_name)
1202
self._log_file_name = None
1204
def setKeepLogfile(self):
1205
"""Make the logfile not be deleted when _finishLogFile is called."""
1206
self._keep_log_file = True
1208
def addCleanup(self, callable):
1209
"""Arrange to run a callable when this case is torn down.
1211
Callables are run in the reverse of the order they are registered,
1212
ie last-in first-out.
1214
if callable in self._cleanups:
1215
raise ValueError("cleanup function %r already registered on %s"
1217
self._cleanups.append(callable)
1219
def _cleanEnvironment(self):
1221
'BZR_HOME': None, # Don't inherit BZR_HOME to all the tests.
1222
'HOME': os.getcwd(),
1223
'APPDATA': None, # bzr now use Win32 API and don't rely on APPDATA
1224
'BZR_EDITOR': None, # test_msgeditor manipulates this variable
1226
'BZREMAIL': None, # may still be present in the environment
1228
'BZR_PROGRESS_BAR': None,
1231
'SSH_AUTH_SOCK': None,
1235
'https_proxy': None,
1236
'HTTPS_PROXY': None,
1241
# Nobody cares about these ones AFAIK. So far at
1242
# least. If you do (care), please update this comment
1246
'BZR_REMOTE_PATH': None,
1249
self.addCleanup(self._restoreEnvironment)
1250
for name, value in new_env.iteritems():
1251
self._captureVar(name, value)
1253
def _captureVar(self, name, newvalue):
1254
"""Set an environment variable, and reset it when finished."""
1255
self.__old_env[name] = osutils.set_or_unset_env(name, newvalue)
1257
def _restore_debug_flags(self):
1258
debug.debug_flags.clear()
1259
debug.debug_flags.update(self._preserved_debug_flags)
1261
def _restoreEnvironment(self):
1262
for name, value in self.__old_env.iteritems():
1263
osutils.set_or_unset_env(name, value)
1265
def _restoreHooks(self):
1266
for klass, hooks in self._preserved_hooks.items():
1267
setattr(klass, 'hooks', hooks)
1269
def knownFailure(self, reason):
1270
"""This test has failed for some known reason."""
1271
raise KnownFailure(reason)
1273
def run(self, result=None):
1274
if result is None: result = self.defaultTestResult()
1275
for feature in getattr(self, '_test_needs_features', []):
1276
if not feature.available():
1277
result.startTest(self)
1278
if getattr(result, 'addNotSupported', None):
1279
result.addNotSupported(self, feature)
1281
result.addSuccess(self)
1282
result.stopTest(self)
1285
return unittest.TestCase.run(self, result)
1288
absent_attr = object()
1289
for attr_name in self.attrs_to_keep:
1290
attr = getattr(self, attr_name, absent_attr)
1291
if attr is not absent_attr:
1292
saved_attrs[attr_name] = attr
1293
self.__dict__ = saved_attrs
1297
unittest.TestCase.tearDown(self)
1299
def time(self, callable, *args, **kwargs):
1300
"""Run callable and accrue the time it takes to the benchmark time.
1302
If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
1303
this will cause lsprofile statistics to be gathered and stored in
1306
if self._benchtime is None:
1310
if not self._gather_lsprof_in_benchmarks:
1311
return callable(*args, **kwargs)
1313
# record this benchmark
1314
ret, stats = bzrlib.lsprof.profile(callable, *args, **kwargs)
1316
self._benchcalls.append(((callable, args, kwargs), stats))
1319
self._benchtime += time.time() - start
1321
def _runCleanups(self):
1322
"""Run registered cleanup functions.
1324
This should only be called from TestCase.tearDown.
1326
# TODO: Perhaps this should keep running cleanups even if
1327
# one of them fails?
1329
# Actually pop the cleanups from the list so tearDown running
1330
# twice is safe (this happens for skipped tests).
1331
while self._cleanups:
1332
self._cleanups.pop()()
1334
def log(self, *args):
1337
def _get_log(self, keep_log_file=False):
1338
"""Get the log from bzrlib.trace calls from this test.
1340
:param keep_log_file: When True, if the log is still a file on disk
1341
leave it as a file on disk. When False, if the log is still a file
1342
on disk, the log file is deleted and the log preserved as
1344
:return: A string containing the log.
1346
# flush the log file, to get all content
1348
bzrlib.trace._trace_file.flush()
1349
if self._log_contents:
1350
# XXX: this can hardly contain the content flushed above --vila
1352
return self._log_contents
1353
if self._log_file_name is not None:
1354
logfile = open(self._log_file_name)
1356
log_contents = logfile.read()
1359
if not keep_log_file:
1360
self._log_contents = log_contents
1362
os.remove(self._log_file_name)
1364
if sys.platform == 'win32' and e.errno == errno.EACCES:
1365
sys.stderr.write(('Unable to delete log file '
1366
' %r\n' % self._log_file_name))
1371
return "DELETED log file to reduce memory footprint"
1373
def requireFeature(self, feature):
1374
"""This test requires a specific feature is available.
1376
:raises UnavailableFeature: When feature is not available.
1378
if not feature.available():
1379
raise UnavailableFeature(feature)
1381
def _run_bzr_autosplit(self, args, retcode, encoding, stdin,
1383
"""Run bazaar command line, splitting up a string command line."""
1384
if isinstance(args, basestring):
1385
# shlex don't understand unicode strings,
1386
# so args should be plain string (bialix 20070906)
1387
args = list(shlex.split(str(args)))
1388
return self._run_bzr_core(args, retcode=retcode,
1389
encoding=encoding, stdin=stdin, working_dir=working_dir,
1392
def _run_bzr_core(self, args, retcode, encoding, stdin,
1394
if encoding is None:
1395
encoding = bzrlib.user_encoding
1396
stdout = StringIOWrapper()
1397
stderr = StringIOWrapper()
1398
stdout.encoding = encoding
1399
stderr.encoding = encoding
1401
self.log('run bzr: %r', args)
1402
# FIXME: don't call into logging here
1403
handler = logging.StreamHandler(stderr)
1404
handler.setLevel(logging.INFO)
1405
logger = logging.getLogger('')
1406
logger.addHandler(handler)
1407
old_ui_factory = ui.ui_factory
1408
ui.ui_factory = TestUIFactory(stdin=stdin, stdout=stdout, stderr=stderr)
1411
if working_dir is not None:
1412
cwd = osutils.getcwd()
1413
os.chdir(working_dir)
1416
result = self.apply_redirected(ui.ui_factory.stdin,
1418
bzrlib.commands.run_bzr_catch_user_errors,
1421
logger.removeHandler(handler)
1422
ui.ui_factory = old_ui_factory
1426
out = stdout.getvalue()
1427
err = stderr.getvalue()
1429
self.log('output:\n%r', out)
1431
self.log('errors:\n%r', err)
1432
if retcode is not None:
1433
self.assertEquals(retcode, result,
1434
message='Unexpected return code')
1437
def run_bzr(self, args, retcode=0, encoding=None, stdin=None,
1438
working_dir=None, error_regexes=[], output_encoding=None):
1439
"""Invoke bzr, as if it were run from the command line.
1441
The argument list should not include the bzr program name - the
1442
first argument is normally the bzr command. Arguments may be
1443
passed in three ways:
1445
1- A list of strings, eg ["commit", "a"]. This is recommended
1446
when the command contains whitespace or metacharacters, or
1447
is built up at run time.
1449
2- A single string, eg "add a". This is the most convenient
1450
for hardcoded commands.
1452
This runs bzr through the interface that catches and reports
1453
errors, and with logging set to something approximating the
1454
default, so that error reporting can be checked.
1456
This should be the main method for tests that want to exercise the
1457
overall behavior of the bzr application (rather than a unit test
1458
or a functional test of the library.)
1460
This sends the stdout/stderr results into the test's log,
1461
where it may be useful for debugging. See also run_captured.
1463
:keyword stdin: A string to be used as stdin for the command.
1464
:keyword retcode: The status code the command should return;
1466
:keyword working_dir: The directory to run the command in
1467
:keyword error_regexes: A list of expected error messages. If
1468
specified they must be seen in the error output of the command.
1470
out, err = self._run_bzr_autosplit(
1475
working_dir=working_dir,
1477
for regex in error_regexes:
1478
self.assertContainsRe(err, regex)
1481
def run_bzr_error(self, error_regexes, *args, **kwargs):
1482
"""Run bzr, and check that stderr contains the supplied regexes
1484
:param error_regexes: Sequence of regular expressions which
1485
must each be found in the error output. The relative ordering
1487
:param args: command-line arguments for bzr
1488
:param kwargs: Keyword arguments which are interpreted by run_bzr
1489
This function changes the default value of retcode to be 3,
1490
since in most cases this is run when you expect bzr to fail.
1492
:return: (out, err) The actual output of running the command (in case
1493
you want to do more inspection)
1497
# Make sure that commit is failing because there is nothing to do
1498
self.run_bzr_error(['no changes to commit'],
1499
['commit', '-m', 'my commit comment'])
1500
# Make sure --strict is handling an unknown file, rather than
1501
# giving us the 'nothing to do' error
1502
self.build_tree(['unknown'])
1503
self.run_bzr_error(['Commit refused because there are unknown files'],
1504
['commit', --strict', '-m', 'my commit comment'])
1506
kwargs.setdefault('retcode', 3)
1507
kwargs['error_regexes'] = error_regexes
1508
out, err = self.run_bzr(*args, **kwargs)
1511
def run_bzr_subprocess(self, *args, **kwargs):
1512
"""Run bzr in a subprocess for testing.
1514
This starts a new Python interpreter and runs bzr in there.
1515
This should only be used for tests that have a justifiable need for
1516
this isolation: e.g. they are testing startup time, or signal
1517
handling, or early startup code, etc. Subprocess code can't be
1518
profiled or debugged so easily.
1520
:keyword retcode: The status code that is expected. Defaults to 0. If
1521
None is supplied, the status code is not checked.
1522
:keyword env_changes: A dictionary which lists changes to environment
1523
variables. A value of None will unset the env variable.
1524
The values must be strings. The change will only occur in the
1525
child, so you don't need to fix the environment after running.
1526
:keyword universal_newlines: Convert CRLF => LF
1527
:keyword allow_plugins: By default the subprocess is run with
1528
--no-plugins to ensure test reproducibility. Also, it is possible
1529
for system-wide plugins to create unexpected output on stderr,
1530
which can cause unnecessary test failures.
1532
env_changes = kwargs.get('env_changes', {})
1533
working_dir = kwargs.get('working_dir', None)
1534
allow_plugins = kwargs.get('allow_plugins', False)
1536
if isinstance(args[0], list):
1538
elif isinstance(args[0], basestring):
1539
args = list(shlex.split(args[0]))
1541
raise ValueError("passing varargs to run_bzr_subprocess")
1542
process = self.start_bzr_subprocess(args, env_changes=env_changes,
1543
working_dir=working_dir,
1544
allow_plugins=allow_plugins)
1545
# We distinguish between retcode=None and retcode not passed.
1546
supplied_retcode = kwargs.get('retcode', 0)
1547
return self.finish_bzr_subprocess(process, retcode=supplied_retcode,
1548
universal_newlines=kwargs.get('universal_newlines', False),
1551
def start_bzr_subprocess(self, process_args, env_changes=None,
1552
skip_if_plan_to_signal=False,
1554
allow_plugins=False):
1555
"""Start bzr in a subprocess for testing.
1557
This starts a new Python interpreter and runs bzr in there.
1558
This should only be used for tests that have a justifiable need for
1559
this isolation: e.g. they are testing startup time, or signal
1560
handling, or early startup code, etc. Subprocess code can't be
1561
profiled or debugged so easily.
1563
:param process_args: a list of arguments to pass to the bzr executable,
1564
for example ``['--version']``.
1565
:param env_changes: A dictionary which lists changes to environment
1566
variables. A value of None will unset the env variable.
1567
The values must be strings. The change will only occur in the
1568
child, so you don't need to fix the environment after running.
1569
:param skip_if_plan_to_signal: raise TestSkipped when true and os.kill
1571
:param allow_plugins: If False (default) pass --no-plugins to bzr.
1573
:returns: Popen object for the started process.
1575
if skip_if_plan_to_signal:
1576
if not getattr(os, 'kill', None):
1577
raise TestSkipped("os.kill not available.")
1579
if env_changes is None:
1583
def cleanup_environment():
1584
for env_var, value in env_changes.iteritems():
1585
old_env[env_var] = osutils.set_or_unset_env(env_var, value)
1587
def restore_environment():
1588
for env_var, value in old_env.iteritems():
1589
osutils.set_or_unset_env(env_var, value)
1591
bzr_path = self.get_bzr_path()
1594
if working_dir is not None:
1595
cwd = osutils.getcwd()
1596
os.chdir(working_dir)
1599
# win32 subprocess doesn't support preexec_fn
1600
# so we will avoid using it on all platforms, just to
1601
# make sure the code path is used, and we don't break on win32
1602
cleanup_environment()
1603
command = [sys.executable, bzr_path]
1604
if not allow_plugins:
1605
command.append('--no-plugins')
1606
command.extend(process_args)
1607
process = self._popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
1609
restore_environment()
1615
def _popen(self, *args, **kwargs):
1616
"""Place a call to Popen.
1618
Allows tests to override this method to intercept the calls made to
1619
Popen for introspection.
1621
return Popen(*args, **kwargs)
1623
def get_bzr_path(self):
1624
"""Return the path of the 'bzr' executable for this test suite."""
1625
bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
1626
if not os.path.isfile(bzr_path):
1627
# We are probably installed. Assume sys.argv is the right file
1628
bzr_path = sys.argv[0]
1631
def finish_bzr_subprocess(self, process, retcode=0, send_signal=None,
1632
universal_newlines=False, process_args=None):
1633
"""Finish the execution of process.
1635
:param process: the Popen object returned from start_bzr_subprocess.
1636
:param retcode: The status code that is expected. Defaults to 0. If
1637
None is supplied, the status code is not checked.
1638
:param send_signal: an optional signal to send to the process.
1639
:param universal_newlines: Convert CRLF => LF
1640
:returns: (stdout, stderr)
1642
if send_signal is not None:
1643
os.kill(process.pid, send_signal)
1644
out, err = process.communicate()
1646
if universal_newlines:
1647
out = out.replace('\r\n', '\n')
1648
err = err.replace('\r\n', '\n')
1650
if retcode is not None and retcode != process.returncode:
1651
if process_args is None:
1652
process_args = "(unknown args)"
1653
mutter('Output of bzr %s:\n%s', process_args, out)
1654
mutter('Error for bzr %s:\n%s', process_args, err)
1655
self.fail('Command bzr %s failed with retcode %s != %s'
1656
% (process_args, retcode, process.returncode))
1659
def check_inventory_shape(self, inv, shape):
1660
"""Compare an inventory to a list of expected names.
1662
Fail if they are not precisely equal.
1665
shape = list(shape) # copy
1666
for path, ie in inv.entries():
1667
name = path.replace('\\', '/')
1668
if ie.kind == 'directory':
1675
self.fail("expected paths not found in inventory: %r" % shape)
1677
self.fail("unexpected paths found in inventory: %r" % extras)
1679
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
1680
a_callable=None, *args, **kwargs):
1681
"""Call callable with redirected std io pipes.
1683
Returns the return code."""
1684
if not callable(a_callable):
1685
raise ValueError("a_callable must be callable.")
1687
stdin = StringIO("")
1689
if getattr(self, "_log_file", None) is not None:
1690
stdout = self._log_file
1694
if getattr(self, "_log_file", None is not None):
1695
stderr = self._log_file
1698
real_stdin = sys.stdin
1699
real_stdout = sys.stdout
1700
real_stderr = sys.stderr
1705
return a_callable(*args, **kwargs)
1707
sys.stdout = real_stdout
1708
sys.stderr = real_stderr
1709
sys.stdin = real_stdin
1711
def reduceLockdirTimeout(self):
1712
"""Reduce the default lock timeout for the duration of the test, so that
1713
if LockContention occurs during a test, it does so quickly.
1715
Tests that expect to provoke LockContention errors should call this.
1717
orig_timeout = bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS
1719
bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = orig_timeout
1720
self.addCleanup(resetTimeout)
1721
bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 0
1723
def make_utf8_encoded_stringio(self, encoding_type=None):
1724
"""Return a StringIOWrapper instance, that will encode Unicode
1727
if encoding_type is None:
1728
encoding_type = 'strict'
1730
output_encoding = 'utf-8'
1731
sio = codecs.getwriter(output_encoding)(sio, errors=encoding_type)
1732
sio.encoding = output_encoding
1736
class TestCaseWithMemoryTransport(TestCase):
1737
"""Common test class for tests that do not need disk resources.
1739
Tests that need disk resources should derive from TestCaseWithTransport.
1741
TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
1743
For TestCaseWithMemoryTransport the test_home_dir is set to the name of
1744
a directory which does not exist. This serves to help ensure test isolation
1745
is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
1746
must exist. However, TestCaseWithMemoryTransport does not offer local
1747
file defaults for the transport in tests, nor does it obey the command line
1748
override, so tests that accidentally write to the common directory should
1751
:cvar TEST_ROOT: Directory containing all temporary directories, plus
1752
a .bzr directory that stops us ascending higher into the filesystem.
1758
def __init__(self, methodName='runTest'):
1759
# allow test parameterization after test construction and before test
1760
# execution. Variables that the parameterizer sets need to be
1761
# ones that are not set by setUp, or setUp will trash them.
1762
super(TestCaseWithMemoryTransport, self).__init__(methodName)
1763
self.vfs_transport_factory = default_transport
1764
self.transport_server = None
1765
self.transport_readonly_server = None
1766
self.__vfs_server = None
1768
def get_transport(self, relpath=None):
1769
"""Return a writeable transport.
1771
This transport is for the test scratch space relative to
1774
:param relpath: a path relative to the base url.
1776
t = get_transport(self.get_url(relpath))
1777
self.assertFalse(t.is_readonly())
1780
def get_readonly_transport(self, relpath=None):
1781
"""Return a readonly transport for the test scratch space
1783
This can be used to test that operations which should only need
1784
readonly access in fact do not try to write.
1786
:param relpath: a path relative to the base url.
1788
t = get_transport(self.get_readonly_url(relpath))
1789
self.assertTrue(t.is_readonly())
1792
def create_transport_readonly_server(self):
1793
"""Create a transport server from class defined at init.
1795
This is mostly a hook for daughter classes.
1797
return self.transport_readonly_server()
1799
def get_readonly_server(self):
1800
"""Get the server instance for the readonly transport
1802
This is useful for some tests with specific servers to do diagnostics.
1804
if self.__readonly_server is None:
1805
if self.transport_readonly_server is None:
1806
# readonly decorator requested
1807
# bring up the server
1808
self.__readonly_server = ReadonlyServer()
1809
self.__readonly_server.setUp(self.get_vfs_only_server())
1811
self.__readonly_server = self.create_transport_readonly_server()
1812
self.__readonly_server.setUp(self.get_vfs_only_server())
1813
self.addCleanup(self.__readonly_server.tearDown)
1814
return self.__readonly_server
1816
def get_readonly_url(self, relpath=None):
1817
"""Get a URL for the readonly transport.
1819
This will either be backed by '.' or a decorator to the transport
1820
used by self.get_url()
1821
relpath provides for clients to get a path relative to the base url.
1822
These should only be downwards relative, not upwards.
1824
base = self.get_readonly_server().get_url()
1825
return self._adjust_url(base, relpath)
1827
def get_vfs_only_server(self):
1828
"""Get the vfs only read/write server instance.
1830
This is useful for some tests with specific servers that need
1833
For TestCaseWithMemoryTransport this is always a MemoryServer, and there
1834
is no means to override it.
1836
if self.__vfs_server is None:
1837
self.__vfs_server = MemoryServer()
1838
self.__vfs_server.setUp()
1839
self.addCleanup(self.__vfs_server.tearDown)
1840
return self.__vfs_server
1842
def get_server(self):
1843
"""Get the read/write server instance.
1845
This is useful for some tests with specific servers that need
1848
This is built from the self.transport_server factory. If that is None,
1849
then the self.get_vfs_server is returned.
1851
if self.__server is None:
1852
if self.transport_server is None or self.transport_server is self.vfs_transport_factory:
1853
return self.get_vfs_only_server()
1855
# bring up a decorated means of access to the vfs only server.
1856
self.__server = self.transport_server()
1858
self.__server.setUp(self.get_vfs_only_server())
1859
except TypeError, e:
1860
# This should never happen; the try:Except here is to assist
1861
# developers having to update code rather than seeing an
1862
# uninformative TypeError.
1863
raise Exception, "Old server API in use: %s, %s" % (self.__server, e)
1864
self.addCleanup(self.__server.tearDown)
1865
return self.__server
1867
def _adjust_url(self, base, relpath):
1868
"""Get a URL (or maybe a path) for the readwrite transport.
1870
This will either be backed by '.' or to an equivalent non-file based
1872
relpath provides for clients to get a path relative to the base url.
1873
These should only be downwards relative, not upwards.
1875
if relpath is not None and relpath != '.':
1876
if not base.endswith('/'):
1878
# XXX: Really base should be a url; we did after all call
1879
# get_url()! But sometimes it's just a path (from
1880
# LocalAbspathServer), and it'd be wrong to append urlescaped data
1881
# to a non-escaped local path.
1882
if base.startswith('./') or base.startswith('/'):
1885
base += urlutils.escape(relpath)
1888
def get_url(self, relpath=None):
1889
"""Get a URL (or maybe a path) for the readwrite transport.
1891
This will either be backed by '.' or to an equivalent non-file based
1893
relpath provides for clients to get a path relative to the base url.
1894
These should only be downwards relative, not upwards.
1896
base = self.get_server().get_url()
1897
return self._adjust_url(base, relpath)
1899
def get_vfs_only_url(self, relpath=None):
1900
"""Get a URL (or maybe a path for the plain old vfs transport.
1902
This will never be a smart protocol. It always has all the
1903
capabilities of the local filesystem, but it might actually be a
1904
MemoryTransport or some other similar virtual filesystem.
1906
This is the backing transport (if any) of the server returned by
1907
get_url and get_readonly_url.
1909
:param relpath: provides for clients to get a path relative to the base
1910
url. These should only be downwards relative, not upwards.
1913
base = self.get_vfs_only_server().get_url()
1914
return self._adjust_url(base, relpath)
1916
def _create_safety_net(self):
1917
"""Make a fake bzr directory.
1919
This prevents any tests propagating up onto the TEST_ROOT directory's
1922
root = TestCaseWithMemoryTransport.TEST_ROOT
1923
bzrdir.BzrDir.create_standalone_workingtree(root)
1925
def _check_safety_net(self):
1926
"""Check that the safety .bzr directory have not been touched.
1928
_make_test_root have created a .bzr directory to prevent tests from
1929
propagating. This method ensures than a test did not leaked.
1931
root = TestCaseWithMemoryTransport.TEST_ROOT
1932
wt = workingtree.WorkingTree.open(root)
1933
last_rev = wt.last_revision()
1934
if last_rev != 'null:':
1935
# The current test have modified the /bzr directory, we need to
1936
# recreate a new one or all the followng tests will fail.
1937
# If you need to inspect its content uncomment the following line
1938
# import pdb; pdb.set_trace()
1939
_rmtree_temp_dir(root + '/.bzr')
1940
self._create_safety_net()
1941
raise AssertionError('%s/.bzr should not be modified' % root)
1943
def _make_test_root(self):
1944
if TestCaseWithMemoryTransport.TEST_ROOT is None:
1945
root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
1946
TestCaseWithMemoryTransport.TEST_ROOT = root
1948
self._create_safety_net()
1950
# The same directory is used by all tests, and we're not
1951
# specifically told when all tests are finished. This will do.
1952
atexit.register(_rmtree_temp_dir, root)
1954
self.addCleanup(self._check_safety_net)
1956
def makeAndChdirToTestDir(self):
1957
"""Create a temporary directories for this one test.
1959
This must set self.test_home_dir and self.test_dir and chdir to
1962
For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
1964
os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
1965
self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
1966
self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
1968
def make_branch(self, relpath, format=None):
1969
"""Create a branch on the transport at relpath."""
1970
repo = self.make_repository(relpath, format=format)
1971
return repo.bzrdir.create_branch()
1973
def make_bzrdir(self, relpath, format=None):
1975
# might be a relative or absolute path
1976
maybe_a_url = self.get_url(relpath)
1977
segments = maybe_a_url.rsplit('/', 1)
1978
t = get_transport(maybe_a_url)
1979
if len(segments) > 1 and segments[-1] not in ('', '.'):
1983
if isinstance(format, basestring):
1984
format = bzrdir.format_registry.make_bzrdir(format)
1985
return format.initialize_on_transport(t)
1986
except errors.UninitializableFormat:
1987
raise TestSkipped("Format %s is not initializable." % format)
1989
def make_repository(self, relpath, shared=False, format=None):
1990
"""Create a repository on our default transport at relpath.
1992
Note that relpath must be a relative path, not a full url.
1994
# FIXME: If you create a remoterepository this returns the underlying
1995
# real format, which is incorrect. Actually we should make sure that
1996
# RemoteBzrDir returns a RemoteRepository.
1997
# maybe mbp 20070410
1998
made_control = self.make_bzrdir(relpath, format=format)
1999
return made_control.create_repository(shared=shared)
2001
def make_branch_and_memory_tree(self, relpath, format=None):
2002
"""Create a branch on the default transport and a MemoryTree for it."""
2003
b = self.make_branch(relpath, format=format)
2004
return memorytree.MemoryTree.create_on_branch(b)
2006
def overrideEnvironmentForTesting(self):
2007
os.environ['HOME'] = self.test_home_dir
2008
os.environ['BZR_HOME'] = self.test_home_dir
2011
super(TestCaseWithMemoryTransport, self).setUp()
2012
self._make_test_root()
2013
_currentdir = os.getcwdu()
2014
def _leaveDirectory():
2015
os.chdir(_currentdir)
2016
self.addCleanup(_leaveDirectory)
2017
self.makeAndChdirToTestDir()
2018
self.overrideEnvironmentForTesting()
2019
self.__readonly_server = None
2020
self.__server = None
2021
self.reduceLockdirTimeout()
2024
class TestCaseInTempDir(TestCaseWithMemoryTransport):
2025
"""Derived class that runs a test within a temporary directory.
2027
This is useful for tests that need to create a branch, etc.
2029
The directory is created in a slightly complex way: for each
2030
Python invocation, a new temporary top-level directory is created.
2031
All test cases create their own directory within that. If the
2032
tests complete successfully, the directory is removed.
2034
:ivar test_base_dir: The path of the top-level directory for this
2035
test, which contains a home directory and a work directory.
2037
:ivar test_home_dir: An initially empty directory under test_base_dir
2038
which is used as $HOME for this test.
2040
:ivar test_dir: A directory under test_base_dir used as the current
2041
directory when the test proper is run.
2044
OVERRIDE_PYTHON = 'python'
2046
def check_file_contents(self, filename, expect):
2047
self.log("check contents of file %s" % filename)
2048
contents = file(filename, 'r').read()
2049
if contents != expect:
2050
self.log("expected: %r" % expect)
2051
self.log("actually: %r" % contents)
2052
self.fail("contents of %s not as expected" % filename)
2054
def makeAndChdirToTestDir(self):
2055
"""See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
2057
For TestCaseInTempDir we create a temporary directory based on the test
2058
name and then create two subdirs - test and home under it.
2060
# create a directory within the top level test directory
2061
candidate_dir = osutils.mkdtemp(dir=self.TEST_ROOT)
2062
# now create test and home directories within this dir
2063
self.test_base_dir = candidate_dir
2064
self.test_home_dir = self.test_base_dir + '/home'
2065
os.mkdir(self.test_home_dir)
2066
self.test_dir = self.test_base_dir + '/work'
2067
os.mkdir(self.test_dir)
2068
os.chdir(self.test_dir)
2069
# put name of test inside
2070
f = file(self.test_base_dir + '/name', 'w')
2075
self.addCleanup(self.deleteTestDir)
2077
def deleteTestDir(self):
2078
os.chdir(self.TEST_ROOT)
2079
_rmtree_temp_dir(self.test_base_dir)
2081
def build_tree(self, shape, line_endings='binary', transport=None):
2082
"""Build a test tree according to a pattern.
2084
shape is a sequence of file specifications. If the final
2085
character is '/', a directory is created.
2087
This assumes that all the elements in the tree being built are new.
2089
This doesn't add anything to a branch.
2091
:type shape: list or tuple.
2092
:param line_endings: Either 'binary' or 'native'
2093
in binary mode, exact contents are written in native mode, the
2094
line endings match the default platform endings.
2095
:param transport: A transport to write to, for building trees on VFS's.
2096
If the transport is readonly or None, "." is opened automatically.
2099
if type(shape) not in (list, tuple):
2100
raise AssertionError("Parameter 'shape' should be "
2101
"a list or a tuple. Got %r instead" % (shape,))
2102
# It's OK to just create them using forward slashes on windows.
2103
if transport is None or transport.is_readonly():
2104
transport = get_transport(".")
2106
self.assert_(isinstance(name, basestring))
2108
transport.mkdir(urlutils.escape(name[:-1]))
2110
if line_endings == 'binary':
2112
elif line_endings == 'native':
2115
raise errors.BzrError(
2116
'Invalid line ending request %r' % line_endings)
2117
content = "contents of %s%s" % (name.encode('utf-8'), end)
2118
transport.put_bytes_non_atomic(urlutils.escape(name), content)
2120
def build_tree_contents(self, shape):
2121
build_tree_contents(shape)
2123
def assertInWorkingTree(self, path, root_path='.', tree=None):
2124
"""Assert whether path or paths are in the WorkingTree"""
2126
tree = workingtree.WorkingTree.open(root_path)
2127
if not isinstance(path, basestring):
2129
self.assertInWorkingTree(p,tree=tree)
2131
self.assertIsNot(tree.path2id(path), None,
2132
path+' not in working tree.')
2134
def assertNotInWorkingTree(self, path, root_path='.', tree=None):
2135
"""Assert whether path or paths are not in the WorkingTree"""
2137
tree = workingtree.WorkingTree.open(root_path)
2138
if not isinstance(path, basestring):
2140
self.assertNotInWorkingTree(p,tree=tree)
2142
self.assertIs(tree.path2id(path), None, path+' in working tree.')
2145
class TestCaseWithTransport(TestCaseInTempDir):
2146
"""A test case that provides get_url and get_readonly_url facilities.
2148
These back onto two transport servers, one for readonly access and one for
2151
If no explicit class is provided for readonly access, a
2152
ReadonlyTransportDecorator is used instead which allows the use of non disk
2153
based read write transports.
2155
If an explicit class is provided for readonly access, that server and the
2156
readwrite one must both define get_url() as resolving to os.getcwd().
2159
def get_vfs_only_server(self):
2160
"""See TestCaseWithMemoryTransport.
2162
This is useful for some tests with specific servers that need
2165
if self.__vfs_server is None:
2166
self.__vfs_server = self.vfs_transport_factory()
2167
self.__vfs_server.setUp()
2168
self.addCleanup(self.__vfs_server.tearDown)
2169
return self.__vfs_server
2171
def make_branch_and_tree(self, relpath, format=None):
2172
"""Create a branch on the transport and a tree locally.
2174
If the transport is not a LocalTransport, the Tree can't be created on
2175
the transport. In that case if the vfs_transport_factory is
2176
LocalURLServer the working tree is created in the local
2177
directory backing the transport, and the returned tree's branch and
2178
repository will also be accessed locally. Otherwise a lightweight
2179
checkout is created and returned.
2181
:param format: The BzrDirFormat.
2182
:returns: the WorkingTree.
2184
# TODO: always use the local disk path for the working tree,
2185
# this obviously requires a format that supports branch references
2186
# so check for that by checking bzrdir.BzrDirFormat.get_default_format()
2188
b = self.make_branch(relpath, format=format)
2190
return b.bzrdir.create_workingtree()
2191
except errors.NotLocalUrl:
2192
# We can only make working trees locally at the moment. If the
2193
# transport can't support them, then we keep the non-disk-backed
2194
# branch and create a local checkout.
2195
if self.vfs_transport_factory is LocalURLServer:
2196
# the branch is colocated on disk, we cannot create a checkout.
2197
# hopefully callers will expect this.
2198
local_controldir= bzrdir.BzrDir.open(self.get_vfs_only_url(relpath))
2199
return local_controldir.create_workingtree()
2201
return b.create_checkout(relpath, lightweight=True)
2203
def assertIsDirectory(self, relpath, transport):
2204
"""Assert that relpath within transport is a directory.
2206
This may not be possible on all transports; in that case it propagates
2207
a TransportNotPossible.
2210
mode = transport.stat(relpath).st_mode
2211
except errors.NoSuchFile:
2212
self.fail("path %s is not a directory; no such file"
2214
if not stat.S_ISDIR(mode):
2215
self.fail("path %s is not a directory; has mode %#o"
2218
def assertTreesEqual(self, left, right):
2219
"""Check that left and right have the same content and properties."""
2220
# we use a tree delta to check for equality of the content, and we
2221
# manually check for equality of other things such as the parents list.
2222
self.assertEqual(left.get_parent_ids(), right.get_parent_ids())
2223
differences = left.changes_from(right)
2224
self.assertFalse(differences.has_changed(),
2225
"Trees %r and %r are different: %r" % (left, right, differences))
2228
super(TestCaseWithTransport, self).setUp()
2229
self.__vfs_server = None
2232
class ChrootedTestCase(TestCaseWithTransport):
2233
"""A support class that provides readonly urls outside the local namespace.
2235
This is done by checking if self.transport_server is a MemoryServer. if it
2236
is then we are chrooted already, if it is not then an HttpServer is used
2239
TODO RBC 20060127: make this an option to TestCaseWithTransport so it can
2240
be used without needed to redo it when a different
2241
subclass is in use ?
2245
super(ChrootedTestCase, self).setUp()
2246
if not self.vfs_transport_factory == MemoryServer:
2247
self.transport_readonly_server = HttpServer
2250
def condition_id_re(pattern):
2251
"""Create a condition filter which performs a re check on a test's id.
2253
:param pattern: A regular expression string.
2254
:return: A callable that returns True if the re matches.
2256
filter_re = re.compile(pattern)
2257
def condition(test):
2259
return filter_re.search(test_id)
2263
def condition_isinstance(klass_or_klass_list):
2264
"""Create a condition filter which returns isinstance(param, klass).
2266
:return: A callable which when called with one parameter obj return the
2267
result of isinstance(obj, klass_or_klass_list).
2270
return isinstance(obj, klass_or_klass_list)
2274
def condition_id_in_list(id_list):
2275
"""Create a condition filter which verify that test's id in a list.
2277
:param id_list: A TestIdList object.
2278
:return: A callable that returns True if the test's id appears in the list.
2280
def condition(test):
2281
return id_list.includes(test.id())
2285
def exclude_tests_by_condition(suite, condition):
2286
"""Create a test suite which excludes some tests from suite.
2288
:param suite: The suite to get tests from.
2289
:param condition: A callable whose result evaluates True when called with a
2290
test case which should be excluded from the result.
2291
:return: A suite which contains the tests found in suite that fail
2295
for test in iter_suite_tests(suite):
2296
if not condition(test):
2298
return TestUtil.TestSuite(result)
2301
def filter_suite_by_condition(suite, condition):
2302
"""Create a test suite by filtering another one.
2304
:param suite: The source suite.
2305
:param condition: A callable whose result evaluates True when called with a
2306
test case which should be included in the result.
2307
:return: A suite which contains the tests found in suite that pass
2311
for test in iter_suite_tests(suite):
2314
return TestUtil.TestSuite(result)
2317
def filter_suite_by_re(suite, pattern):
2318
"""Create a test suite by filtering another one.
2320
:param suite: the source suite
2321
:param pattern: pattern that names must match
2322
:returns: the newly created suite
2324
condition = condition_id_re(pattern)
2325
result_suite = filter_suite_by_condition(suite, condition)
2329
def filter_suite_by_id_list(suite, test_id_list):
2330
"""Create a test suite by filtering another one.
2332
:param suite: The source suite.
2333
:param test_id_list: A list of the test ids to keep as strings.
2334
:returns: the newly created suite
2336
condition = condition_id_in_list(test_id_list)
2337
result_suite = filter_suite_by_condition(suite, condition)
2341
def exclude_tests_by_re(suite, pattern):
2342
"""Create a test suite which excludes some tests from suite.
2344
:param suite: The suite to get tests from.
2345
:param pattern: A regular expression string. Test ids that match this
2346
pattern will be excluded from the result.
2347
:return: A TestSuite that contains all the tests from suite without the
2348
tests that matched pattern. The order of tests is the same as it was in
2351
return exclude_tests_by_condition(suite, condition_id_re(pattern))
2354
def preserve_input(something):
2355
"""A helper for performing test suite transformation chains.
2357
:param something: Anything you want to preserve.
2363
def randomize_suite(suite):
2364
"""Return a new TestSuite with suite's tests in random order.
2366
The tests in the input suite are flattened into a single suite in order to
2367
accomplish this. Any nested TestSuites are removed to provide global
2370
tests = list(iter_suite_tests(suite))
2371
random.shuffle(tests)
2372
return TestUtil.TestSuite(tests)
2375
def split_suite_by_re(suite, pattern):
2376
"""Split a test suite into two by a regular expression.
2378
:param suite: The suite to split.
2379
:param pattern: A regular expression string. Test ids that match this
2380
pattern will be in the first test suite returned, and the others in the
2381
second test suite returned.
2382
:return: A tuple of two test suites, where the first contains tests from
2383
suite matching pattern, and the second contains the remainder from
2384
suite. The order within each output suite is the same as it was in
2389
filter_re = re.compile(pattern)
2390
for test in iter_suite_tests(suite):
2392
if filter_re.search(test_id):
2393
matched.append(test)
2395
did_not_match.append(test)
2396
return TestUtil.TestSuite(matched), TestUtil.TestSuite(did_not_match)
2399
def run_suite(suite, name='test', verbose=False, pattern=".*",
2400
stop_on_failure=False,
2401
transport=None, lsprof_timed=None, bench_history=None,
2402
matching_tests_first=None,
2405
exclude_pattern=None,
2407
TestCase._gather_lsprof_in_benchmarks = lsprof_timed
2412
runner = TextTestRunner(stream=sys.stdout,
2414
verbosity=verbosity,
2415
bench_history=bench_history,
2416
list_only=list_only,
2418
runner.stop_on_failure=stop_on_failure
2419
# Initialise the random number generator and display the seed used.
2420
# We convert the seed to a long to make it reuseable across invocations.
2421
random_order = False
2422
if random_seed is not None:
2424
if random_seed == "now":
2425
random_seed = long(time.time())
2427
# Convert the seed to a long if we can
2429
random_seed = long(random_seed)
2432
runner.stream.writeln("Randomizing test order using seed %s\n" %
2434
random.seed(random_seed)
2435
# Customise the list of tests if requested
2436
if exclude_pattern is not None:
2437
suite = exclude_tests_by_re(suite, exclude_pattern)
2439
order_changer = randomize_suite
2441
order_changer = preserve_input
2442
if pattern != '.*' or random_order:
2443
if matching_tests_first:
2444
suites = map(order_changer, split_suite_by_re(suite, pattern))
2445
suite = TestUtil.TestSuite(suites)
2447
suite = order_changer(filter_suite_by_re(suite, pattern))
2449
result = runner.run(suite)
2452
return result.wasStrictlySuccessful()
2454
return result.wasSuccessful()
2457
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
2459
test_suite_factory=None,
2462
matching_tests_first=None,
2465
exclude_pattern=None,
2469
"""Run the whole test suite under the enhanced runner"""
2470
# XXX: Very ugly way to do this...
2471
# Disable warning about old formats because we don't want it to disturb
2472
# any blackbox tests.
2473
from bzrlib import repository
2474
repository._deprecation_warning_done = True
2476
global default_transport
2477
if transport is None:
2478
transport = default_transport
2479
old_transport = default_transport
2480
default_transport = transport
2482
if load_list is None:
2485
keep_only = load_test_id_list(load_list)
2486
if test_suite_factory is None:
2487
suite = test_suite(keep_only)
2489
suite = test_suite_factory()
2490
return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
2491
stop_on_failure=stop_on_failure,
2492
transport=transport,
2493
lsprof_timed=lsprof_timed,
2494
bench_history=bench_history,
2495
matching_tests_first=matching_tests_first,
2496
list_only=list_only,
2497
random_seed=random_seed,
2498
exclude_pattern=exclude_pattern,
2501
default_transport = old_transport
2504
def load_test_id_list(file_name):
2505
"""Load a test id list from a text file.
2507
The format is one test id by line. No special care is taken to impose
2508
strict rules, these test ids are used to filter the test suite so a test id
2509
that do not match an existing test will do no harm. This allows user to add
2510
comments, leave blank lines, etc.
2514
ftest = open(file_name, 'rt')
2516
if e.errno != errno.ENOENT:
2519
raise errors.NoSuchFile(file_name)
2521
for test_name in ftest.readlines():
2522
test_list.append(test_name.strip())
2527
def suite_matches_id_list(test_suite, id_list):
2528
"""Warns about tests not appearing or appearing more than once.
2530
:param test_suite: A TestSuite object.
2531
:param test_id_list: The list of test ids that should be found in
2534
:return: (absents, duplicates) absents is a list containing the test found
2535
in id_list but not in test_suite, duplicates is a list containing the
2536
test found multiple times in test_suite.
2538
When using a prefined test id list, it may occurs that some tests do not
2539
exist anymore or that some tests use the same id. This function warns the
2540
tester about potential problems in his workflow (test lists are volatile)
2541
or in the test suite itself (using the same id for several tests does not
2542
help to localize defects).
2544
# Build a dict counting id occurrences
2546
for test in iter_suite_tests(test_suite):
2548
tests[id] = tests.get(id, 0) + 1
2553
occurs = tests.get(id, 0)
2555
not_found.append(id)
2557
duplicates.append(id)
2559
return not_found, duplicates
2562
class TestIdList(object):
2563
"""Test id list to filter a test suite.
2565
Relying on the assumption that test ids are built as:
2566
<module>[.<class>.<method>][(<param>+)], <module> being in python dotted
2567
notation, this class offers methods to :
2568
- avoid building a test suite for modules not refered to in the test list,
2569
- keep only the tests listed from the module test suite.
2572
def __init__(self, test_id_list):
2573
# When a test suite needs to be filtered against us we compare test ids
2574
# for equality, so a simple dict offers a quick and simple solution.
2575
self.tests = dict().fromkeys(test_id_list, True)
2577
# While unittest.TestCase have ids like:
2578
# <module>.<class>.<method>[(<param+)],
2579
# doctest.DocTestCase can have ids like:
2582
# <module>.<function>
2583
# <module>.<class>.<method>
2585
# Since we can't predict a test class from its name only, we settle on
2586
# a simple constraint: a test id always begins with its module name.
2589
for test_id in test_id_list:
2590
parts = test_id.split('.')
2591
mod_name = parts.pop(0)
2592
modules[mod_name] = True
2594
mod_name += '.' + part
2595
modules[mod_name] = True
2596
self.modules = modules
2598
def refers_to(self, module_name):
2599
"""Is there tests for the module or one of its sub modules."""
2600
return self.modules.has_key(module_name)
2602
def includes(self, test_id):
2603
return self.tests.has_key(test_id)
2606
def test_suite(keep_only=None):
2607
"""Build and return TestSuite for the whole of bzrlib.
2609
:param keep_only: A list of test ids limiting the suite returned.
2611
This function can be replaced if you need to change the default test
2612
suite on a global basis, but it is not encouraged.
2615
'bzrlib.util.tests.test_bencode',
2616
'bzrlib.tests.test__dirstate_helpers',
2617
'bzrlib.tests.test_ancestry',
2618
'bzrlib.tests.test_annotate',
2619
'bzrlib.tests.test_api',
2620
'bzrlib.tests.test_atomicfile',
2621
'bzrlib.tests.test_bad_files',
2622
'bzrlib.tests.test_bisect_multi',
2623
'bzrlib.tests.test_branch',
2624
'bzrlib.tests.test_branchbuilder',
2625
'bzrlib.tests.test_bugtracker',
2626
'bzrlib.tests.test_bundle',
2627
'bzrlib.tests.test_bzrdir',
2628
'bzrlib.tests.test_cache_utf8',
2629
'bzrlib.tests.test_commands',
2630
'bzrlib.tests.test_commit',
2631
'bzrlib.tests.test_commit_merge',
2632
'bzrlib.tests.test_config',
2633
'bzrlib.tests.test_conflicts',
2634
'bzrlib.tests.test_counted_lock',
2635
'bzrlib.tests.test_decorators',
2636
'bzrlib.tests.test_delta',
2637
'bzrlib.tests.test_deprecated_graph',
2638
'bzrlib.tests.test_diff',
2639
'bzrlib.tests.test_dirstate',
2640
'bzrlib.tests.test_directory_service',
2641
'bzrlib.tests.test_email_message',
2642
'bzrlib.tests.test_errors',
2643
'bzrlib.tests.test_escaped_store',
2644
'bzrlib.tests.test_extract',
2645
'bzrlib.tests.test_fetch',
2646
'bzrlib.tests.test_ftp_transport',
2647
'bzrlib.tests.test_generate_docs',
2648
'bzrlib.tests.test_generate_ids',
2649
'bzrlib.tests.test_globbing',
2650
'bzrlib.tests.test_gpg',
2651
'bzrlib.tests.test_graph',
2652
'bzrlib.tests.test_hashcache',
2653
'bzrlib.tests.test_help',
2654
'bzrlib.tests.test_hooks',
2655
'bzrlib.tests.test_http',
2656
'bzrlib.tests.test_http_implementations',
2657
'bzrlib.tests.test_http_response',
2658
'bzrlib.tests.test_https_ca_bundle',
2659
'bzrlib.tests.test_identitymap',
2660
'bzrlib.tests.test_ignores',
2661
'bzrlib.tests.test_index',
2662
'bzrlib.tests.test_info',
2663
'bzrlib.tests.test_inv',
2664
'bzrlib.tests.test_knit',
2665
'bzrlib.tests.test_lazy_import',
2666
'bzrlib.tests.test_lazy_regex',
2667
'bzrlib.tests.test_lockdir',
2668
'bzrlib.tests.test_lockable_files',
2669
'bzrlib.tests.test_log',
2670
'bzrlib.tests.test_lsprof',
2671
'bzrlib.tests.test_lru_cache',
2672
'bzrlib.tests.test_mail_client',
2673
'bzrlib.tests.test_memorytree',
2674
'bzrlib.tests.test_merge',
2675
'bzrlib.tests.test_merge3',
2676
'bzrlib.tests.test_merge_core',
2677
'bzrlib.tests.test_merge_directive',
2678
'bzrlib.tests.test_missing',
2679
'bzrlib.tests.test_msgeditor',
2680
'bzrlib.tests.test_multiparent',
2681
'bzrlib.tests.test_mutabletree',
2682
'bzrlib.tests.test_nonascii',
2683
'bzrlib.tests.test_options',
2684
'bzrlib.tests.test_osutils',
2685
'bzrlib.tests.test_osutils_encodings',
2686
'bzrlib.tests.test_pack',
2687
'bzrlib.tests.test_patch',
2688
'bzrlib.tests.test_patches',
2689
'bzrlib.tests.test_permissions',
2690
'bzrlib.tests.test_plugins',
2691
'bzrlib.tests.test_progress',
2692
'bzrlib.tests.test_reconfigure',
2693
'bzrlib.tests.test_reconcile',
2694
'bzrlib.tests.test_registry',
2695
'bzrlib.tests.test_remote',
2696
'bzrlib.tests.test_repository',
2697
'bzrlib.tests.test_revert',
2698
'bzrlib.tests.test_revision',
2699
'bzrlib.tests.test_revisionspec',
2700
'bzrlib.tests.test_revisiontree',
2701
'bzrlib.tests.test_rio',
2702
'bzrlib.tests.test_sampler',
2703
'bzrlib.tests.test_selftest',
2704
'bzrlib.tests.test_setup',
2705
'bzrlib.tests.test_sftp_transport',
2706
'bzrlib.tests.test_smart',
2707
'bzrlib.tests.test_smart_add',
2708
'bzrlib.tests.test_smart_transport',
2709
'bzrlib.tests.test_smtp_connection',
2710
'bzrlib.tests.test_source',
2711
'bzrlib.tests.test_ssh_transport',
2712
'bzrlib.tests.test_status',
2713
'bzrlib.tests.test_store',
2714
'bzrlib.tests.test_strace',
2715
'bzrlib.tests.test_subsume',
2716
'bzrlib.tests.test_switch',
2717
'bzrlib.tests.test_symbol_versioning',
2718
'bzrlib.tests.test_tag',
2719
'bzrlib.tests.test_testament',
2720
'bzrlib.tests.test_textfile',
2721
'bzrlib.tests.test_textmerge',
2722
'bzrlib.tests.test_timestamp',
2723
'bzrlib.tests.test_trace',
2724
'bzrlib.tests.test_transactions',
2725
'bzrlib.tests.test_transform',
2726
'bzrlib.tests.test_transport',
2727
'bzrlib.tests.test_tree',
2728
'bzrlib.tests.test_treebuilder',
2729
'bzrlib.tests.test_tsort',
2730
'bzrlib.tests.test_tuned_gzip',
2731
'bzrlib.tests.test_ui',
2732
'bzrlib.tests.test_uncommit',
2733
'bzrlib.tests.test_upgrade',
2734
'bzrlib.tests.test_urlutils',
2735
'bzrlib.tests.test_versionedfile',
2736
'bzrlib.tests.test_version',
2737
'bzrlib.tests.test_version_info',
2738
'bzrlib.tests.test_weave',
2739
'bzrlib.tests.test_whitebox',
2740
'bzrlib.tests.test_win32utils',
2741
'bzrlib.tests.test_workingtree',
2742
'bzrlib.tests.test_workingtree_4',
2743
'bzrlib.tests.test_wsgi',
2744
'bzrlib.tests.test_xml',
2746
test_transport_implementations = [
2747
'bzrlib.tests.test_transport_implementations',
2748
'bzrlib.tests.test_read_bundle',
2750
loader = TestUtil.TestLoader()
2752
if keep_only is None:
2753
loader = TestUtil.TestLoader()
2755
id_filter = TestIdList(keep_only)
2756
loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
2757
suite = loader.suiteClass()
2759
# modules building their suite with loadTestsFromModuleNames
2760
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2762
# modules adapted for transport implementations
2763
from bzrlib.tests.test_transport_implementations import TransportTestProviderAdapter
2764
adapter = TransportTestProviderAdapter()
2765
adapt_modules(test_transport_implementations, adapter, loader, suite)
2767
# modules defining their own test_suite()
2768
for package in [p for p in packages_to_test()
2769
if (keep_only is None
2770
or id_filter.refers_to(p.__name__))]:
2771
pack_suite = package.test_suite()
2772
suite.addTest(pack_suite)
2774
modules_to_doctest = [
2779
'bzrlib.iterablefile',
2784
'bzrlib.symbol_versioning',
2787
'bzrlib.version_info_formats.format_custom',
2790
for mod in modules_to_doctest:
2791
if not (keep_only is None or id_filter.refers_to(mod)):
2792
# No tests to keep here, move along
2795
doc_suite = doctest.DocTestSuite(mod)
2796
except ValueError, e:
2797
print '**failed to get doctest for: %s\n%s' % (mod, e)
2799
suite.addTest(doc_suite)
2801
default_encoding = sys.getdefaultencoding()
2802
for name, plugin in bzrlib.plugin.plugins().items():
2803
if keep_only is not None:
2804
if not id_filter.refers_to(plugin.module.__name__):
2806
plugin_suite = plugin.test_suite()
2807
# We used to catch ImportError here and turn it into just a warning,
2808
# but really if you don't have --no-plugins this should be a failure.
2809
# mbp 20080213 - see http://bugs.launchpad.net/bugs/189771
2810
if plugin_suite is None:
2811
plugin_suite = plugin.load_plugin_tests(loader)
2812
if plugin_suite is not None:
2813
suite.addTest(plugin_suite)
2814
if default_encoding != sys.getdefaultencoding():
2815
bzrlib.trace.warning(
2816
'Plugin "%s" tried to reset default encoding to: %s', name,
2817
sys.getdefaultencoding())
2819
sys.setdefaultencoding(default_encoding)
2821
if keep_only is not None:
2822
# Now that the referred modules have loaded their tests, keep only the
2824
suite = filter_suite_by_id_list(suite, id_filter)
2825
# Do some sanity checks on the id_list filtering
2826
not_found, duplicates = suite_matches_id_list(suite, keep_only)
2827
for id in not_found:
2828
bzrlib.trace.warning('"%s" not found in the test suite', id)
2829
for id in duplicates:
2830
bzrlib.trace.warning('"%s" is used as an id by several tests', id)
2835
def multiply_tests_from_modules(module_name_list, scenario_iter, loader=None):
2836
"""Adapt all tests in some given modules to given scenarios.
2838
This is the recommended public interface for test parameterization.
2839
Typically the test_suite() method for a per-implementation test
2840
suite will call multiply_tests_from_modules and return the
2843
:param module_name_list: List of fully-qualified names of test
2845
:param scenario_iter: Iterable of pairs of (scenario_name,
2846
scenario_param_dict).
2847
:param loader: If provided, will be used instead of a new
2848
bzrlib.tests.TestLoader() instance.
2850
This returns a new TestSuite containing the cross product of
2851
all the tests in all the modules, each repeated for each scenario.
2852
Each test is adapted by adding the scenario name at the end
2853
of its name, and updating the test object's __dict__ with the
2854
scenario_param_dict.
2856
>>> r = multiply_tests_from_modules(
2857
... ['bzrlib.tests.test_sampler'],
2858
... [('one', dict(param=1)),
2859
... ('two', dict(param=2))])
2860
>>> tests = list(iter_suite_tests(r))
2864
'bzrlib.tests.test_sampler.DemoTest.test_nothing(one)'
2870
# XXX: Isn't load_tests() a better way to provide the same functionality
2871
# without forcing a predefined TestScenarioApplier ? --vila 080215
2873
loader = TestUtil.TestLoader()
2875
suite = loader.suiteClass()
2877
adapter = TestScenarioApplier()
2878
adapter.scenarios = list(scenario_iter)
2879
adapt_modules(module_name_list, adapter, loader, suite)
2883
def multiply_scenarios(scenarios_left, scenarios_right):
2884
"""Multiply two sets of scenarios.
2886
:returns: the cartesian product of the two sets of scenarios, that is
2887
a scenario for every possible combination of a left scenario and a
2891
('%s,%s' % (left_name, right_name),
2892
dict(left_dict.items() + right_dict.items()))
2893
for left_name, left_dict in scenarios_left
2894
for right_name, right_dict in scenarios_right]
2898
def adapt_modules(mods_list, adapter, loader, suite):
2899
"""Adapt the modules in mods_list using adapter and add to suite."""
2900
for test in iter_suite_tests(loader.loadTestsFromModuleNames(mods_list)):
2901
suite.addTests(adapter.adapt(test))
2904
def adapt_tests(tests_list, adapter, loader, suite):
2905
"""Adapt the tests in tests_list using adapter and add to suite."""
2906
for test in tests_list:
2907
suite.addTests(adapter.adapt(loader.loadTestsFromName(test)))
2910
def _rmtree_temp_dir(dirname):
2911
# If LANG=C we probably have created some bogus paths
2912
# which rmtree(unicode) will fail to delete
2913
# so make sure we are using rmtree(str) to delete everything
2914
# except on win32, where rmtree(str) will fail
2915
# since it doesn't have the property of byte-stream paths
2916
# (they are either ascii or mbcs)
2917
if sys.platform == 'win32':
2918
# make sure we are using the unicode win32 api
2919
dirname = unicode(dirname)
2921
dirname = dirname.encode(sys.getfilesystemencoding())
2923
osutils.rmtree(dirname)
2925
if sys.platform == 'win32' and e.errno == errno.EACCES:
2926
sys.stderr.write(('Permission denied: '
2927
'unable to remove testing dir '
2928
'%s\n' % os.path.basename(dirname)))
2933
class Feature(object):
2934
"""An operating system Feature."""
2937
self._available = None
2939
def available(self):
2940
"""Is the feature available?
2942
:return: True if the feature is available.
2944
if self._available is None:
2945
self._available = self._probe()
2946
return self._available
2949
"""Implement this method in concrete features.
2951
:return: True if the feature is available.
2953
raise NotImplementedError
2956
if getattr(self, 'feature_name', None):
2957
return self.feature_name()
2958
return self.__class__.__name__
2961
class _SymlinkFeature(Feature):
2964
return osutils.has_symlinks()
2966
def feature_name(self):
2969
SymlinkFeature = _SymlinkFeature()
2972
class _HardlinkFeature(Feature):
2975
return osutils.has_hardlinks()
2977
def feature_name(self):
2980
HardlinkFeature = _HardlinkFeature()
2983
class _OsFifoFeature(Feature):
2986
return getattr(os, 'mkfifo', None)
2988
def feature_name(self):
2989
return 'filesystem fifos'
2991
OsFifoFeature = _OsFifoFeature()
2994
class TestScenarioApplier(object):
2995
"""A tool to apply scenarios to tests."""
2997
def adapt(self, test):
2998
"""Return a TestSuite containing a copy of test for each scenario."""
2999
result = unittest.TestSuite()
3000
for scenario in self.scenarios:
3001
result.addTest(self.adapt_test_to_scenario(test, scenario))
3004
def adapt_test_to_scenario(self, test, scenario):
3005
"""Copy test and apply scenario to it.
3007
:param test: A test to adapt.
3008
:param scenario: A tuple describing the scenarion.
3009
The first element of the tuple is the new test id.
3010
The second element is a dict containing attributes to set on the
3012
:return: The adapted test.
3014
from copy import deepcopy
3015
new_test = deepcopy(test)
3016
for name, value in scenario[1].items():
3017
setattr(new_test, name, value)
3018
new_id = "%s(%s)" % (new_test.id(), scenario[0])
3019
new_test.id = lambda: new_id
3023
def probe_unicode_in_user_encoding():
3024
"""Try to encode several unicode strings to use in unicode-aware tests.
3025
Return first successfull match.
3027
:return: (unicode value, encoded plain string value) or (None, None)
3029
possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
3030
for uni_val in possible_vals:
3032
str_val = uni_val.encode(bzrlib.user_encoding)
3033
except UnicodeEncodeError:
3034
# Try a different character
3037
return uni_val, str_val
3041
def probe_bad_non_ascii(encoding):
3042
"""Try to find [bad] character with code [128..255]
3043
that cannot be decoded to unicode in some encoding.
3044
Return None if all non-ascii characters is valid
3047
for i in xrange(128, 256):
3050
char.decode(encoding)
3051
except UnicodeDecodeError:
3056
class _FTPServerFeature(Feature):
3057
"""Some tests want an FTP Server, check if one is available.
3059
Right now, the only way this is available is if 'medusa' is installed.
3060
http://www.amk.ca/python/code/medusa.html
3065
import bzrlib.tests.ftp_server
3070
def feature_name(self):
3073
FTPServerFeature = _FTPServerFeature()
3076
class _CaseInsensitiveFilesystemFeature(Feature):
3077
"""Check if underlined filesystem is case-insensitive
3078
(e.g. on Windows, Cygwin, MacOS)
3082
if TestCaseWithMemoryTransport.TEST_ROOT is None:
3083
root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
3084
TestCaseWithMemoryTransport.TEST_ROOT = root
3086
root = TestCaseWithMemoryTransport.TEST_ROOT
3087
tdir = osutils.mkdtemp(prefix='case-sensitive-probe-', suffix='',
3089
name_a = osutils.pathjoin(tdir, 'a')
3090
name_A = osutils.pathjoin(tdir, 'A')
3092
result = osutils.isdir(name_A)
3093
_rmtree_temp_dir(tdir)
3096
def feature_name(self):
3097
return 'case-insensitive filesystem'
3099
CaseInsensitiveFilesystemFeature = _CaseInsensitiveFilesystemFeature()