135
135
SUBUNIT_SEEK_SET = 0
136
136
SUBUNIT_SEEK_CUR = 1
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
139
143
class ExtendedTestResult(testtools.TextTestResult):
140
144
"""Accepts, reports and accumulates the results of running tests.
1455
1459
The file is removed as the test is torn down.
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)
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
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)
1675
log_contents = logfile.read()
1670
if self._log_file is not None:
1671
log_contents = self._log_file.getvalue()
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:
1685
max_close_attempts = 100
1686
first_close_error = None
1687
while close_attempts < max_close_attempts:
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
1697
if first_close_error is None:
1698
first_close_error = ioe
1703
if close_attempts > 1:
1705
'Unable to close log file on first attempt, '
1706
'will retry: %s\n' % (first_close_error,))
1707
if close_attempts == max_close_attempts:
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
1716
os.remove(self._log_file_name)
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))
1723
self._log_file_name = None
1724
1682
return log_contents
1726
return "No log file content and no log file name."
1684
return "No log file content."
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
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.
1951
1909
:returns: Popen object for the started process.
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")
1957
1915
if env_changes is None:
1958
1916
env_changes = {}