/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: Andrew Bennetts
  • Date: 2009-06-17 02:02:44 UTC
  • mfrom: (4449 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4452.
  • Revision ID: andrew.bennetts@canonical.com-20090617020244-50aantdf95aakvjx
Merge bzr.dev, resolving NEWS conflict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
    debug,
56
56
    errors,
57
57
    hooks,
 
58
    lock as _mod_lock,
58
59
    memorytree,
59
60
    osutils,
60
61
    progress,
727
728
    def finished(self):
728
729
        """See progress.ProgressBar.finished()."""
729
730
 
730
 
    def note(self, fmt_string, *args, **kwargs):
 
731
    def note(self, fmt_string, *args):
731
732
        """See progress.ProgressBar.note()."""
732
 
        self.stdout.write((fmt_string + "\n") % args)
 
733
        if args:
 
734
            fmt_string = fmt_string % args
 
735
        self.stdout.write(fmt_string + "\n")
733
736
 
734
737
    def progress_bar(self):
735
738
        return self
781
784
    _gather_lsprof_in_benchmarks = False
782
785
    attrs_to_keep = ('id', '_testMethodName', '_testMethodDoc',
783
786
                     '_log_contents', '_log_file_name', '_benchtime',
784
 
                     '_TestCase__testMethodName')
 
787
                     '_TestCase__testMethodName', '_TestCase__testMethodDoc',)
785
788
 
786
789
    def __init__(self, methodName='testMethod'):
787
790
        super(TestCase, self).__init__(methodName)
798
801
        self._benchcalls = []
799
802
        self._benchtime = None
800
803
        self._clear_hooks()
 
804
        # Track locks - needs to be called before _clear_debug_flags.
 
805
        self._track_locks()
801
806
        self._clear_debug_flags()
802
807
        TestCase._active_threads = threading.activeCount()
803
808
        self.addCleanup(self._check_leaked_threads)
850
855
        ui.ui_factory = ui.SilentUIFactory()
851
856
        self.addCleanup(_restore)
852
857
 
 
858
    def _check_locks(self):
 
859
        """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.
 
865
        # unhook:
 
866
        acquired_locks = [lock for action, lock in self._lock_actions
 
867
                          if action == 'acquired']
 
868
        released_locks = [lock for action, lock in self._lock_actions
 
869
                          if action == 'released']
 
870
        broken_locks = [lock for action, lock in self._lock_actions
 
871
                        if action == 'broken']
 
872
        # trivially, given the tests for lock acquistion and release, if we
 
873
        # have as many in each list, it should be ok. Some lock tests also
 
874
        # break some locks on purpose and should be taken into account by
 
875
        # considering that breaking a lock is just a dirty way of releasing it.
 
876
        if len(acquired_locks) != (len(released_locks) + len(broken_locks)):
 
877
            message = ('Different number of acquired and '
 
878
                       'released or broken locks. (%s, %s + %s)' %
 
879
                       (acquired_locks, released_locks, broken_locks))
 
880
            if not self._lock_check_thorough:
 
881
                # Rather than fail, just warn
 
882
                print "Broken test %s: %s" % (self, message)
 
883
                return
 
884
            self.fail(message)
 
885
 
 
886
    def _track_locks(self):
 
887
        """Track lock activity during tests."""
 
888
        self._lock_actions = []
 
889
        self._lock_check_thorough = 'lock' not in debug.debug_flags
 
890
        self.addCleanup(self._check_locks)
 
891
        _mod_lock.Lock.hooks.install_named_hook('lock_acquired',
 
892
                                                self._lock_acquired, None)
 
893
        _mod_lock.Lock.hooks.install_named_hook('lock_released',
 
894
                                                self._lock_released, None)
 
895
        _mod_lock.Lock.hooks.install_named_hook('lock_broken',
 
896
                                                self._lock_broken, None)
 
897
 
 
898
    def _lock_acquired(self, result):
 
899
        self._lock_actions.append(('acquired', result))
 
900
 
 
901
    def _lock_released(self, result):
 
902
        self._lock_actions.append(('released', result))
 
903
 
 
904
    def _lock_broken(self, result):
 
905
        self._lock_actions.append(('broken', result))
 
906
 
853
907
    def _ndiff_strings(self, a, b):
854
908
        """Return ndiff between two strings containing lines.
