~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Carl Friedrich Bolz
  • Date: 2006-08-17 11:10:57 UTC
  • mto: This revision was merged to the branch mainline in revision 2026.
  • Revision ID: cfbolz@gmx.de-20060817111057-108585811d3d24bb
Throw away on-disk logfile when possible.

Show diffs side-by-side

added added

removed removed

Lines of Context:
216
216
        if isinstance(err[1], TestSkipped):
217
217
            return self.addSkipped(test, err)    
218
218
        unittest.TestResult.addError(self, test, err)
 
219
        test.setKeepLogfile()
219
220
        self.extractBenchmarkTime(test)
220
221
        if self.showAll:
221
222
            self.stream.writeln("ERROR %s" % self._testTimeString())
232
233
 
233
234
    def addFailure(self, test, err):
234
235
        unittest.TestResult.addFailure(self, test, err)
 
236
        test.setKeepLogfile()
235
237
        self.extractBenchmarkTime(test)
236
238
        if self.showAll:
237
239
            self.stream.writeln(" FAIL %s" % self._testTimeString())
451
453
 
452
454
    _log_file_name = None
453
455
    _log_contents = ''
 
456
    _keep_log_file = False
454
457
    # record lsprof data when performing benchmark calls.
455
458
    _gather_lsprof_in_benchmarks = False
456
459
 
567
570
    def _finishLogFile(self):
568
571
        """Finished with the log file.
569
572
 
570
 
        Read contents into memory, close, and delete.
 
573
        Close the file and delete it, unless setKeepLogfile was called.
571
574
        """
572
575
        if self._log_file is None:
573
576
            return
574
577
        bzrlib.trace.disable_test_log(self._log_nonce)
575
 
        self._log_file.seek(0)
576
 
        self._log_contents = self._log_file.read()
577
578
        self._log_file.close()
578
 
        os.remove(self._log_file_name)
579
 
        self._log_file = self._log_file_name = None
 
579
        self._log_file = None
 
580
        if not self._keep_log_file:
 
581
            os.remove(self._log_file_name)
 
582
            self._log_file_name = None
 
583
 
 
584
    def setKeepLogfile(self):
 
585
        """Make the logfile not be deleted when _finishLogFile is called."""
 
586
        self._keep_log_file = True
580
587
 
581
588
    def addCleanup(self, callable):
582
589
        """Arrange to run a callable when this case is torn down.
663
670
    def log(self, *args):
664
671
        mutter(*args)
665
672
 
666
 
    def _get_log(self):
 
673
    def _get_log(self, keep_log_file=False):
667
674
        """Return as a string the log for this test"""
668
 
        if self._log_file_name:
669
 
            return open(self._log_file_name).read()
670
 
        else:
 
675
        # flush the log file, to get all content
 
676
        import bzrlib.trace
 
677
        bzrlib.trace._trace_file.flush()
 
678
        if self._log_contents:
671
679
            return self._log_contents
672
 
        # TODO: Delete the log after it's been read in
 
680
        if self._log_file_name is not None:
 
681
            logfile = open(self._log_file_name)
 
682
            try:
 
683
                log_contents = logfile.read()
 
684
            finally:
 
685
                logfile.close()
 
686
            if not keep_log_file:
 
687
                self._log_contents = log_contents
 
688
                os.remove(self._log_file_name)
 
689
            return log_contents
 
690
        else:
 
691
            return "DELETED log file to reduce memory footprint"
673
692
 
674
693
    def capture(self, cmd, retcode=0):
675
694
        """Shortcut that splits cmd into words, runs, and returns stdout"""