Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ coverage.xml
htmlcov/
*.lcov

# Local code graph
.code-review-graph/

# Design docs (local only)
TODO.md

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ The two compose: the final exclude set is `exclude-patterns ∪ extra-exclude-pa

So a PR that only adds fields to a TypeScript `interface` passes without weakening the gate: add real logic to that same file later and its executable lines are measured and gated as usual.

For Clover reports, diff-cover matches paths exactly. If the report mentions a changed file only under a longer or shorter path (a second container root, or a path relative to a subdirectory), the file still passes as above, but Test-Guard emits a `::warning::Coverage path mismatch` annotation: its changed lines were not actually measured.

### Custom test patterns

`test-patterns` defaults to `auto`, which uses the built-in mappings for 19 languages. To teach Test-Guard a project-specific test layout, pass a **JSON object**. Each entry maps an arbitrary id to a `src_pattern` (glob identifying a source file) and a `test_template` (glob for the expected test, with a `{name}` placeholder for the source file's stem):
Expand Down Expand Up @@ -476,7 +478,7 @@ When tests run inside a Docker container (or any environment with a different fi

| Format | How it works |
|:-------|:-------------|
| **Clover** | Reads `<file name="...">` paths, suffix-matches against PR file list to detect the container prefix (e.g., `/var/www/app/`), rewrites paths to relative. |
| **Clover** | Reads the absolute one of `<file name="...">` (PHPUnit) and `<file path="...">` (Jest, which keeps only the basename in `name`), suffix-matches against PR file list to detect the container prefix (e.g., `/var/www/app/`), rewrites paths to relative. |
| **Cobertura** | Checks `<class filename="...">` — if absolute, detects the prefix and rewrites to relative. If already relative (e.g., pytest-cov output), no action. |
| **JaCoCo** | Uses diff-cover's built-in `--src-roots` handling. |
| **LCOV** | No XML normalization possible — ensure paths are relative in your LCOV output. |
Expand Down
36 changes: 28 additions & 8 deletions src/coverage_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,20 @@ def detect_path_prefix(xml_paths: list[str], diff_files: list[str]) -> str:


def _extract_clover_paths(root: ET.Element) -> list[str]:
"""Extract all file paths from a Clover XML tree."""
"""Extract all file paths from a Clover XML tree.

Returns, per <file>, whichever attribute is an absolute path: PHPUnit
qualifies name=, Jest qualifies path= and leaves a basename in name=.
Prefix detection needs the qualified one; with neither, name= wins.
"""
paths: list[str] = []
for file_elem in root.findall(".//file"):
name = file_elem.get("name") or file_elem.get("path") or ""
if name:
paths.append(name)
name = file_elem.get("name") or ""
path = file_elem.get("path") or ""
absolute = next((p for p in (name, path) if _is_absolute_path(p)), "")
chosen = absolute or name or path
if chosen:
paths.append(chosen)
return paths


Expand Down Expand Up @@ -350,10 +358,22 @@ def is_in_report(diff_file: str, reported_files: set[str]) -> bool:
"""
if diff_file in reported_files:
return True
return find_suffix_only_match(diff_file, reported_files) is not None


def find_suffix_only_match(diff_file: str, reported_files: set[str]) -> str | None:
"""Return a reported path that matches a git path only by suffix, else None.

Same two suffix directions as is_in_report(). A consumer that compares
paths exactly (diff-cover does for Clover) cannot match such a file, so
"in the report" says nothing about it having been measured.
"""
if diff_file in reported_files:
return None
needle = "/" + diff_file
for reported in reported_files:
for reported in sorted(reported_files):
if reported.endswith(needle):
return True
return reported
if "/" in reported and diff_file.endswith("/" + reported):
return True
return False
return reported
return None
18 changes: 17 additions & 1 deletion src/layer1_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
from pathlib import Path

from src.coverage_normalizer import (
CoverageFormat,
detect_format,
extract_reported_files,
find_suffix_only_match,
is_in_report,
normalize_coverage_file,
)
Expand Down Expand Up @@ -148,8 +151,12 @@ def run_layer1(
# src_stats. Used below to tell "nothing to measure" apart from
# "never instrumented".
reported_files: set[str] = set()
clover_reported: set[str] = set()
for nf in normalized_files:
reported_files |= extract_reported_files(nf)
paths = extract_reported_files(nf)
reported_files |= paths
if detect_format(nf) == CoverageFormat.CLOVER:
clover_reported |= paths
finally:
# Clean up temp files created by normalization
for nf, vf in zip(normalized_files, valid_files, strict=True):
Expand Down Expand Up @@ -229,6 +236,15 @@ def run_layer1(
layer="layer1",
))
for f in unmeasurable_absent:
# diff-cover matches Clover paths exactly, so a report path that only
# suffix-matches the git path was never measured, whatever "no
# executable lines changed" suggests. Paths under the checkout resolve.
stale = find_suffix_only_match(f, clover_reported)
if stale and not stale.startswith(Path.cwd().as_posix() + "/"):
print(
f"::warning::Coverage path mismatch: {f} is in the report as '{stale}', "
f"a path diff-cover cannot match; its changed lines were not measured"
)
file_verdicts.append(FileVerdict(
file=f,
verdict=Verdict.PASS,
Expand Down
216 changes: 216 additions & 0 deletions tests/fixtures/jest_clover_container_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?xml version='1.0' encoding='UTF-8'?>
<coverage generated="1792500048692" clover="3.2.0">
<project timestamp="1792500048692" name="All files">
<metrics statements="23629" coveredstatements="19989" conditionals="15564" coveredconditionals="11349" methods="6894" coveredmethods="5605" elements="46087" coveredelements="36943" complexity="0" loc="23629" ncloc="23629" packages="262" files="717" classes="717" />
<package name="js.actions">
<metrics statements="949" coveredstatements="917" conditionals="433" coveredconditionals="375" methods="422" coveredmethods="402" />
<file name="ModalsActions.js" path="/var/www/matecat/public/js/actions/ModalsActions.js">
<metrics statements="24" coveredstatements="24" conditionals="9" coveredconditionals="8" methods="16" coveredmethods="16" />
<line num="9" count="123" type="stmt" />
<line num="20" count="23" type="stmt" />
<line num="33" count="1" type="stmt" />
<line num="44" count="1" type="stmt" />
<line num="58" count="3" type="stmt" />
<line num="62" count="3" type="stmt" />
<line num="70" count="1" type="stmt" />
<line num="73" count="2" type="stmt" />
<line num="89" count="5" type="stmt" />
<line num="94" count="1" type="stmt" />
<line num="97" count="1" type="stmt" />
<line num="101" count="1" type="stmt" />
<line num="105" count="1" type="stmt" />
<line num="110" count="1" type="stmt" />
<line num="111" count="1" type="stmt" />
<line num="114" count="4" type="stmt" />
<line num="121" count="3" type="stmt" />
<line num="123" count="1" type="cond" truecount="1" falsecount="1" />
<line num="124" count="1" type="stmt" />
<line num="131" count="2" type="stmt" />
<line num="138" count="3" type="stmt" />
<line num="142" count="1" type="stmt" />
<line num="145" count="4" type="stmt" />
<line num="157" count="1" type="stmt" />
</file>
</package>
<package name="js.api.confirmSplitRequest">
<metrics statements="14" coveredstatements="14" conditionals="10" coveredconditionals="9" methods="2" coveredmethods="2" />
<file name="confirmSplitRequest.js" path="/var/www/matecat/public/js/api/confirmSplitRequest/confirmSplitRequest.js">
<metrics statements="14" coveredstatements="14" conditionals="10" coveredconditionals="9" methods="2" coveredmethods="2" />
<line num="4" count="6" type="stmt" />
<line num="11" count="4" type="stmt" />
<line num="20" count="4" type="stmt" />
<line num="22" count="4" type="stmt" />
<line num="23" count="32" type="stmt" />
<line num="25" count="4" type="stmt" />
<line num="34" count="4" type="cond" truecount="2" falsecount="0" />
<line num="35" count="2" type="cond" truecount="2" falsecount="0" />
<line num="36" count="1" type="stmt" />
<line num="37" count="1" type="cond" truecount="1" falsecount="1" />
<line num="39" count="1" type="stmt" />
<line num="43" count="2" type="stmt" />
<line num="44" count="2" type="cond" truecount="4" falsecount="0" />
<line num="46" count="1" type="stmt" />
</file>
</package>
<package name="js.api.mergeJobChunks">
<metrics statements="14" coveredstatements="14" conditionals="10" coveredconditionals="9" methods="2" coveredmethods="2" />
<file name="mergeJobChunks.js" path="/var/www/matecat/public/js/api/mergeJobChunks/mergeJobChunks.js">
<metrics statements="14" coveredstatements="14" conditionals="10" coveredconditionals="9" methods="2" coveredmethods="2" />
<line num="4" count="123" type="stmt" />
<line num="5" count="4" type="stmt" />
<line num="10" count="4" type="stmt" />
<line num="12" count="4" type="stmt" />
<line num="13" count="12" type="stmt" />
<line num="15" count="4" type="stmt" />
<line num="24" count="4" type="cond" truecount="2" falsecount="0" />
<line num="25" count="2" type="cond" truecount="2" falsecount="0" />
<line num="26" count="1" type="stmt" />
<line num="27" count="1" type="cond" truecount="1" falsecount="1" />
<line num="29" count="1" type="stmt" />
<line num="33" count="2" type="stmt" />
<line num="34" count="2" type="cond" truecount="4" falsecount="0" />
<line num="36" count="1" type="stmt" />
</file>
</package>
<package name="js.components.modals">
<metrics statements="680" coveredstatements="655" conditionals="372" coveredconditionals="327" methods="219" coveredmethods="205" />
<file name="SplitJob.js" path="/var/www/matecat/public/js/components/modals/SplitJob.js">
<metrics statements="134" coveredstatements="132" conditionals="78" coveredconditionals="67" methods="29" coveredmethods="29" />
<line num="11" count="5" type="stmt" />
<line num="12" count="69" type="stmt" />
<line num="13" count="69" type="stmt" />
<line num="14" count="69" type="stmt" />
<line num="15" count="69" type="stmt" />
<line num="16" count="69" type="stmt" />
<line num="17" count="69" type="stmt" />
<line num="18" count="69" type="stmt" />
<line num="19" count="69" type="stmt" />
<line num="20" count="69" type="stmt" />
<line num="23" count="69" type="stmt" />
<line num="24" count="69" type="stmt" />
<line num="25" count="69" type="stmt" />
<line num="27" count="69" type="stmt" />
<line num="30" count="69" type="stmt" />
<line num="31" count="69" type="stmt" />
<line num="33" count="69" type="stmt" />
<line num="34" count="18" type="stmt" />
<line num="37" count="69" type="stmt" />
<line num="38" count="34" type="stmt" />
<line num="41" count="69" type="stmt" />
<line num="42" count="34" type="stmt" />
<line num="43" count="34" type="cond" truecount="4" falsecount="0" />
<line num="45" count="16" type="cond" truecount="2" falsecount="0" />
<line num="46" count="14" type="stmt" />
<line num="48" count="2" type="stmt" />
<line num="51" count="16" type="stmt" />
<line num="52" count="32" type="cond" truecount="1" falsecount="1" />
<line num="53" count="0" type="stmt" />
<line num="55" count="32" type="cond" truecount="2" falsecount="0" />
<line num="56" count="4" type="stmt" />
<line num="60" count="28" type="stmt" />
<line num="64" count="16" type="cond" truecount="2" falsecount="0" />
<line num="65" count="2" type="stmt" />
<line num="67" count="16" type="stmt" />
<line num="68" count="16" type="stmt" />
<line num="69" count="16" type="stmt" />
<line num="70" count="16" type="stmt" />
<line num="71" count="16" type="stmt" />
<line num="75" count="69" type="stmt" />
<line num="76" count="18" type="stmt" />
<line num="84" count="14" type="stmt" />
<line num="87" count="2" type="cond" truecount="3" falsecount="1" />
<line num="88" count="2" type="cond" truecount="2" falsecount="0" />
<line num="89" count="1" type="stmt" />
<line num="90" count="1" type="stmt" />
<line num="92" count="1" type="stmt" />
<line num="94" count="2" type="stmt" />
<line num="95" count="2" type="stmt" />
<line num="96" count="2" type="stmt" />
<line num="97" count="2" type="stmt" />
<line num="102" count="69" type="stmt" />
<line num="103" count="1" type="stmt" />
<line num="106" count="69" type="stmt" />
<line num="107" count="1" type="stmt" />
<line num="110" count="1" type="stmt" />
<line num="111" count="1" type="stmt" />
<line num="112" count="1" type="stmt" />
<line num="113" count="1" type="stmt" />
<line num="116" count="69" type="stmt" />
<line num="118" count="1" type="stmt" />
<line num="119" count="1" type="stmt" />
<line num="121" count="1" type="stmt" />
<line num="122" count="1" type="stmt" />
<line num="123" count="1" type="stmt" />
<line num="124" count="4" type="stmt" />
<line num="125" count="4" type="cond" truecount="1" falsecount="1" />
<line num="126" count="0" type="stmt" />
<line num="129" count="4" type="stmt" />
<line num="131" count="1" type="stmt" />
<line num="134" count="69" type="stmt" />
<line num="135" count="3" type="cond" truecount="1" falsecount="1" />
<line num="136" count="3" type="stmt" />
<line num="137" count="3" type="stmt" />
<line num="138" count="6" type="cond" truecount="2" falsecount="0" />
<line num="139" count="3" type="stmt" />
<line num="142" count="3" type="stmt" />
<line num="143" count="3" type="stmt" />
<line num="146" count="69" type="stmt" />
<line num="147" count="69" type="cond" truecount="2" falsecount="0" />
<line num="148" count="34" type="stmt" />
<line num="150" count="72" type="stmt" />
<line num="151" count="35" type="stmt" />
<line num="152" count="35" type="cond" truecount="2" falsecount="0" />
<line num="153" count="8" type="stmt" />
<line num="160" count="69" type="stmt" />
<line num="161" count="2" type="stmt" />
<line num="162" count="2" type="stmt" />
<line num="170" count="1" type="stmt" />
<line num="173" count="1" type="cond" truecount="3" falsecount="1" />
<line num="174" count="1" type="stmt" />
<line num="175" count="1" type="stmt" />
<line num="176" count="1" type="stmt" />
<line num="177" count="1" type="stmt" />
<line num="182" count="69" type="stmt" />
<line num="183" count="4" type="stmt" />
<line num="184" count="4" type="stmt" />
<line num="185" count="4" type="stmt" />
<line num="188" count="69" type="stmt" />
<line num="189" count="6" type="cond" truecount="1" falsecount="1" />
<line num="190" count="6" type="stmt" />
<line num="191" count="6" type="stmt" />
<line num="192" count="6" type="stmt" />
<line num="193" count="12" type="stmt" />
<line num="195" count="6" type="stmt" />
<line num="206" count="3" type="cond" truecount="4" falsecount="0" />
<line num="207" count="2" type="stmt" />
<line num="208" count="2" type="stmt" />
<line num="210" count="1" type="stmt" />
<line num="215" count="3" type="cond" truecount="2" falsecount="0" />
<line num="216" count="2" type="stmt" />
<line num="217" count="2" type="stmt" />
<line num="218" count="2" type="stmt" />
<line num="219" count="2" type="stmt" />
<line num="221" count="3" type="stmt" />
<line num="226" count="69" type="stmt" />
<line num="227" count="69" type="cond" truecount="2" falsecount="0" />
<line num="228" count="1" type="stmt" />
<line num="229" count="68" type="cond" truecount="2" falsecount="0" />
<line num="230" count="33" type="stmt" />
<line num="236" count="35" type="stmt" />
<line num="238" count="72" type="cond" truecount="3" falsecount="1" />
<line num="239" count="72" type="cond" truecount="1" falsecount="1" />
<line num="240" count="72" type="cond" truecount="2" falsecount="2" />
<line num="241" count="72" type="stmt" />
<line num="260" count="3" type="stmt" />
<line num="269" count="69" type="stmt" />
<line num="270" count="69" type="stmt" />
<line num="271" count="69" type="stmt" />
<line num="273" count="69" type="cond" truecount="3" falsecount="1" />
<line num="276" count="69" type="stmt" />
<line num="278" count="69" type="stmt" />
<line num="297" count="1" type="stmt" />
<line num="324" count="3381" type="stmt" />
</file>
</package>
</project>
</coverage>
Loading
Loading