855
909
 
1331
1385
                else:
1332
1386
                    result.addSuccess(self)
1333
1387
                result.stopTest(self)
1334
 
                return
 
1388
                return result
1335
1389
        try:
1336
1390
            try:
1337
1391
                result.startTest(self)
1350
1404
                                "test setUp did not invoke "
1351
1405
                                "bzrlib.tests.TestCase's setUp")
1352
1406
                    except KeyboardInterrupt:
 
1407
                        self._runCleanups()
1353
1408
                        raise
1354
1409
                    except TestSkipped, e:
1355
1410
                        self._do_skip(result, e.args[0])
1356
1411
                        self.tearDown()
1357
 
                        return
 
1412
                        return result
1358
1413
                    except:
1359
1414
                        result.addError(self, sys.exc_info())
1360
 
                        return
 
1415
                        self._runCleanups()
 
1416
                        return result
1361
1417
 
1362
1418
                    ok = False
1363
1419
                    try:
1372
1428
                            reason = e.args[0]
1373
1429
                        self._do_skip(result, reason)
1374
1430
                    except KeyboardInterrupt:
 
1431
                        self._runCleanups()
1375
1432
                        raise
1376
1433
                    except:
1377
1434
                        result.addError(self, sys.exc_info())
1383
1440
                                "test tearDown did not invoke "
1384
1441
                                "bzrlib.tests.TestCase's tearDown")
1385
1442
                    except KeyboardInterrupt:
 
1443
                        self._runCleanups()
1386
1444
                        raise
1387
1445
                    except:
1388
1446
                        result.addError(self, sys.exc_info())
 
1447
                        self._runCleanups()
1389
1448
                        ok = False
1390
1449
                    if ok: result.addSuccess(self)
1391
1450
                finally:
1392
1451
                    result.stopTest(self)
1393
 
                return
 
1452
                return result
1394
1453
            except TestNotApplicable:
1395
1454
                # Not moved from the result [yet].
 
1455
                self._runCleanups()
1396
1456
                raise
1397
1457
            except KeyboardInterrupt:
 
1458
                self._runCleanups()
1398
1459
                raise
1399
1460
        finally:
1400
1461
            saved_attrs = {}
1401
 
            absent_attr = object()
1402
1462
            for attr_name in self.attrs_to_keep:
1403
 
                attr = getattr(self, attr_name, absent_attr)
1404
 
                if attr is not absent_attr:
1405
 
                    saved_attrs[attr_name] = attr
 
1463
                if attr_name in self.__dict__:
 
1464
                    saved_attrs[attr_name] = self.__dict__[attr_name]
1406
1465
            self.__dict__ = saved_attrs
1407
1466
 
1408
1467
    def tearDown(self):
1409
 
        self._bzr_test_tearDown_run = True
1410
1468
        self._runCleanups()
1411
1469
        self._log_contents = ''
 
1470
        self._bzr_test_tearDown_run = True
1412
1471
        unittest.TestCase.tearDown(self)
1413
1472
 
1414
1473
    def time(self, callable, *args, **kwargs):
1591
1650
            stdin=stdin,
1592
1651
            working_dir=working_dir,
1593
1652
            )
 
1653
        self.assertIsInstance(error_regexes, (list, tuple))
1594
1654
        for regex in error_regexes:
1595
1655
            self.assertContainsRe(err, regex)
1596
1656
        return out, err
2695
2755
 
2696
2756
 
2697
2757
def fork_decorator(suite):
2698
 
    concurrency = local_concurrency()
 
2758
    concurrency = osutils.local_concurrency()
2699
2759
    if concurrency == 1:
2700
2760
        return suite
2701
2761
    from testtools import ConcurrentTestSuite
2704
2764
 
2705
2765
 
2706
2766
def subprocess_decorator(suite):
2707
 
    concurrency = local_concurrency()
 
2767
    concurrency = osutils.local_concurrency()
