~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

(spiv) Include log details from start_bzr_subprocess in test failures.
 (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
988
988
        # settled on or a the FIXME associated with _get_expand_default_value
989
989
        # is addressed -- vila 20110219
990
990
        self.overrideAttr(config, '_expand_default_value', None)
 
991
        self._log_files = set()
991
992
 
992
993
    def debug(self):
993
994
        # debug a frame up.
1638
1639
        pseudo_log_file = StringIO()
1639
1640
        def _get_log_contents_for_weird_testtools_api():
1640
1641
            return [pseudo_log_file.getvalue().decode(
1641
 
                "utf-8", "replace").encode("utf-8")]          
 
1642
                "utf-8", "replace").encode("utf-8")]
1642
1643
        self.addDetail("log", content.Content(content.ContentType("text",
1643
1644
            "plain", {"charset": "utf8"}),
1644
1645
            _get_log_contents_for_weird_testtools_api))
2066
2067
            # so we will avoid using it on all platforms, just to
2067
2068
            # make sure the code path is used, and we don't break on win32
2068
2069
            cleanup_environment()
 
2070
            # Include the subprocess's log file in the test details, in case
 
2071
            # the test fails due to an error in the subprocess.
 
2072
            self._add_subprocess_log(trace._get_bzr_log_filename())
2069
2073
            command = [sys.executable]
2070
2074
            # frozen executables don't need the path to bzr
2071
2075
            if getattr(sys, "frozen", None) is None:
2083
2087
 
2084
2088
        return process
2085
2089
 
 
2090
    def _add_subprocess_log(self, log_file_path):
 
2091
        if len(self._log_files) == 0:
 
2092
            # Register an addCleanup func.  We do this on the first call to
 
2093
            # _add_subprocess_log rather than in TestCase.setUp so that this
 
2094
            # addCleanup is registered after any cleanups for tempdirs that
 
2095
            # subclasses might create, which will probably remove the log file
 
2096
            # we want to read.
 
2097
            self.addCleanup(self._subprocess_log_cleanup)
 
2098
        # self._log_files is a set, so if a log file is reused we won't grab it
 
2099
        # twice.
 
2100
        self._log_files.add(log_file_path)
 
2101
 
 
2102
    def _subprocess_log_cleanup(self):
 
2103
        for count, log_file_path in enumerate(self._log_files):
 
2104
            # We use buffer_now=True to avoid holding the file open beyond
 
2105
            # the life of this function, which might interfere with e.g.
 
2106
            # cleaning tempdirs on Windows.
 
2107
            # XXX: Testtools 0.9.5 doesn't have the content_from_file helper
 
2108
            #detail_content = content.content_from_file(
 
2109
            #    log_file_path, buffer_now=True)
 
2110
            with open(log_file_path, 'rb') as log_file:
 
2111
                log_file_bytes = log_file.read()
 
2112
            detail_content = content.Content(content.ContentType("text",
 
2113
                "plain", {"charset": "utf8"}), lambda: [log_file_bytes])
 
2114
            self.addDetail("start_bzr_subprocess-log-%d" % (count,),
 
2115
                detail_content)
 
2116
 
2086
2117
    def _popen(self, *args, **kwargs):
2087
2118
        """Place a call to Popen.
2088
2119