~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Andrea Corbellini
  • Date: 2010-09-04 15:34:10 UTC
  • mfrom: (5409 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5416.
  • Revision ID: corbellini.andrea@gmail.com-20100904153410-yybczvr4j1tmztdy
Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
SUBUNIT_SEEK_SET = 0
136
136
SUBUNIT_SEEK_CUR = 1
137
137
 
 
138
# These are intentionally brought into this namespace. That way plugins, etc
 
139
# can just "from bzrlib.tests import TestCase, TestLoader, etc"
 
140
TestSuite = TestUtil.TestSuite
 
141
TestLoader = TestUtil.TestLoader
138
142
 
139
143
class ExtendedTestResult(testtools.TextTestResult):
140
144
    """Accepts, reports and accumulates the results of running tests.
794
798
    _active_threads = None
795
799
    _leaking_threads_tests = 0
796
800
    _first_thread_leaker_id = None
797
 
    _log_file_name = None
 
801
    _log_file = None
798
802
    # record lsprof data when performing benchmark calls.
799
803
    _gather_lsprof_in_benchmarks = False
800
804
 
1454
1458
 
1455
1459
        The file is removed as the test is torn down.
1456
1460
        """
1457
 
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
1458
 
        self._log_file = os.fdopen(fileno, 'w+')
 
1461
        self._log_file = StringIO()
1459
1462
        self._log_memento = bzrlib.trace.push_log_file(self._log_file)
1460
 
        self._log_file_name = name
1461
1463
        self.addCleanup(self._finishLogFile)
1462
1464
 
1463
1465
    def _finishLogFile(self):
1665
1667
                unicodestr = self._log_contents.decode('utf8', 'replace')
1666
1668
                self._log_contents = unicodestr.encode('utf8')
1667
1669
            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()
 
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.
1944
1902
            variables. A value of None will unset the env variable.
1945
1903
            The values must be strings. The change will only occur in the
1946
1904
            child, so you don't need to fix the environment after running.
1947
 
        :param skip_if_plan_to_signal: raise TestSkipped when true and os.kill
1948
 
            is not available.
 
1905
        :param skip_if_plan_to_signal: raise TestSkipped when true and system
 
1906
            doesn't support signalling subprocesses.
1949
1907
        :param allow_plugins: If False (default) pass --no-plugins to bzr.
1950
1908
 
1951
1909
        :returns: Popen object for the started process.
1952
1910
        """
1953
1911
        if skip_if_plan_to_signal:
1954
 
            if not getattr(os, 'kill', None):
1955
 
                raise TestSkipped("os.kill not available.")
 
1912
            if os.name != "posix":
 
1913
                raise TestSkipped("Sending signals not supported")
1956
1914
 
1957
1915
        if env_changes is None:
1958
1916
            env_changes = {}