Ejemplo n.º 1
0
def generate_coverage_report(coverage_xml,
                             compare_branch,
                             html_report=None,
                             css_file=None,
                             ignore_staged=False,
                             ignore_unstaged=False,
                             exclude=None,
                             src_roots=None,
                             diff_range_notation=None):
    """
    Generate the diff coverage report, using kwargs from `parse_args()`.
    """
    diff = GitDiffReporter(compare_branch,
                           git_diff=GitDiffTool(diff_range_notation),
                           ignore_staged=ignore_staged,
                           ignore_unstaged=ignore_unstaged,
                           exclude=exclude)

    xml_roots = [etree.parse(xml_root) for xml_root in coverage_xml]
    coverage = XmlCoverageReporter(xml_roots, src_roots)

    # Build a report generator
    if html_report is not None:
        css_url = css_file
        if css_url is not None:
            css_url = os.path.relpath(css_file, os.path.dirname(html_report))
        reporter = HtmlReportGenerator(coverage, diff, css_url=css_url)
        with open(html_report, "wb") as output_file:
            reporter.generate_report(output_file)
        if css_file is not None:
            with open(css_file, "wb") as output_file:
                reporter.generate_css(output_file)

    reporter = StringReportGenerator(coverage, diff)
    output_file = sys.stdout if six.PY2 else sys.stdout.buffer

    # Generate the report
    reporter.generate_report(output_file)
    return reporter.total_num_lines(), reporter.total_percent_covered()