~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
import bzrlib.commands
50
50
import bzrlib.bundle.serializer
51
51
import bzrlib.errors as errors
 
52
import bzrlib.export
52
53
import bzrlib.inventory
53
54
import bzrlib.iterablefile
54
55
import bzrlib.lockdir
89
90
                      bzrlib.bundle.serializer,
90
91
                      bzrlib.commands,
91
92
                      bzrlib.errors,
 
93
                      bzrlib.export,
92
94
                      bzrlib.inventory,
93
95
                      bzrlib.iterablefile,
94
96
                      bzrlib.lockdir,
240
242
        if isinstance(err[1], TestSkipped):
241
243
            return self.addSkipped(test, err)    
242
244
        unittest.TestResult.addError(self, test, err)
 
245
        # We can only do this if we have one of our TestCases, not if
 
246
        # we have a doctest.
 
247
        setKeepLogfile = getattr(test, 'setKeepLogfile', None)
 
248
        if setKeepLogfile is not None:
 
249
            setKeepLogfile()
243
250
        self.extractBenchmarkTime(test)
244
251
        if self.showAll:
245
252
            self.stream.writeln("ERROR %s" % self._testTimeString())
256
263
 
257
264
    def addFailure(self, test, err):
258
265
        unittest.TestResult.addFailure(self, test, err)
 
266
        # We can only do this if we have one of our TestCases, not if
 
267
        # we have a doctest.
 
268
        setKeepLogfile = getattr(test, 'setKeepLogfile', None)
 
269
        if setKeepLogfile is not None:
 
270
            setKeepLogfile()
259
271
        self.extractBenchmarkTime(test)
260
272
        if self.showAll:
261
273
            self.stream.writeln(" FAIL %s" % self._testTimeString())
482
494
 
483
495
    _log_file_name = None
484
496
    _log_contents = ''
 
497
    _keep_log_file = False
485
498
    # record lsprof data when performing benchmark calls.
486
499
    _gather_lsprof_in_benchmarks = False
487
500
 
662
675
    def _finishLogFile(self):
663
676
        """Finished with the log file.
664
677
 
665
 
        Read contents into memory, close, and delete.
 
678
        Close the file and delete it, unless setKeepLogfile was called.
666
679
        """
667
680
        if self._log_file is None:
668
681
            return
669
682
        bzrlib.trace.disable_test_log(self._log_nonce)
670
 
        self._log_file.seek(0)
671
 
        self._log_contents = self._log_file.read()
672
683
        self._log_file.close()
673
 
        os.remove(self._log_file_name)
674
 
        self._log_file = self._log_file_name = None
 
684
        self._log_file = None
 
685
        if not self._keep_log_file:
 
686
            os.remove(self._log_file_name)
 
687
            self._log_file_name = None
 
688
 
 
689
    def setKeepLogfile(self):
 
690
        """Make the logfile not be deleted when _finishLogFile is called."""
 
691
        self._keep_log_file = True
675
692
 
676
693
    def addCleanup(self, callable):
677
694
        """Arrange to run a callable when this case is torn down.
745
762
    def log(self, *args):
746
763
        mutter(*args)
747
764
 
748
 
    def _get_log(self):
749
 
        """Return as a string the log for this test"""
750
 
        if self._log_file_name:
751
 
            return open(self._log_file_name).read()
752
 
        else:
 
765
    def _get_log(self, keep_log_file=False):
 
766
        """Return as a string the log for this test. If the file is still
 
767
        on disk and keep_log_file=False, delete the log file and store the
 
768
        content in self._log_contents."""
 
769
        # flush the log file, to get all content
 
770
        import bzrlib.trace
 
771
        bzrlib.trace._trace_file.flush()
 
772
        if self._log_contents:
753
773
            return self._log_contents
754
 
        # TODO: Delete the log after it's been read in
 
774
        if self._log_file_name is not None:
 
775
            logfile = open(self._log_file_name)
 
776
            try:
 
777
                log_contents = logfile.read()
 
778
            finally:
 
779
                logfile.close()
 
780
            if not keep_log_file:
 
781
                self._log_contents = log_contents
 
782
                os.remove(self._log_file_name)
 
783
            return log_contents
 
784
        else:
 
785
            return "DELETED log file to reduce memory footprint"
755
786
 
756
787
    def capture(self, cmd, retcode=0):
757
788
        """Shortcut that splits cmd into words, runs, and returns stdout"""
932
963
            for env_var, value in old_env.iteritems():
933
964
                osutils.set_or_unset_env(env_var, value)
934
965
 
935
 
        bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
936
 
        if not os.path.isfile(bzr_path):
937
 
            # We are probably installed. Assume sys.argv is the right file
938
 
            bzr_path = sys.argv[0]
 
966
        bzr_path = self.get_bzr_path()
939
967
 
940
968
        try:
941
969
            # win32 subprocess doesn't support preexec_fn
948
976
            restore_environment()
949
977
        return process
950
978
 
 
979
    def get_bzr_path(self):
 
980
        """Return the path of the 'bzr' executable for this test suite."""
 
981
        bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
 
982
        if not os.path.isfile(bzr_path):
 
983
            # We are probably installed. Assume sys.argv is the right file
 
984
            bzr_path = sys.argv[0]
 
985
        return bzr_path
 
986
 
951
987
    def finish_bzr_subprocess(self, process, retcode=0, send_signal=None,
952
988
                              universal_newlines=False, process_args=None):
953
989
        """Finish the execution of process.
1542
1578
                   'bzrlib.tests.test_urlutils',
1543
1579
                   'bzrlib.tests.test_versionedfile',
1544
1580
                   'bzrlib.tests.test_version',
 
1581
                   'bzrlib.tests.test_version_info',
1545
1582
                   'bzrlib.tests.test_weave',
1546
1583
                   'bzrlib.tests.test_whitebox',
1547
1584
                   'bzrlib.tests.test_workingtree',