~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Vincent Ladeuil
  • Date: 2011-05-26 20:30:53 UTC
  • mfrom: (5920 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5924.
  • Revision ID: v.ladeuil+lp@free.fr-20110526203053-hbjn6yuzwg03wnuv
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
377
377
        if isinstance(test, TestCase):
378
378
            test.addCleanup(self._check_leaked_threads, test)
379
379
 
 
380
    def stopTest(self, test):
 
381
        super(ExtendedTestResult, self).stopTest(test)
 
382
        # Manually break cycles, means touching various private things but hey
 
383
        getDetails = getattr(test, "getDetails", None)
 
384
        if getDetails is not None:
 
385
            getDetails().clear()
 
386
        type_equality_funcs = getattr(test, "_type_equality_funcs", None)
 
387
        if type_equality_funcs is not None:
 
388
            type_equality_funcs.clear()
 
389
        self._traceback_from_test = None
 
390
 
380
391
    def startTests(self):
381
392
        self.report_tests_starting()
382
393
        self._active_threads = threading.enumerate()
383
394
 
384
 
    def stopTest(self, test):
385
 
        self._traceback_from_test = None
386
 
 
387
395
    def _check_leaked_threads(self, test):
388
396
        """See if any threads have leaked since last call
389
397
 
449
457
        self.known_failure_count += 1
450
458
        self.report_known_failure(test, err)
451
459
 
 
460
    def addUnexpectedSuccess(self, test, details=None):
 
461
        """Tell result the test unexpectedly passed, counting as a failure
 
462
 
 
463
        When the minimum version of testtools required becomes 0.9.8 this
 
464
        can be updated to use the new handling there.
 
465
        """
 
466
        super(ExtendedTestResult, self).addFailure(test, details=details)
 
467
        self.failure_count += 1
 
468
        self.report_unexpected_success(test,
 
469
            "".join(details["reason"].iter_text()))
 
470
        if self.stop_early:
 
471
            self.stop()
 
472
 
452
473
    def addNotSupported(self, test, feature):
453
474
        """The test will not be run because of a missing feature.
454
475
        """
613
634
    def report_known_failure(self, test, err):
614
635
        pass
615
636
 
 
637
    def report_unexpected_success(self, test, reason):
 
638
        self.stream.write('FAIL: %s\n    %s: %s\n' % (
 
639
            self._test_description(test),
 
640
            "Unexpected success. Should have failed",
 
641
            reason,
 
642
            ))
 
643
 
616
644
    def report_skip(self, test, reason):
617
645
        pass
618
646
 
670
698
                % (self._testTimeString(test),
671
699
                   self._error_summary(err)))
672
700
 
 
701
    def report_unexpected_success(self, test, reason):
 
702
        self.stream.write(' FAIL %s\n%s: %s\n'
 
703
                % (self._testTimeString(test),
 
704
                   "Unexpected success. Should have failed",
 
705
                   reason))
 
706
 
673
707
    def report_success(self, test):
674
708
        self.stream.write('   OK %s\n' % self._testTimeString(test))
675
709
        for bench_called, stats in getattr(test, '_benchcalls', []):
937
971
        super(TestCase, self).setUp()
938
972
        for feature in getattr(self, '_test_needs_features', []):
939
973
            self.requireFeature(feature)
940
 
        self._log_contents = None
941
 
        self.addDetail("log", content.Content(content.ContentType("text",
942
 
            "plain", {"charset": "utf8"}),
943
 
            lambda:[self._get_log(keep_log_file=True)]))
944
974
        self._cleanEnvironment()
945
975
        self._silenceUI()
946
976
        self._startLogFile()
1290
1320
                length, len(obj_with_len), obj_with_len))
1291
1321
 
1292
1322
    def assertLogsError(self, exception_class, func, *args, **kwargs):
1293
 
        """Assert that func(*args, **kwargs) quietly logs a specific exception.
 
1323
        """Assert that `func(*args, **kwargs)` quietly logs a specific error.
1294
1324
        """
1295
1325
        captured = []
1296
1326
        orig_log_exception_quietly = trace.log_exception_quietly
1297
1327
        try:
1298
1328
            def capture():
1299
1329
                orig_log_exception_quietly()
1300
 
                captured.append(sys.exc_info())
 
1330
                captured.append(sys.exc_info()[1])
1301
1331
            trace.log_exception_quietly = capture
1302
1332
            func(*args, **kwargs)
1303
1333
        finally:
1304
1334
            trace.log_exception_quietly = orig_log_exception_quietly
1305
1335
        self.assertLength(1, captured)
1306
 
        err = captured[0][1]
 
1336
        err = captured[0]
1307
1337
        self.assertIsInstance(err, exception_class)
1308
1338
        return err
1309
1339
 
1604
1634
 
1605
1635
        The file is removed as the test is torn down.
1606
1636
        """
1607
 
        self._log_file = StringIO()
 
1637
        pseudo_log_file = StringIO()
 
1638
        def _get_log_contents_for_weird_testtools_api():
 
1639
            return [pseudo_log_file.getvalue().decode(
 
1640
                "utf-8", "replace").encode("utf-8")]          
 
1641
        self.addDetail("log", content.Content(content.ContentType("text",
 
1642
            "plain", {"charset": "utf8"}),
 
1643
            _get_log_contents_for_weird_testtools_api))
 
1644
        self._log_file = pseudo_log_file
1608
1645
        self._log_memento = trace.push_log_file(self._log_file)
1609
1646
        self.addCleanup(self._finishLogFile)
1610
1647
 
1617
1654
            # flush the log file, to get all content
1618
1655
            trace._trace_file.flush()
1619
1656
        trace.pop_log_file(self._log_memento)
1620
 
        # Cache the log result and delete the file on disk
1621
 
        self._get_log(False)
1622
1657
 
1623
1658
    def thisFailsStrictLockCheck(self):
1624
1659
        """It is known that this test would fail with -Dstrict_locks.
1673
1708
    def _restoreHooks(self):
1674
1709
        for klass, (name, hooks) in self._preserved_hooks.items():
1675
1710
            setattr(klass, name, hooks)
1676
 
        hooks._lazy_hooks = self._preserved_lazy_hooks
 
1711
        self._preserved_hooks.clear()
 
1712
        bzrlib.hooks._lazy_hooks = self._preserved_lazy_hooks
 
1713
        self._preserved_lazy_hooks.clear()
1677
1714
 
1678
1715
    def knownFailure(self, reason):
1679
1716
        """This test has failed for some known reason."""
1771
1808
    def log(self, *args):
1772
1809
        trace.mutter(*args)
1773
1810
 
1774
 
    def _get_log(self, keep_log_file=False):
1775
 
        """Internal helper to get the log from bzrlib.trace for this test.
1776
 
 
1777
 
        Please use self.getDetails, or self.get_log to access this in test case
1778
 
        code.
1779
 
 
1780
 
        :param keep_log_file: When True, if the log is still a file on disk
1781
 
            leave it as a file on disk. When False, if the log is still a file
1782
 
            on disk, the log file is deleted and the log preserved as
1783
 
            self._log_contents.
1784
 
        :return: A string containing the log.
1785
 
        """
1786
 
        if self._log_contents is not None:
1787
 
            try:
1788
 
                self._log_contents.decode('utf8')
1789
 
            except UnicodeDecodeError:
1790
 
                unicodestr = self._log_contents.decode('utf8', 'replace')
1791
 
                self._log_contents = unicodestr.encode('utf8')
1792
 
            return self._log_contents
1793
 
        if self._log_file is not None:
1794
 
            log_contents = self._log_file.getvalue()
1795
 
            try:
1796
 
                log_contents.decode('utf8')
1797
 
            except UnicodeDecodeError:
1798
 
                unicodestr = log_contents.decode('utf8', 'replace')
1799
 
                log_contents = unicodestr.encode('utf8')
1800
 
            if not keep_log_file:
1801
 
                self._log_file = None
1802
 
                # Permit multiple calls to get_log until we clean it up in
1803
 
                # finishLogFile
1804
 
                self._log_contents = log_contents
1805
 
            return log_contents
1806
 
        else:
1807
 
            return "No log file content."
1808
 
 
1809
1811
    def get_log(self):
1810
1812
        """Get a unicode string containing the log from bzrlib.trace.
1811
1813
 
3783
3785
        'bzrlib.tests.test_eol_filters',
3784
3786
        'bzrlib.tests.test_errors',
3785
3787
        'bzrlib.tests.test_export',
 
3788
        'bzrlib.tests.test_export_pot',
3786
3789
        'bzrlib.tests.test_extract',
3787
3790
        'bzrlib.tests.test_fetch',
3788
3791
        'bzrlib.tests.test_fixtures',
3899
3902
        'bzrlib.tests.test_upgrade',
3900
3903
        'bzrlib.tests.test_upgrade_stacked',
3901
3904
        'bzrlib.tests.test_urlutils',
 
3905
        'bzrlib.tests.test_utextwrap',
3902
3906
        'bzrlib.tests.test_version',
3903
3907
        'bzrlib.tests.test_version_info',
3904
3908
        'bzrlib.tests.test_versionedfile',