From 9e6abf4dd9ad5bb159559d75dbbec89bcfce979f Mon Sep 17 00:00:00 2001 From: mignot Date: Wed, 25 Nov 2026 16:08:25 +0100 Subject: [PATCH 1/2] fix(coverage): resolve report paths against tracked files and reject non-Cobertura XML Refs #80 Co-Authored-By: Claude Sonnet 5.5 --- .github/tests/coverage.sh | 9 ++++- .github/workflows/coverage.yml | 72 ++++++++++++++++++++-------------- CHANGELOG.md | 1 + README.md | 2 + 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/.github/tests/coverage.sh b/.github/tests/coverage.sh index 78e0d9c..c177493 100755 --- a/.github/tests/coverage.sh +++ b/.github/tests/coverage.sh @@ -99,9 +99,16 @@ scenario -- 'a*b.py'; multi_report 'a*b.py=1'; run_chec scenario -- ; : > empty.py; git add . && git commit -q -m empty; multi_report other.py=1; run_check pass 'new empty file is not flagged' scenario -- new.md; multi_report other.py=1; run_check pass 'new non-source file is not flagged' scenario old.py -- old.py; multi_report other.py=1; run_check pass 'modified file absent from the reports is skipped' -scenario util.py -- util.py; multi_report api/util.py=1 web/util.py=1; run_check fail 'ambiguous suffix match' +scenario util.py -- util.py; multi_report api/util.py=1 web/util.py=1; run_check pass 'longer report paths collapse onto the tracked file' +scenario pkg/a/util.py pkg/b/util.py -- pkg/a/util.py; multi_report util.py=1; run_check fail 'ambiguous suffix match against tracked files' +grep -q 'matches several tracked files' "$work/out" || { echo 'FAIL: ambiguous suffix message' >&2; cat "$work/out" >&2; exit 1; } +scenario pkg/a/util.py -- pkg/a/util.py; multi_report util.py=0; run_check fail 'unique suffix match resolves the changed file' scenario pkg/web/util.py -- pkg/web/util.py; multi_report util.py=0 web/util.py=1; run_check pass 'longest suffix match wins' scenario web/util.py -- web/util.py; multi_report util.py=0 web/util.py=1; run_check pass 'exact match wins over a suffix match' scenario 'dir é/a b.py' -- 'dir é/a b.py'; multi_report 'dir é/a b.py=0'; run_check fail 'uncovered changes in a path with spaces and non-ASCII' scenario 'dir é/a b.py' -- 'dir é/a b.py'; multi_report 'dir é/a b.py=1'; run_check pass 'covered changes in a path with spaces and non-ASCII' +cobertura 10 10 yes; printf '' > reports/junit.xml; run_check fail 'non-Cobertura XML in the reports' +grep -q 'not a Cobertura report' "$work/out" || { echo 'FAIL: non-Cobertura message' >&2; cat "$work/out" >&2; exit 1; } +cobertura 10 10 yes; printf ' reports/bad.xml; run_check fail 'invalid XML in the reports' +grep -q 'not valid XML' "$work/out" || { echo 'FAIL: invalid XML message' >&2; cat "$work/out" >&2; exit 1; } echo 'all coverage tests passed' diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 0024b09..0e99b8b 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -70,10 +70,40 @@ jobs: workspace = os.getcwd().rstrip('/') + '/' lines, branches = {}, {} # file -> {line: hits}, file -> {(line, branch): taken} + def git(*args): + # -z and quotePath=false keep paths with spaces or non-ASCII characters verbatim. + return subprocess.run(['git', '--literal-pathspecs', '-c', 'core.quotePath=false', *args], capture_output=True, + text=True, encoding='utf-8', errors='replace', check=True).stdout + + tracked = set(git('ls-files', '-z').split('\0')) - {''} + tracked_by_name = {} + for t in tracked: + tracked_by_name.setdefault(t.rsplit('/', 1)[-1], []).append(t) + def norm(path): path = path.replace('\\', '/') return path[len(workspace):] if path.startswith(workspace) else path.removeprefix('./') + def canonical(path, report, sources=()): + # Map a report path onto the tracked file it names, so every later lookup is an exact match. + p = norm(path) + if p in tracked: + return p + for source in sources: + c = norm(os.path.join(source, path)) + if c in tracked: + return c + same_name = tracked_by_name.get(p.rsplit('/', 1)[-1], []) + shorter = [t for t in same_name if t.endswith('/' + p)] + if len(shorter) == 1: + return shorter[0] + if shorter: + errors.append(f'{report}: report path {p} matches several tracked files ({", ".join(sorted(shorter)[:5])}); ' + 'emit repository-relative paths (coverage.py relative_files = true)') + return None + longer = [t for t in same_name if p.endswith('/' + t)] + return max(longer, key=len) if longer else p + def add_line(f, n, hits): d = lines.setdefault(f, {}) d[n] = max(d.get(n, 0), hits) @@ -82,17 +112,12 @@ jobs: d = branches.setdefault(f, {}) d[key] = d.get(key, False) or taken - def read_cobertura(path): - root = ET.parse(path).getroot() + def read_cobertura(path, root): sources = [s.text.strip() for s in root.iter('source') if s.text and s.text.strip()] for cls in root.iter('class'): - name = cls.get('filename') - f = norm(name) - for source in sources: - candidate = norm(os.path.join(source, name)) - if os.path.isfile(candidate): - f = candidate - break + f = canonical(cls.get('filename'), path, sources) + if f is None: + continue for ln in cls.iter('line'): n, hits = int(ln.get('number')), int(ln.get('hits')) add_line(f, n, hits) @@ -106,7 +131,7 @@ jobs: f = None for raw in Path(path).read_text().splitlines(): if raw.startswith('SF:'): - f = norm(raw[3:]) + f = canonical(raw[3:], path) elif raw.startswith('DA:') and f: n, hits = raw[3:].split(',')[:2] add_line(f, int(n), int(hits)) @@ -117,7 +142,13 @@ jobs: found = 0 for path in sorted(Path(os.environ['REPORTS_DIR']).rglob('*')): if path.suffix == '.xml': - read_cobertura(path) + try: + root = ET.parse(path).getroot() + except ET.ParseError as error: + sys.exit(f'coverage: {path}: not valid XML ({error})') + if root.tag != 'coverage': + sys.exit(f'coverage: {path}: not a Cobertura report (root <{root.tag}>); upload only coverage reports in the coverage-* artifacts') + read_cobertura(path, root) elif path.name == 'lcov.info' or path.suffix == '.lcov': read_lcov(path) else: @@ -143,23 +174,8 @@ jobs: base = os.environ.get('BASE_REF', '').strip() if base: - def git(*args): - # -z and quotePath=false keep paths with spaces or non-ASCII characters verbatim. - return subprocess.run(['git', '--literal-pathspecs', '-c', 'core.quotePath=false', *args], capture_output=True, - text=True, encoding='utf-8', errors='replace', check=True).stdout - def resolve(path): - if path in lines: - return path - candidates = [f for f in lines if f.endswith('/' + path) or path.endswith('/' + f)] - if not candidates: - return None - best = max(min(len(f), len(path)) for f in candidates) - top = [f for f in candidates if min(len(f), len(path)) == best] - if len(top) > 1: - errors.append(f'patch coverage: {path} matches several report paths ({", ".join(sorted(top))}); make the report paths unambiguous') - return '' - return top[0] + return path if path in lines else None base_range = f'origin/{base}...HEAD' tokens = git('diff', '--name-status', '--no-renames', '-z', '--diff-filter=AM', base_range).split('\0') @@ -174,8 +190,6 @@ jobs: missed, unreported = [], [] for status, path in zip(tokens[0::2], tokens[1::2]): match = resolve(path) - if match == '': - continue added = set() for raw in git('diff', '-U0', '--no-color', base_range, '--', path).splitlines(): m = re.match(r'@@ .* \+(\d+)(?:,(\d+))? @@', raw) diff --git a/CHANGELOG.md b/CHANGELOG.md index 362b0f7..5a8dab4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `coverage.yml` resolves report paths against the repository's tracked files (exact, `` roots, or a unique suffix) instead of matching only the changed files, so an ambiguous suffix outside the diff is no longer missed and one file reported under several prefixes counts once. A `.xml` file in a coverage artifact that is not valid Cobertura XML now fails the job. - The concurrency conformance check treats a workflow in which any job calls a release workflow as a release caller and expects the event form, so `examples/release-please-python.yml` (release jobs mixed with build and publish jobs) passes instead of being steered to a form that can lose a version bump. - `examples/pr-title.yml` declares the CI concurrency form and a `# Place at` header; a new CI step checks that every example passes the shipped conformance checks. diff --git a/README.md b/README.md index e977fcc..68ee9a8 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,8 @@ Enforces the organization coverage standard in one place: global lines ≥ 80 %, The job fails when no report is found or when the reports hold no branch data. Cobertura reports only carry per-line branch counts, so reports of sharded test runs for the same code (unit and e2e, a matrix) must be combined into one report before upload (`coverage combine`, lcov merge); otherwise the branch figure is the per-line maximum and under-reports. +Report paths are resolved against the files tracked by the repository (exact path, the Cobertura `` roots, or a unique suffix match), so the same file reported under several prefixes counts once. A report path that is a suffix of several tracked files, or a `.xml` file in a `coverage-*` artifact that is not a Cobertura report (invalid XML, or a root other than ``), fails the job: emit repository-relative paths (`relative_files = true` in coverage.py) and upload only coverage reports. + ```yaml jobs: coverage: From 065b5aebc4d77c79bbd43763d86e985be10d2741 Mon Sep 17 00:00:00 2001 From: mignot Date: Wed, 25 Nov 2026 16:11:24 +0100 Subject: [PATCH 2/2] fix(coverage): prefer Cobertura source roots over a same-named root file Co-Authored-By: Claude Sonnet 5.5 --- .github/tests/coverage.sh | 7 +++++++ .github/workflows/coverage.yml | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/tests/coverage.sh b/.github/tests/coverage.sh index c177493..f80731b 100755 --- a/.github/tests/coverage.sh +++ b/.github/tests/coverage.sh @@ -111,4 +111,11 @@ cobertura 10 10 yes; printf '' > reports/junit.xml; run_check fail grep -q 'not a Cobertura report' "$work/out" || { echo 'FAIL: non-Cobertura message' >&2; cat "$work/out" >&2; exit 1; } cobertura 10 10 yes; printf ' reports/bad.xml; run_check fail 'invalid XML in the reports' grep -q 'not valid XML' "$work/out" || { echo 'FAIL: invalid XML message' >&2; cat "$work/out" >&2; exit 1; } +scenario util.py pkg/util.py -- pkg/util.py +multi_report util.py=1 +sed -i.bak "s||$(pwd -P)/pkg|" reports/cov.xml; rm -f reports/cov.xml.bak +run_check pass 'source root resolves the covered file' +multi_report util.py=0 +sed -i.bak "s||$(pwd -P)/pkg|" reports/cov.xml; rm -f reports/cov.xml.bak +run_check fail 'source root wins over a same-named file at the root' echo 'all coverage tests passed' diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 0e99b8b..6a6021c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -87,12 +87,13 @@ jobs: def canonical(path, report, sources=()): # Map a report path onto the tracked file it names, so every later lookup is an exact match. p = norm(path) - if p in tracked: - return p + # Cobertura filenames are relative to their , so the source roots win over a same-named file at the root. for source in sources: c = norm(os.path.join(source, path)) if c in tracked: return c + if p in tracked: + return p same_name = tracked_by_name.get(p.rsplit('/', 1)[-1], []) shorter = [t for t in same_name if t.endswith('/' + p)] if len(shorter) == 1: