~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-03 01:32:46 UTC
  • mfrom: (5404.1.3 test-logs-not-on-disk)
  • Revision ID: pqm@pqm.ubuntu.com-20100903013246-mydkx60um8b2trfq
(jameinel) Use StringIO rather than real files on disk for log files in
 tests. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
794
794
    _active_threads = None
795
795
    _leaking_threads_tests = 0
796
796
    _first_thread_leaker_id = None
797
 
    _log_file_name = None
 
797
    _log_file = None
798
798
    # record lsprof data when performing benchmark calls.
799
799
    _gather_lsprof_in_benchmarks = False
800
800
 
1454
1454
 
1455
1455
        The file is removed as the test is torn down.
1456
1456
        """
1457
 
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
1458
 
        self._log_file = os.fdopen(fileno, 'w+')
 
1457
        self._log_file = StringIO()
1459
1458
        self._log_memento = bzrlib.trace.push_log_file(self._log_file)
1460
 
        self._log_file_name = name
1461
1459
        self.addCleanup(self._finishLogFile)
1462
1460
 
1463
1461
    def _finishLogFile(self):
1665
1663
                unicodestr = self._log_contents.decode('utf8', 'replace')
1666
1664
                self._log_contents = unicodestr.encode('utf8')
1667
1665
            return self._log_contents
1668
 
        import bzrlib.trace
1669
 
        if bzrlib.trace._trace_file:
1670
 
            # flush the log file, to get all content
1671
 
            bzrlib.trace._trace_file.flush()
1672
 
        if self._log_file_name is not None:
1673
 
            logfile = open(self._log_file_name)
1674
 
            try:
1675
 
                log_contents = logfile.read()
1676
 
            finally:
1677
 
                logfile.close()
 
1666
        if self._log_file is not None:
 
1667
            log_contents = self._log_file.getvalue()
1678
1668
            try:
1679
1669
                log_contents.decode('utf8')
1680
1670
            except UnicodeDecodeError:
1681
1671
                unicodestr = log_contents.decode('utf8', 'replace')
1682
1672
                log_contents = unicodestr.encode('utf8')
1683
1673
            if not keep_log_file:
1684
 
                close_attempts = 0
1685
 
                max_close_attempts = 100
1686
 
                first_close_error = None
1687
 
                while close_attempts < max_close_attempts:
1688
 
                    close_attempts += 1
1689
 
                    try:
1690
 
                        self._log_file.close()
1691
 
                    except IOError, ioe:
1692
 
                        if ioe.errno is None:
1693
 
                            # No errno implies 'close() called during
1694
 
                            # concurrent operation on the same file object', so
1695
 
                            # retry.  Probably a thread is trying to write to
1696
 
                            # the log file.
1697
 
                            if first_close_error is None:
1698
 
                                first_close_error = ioe
1699
 
                            continue
1700
 
                        raise
1701
 
                    else:
1702
 
                        break
1703
 
                if close_attempts > 1:
1704
 
                    sys.stderr.write(
1705
 
                        'Unable to close log file on first attempt, '
1706
 
                        'will retry: %s\n' % (first_close_error,))
1707
 
                    if close_attempts == max_close_attempts:
1708
 
                        sys.stderr.write(
1709
 
                            'Unable to close log file after %d attempts.\n'
1710
 
                            % (max_close_attempts,))
1711
1674
                self._log_file = None
1712
1675
                # Permit multiple calls to get_log until we clean it up in
1713
1676
                # finishLogFile
1714
1677
                self._log_contents = log_contents
1715
 
                try:
1716
 
                    os.remove(self._log_file_name)
1717
 
                except OSError, e:
1718
 
                    if sys.platform == 'win32' and e.errno == errno.EACCES:
1719
 
                        sys.stderr.write(('Unable to delete log file '
1720
 
                                             ' %r\n' % self._log_file_name))
1721
 
                    else:
1722
 
                        raise
1723
 
                self._log_file_name = None
1724
1678
            return log_contents
1725
1679
        else:
1726
 
            return "No log file content and no log file name."
 
1680
            return "No log file content."
1727
1681
 
1728
1682
    def get_log(self):
1729
1683
        """Get a unicode string containing the log from bzrlib.trace.