From 67a5d3c376131f646e2320e8ac8dd35d5ba899c7 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Thu, 3 Dec 2020 14:46:29 +0100 Subject: [PATCH 1/4] add per directory and yml-groupeable statistic reporter --- pycobertura/cli.py | 6 ++- pycobertura/reporters.py | 81 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/pycobertura/cli.py b/pycobertura/cli.py index dbfa035c..453f83a5 100644 --- a/pycobertura/cli.py +++ b/pycobertura/cli.py @@ -4,6 +4,7 @@ from pycobertura.reporters import ( HtmlReporter, TextReporter, + TextDirectorySummaryReporter, HtmlReporterDelta, TextReporterDelta, ) @@ -16,6 +17,7 @@ reporters = { "html": HtmlReporter, "text": TextReporter, + "textdir": TextDirectorySummaryReporter } @@ -46,7 +48,7 @@ def get_exit_code(differ, source): @pycobertura.command() @click.argument("cobertura_file") -@click.option("-f", "--format", default="text", type=click.Choice(list(reporters))) +@click.option("-f", "--format", default="textdir", type=click.Choice(list(reporters))) @click.option( "-o", "--output", @@ -180,6 +182,8 @@ def diff( source, ): """compare coverage of two Cobertura reports""" + print("diffsnaotehusanotehusaontehu") + # Assume that the source is located in the same directory as the provided # coverage files if no source directories are provided. if not source1: diff --git a/pycobertura/reporters.py b/pycobertura/reporters.py index 0eab2f3f..30516069 100644 --- a/pycobertura/reporters.py +++ b/pycobertura/reporters.py @@ -4,7 +4,8 @@ from pycobertura.utils import green, rangify, red from pycobertura.templates import filters from tabulate import tabulate - +from pathlib import Path +import yaml env = Environment(loader=PackageLoader("pycobertura", "templates")) env.filters["line_status"] = filters.line_status @@ -70,8 +71,6 @@ def format_row(self, row): return row def generate(self): - lines = self.get_report_lines() - formatted_lines = [] for row in lines: formatted_row = self.format_row(row) @@ -83,6 +82,82 @@ def generate(self): return report +class TextDirectorySummaryReporter(Reporter): + def format_row(self, last_path, dir_total_lines, dir_total_missed): + formatted_missed_lines = "" + percent = 0 + if (dir_total_lines != 0 and dir_total_missed != 0): + percent = (dir_total_lines - dir_total_missed) / dir_total_lines * 100 + + row = file_row_missed( + last_path, + dir_total_lines, + dir_total_missed, + percent, + formatted_missed_lines, + ) + + return row + + def generate(self): + fileset = {} + reverseset = {} + resultgroup = {} + lines = self.get_report_lines() + with open("/home/willi/src/pycobertura/pycobertura/groups.yml") as fileh: + fileset = yaml.load(fileh, Loader=yaml.Loader) + for key in fileset.keys(): + for directory in fileset[key].split(' '): + reverseset[directory] = key + dir_total_lines = 0 + dir_total_missed = 0 + + lines = self.get_report_lines() + last_path = Path() + formatted_lines = [] + for row in lines: + filename, total_lines, total_misses, line_rate, missed_lines = row + path = Path(filename).parents[0] + if last_path == Path(): + last_path = path + dir_total_lines = total_lines + dir_total_missed = total_misses + elif last_path != path: + formatted_row = self.format_row(last_path, dir_total_lines, dir_total_missed) + formatted_lines.append(formatted_row) + strpath = str(last_path) + if strpath in reverseset: + k = reverseset[strpath] + if k in resultgroup: + resultgroup[k]['dir_total_lines'] += dir_total_lines + resultgroup[k]['dir_total_missed'] += dir_total_missed + else: + resultgroup[k] = { + 'dir_total_lines': dir_total_lines, + 'dir_total_missed': dir_total_missed + } + else: + print('path not covered in config: ' + strpath) + last_path = path + dir_total_lines = 0 + dir_total_missed = 0 + else: + dir_total_lines += total_lines + dir_total_missed += total_misses + + report = tabulate( + formatted_lines, headers=["Directories", "Stmts", "Miss", "Cover", "Missing"] + ) + + for key in fileset.keys(): + res = resultgroup[key] + percent = 0 + if (res['dir_total_lines'] != 0 and res['dir_total_missed'] != 0): + percent = (res['dir_total_lines'] - res['dir_total_missed']) / res['dir_total_lines'] * 100 + + print(key + " - %+.2f%%" %(percent) + " %") + + return report class HtmlReporter(TextReporter): def __init__(self, *args, **kwargs): From 9de417e625aa27816b84bb588d373286d00a574a Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Thu, 3 Dec 2020 14:49:34 +0100 Subject: [PATCH 2/4] remove debug --- pycobertura/cli.py | 2 -- pycobertura/reporters.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pycobertura/cli.py b/pycobertura/cli.py index 453f83a5..79c1c638 100644 --- a/pycobertura/cli.py +++ b/pycobertura/cli.py @@ -182,8 +182,6 @@ def diff( source, ): """compare coverage of two Cobertura reports""" - print("diffsnaotehusanotehusaontehu") - # Assume that the source is located in the same directory as the provided # coverage files if no source directories are provided. if not source1: diff --git a/pycobertura/reporters.py b/pycobertura/reporters.py index 30516069..cfdbcaef 100644 --- a/pycobertura/reporters.py +++ b/pycobertura/reporters.py @@ -104,7 +104,7 @@ def generate(self): reverseset = {} resultgroup = {} lines = self.get_report_lines() - with open("/home/willi/src/pycobertura/pycobertura/groups.yml") as fileh: + with open("pycobertura/groups.yml") as fileh: fileset = yaml.load(fileh, Loader=yaml.Loader) for key in fileset.keys(): for directory in fileset[key].split(' '): From 3cc5b553bff31c5879f3d5252042682d1bd1a2c0 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Thu, 3 Dec 2020 14:52:13 +0100 Subject: [PATCH 3/4] don't edit unrelated code --- pycobertura/cli.py | 2 +- pycobertura/reporters.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pycobertura/cli.py b/pycobertura/cli.py index 79c1c638..25eb38e4 100644 --- a/pycobertura/cli.py +++ b/pycobertura/cli.py @@ -48,7 +48,7 @@ def get_exit_code(differ, source): @pycobertura.command() @click.argument("cobertura_file") -@click.option("-f", "--format", default="textdir", type=click.Choice(list(reporters))) +@click.option("-f", "--format", default="text", type=click.Choice(list(reporters))) @click.option( "-o", "--output", diff --git a/pycobertura/reporters.py b/pycobertura/reporters.py index cfdbcaef..2616541f 100644 --- a/pycobertura/reporters.py +++ b/pycobertura/reporters.py @@ -71,6 +71,8 @@ def format_row(self, row): return row def generate(self): + lines = self.get_report_lines() + formatted_lines = [] for row in lines: formatted_row = self.format_row(row) From 1de92ae8a4f6d2263b0340ec5971745c3ba8f80e Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Fri, 4 Dec 2020 11:05:16 +0100 Subject: [PATCH 4/4] fix next-file handling --- pycobertura/reporters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pycobertura/reporters.py b/pycobertura/reporters.py index 2616541f..89e08d83 100644 --- a/pycobertura/reporters.py +++ b/pycobertura/reporters.py @@ -141,8 +141,8 @@ def generate(self): else: print('path not covered in config: ' + strpath) last_path = path - dir_total_lines = 0 - dir_total_missed = 0 + dir_total_lines = total_lines + dir_total_missed = total_misses else: dir_total_lines += total_lines dir_total_missed += total_misses