/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: Canonical.com Patch Queue Manager
  • Date: 2006-09-21 16:03:07 UTC
  • mfrom: (1927.3.5 throw_log_away)
  • Revision ID: pqm@pqm.ubuntu.com-20060921160307-6081481c9a444cce
(cfbolz) Throw away disk logfile if not used

Show diffs side-by-side

added added

removed removed

Lines of Context:
240
240
        if isinstance(err[1], TestSkipped):
241
241
            return self.addSkipped(test, err)    
242
242
        unittest.TestResult.addError(self, test, err)
 
243
        test.setKeepLogfile()
243
244
        self.extractBenchmarkTime(test)
244
245
        if self.showAll:
245
246
            self.stream.writeln("ERROR %s" % self._testTimeString())
256
257
 
257
258
    def addFailure(self, test, err):
258
259
        unittest.TestResult.addFailure(self, test, err)
 
260
        test.setKeepLogfile()
259
261
        self.extractBenchmarkTime(test)
260
262
        if self.showAll:
261
263
            self.stream.writeln(" FAIL %s" % self._testTimeString())
482
484
 
483
485
    _log_file_name = None
484
486
    _log_contents = ''
 
487
    _keep_log_file = False
485
488
    # record lsprof data when performing benchmark calls.
486
489
    _gather_lsprof_in_benchmarks = False
487
490
 
662
665
    def _finishLogFile(self):
663
666
        """Finished with the log file.
664
667
 
665
 
        Read contents into memory, close, and delete.
 
668
        Close the file and delete it, unless setKeepLogfile was called.
666
669
        """
667
670
        if self._log_file is None:
668
671
            return
669
672
        bzrlib.trace.disable_test_log(self._log_nonce)
670
 
        self._log_file.seek(0)
671
 
        self._log_contents = self._log_file.read()
672
673
        self._log_file.close()
673
 
        os.remove(self._log_file_name)
674
 
        self._log_file = self._log_file_name = None
 
674
        self._log_file = None
 
675
        if not self._keep_log_file:
 
676
            os.remove(self._log_file_name)
 
677
            self._log_file_name = None
 
678
 
 
679
    def setKeepLogfile(self):
 
680
        """Make the logfile not be deleted when _finishLogFile is called."""
 
681
        self._keep_log_file = True
675
682
 
676
683
    def addCleanup(self, callable):
677
684
        """Arrange to run a callable when this case is torn down.
745
752
    def log(self, *args):
746
753
        mutter(*args)
747
754
 
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:
 
755
    def _get_log(self, keep_log_file=False):
 
756
        """Return as a string the log for this test. If the file is still
 
757
        on disk and keep_log_file=False, delete the log file and store the
 
758
        content in self._log_contents."""
 
759
        # flush the log file, to get all content
 
760
        import bzrlib.trace
 
761
        bzrlib.trace._trace_file.flush()
 
762
        if self._log_contents:
753
763
            return self._log_contents
754
 
        # TODO: Delete the log after it's been read in
 
764
        if self._log_file_name is not None:
 
765
            logfile = open(self._log_file_name)
 
766
            try:
 
767
                log_contents = logfile.read()
 
768
            finally:
 
769
                logfile.close()
 
770
            if not keep_log_file:
 
771
                self._log_contents = log_contents
 
772
                os.remove(self._log_file_name)
 
773
            return log_contents
 
774
        else:
 
775
            return "DELETED log file to reduce memory footprint"
755
776
 
756
777
    def capture(self, cmd, retcode=0):
757
778
        """Shortcut that splits cmd into words, runs, and returns stdout"""