2708
2768
    if concurrency == 1:
2709
2769
        return suite
2710
2770
    from testtools import ConcurrentTestSuite
2896
2956
    :return: An iterable of TestCase-like objects which can each have
2897
2957
        run(result) called on them to feed tests to result.
2898
2958
    """
2899
 
    concurrency = local_concurrency()
 
2959
    concurrency = osutils.local_concurrency()
2900
2960
    result = []
2901
2961
    from subunit import TestProtocolClient, ProtocolTestCase
2902
2962
    class TestInOtherProcess(ProtocolTestCase):
2945
3005
    :return: An iterable of TestCase-like objects which can each have
2946
3006
        run(result) called on them to feed tests to result.
2947
3007
    """
2948
 
    concurrency = local_concurrency()
 
3008
    concurrency = osutils.local_concurrency()
2949
3009
    result = []
2950
3010
    from subunit import TestProtocolClient, ProtocolTestCase
2951
3011
    class TestInSubprocess(ProtocolTestCase):
2991
3051
    return result
2992
3052
 
2993
3053
 
2994
 
def cpucount(content):
2995
 
    lines = content.splitlines()
2996
 
    prefix = 'processor'
2997
 
    for line in lines:
2998
 
        if line.startswith(prefix):
2999
 
            concurrency = int(line[line.find(':')+1:]) + 1
3000
 
    return concurrency
3001
 
 
3002
 
 
3003
 
def local_concurrency():
3004
 
    try:
3005
 
        content = file('/proc/cpuinfo', 'rb').read()
3006
 
        concurrency = cpucount(content)
3007
 
    except Exception, e:
3008
 
        concurrency = 1
3009
 
    return concurrency
3010
 
 
3011
 
 
3012
3054
class BZRTransformingResult(unittest.TestResult):
3013
3055
 
3014
3056
    def __init__(self, target):
3305
3347
                   'bzrlib.tests.test__chk_map',
3306
3348
                   'bzrlib.tests.test__dirstate_helpers',
3307
3349
                   'bzrlib.tests.test__groupcompress',
 
3350
                   'bzrlib.tests.test__known_graph',
 
3351
                   'bzrlib.tests.test__rio',
3308
3352
                   'bzrlib.tests.test__walkdirs_win32',
3309
3353
                   'bzrlib.tests.test_ancestry',
3310
3354
                   'bzrlib.tests.test_annotate',
3311
3355
                   'bzrlib.tests.test_api',
3312
3356
                   'bzrlib.tests.test_atomicfile',
3313
3357
                   'bzrlib.tests.test_bad_files',
 
3358
                   'bzrlib.tests.test_bencode',
3314
3359
                   'bzrlib.tests.test_bisect_multi',
3315
3360
                   'bzrlib.tests.test_branch',
3316
3361
                   'bzrlib.tests.test_branchbuilder',
3321
3366
                   'bzrlib.tests.test__chunks_to_lines',
3322
3367
                   'bzrlib.tests.test_cache_utf8',
3323
3368
                   'bzrlib.tests.test_chk_map',
 
3369
                   'bzrlib.tests.test_chk_serializer',
3324
3370
                   'bzrlib.tests.test_chunk_writer',
3325
3371
                   'bzrlib.tests.test_clean_tree',
3326
3372
                   'bzrlib.tests.test_commands',
3356
3402
                   'bzrlib.tests.test_help',
3357
3403
                   'bzrlib.tests.test_hooks',
3358
3404
                   'bzrlib.tests.test_http',
3359
 
                   'bzrlib.tests.test_http_implementations',
3360
3405
                   'bzrlib.tests.test_http_response',
3361
3406
                   'bzrlib.tests.test_https_ca_bundle',
3362
3407
                   'bzrlib.tests.test_identitymap',
3459
3504
                   'bzrlib.tests.test_xml',
3460
3505
                   'bzrlib.tests.tree_implementations',
3461
3506
                   'bzrlib.tests.workingtree_implementations',
3462
 
                   'bzrlib.util.tests.test_bencode',
3463
3507
                   ]
3464
3508
 
3465
3509
    loader = TestUtil.TestLoader()