/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: Andrew Bennetts
  • Date: 2010-09-02 00:49:06 UTC
  • mto: This revision was merged to the branch mainline in revision 5408.
  • Revision ID: andrew.bennetts@canonical.com-20100902004906-yezw3oexov6qzsno
Use StringIO rather than real files on disk for log files in tests.

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):
1669
1667
        if bzrlib.trace._trace_file:
1670
1668
            # flush the log file, to get all content
1671
1669
            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()
 
1670
        if self._log_file is not None:
 
1671
            log_contents = self._log_file.getvalue()
1678
1672
            try:
1679
1673
                log_contents.decode('utf8')
1680
1674
            except UnicodeDecodeError:
1681
1675
                unicodestr = log_contents.decode('utf8', 'replace')
1682
1676
                log_contents = unicodestr.encode('utf8')
1683
1677
            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
1678
                self._log_file = None
1712
1679
                # Permit multiple calls to get_log until we clean it up in
1713
1680
                # finishLogFile
1714
1681
                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
1682
            return log_contents
1725
1683
        else:
1726
 
            return "No log file content and no log file name."
 
1684
            return "No log file content."
1727
1685
 
1728
1686
    def get_log(self):
1729
1687
        """Get a unicode string containing the log from bzrlib.trace.