104
104
from bzrlib.tests.treeshape import build_tree_contents
105
from bzrlib.ui import NullProgressView
106
from bzrlib.ui.text import TextUIFactory
105
107
import bzrlib.version_info_formats.format_custom
106
108
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
113
115
default_transport = LocalURLServer
117
# Subunit result codes, defined here to prevent a hard dependency on subunit.
116
122
class ExtendedTestResult(unittest._TextTestResult):
117
123
"""Accepts, reports and accumulates the results of running tests.
158
163
bench_history.write("--date %s %s\n" % (time.time(), revision_id))
159
164
self._bench_history = bench_history
160
165
self.ui = ui.ui_factory
161
self.num_tests = num_tests
162
167
self.error_count = 0
163
168
self.failure_count = 0
164
169
self.known_failure_count = 0
195
202
def _testTimeString(self, testCase):
196
203
benchmark_time = self._extractBenchmarkTime(testCase)
197
204
if benchmark_time is not None:
199
self._formatTime(benchmark_time),
200
self._elapsedTestTimeString())
205
return self._formatTime(benchmark_time) + "*"
202
return " %s" % self._elapsedTestTimeString()
207
return self._elapsedTestTimeString()
204
209
def _formatTime(self, seconds):
205
210
"""Format seconds as milliseconds with leading spaces."""
221
226
self._recordTestStartTime()
223
228
def startTests(self):
225
'testing: %s\n' % (osutils.realpath(sys.argv[0]),))
227
' %s (%s python%s)\n' % (
230
if getattr(sys, 'frozen', None) is None:
231
bzr_path = osutils.realpath(sys.argv[0])
233
bzr_path = sys.executable
235
'testing: %s\n' % (bzr_path,))
238
bzrlib.__path__[0],))
240
' bzr-%s python-%s %s\n' % (
229
241
bzrlib.version_string,
230
242
bzrlib._format_version_tuple(sys.version_info),
243
platform.platform(aliased=1),
232
245
self.stream.write('\n')
346
359
self.stream.write("%s: " % flavour)
347
360
self.stream.writeln(self.getDescription(test))
348
361
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')
362
log_contents = test._get_log()
364
self.stream.write('\n')
366
('vvvv[log from %s]' % test.id()).ljust(78,'-'))
367
self.stream.write('\n')
368
self.stream.write(log_contents)
369
self.stream.write('\n')
371
('^^^^[log from %s]' % test.id()).ljust(78,'-'))
372
self.stream.write('\n')
358
373
self.stream.writeln(self.separator2)
359
374
self.stream.writeln("%s" % err)
376
def progress(self, offset, whence):
377
"""The test is adjusting the count of tests to run."""
378
if whence == SUBUNIT_SEEK_SET:
379
self.num_tests = offset
380
elif whence == SUBUNIT_SEEK_CUR:
381
self.num_tests += offset
383
raise errors.BzrError("Unknown whence %r" % whence)
361
385
def finished(self):
379
403
def __init__(self, stream, descriptions, verbosity,
380
404
bench_history=None,
385
408
ExtendedTestResult.__init__(self, stream, descriptions, verbosity,
386
bench_history, num_tests, strict)
388
self.pb = self.ui.nested_progress_bar()
389
self._supplied_pb = False
392
self._supplied_pb = True
409
bench_history, strict)
410
# We no longer pass them around, but just rely on the UIFactory stack
413
warnings.warn("Passing pb to TextTestResult is deprecated")
414
self.pb = self.ui.nested_progress_bar()
393
415
self.pb.show_pct = False
394
416
self.pb.show_spinner = False
395
417
self.pb.show_eta = False,
396
418
self.pb.show_count = False
397
419
self.pb.show_bar = False
420
self.pb.update_latency = 0
421
self.pb.show_transport_activity = False
424
# called when the tests that are going to run have run
426
super(TextTestResult, self).done()
399
431
def report_starting(self):
400
432
self.pb.update('[test 0/%d] Starting' % (self.num_tests))
434
def printErrors(self):
435
# clear the pb to make room for the error listing
437
super(TextTestResult, self).printErrors()
402
439
def _progress_prefix_text(self):
403
440
# the longer this text, the less space we have to show the test
409
446
## a += ', %d skip' % self.skip_count
410
447
## if self.known_failure_count:
411
448
## a += '+%dX' % self.known_failure_count
412
if self.num_tests is not None:
413
450
a +='/%d' % self.num_tests
415
452
runtime = time.time() - self._overall_start_time
464
501
def report_cleaning_up(self):
465
502
self.pb.update('Cleaning up')
468
if not self._supplied_pb:
472
505
class VerboseTestResult(ExtendedTestResult):
473
506
"""Produce long output, with one line per test run plus times"""
486
519
def report_test_start(self, test):
488
521
name = self._shortened_test_description(test)
489
# width needs space for 6 char status, plus 1 for slash, plus 2 10-char
490
# numbers, plus a trailing blank
522
# width needs space for 6 char status, plus 1 for slash, plus an
523
# 11-char time string, plus a trailing blank
491
524
# when NUMBERED_DIRS: plus 5 chars on test number, plus 1 char on space
492
525
self.stream.write(self._ellipsize_to_right(name,
493
osutils.terminal_width()-30))
526
osutils.terminal_width()-18))
494
527
self.stream.flush()
496
529
def _error_summary(self, err):
702
734
return setattr(self._cstring, name, val)
705
class TestUIFactory(ui.CLIUIFactory):
737
class TestUIFactory(TextUIFactory):
706
738
"""A UI Factory for testing.
708
740
Hide the progress bar but emit note()s.
710
742
Allows get_password to be tested without real tty attached.
744
See also CannedInputUIFactory which lets you provide programmatic input in
747
# TODO: Capture progress events at the model level and allow them to be
748
# observed by tests that care.
750
# XXX: Should probably unify more with CannedInputUIFactory or a
751
# particular configuration of TextUIFactory, or otherwise have a clearer
752
# idea of how they're supposed to be different.
753
# See https://bugs.edge.launchpad.net/bzr/+bug/408213
713
755
def __init__(self, stdout=None, stderr=None, stdin=None):
714
756
if stdin is not None:
719
761
stdin = StringIOWrapper(stdin)
720
762
super(TestUIFactory, self).__init__(stdin, stdout, stderr)
723
"""See progress.ProgressBar.clear()."""
725
def clear_term(self):
726
"""See progress.ProgressBar.clear_term()."""
729
"""See progress.ProgressBar.finished()."""
731
def note(self, fmt_string, *args):
732
"""See progress.ProgressBar.note()."""
734
fmt_string = fmt_string % args
735
self.stdout.write(fmt_string + "\n")
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
764
def get_non_echoed_password(self):
747
765
"""Get password from stdin without trying to handle the echo mode"""
748
766
password = self.stdin.readline()
830
850
self._preserved_debug_flags = set(debug.debug_flags)
831
851
if 'allow_debug' not in selftest_debug_flags:
832
852
debug.debug_flags.clear()
853
if 'disable_lock_checks' not in selftest_debug_flags:
854
debug.debug_flags.add('strict_locks')
833
855
self.addCleanup(self._restore_debug_flags)
835
857
def _clear_hooks(self):
858
880
def _check_locks(self):
859
881
"""Check that all lock take/release actions have been paired."""
860
# once we have fixed all the current lock problems, we can change the
861
# following code to always check for mismatched locks, but only do
862
# traceback showing with -Dlock (self._lock_check_thorough is True).
863
# For now, because the test suite will fail, we only assert that lock
864
# matching has occured with -Dlock.
882
# We always check for mismatched locks. If a mismatch is found, we
883
# fail unless -Edisable_lock_checks is supplied to selftest, in which
884
# case we just print a warning.
866
886
acquired_locks = [lock for action, lock in self._lock_actions
867
887
if action == 'acquired']
886
906
def _track_locks(self):
887
907
"""Track lock activity during tests."""
888
908
self._lock_actions = []
889
self._lock_check_thorough = 'lock' not in debug.debug_flags
909
if 'disable_lock_checks' in selftest_debug_flags:
910
self._lock_check_thorough = False
912
self._lock_check_thorough = True
890
914
self.addCleanup(self._check_locks)
891
915
_mod_lock.Lock.hooks.install_named_hook('lock_acquired',
892
916
self._lock_acquired, None)
1093
1117
osutils.realpath(path2),
1094
1118
"apparent paths:\na = %s\nb = %s\n," % (path1, path2))
1096
def assertIsInstance(self, obj, kls):
1097
"""Fail if obj is not an instance of kls"""
1120
def assertIsInstance(self, obj, kls, msg=None):
1121
"""Fail if obj is not an instance of kls
1123
:param msg: Supplementary message to show if the assertion fails.
1098
1125
if not isinstance(obj, kls):
1099
self.fail("%r is an instance of %s rather than %s" % (
1100
obj, obj.__class__, kls))
1126
m = "%r is an instance of %s rather than %s" % (
1127
obj, obj.__class__, kls)
1102
1132
def expectFailure(self, reason, assertion, *args, **kwargs):
1103
1133
"""Invoke a test, expecting it to fail for the given reason.
1302
1332
"""Make the logfile not be deleted when _finishLogFile is called."""
1303
1333
self._keep_log_file = True
1335
def thisFailsStrictLockCheck(self):
1336
"""It is known that this test would fail with -Dstrict_locks.
1338
By default, all tests are run with strict lock checking unless
1339
-Edisable_lock_checks is supplied. However there are some tests which
1340
we know fail strict locks at this point that have not been fixed.
1341
They should call this function to disable the strict checking.
1343
This should be used sparingly, it is much better to fix the locking
1344
issues rather than papering over the problem by calling this function.
1346
debug.debug_flags.discard('strict_locks')
1305
1348
def addCleanup(self, callable, *args, **kwargs):
1306
1349
"""Arrange to run a callable when this case is torn down.
1325
1368
'BZR_PROGRESS_BAR': None,
1326
1369
'BZR_LOG': None,
1327
1370
'BZR_PLUGIN_PATH': None,
1371
# Make sure that any text ui tests are consistent regardless of
1372
# the environment the test case is run in; you may want tests that
1373
# test other combinations. 'dumb' is a reasonable guess for tests
1374
# going to a pipe or a StringIO.
1329
1379
'SSH_AUTH_SOCK': None,
1912
1962
sio.encoding = output_encoding
1965
def disable_verb(self, verb):
1966
"""Disable a smart server verb for one test."""
1967
from bzrlib.smart import request
1968
request_handlers = request.request_handlers
1969
orig_method = request_handlers.get(verb)
1970
request_handlers.remove(verb)
1972
request_handlers.register(verb, orig_method)
1973
self.addCleanup(restoreVerb)
1916
1976
class CapturedCall(object):
1917
1977
"""A helper for capturing smart server calls for easy debug analysis."""
2285
2345
def _getTestDirPrefix(self):
2286
2346
# create a directory within the top level test directory
2287
if sys.platform == 'win32':
2347
if sys.platform in ('win32', 'cygwin'):
2288
2348
name_prefix = re.sub('[<>*=+",:;_/\\-]', '_', self.id())
2289
2349
# windows is likely to have path-length limits so use a short name
2290
2350
name_prefix = name_prefix[-30:]
2738
2798
decorators.append(filter_tests(pattern))
2739
2799
if suite_decorators:
2740
2800
decorators.extend(suite_decorators)
2801
# tell the result object how many tests will be running: (except if
2802
# --parallel=fork is being used. Robert said he will provide a better
2803
# progress design later -- vila 20090817)
2804
if fork_decorator not in decorators:
2805
decorators.append(CountingDecorator)
2741
2806
for decorator in decorators:
2742
2807
suite = decorator(suite)
2743
2808
result = runner.run(suite)
2915
class CountingDecorator(TestDecorator):
2916
"""A decorator which calls result.progress(self.countTestCases)."""
2918
def run(self, result):
2919
progress_method = getattr(result, 'progress', None)
2920
if callable(progress_method):
2921
progress_method(self.countTestCases(), SUBUNIT_SEEK_SET)
2922
return super(CountingDecorator, self).run(result)
2850
2925
class ExcludeDecorator(TestDecorator):
2851
2926
"""A decorator which excludes test matching an exclude pattern."""
2959
3034
concurrency = osutils.local_concurrency()
2961
3036
from subunit import TestProtocolClient, ProtocolTestCase
3038
from subunit.test_results import AutoTimingTestResultDecorator
3040
AutoTimingTestResultDecorator = lambda x:x
2962
3041
class TestInOtherProcess(ProtocolTestCase):
2963
3042
# Should be in subunit, I think. RBC.
2964
3043
def __init__(self, stream, pid):
2987
3066
sys.stdin.close()
2988
3067
sys.stdin = None
2989
3068
stream = os.fdopen(c2pwrite, 'wb', 1)
2990
subunit_result = TestProtocolClient(stream)
3069
subunit_result = AutoTimingTestResultDecorator(
3070
TestProtocolClient(stream))
2991
3071
process_suite.run(subunit_result)
3008
3088
concurrency = osutils.local_concurrency()
3010
from subunit import TestProtocolClient, ProtocolTestCase
3090
from subunit import ProtocolTestCase
3011
3091
class TestInSubprocess(ProtocolTestCase):
3012
3092
def __init__(self, process, name):
3013
3093
ProtocolTestCase.__init__(self, process.stdout)
3104
3184
# Controlled by "bzr selftest -E=..." option
3185
# Currently supported:
3186
# -Eallow_debug Will no longer clear debug.debug_flags() so it
3187
# preserves any flags supplied at the command line.
3188
# -Edisable_lock_checks Turns errors in mismatched locks into simple prints
3189
# rather than failing tests. And no longer raise
3190
# LockContention when fctnl locks are not being used
3191
# with proper exclusion rules.
3105
3192
selftest_debug_flags = set()
3142
3230
keep_only = None
3144
3232
keep_only = load_test_id_list(load_list)
3234
starting_with = [test_prefix_alias_registry.resolve_alias(start)
3235
for start in starting_with]
3145
3236
if test_suite_factory is None:
3237
# Reduce loading time by loading modules based on the starting_with
3146
3239
suite = test_suite(keep_only, starting_with)
3148
3241
suite = test_suite_factory()
3243
# But always filter as requested.
3244
suite = filter_suite_by_id_startswith(suite, starting_with)
3149
3245
return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
3150
3246
stop_on_failure=stop_on_failure,
3151
3247
transport=transport,
3333
3430
testmod_names = [
3335
3432
'bzrlib.tests.blackbox',
3336
'bzrlib.tests.branch_implementations',
3337
'bzrlib.tests.bzrdir_implementations',
3338
3433
'bzrlib.tests.commands',
3339
'bzrlib.tests.interrepository_implementations',
3340
'bzrlib.tests.intertree_implementations',
3341
'bzrlib.tests.inventory_implementations',
3434
'bzrlib.tests.per_branch',
3435
'bzrlib.tests.per_bzrdir',
3436
'bzrlib.tests.per_interrepository',
3437
'bzrlib.tests.per_intertree',
3438
'bzrlib.tests.per_inventory',
3342
3439
'bzrlib.tests.per_interbranch',
3343
3440
'bzrlib.tests.per_lock',
3441
'bzrlib.tests.per_transport',
3442
'bzrlib.tests.per_tree',
3443
'bzrlib.tests.per_pack_repository',
3344
3444
'bzrlib.tests.per_repository',
3345
3445
'bzrlib.tests.per_repository_chk',
3346
3446
'bzrlib.tests.per_repository_reference',
3447
'bzrlib.tests.per_versionedfile',
3448
'bzrlib.tests.per_workingtree',
3449
'bzrlib.tests.test__annotator',
3347
3450
'bzrlib.tests.test__chk_map',
3348
3451
'bzrlib.tests.test__dirstate_helpers',
3349
3452
'bzrlib.tests.test__groupcompress',
3453
'bzrlib.tests.test__known_graph',
3350
3454
'bzrlib.tests.test__rio',
3351
3455
'bzrlib.tests.test__walkdirs_win32',
3352
3456
'bzrlib.tests.test_ancestry',
3374
3478
'bzrlib.tests.test_config',
3375
3479
'bzrlib.tests.test_conflicts',
3376
3480
'bzrlib.tests.test_counted_lock',
3481
'bzrlib.tests.test_crash',
3377
3482
'bzrlib.tests.test_decorators',
3378
3483
'bzrlib.tests.test_delta',
3379
3484
'bzrlib.tests.test_debug',
3412
3517
'bzrlib.tests.test_knit',
3413
3518
'bzrlib.tests.test_lazy_import',
3414
3519
'bzrlib.tests.test_lazy_regex',
3520
'bzrlib.tests.test_lock',
3415
3521
'bzrlib.tests.test_lockable_files',
3416
3522
'bzrlib.tests.test_lockdir',
3417
3523
'bzrlib.tests.test_log',
3432
3538
'bzrlib.tests.test_osutils',
3433
3539
'bzrlib.tests.test_osutils_encodings',
3434
3540
'bzrlib.tests.test_pack',
3435
'bzrlib.tests.test_pack_repository',
3436
3541
'bzrlib.tests.test_patch',
3437
3542
'bzrlib.tests.test_patches',
3438
3543
'bzrlib.tests.test_permissions',
3480
3585
'bzrlib.tests.test_transactions',
3481
3586
'bzrlib.tests.test_transform',
3482
3587
'bzrlib.tests.test_transport',
3483
'bzrlib.tests.test_transport_implementations',
3484
3588
'bzrlib.tests.test_transport_log',
3485
3589
'bzrlib.tests.test_tree',
3486
3590
'bzrlib.tests.test_treebuilder',
3493
3597
'bzrlib.tests.test_urlutils',
3494
3598
'bzrlib.tests.test_version',
3495
3599
'bzrlib.tests.test_version_info',
3496
'bzrlib.tests.test_versionedfile',
3497
3600
'bzrlib.tests.test_weave',
3498
3601
'bzrlib.tests.test_whitebox',
3499
3602
'bzrlib.tests.test_win32utils',
3501
3604
'bzrlib.tests.test_workingtree_4',
3502
3605
'bzrlib.tests.test_wsgi',
3503
3606
'bzrlib.tests.test_xml',
3504
'bzrlib.tests.tree_implementations',
3505
'bzrlib.tests.workingtree_implementations',
3508
3609
loader = TestUtil.TestLoader()
3611
if keep_only is not None:
3612
id_filter = TestIdList(keep_only)
3510
3613
if starting_with:
3511
starting_with = [test_prefix_alias_registry.resolve_alias(start)
3512
for start in starting_with]
3513
3614
# We take precedence over keep_only because *at loading time* using
3514
3615
# both options means we will load less tests for the same final result.
3515
3616
def interesting_module(name):
3525
3626
loader = TestUtil.FilteredByModuleTestLoader(interesting_module)
3527
3628
elif keep_only is not None:
3528
id_filter = TestIdList(keep_only)
3529
3629
loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
3530
3630
def interesting_module(name):
3531
3631
return id_filter.refers_to(name)
3592
3692
sys.setdefaultencoding(default_encoding)
3595
suite = filter_suite_by_id_startswith(suite, starting_with)
3597
3694
if keep_only is not None:
3598
3695
# Now that the referred modules have loaded their tests, keep only the
3599
3696
# requested ones.
3728
3825
osutils.rmtree(dirname)
3729
3826
except OSError, e:
3730
if sys.platform == 'win32' and e.errno == errno.EACCES:
3731
sys.stderr.write('Permission denied: '
3732
'unable to remove testing dir '
3734
% (os.path.basename(dirname), e))
3827
# We don't want to fail here because some useful display will be lost
3828
# otherwise. Polluting the tmp dir is bad, but not giving all the
3829
# possible info to the test runner is even worse.
3830
sys.stderr.write('Unable to remove testing dir %s\n%s'
3831
% (os.path.basename(dirname), e))
3739
3834
class Feature(object):
3978
4073
# Only define SubUnitBzrRunner if subunit is available.
3980
4075
from subunit import TestProtocolClient
4077
from subunit.test_results import AutoTimingTestResultDecorator
4079
AutoTimingTestResultDecorator = lambda x:x
3981
4080
class SubUnitBzrRunner(TextTestRunner):
3982
4081
def run(self, test):
3983
result = TestProtocolClient(self.stream)
4082
result = AutoTimingTestResultDecorator(
4083
TestProtocolClient(self.stream))
3984
4084
test.run(result)
3986
4086
except ImportError: