/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Robert Collins
  • Date: 2009-08-25 19:29:41 UTC
  • mfrom: (4648 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4649.
  • Revision ID: robertc@robertcollins.net-20090825192941-x2kg9ikhsapjbs7b
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
226
226
        self._recordTestStartTime()
227
227
 
228
228
    def startTests(self):
229
 
        self.stream.write(
230
 
            'testing: %s\n' % (osutils.realpath(sys.argv[0]),))
231
 
        self.stream.write(
232
 
            '   %s (%s python%s)\n' % (
233
 
                    bzrlib.__path__[0],
 
229
        import platform
 
230
        if getattr(sys, 'frozen', None) is None:
 
231
            bzr_path = osutils.realpath(sys.argv[0])
 
232
        else:
 
233
            bzr_path = sys.executable
 
234
        self.stream.write(
 
235
            'testing: %s\n' % (bzr_path,))
 
236
        self.stream.write(
 
237
            '   %s\n' % (
 
238
                    bzrlib.__path__[0],))
 
239
        self.stream.write(
 
240
            '   bzr-%s python-%s %s\n' % (
234
241
                    bzrlib.version_string,
235
242
                    bzrlib._format_version_tuple(sys.version_info),
 
243
                    platform.platform(aliased=1),
236
244
                    ))
237
245
        self.stream.write('\n')
238
246
 
571
579
                 bench_history=None,
572
580
                 list_only=False,
573
581
                 strict=False,
 
582
                 result_decorators=None,
574
583
                 ):
 
584
        """Create a TextTestRunner.
 
585
 
 
586
        :param result_decorators: An optional list of decorators to apply
 
587
            to the result object being used by the runner. Decorators are
 
588
            applied left to right - the first element in the list is the 
 
589
            innermost decorator.
 
590
        """
575
591
        self.stream = unittest._WritelnDecorator(stream)
576
592
        self.descriptions = descriptions
577
593
        self.verbosity = verbosity
578
594
        self._bench_history = bench_history
579
595
        self.list_only = list_only
580
596
        self._strict = strict
 
597
        self._result_decorators = result_decorators or []
581
598
 
582
599
    def run(self, test):
583
600
        "Run the given test case or test suite."
592
609
                              bench_history=self._bench_history,
593
610
                              strict=self._strict,
594
611
                              )
 
612
        run_result = result
 
613
        for decorator in self._result_decorators:
 
614
            run_result = decorator(run_result)
595
615
        result.stop_early = self.stop_on_failure
596
616
        result.report_starting()
597
617
        if self.list_only:
606
626
            try:
607
627
                import testtools
608
628
            except ImportError:
609
 
                test.run(result)
 
629
                test.run(run_result)
610
630
            else:
611
631
                if isinstance(test, testtools.ConcurrentTestSuite):
612
632
                    # We need to catch bzr specific behaviors
613
 
                    test.run(BZRTransformingResult(result))
 
633
                    test.run(BZRTransformingResult(run_result))
614
634
                else:
615
 
                    test.run(result)
 
635
                    test.run(run_result)
616
636
            run = result.testsRun
617
637
            actionTaken = "Ran"
618
638
        stopTime = time.time()
814
834
        self._benchcalls = []
815
835
        self._benchtime = None
816
836
        self._clear_hooks()
817
 
        # Track locks - needs to be called before _clear_debug_flags.
818
837
        self._track_locks()
819
838
        self._clear_debug_flags()
820
839
        TestCase._active_threads = threading.activeCount()
843
862
        self._preserved_debug_flags = set(debug.debug_flags)
844
863
        if 'allow_debug' not in selftest_debug_flags:
845
864
            debug.debug_flags.clear()
 
865
        if 'disable_lock_checks' not in selftest_debug_flags:
 
866
            debug.debug_flags.add('strict_locks')
846
867
        self.addCleanup(self._restore_debug_flags)
847
868
 
848
869
    def _clear_hooks(self):
870
891
 
871
892
    def _check_locks(self):
872
893
        """Check that all lock take/release actions have been paired."""
873
 
        # once we have fixed all the current lock problems, we can change the
874
 
        # following code to always check for mismatched locks, but only do
875
 
        # traceback showing with -Dlock (self._lock_check_thorough is True).
876
 
        # For now, because the test suite will fail, we only assert that lock
877
 
        # matching has occured with -Dlock.
 
894
        # We always check for mismatched locks. If a mismatch is found, we
 
895
        # fail unless -Edisable_lock_checks is supplied to selftest, in which
 
896
        # case we just print a warning.
878
897
        # unhook:
879
898
        acquired_locks = [lock for action, lock in self._lock_actions
880
899
                          if action == 'acquired']
899
918
    def _track_locks(self):
900
919
        """Track lock activity during tests."""
901
920
        self._lock_actions = []
902
 
        self._lock_check_thorough = 'lock' not in debug.debug_flags
 
921
        if 'disable_lock_checks' in selftest_debug_flags:
 
922
            self._lock_check_thorough = False
 
923
        else:
 
924
            self._lock_check_thorough = True
 
925
            
903
926
        self.addCleanup(self._check_locks)
904
927
        _mod_lock.Lock.hooks.install_named_hook('lock_acquired',
905
928
                                                self._lock_acquired, None)
1321
1344
        """Make the logfile not be deleted when _finishLogFile is called."""
1322
1345
        self._keep_log_file = True
1323
1346
 
 
1347
    def thisFailsStrictLockCheck(self):
 
1348
        """It is known that this test would fail with -Dstrict_locks.
 
1349
 
 
1350
        By default, all tests are run with strict lock checking unless
 
1351
        -Edisable_lock_checks is supplied. However there are some tests which
 
1352
        we know fail strict locks at this point that have not been fixed.
 
1353
        They should call this function to disable the strict checking.
 
1354
 
 
1355
        This should be used sparingly, it is much better to fix the locking
 
1356
        issues rather than papering over the problem by calling this function.
 
1357
        """
 
1358
        debug.debug_flags.discard('strict_locks')
 
1359
 
1324
1360
    def addCleanup(self, callable, *args, **kwargs):
1325
1361
        """Arrange to run a callable when this case is torn down.
1326
1362
 
1938
1974
        sio.encoding = output_encoding
1939
1975
        return sio
1940
1976
 
 
1977
    def disable_verb(self, verb):
 
1978
        """Disable a smart server verb for one test."""
 
1979
        from bzrlib.smart import request
 
1980
        request_handlers = request.request_handlers
 
1981
        orig_method = request_handlers.get(verb)
 
1982
        request_handlers.remove(verb)
 
1983
        def restoreVerb():
 
1984
            request_handlers.register(verb, orig_method)
 
1985
        self.addCleanup(restoreVerb)
 
1986
 
1941
1987
 
1942
1988
class CapturedCall(object):
1943
1989
    """A helper for capturing smart server calls for easy debug analysis."""
2310
2356
 
2311
2357
    def _getTestDirPrefix(self):
2312
2358
        # create a directory within the top level test directory
2313
 
        if sys.platform == 'win32':
 
2359
        if sys.platform in ('win32', 'cygwin'):
2314
2360
            name_prefix = re.sub('[<>*=+",:;_/\\-]', '_', self.id())
2315
2361
            # windows is likely to have path-length limits so use a short name
2316
2362
            name_prefix = name_prefix[-30:]
2728
2774
              strict=False,
2729
2775
              runner_class=None,
2730
2776
              suite_decorators=None,
2731
 
              stream=None):
 
2777
              stream=None,
 
2778
              result_decorators=None,
 
2779
              ):
2732
2780
    """Run a test suite for bzr selftest.
2733
2781
 
2734
2782
    :param runner_class: The class of runner to use. Must support the
2751
2799
                            bench_history=bench_history,
2752
2800
                            list_only=list_only,
2753
2801
                            strict=strict,
 
2802
                            result_decorators=result_decorators,
2754
2803
                            )
2755
2804
    runner.stop_on_failure=stop_on_failure
2756
2805
    # built in decorator factories:
2764
2813
        decorators.append(filter_tests(pattern))
2765
2814
    if suite_decorators:
2766
2815
        decorators.extend(suite_decorators)
2767
 
    # tell the result object how many tests will be running:
2768
 
    decorators.append(CountingDecorator)
 
2816
    # tell the result object how many tests will be running: (except if
 
2817
    # --parallel=fork is being used. Robert said he will provide a better
 
2818
    # progress design later -- vila 20090817)
 
2819
    if fork_decorator not in decorators:
 
2820
        decorators.append(CountingDecorator)
2769
2821
    for decorator in decorators:
2770
2822
        suite = decorator(suite)
2771
2823
    result = runner.run(suite)
3094
3146
    return result
3095
3147
 
3096
3148
 
3097
 
class BZRTransformingResult(unittest.TestResult):
 
3149
class ForwardingResult(unittest.TestResult):
3098
3150
 
3099
3151
    def __init__(self, target):
3100
3152
        unittest.TestResult.__init__(self)
3106
3158
    def stopTest(self, test):
3107
3159
        self.result.stopTest(test)
3108
3160
 
 
3161
    def addSkip(self, test, reason):
 
3162
        self.result.addSkip(test, reason)
 
3163
 
 
3164
    def addSuccess(self, test):
 
3165
        self.result.addSuccess(test)
 
3166
 
 
3167
    def addError(self, test, err):
 
3168
        self.result.addError(test, err)
 
3169
 
 
3170
    def addFailure(self, test, err):
 
3171
        self.result.addFailure(test, err)
 
3172
 
 
3173
 
 
3174
class BZRTransformingResult(ForwardingResult):
 
3175
 
3109
3176
    def addError(self, test, err):
3110
3177
        feature = self._error_looks_like('UnavailableFeature: ', err)
3111
3178
        if feature is not None:
3121
3188
        else:
3122
3189
            self.result.addFailure(test, err)
3123
3190
 
3124
 
    def addSkip(self, test, reason):
3125
 
        self.result.addSkip(test, reason)
3126
 
 
3127
 
    def addSuccess(self, test):
3128
 
        self.result.addSuccess(test)
3129
 
 
3130
3191
    def _error_looks_like(self, prefix, err):
3131
3192
        """Deserialize exception and returns the stringify value."""
3132
3193
        import subunit
3144
3205
        return value
3145
3206
 
3146
3207
 
 
3208
class ProfileResult(ForwardingResult):
 
3209
    """Generate profiling data for all activity between start and success.
 
3210
    
 
3211
    The profile data is appended to the test's _benchcalls attribute and can
 
3212
    be accessed by the forwarded-to TestResult.
 
3213
 
 
3214
    While it might be cleaner do accumulate this in stopTest, addSuccess is
 
3215
    where our existing output support for lsprof is, and this class aims to
 
3216
    fit in with that: while it could be moved it's not necessary to accomplish
 
3217
    test profiling, nor would it be dramatically cleaner.
 
3218
    """
 
3219
 
 
3220
    def startTest(self, test):
 
3221
        self.profiler = bzrlib.lsprof.BzrProfiler()
 
3222
        self.profiler.start()
 
3223
        ForwardingResult.startTest(self, test)
 
3224
 
 
3225
    def addSuccess(self, test):
 
3226
        stats = self.profiler.stop()
 
3227
        try:
 
3228
            calls = test._benchcalls
 
3229
        except AttributeError:
 
3230
            test._benchcalls = []
 
3231
            calls = test._benchcalls
 
3232
        calls.append(((test.id(), "", ""), stats))
 
3233
        ForwardingResult.addSuccess(self, test)
 
3234
 
 
3235
    def stopTest(self, test):
 
3236
        ForwardingResult.stopTest(self, test)
 
3237
        self.profiler = None
 
3238
 
 
3239
 
3147
3240
# Controlled by "bzr selftest -E=..." option
 
3241
# Currently supported:
 
3242
#   -Eallow_debug           Will no longer clear debug.debug_flags() so it
 
3243
#                           preserves any flags supplied at the command line.
 
3244
#   -Edisable_lock_checks   Turns errors in mismatched locks into simple prints
 
3245
#                           rather than failing tests. And no longer raise
 
3246
#                           LockContention when fctnl locks are not being used
 
3247
#                           with proper exclusion rules.
3148
3248
selftest_debug_flags = set()
3149
3249
 
3150
3250
 
3163
3263
             starting_with=None,
3164
3264
             runner_class=None,
3165
3265
             suite_decorators=None,
 
3266
             stream=None,
 
3267
             lsprof_tests=False,
3166
3268
             ):
3167
3269
    """Run the whole test suite under the enhanced runner"""
3168
3270
    # XXX: Very ugly way to do this...
3185
3287
            keep_only = None
3186
3288
        else:
3187
3289
            keep_only = load_test_id_list(load_list)
 
3290
        if starting_with:
 
3291
            starting_with = [test_prefix_alias_registry.resolve_alias(start)
 
3292
                             for start in starting_with]
3188
3293
        if test_suite_factory is None:
 
3294
            # Reduce loading time by loading modules based on the starting_with
 
3295
            # patterns.
3189
3296
            suite = test_suite(keep_only, starting_with)
3190
3297
        else:
3191
3298
            suite = test_suite_factory()
 
3299
        if starting_with:
 
3300
            # But always filter as requested.
 
3301
            suite = filter_suite_by_id_startswith(suite, starting_with)
 
3302
        result_decorators = []
 
3303
        if lsprof_tests:
 
3304
            result_decorators.append(ProfileResult)
3192
3305
        return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
3193
3306
                     stop_on_failure=stop_on_failure,
3194
3307
                     transport=transport,
3201
3314
                     strict=strict,
3202
3315
                     runner_class=runner_class,
3203
3316
                     suite_decorators=suite_decorators,
 
3317
                     stream=stream,
 
3318
                     result_decorators=result_decorators,
3204
3319
                     )
3205
3320
    finally:
3206
3321
        default_transport = old_transport
3386
3501
                   'bzrlib.tests.per_lock',
3387
3502
                   'bzrlib.tests.per_transport',
3388
3503
                   'bzrlib.tests.per_tree',
 
3504
                   'bzrlib.tests.per_pack_repository',
3389
3505
                   'bzrlib.tests.per_repository',
3390
3506
                   'bzrlib.tests.per_repository_chk',
3391
3507
                   'bzrlib.tests.per_repository_reference',
 
3508
                   'bzrlib.tests.per_versionedfile',
3392
3509
                   'bzrlib.tests.per_workingtree',
3393
3510
                   'bzrlib.tests.test__annotator',
3394
3511
                   'bzrlib.tests.test__chk_map',
3422
3539
                   'bzrlib.tests.test_config',
3423
3540
                   'bzrlib.tests.test_conflicts',
3424
3541
                   'bzrlib.tests.test_counted_lock',
 
3542
                   'bzrlib.tests.test_crash',
3425
3543
                   'bzrlib.tests.test_decorators',
3426
3544
                   'bzrlib.tests.test_delta',
3427
3545
                   'bzrlib.tests.test_debug',
3460
3578
                   'bzrlib.tests.test_knit',
3461
3579
                   'bzrlib.tests.test_lazy_import',
3462
3580
                   'bzrlib.tests.test_lazy_regex',
 
3581
                   'bzrlib.tests.test_lock',
3463
3582
                   'bzrlib.tests.test_lockable_files',
3464
3583
                   'bzrlib.tests.test_lockdir',
3465
3584
                   'bzrlib.tests.test_log',
3480
3599
                   'bzrlib.tests.test_osutils',
3481
3600
                   'bzrlib.tests.test_osutils_encodings',
3482
3601
                   'bzrlib.tests.test_pack',
3483
 
                   'bzrlib.tests.test_pack_repository',
3484
3602
                   'bzrlib.tests.test_patch',
3485
3603
                   'bzrlib.tests.test_patches',
3486
3604
                   'bzrlib.tests.test_permissions',
3540
3658
                   'bzrlib.tests.test_urlutils',
3541
3659
                   'bzrlib.tests.test_version',
3542
3660
                   'bzrlib.tests.test_version_info',
3543
 
                   'bzrlib.tests.test_versionedfile',
3544
3661
                   'bzrlib.tests.test_weave',
3545
3662
                   'bzrlib.tests.test_whitebox',
3546
3663
                   'bzrlib.tests.test_win32utils',
3555
3672
    if keep_only is not None:
3556
3673
        id_filter = TestIdList(keep_only)
3557
3674
    if starting_with:
3558
 
        starting_with = [test_prefix_alias_registry.resolve_alias(start)
3559
 
                         for start in starting_with]
3560
3675
        # We take precedence over keep_only because *at loading time* using
3561
3676
        # both options means we will load less tests for the same final result.
3562
3677
        def interesting_module(name):
3637
3752
            reload(sys)
3638
3753
            sys.setdefaultencoding(default_encoding)
3639
3754
 
3640
 
    if starting_with:
3641
 
        suite = filter_suite_by_id_startswith(suite, starting_with)
3642
 
 
3643
3755
    if keep_only is not None:
3644
3756
        # Now that the referred modules have loaded their tests, keep only the
3645
3757
        # requested ones.
3773
3885
    try:
3774
3886
        osutils.rmtree(dirname)
3775
3887
    except OSError, e:
3776
 
        if sys.platform == 'win32' and e.errno == errno.EACCES:
3777
 
            sys.stderr.write('Permission denied: '
3778
 
                             'unable to remove testing dir '
3779
 
                             '%s\n%s'
3780
 
                             % (os.path.basename(dirname), e))
3781
 
        else:
3782
 
            raise
 
3888
        # We don't want to fail here because some useful display will be lost
 
3889
        # otherwise. Polluting the tmp dir is bad, but not giving all the
 
3890
        # possible info to the test runner is even worse.
 
3891
        sys.stderr.write('Unable to remove testing dir %s\n%s'
 
3892
                         % (os.path.basename(dirname), e))
3783
3893
 
3784
3894
 
3785
3895
class Feature(object):