Example #1
0
def render_report(output_path, reporter, morfs):
    """Run a report generator, managing the output file.

    This function ensures the output file is ready to be written to. Then writes
    the report to it. Then closes the file and cleans up.

    """
    file_to_close = None
    delete_file = False

    if output_path == "-":
        outfile = sys.stdout
    else:
        # Ensure that the output directory is created; done here
        # because this report pre-opens the output file.
        # HTMLReport does this using the Report plumbing because
        # its task is more complex, being multiple files.
        ensure_dir_for_file(output_path)
        open_kwargs = {}
        if env.PY3:
            open_kwargs["encoding"] = "utf8"
        outfile = open(output_path, "w", **open_kwargs)
        file_to_close = outfile

    try:
        return reporter.report(morfs, outfile=outfile)
    except CoverageException:
        delete_file = True
        raise
    finally:
        if file_to_close:
            file_to_close.close()
            if delete_file:
                file_be_gone(
                    output_path)  # pragma: part covered (doesn't return)
Example #2
0
def render_report(output_path, reporter, morfs):
    """Run the provided reporter ensuring any required setup and cleanup is done

    At a high level this method ensures the output file is ready to be written to. Then writes the
    report to it. Then closes the file and deletes any garbage created if necessary.
    """
    file_to_close = None
    delete_file = False
    if output_path:
        if output_path == '-':
            outfile = sys.stdout
        else:
            # Ensure that the output directory is created; done here
            # because this report pre-opens the output file.
            # HTMLReport does this using the Report plumbing because
            # its task is more complex, being multiple files.
            ensure_dir_for_file(output_path)
            open_kwargs = {}
            if env.PY3:
                open_kwargs['encoding'] = 'utf8'
            outfile = open(output_path, "w", **open_kwargs)
            file_to_close = outfile
    try:
        return reporter.report(morfs, outfile=outfile)
    except CoverageException:
        delete_file = True
        raise
    finally:
        if file_to_close:
            file_to_close.close()
            if delete_file:
                file_be_gone(output_path)
Example #3
0
    def xml_report(
        self,
        morfs=None,
        outfile=None,
        ignore_errors=None,
        omit=None,
        include=None,
        contexts=None,
    ):
        """Generate an XML report of coverage results.

        The report is compatible with Cobertura reports.

        Each module in `morfs` is included in the report.  `outfile` is the
        path to write the file to, "-" will write to stdout.

        See :meth:`report` for other arguments.

        Returns a float, the total percentage covered.

        """
        self.config.from_args(
            ignore_errors=ignore_errors,
            report_omit=omit,
            report_include=include,
            xml_output=outfile,
            report_contexts=contexts,
        )
        file_to_close = None
        delete_file = False
        if self.config.xml_output:
            if self.config.xml_output == '-':
                outfile = sys.stdout
            else:
                # Ensure that the output directory is created; done here
                # because this report pre-opens the output file.
                # HTMLReport does this using the Report plumbing because
                # its task is more complex, being multiple files.
                ensure_dir_for_file(self.config.xml_output)
                open_kwargs = {}
                if env.PY3:
                    open_kwargs['encoding'] = 'utf8'
                outfile = open(self.config.xml_output, "w", **open_kwargs)
                file_to_close = outfile
        try:
            reporter = XmlReporter(self)
            return reporter.report(morfs, outfile=outfile)
        except CoverageException:
            delete_file = True
            raise
        finally:
            if file_to_close:
                file_to_close.close()
                if delete_file:
                    file_be_gone(self.config.xml_output)
Example #4
0
 def _init_data(self, suffix):
     """Create a data file if we don't have one yet."""
     if self._data is None:
         # Create the data file.  We do this at construction time so that the
         # data file will be written into the directory where the process
         # started rather than wherever the process eventually chdir'd to.
         ensure_dir_for_file(self.config.data_file)
         self._data = CoverageData(
             basename=self.config.data_file,
             suffix=suffix,
             warn=self._warn,
             debug=self._debug,
         )
Example #5
0
 def _init_data(self, suffix):
     """Create a data file if we don't have one yet."""
     if self._data is None:
         # Create the data file.  We do this at construction time so that the
         # data file will be written into the directory where the process
         # started rather than wherever the process eventually chdir'd to.
         ensure_dir_for_file(self.config.data_file)
         self._data = CoverageData(
             basename=self.config.data_file,
             suffix=suffix,
             warn=self._warn,
             debug=self._debug,
         )
Example #6
0
    def xml_report(
        self, morfs=None, outfile=None, ignore_errors=None,
        omit=None, include=None,
    ):
        """Generate an XML report of coverage results.

        The report is compatible with Cobertura reports.

        Each module in `morfs` is included in the report.  `outfile` is the
        path to write the file to, "-" will write to stdout.

        See :meth:`report` for other arguments.

        Returns a float, the total percentage covered.

        """
        self.config.from_args(
            ignore_errors=ignore_errors, report_omit=omit, report_include=include,
            xml_output=outfile,
            )
        file_to_close = None
        delete_file = False
        if self.config.xml_output:
            if self.config.xml_output == '-':
                outfile = sys.stdout
            else:
                # Ensure that the output directory is created; done here
                # because this report pre-opens the output file.
                # HTMLReport does this using the Report plumbing because
                # its task is more complex, being multiple files.
                ensure_dir_for_file(self.config.xml_output)
                open_kwargs = {}
                if env.PY3:
                    open_kwargs['encoding'] = 'utf8'
                outfile = open(self.config.xml_output, "w", **open_kwargs)
                file_to_close = outfile
        try:
            reporter = XmlReporter(self, self.config)
            return reporter.report(morfs, outfile=outfile)
        except CoverageException:
            delete_file = True
            raise
        finally:
            if file_to_close:
                file_to_close.close()
                if delete_file:
                    file_be_gone(self.config.xml_output)