diff --git a/.gitignore b/.gitignore index ebc9102..5f557bc 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,9 @@ coverage.xml htmlcov/ *.lcov +# Local code graph +.code-review-graph/ + # Design docs (local only) TODO.md diff --git a/README.md b/README.md index 5cf5fa3..c602040 100644 --- a/README.md +++ b/README.md @@ -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): @@ -476,7 +478,7 @@ When tests run inside a Docker container (or any environment with a different fi | Format | How it works | |:-------|:-------------| -| **Clover** | Reads `` 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 `` (PHPUnit) and `` (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 `` — 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. | diff --git a/src/coverage_normalizer.py b/src/coverage_normalizer.py index 3a2bc80..2d3ddb0 100644 --- a/src/coverage_normalizer.py +++ b/src/coverage_normalizer.py @@ -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 , 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 @@ -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 diff --git a/src/layer1_coverage.py b/src/layer1_coverage.py index 083a681..9d77bb1 100644 --- a/src/layer1_coverage.py +++ b/src/layer1_coverage.py @@ -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, ) @@ -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): @@ -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, diff --git a/tests/fixtures/jest_clover_container_paths.xml b/tests/fixtures/jest_clover_container_paths.xml new file mode 100644 index 0000000..37fcbaa --- /dev/null +++ b/tests/fixtures/jest_clover_container_paths.xml @@ -0,0 +1,216 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/test_coverage_normalizer.py b/tests/test_coverage_normalizer.py index 861a58e..2204907 100644 --- a/tests/test_coverage_normalizer.py +++ b/tests/test_coverage_normalizer.py @@ -883,3 +883,109 @@ def test_clover_parse_error_returns_original_path(self, tmp_path): return_value=CoverageFormat.CLOVER, ): assert normalize_coverage_file(str(report), ["src/foo.py"]) == str(report) + + +# Jest clover produced in a container: basename in name=, container path in path=. +_CLOVER_JEST_CONTAINER_REPORT = """ + + + + + + + + + + +""" + +# Jest clover whose path= is already git-relative. +_CLOVER_JEST_RELATIVE_REPORT = """ + + + + + + + +""" + +# One file qualified through name= (PHPUnit shape), one only through path= (Jest shape). +_CLOVER_MIXED_REPORT = """ + + + + + + + + + + +""" + + +class TestCloverContainerPathPrefix: + def test_jest_container_path_is_stripped(self, tmp_path, capsys): + # Regression, Jest clover, container path, MateCat PR #4927: name= is a + # basename, so the prefix has to come from path=. + report = tmp_path / "js-coverage.xml" + report.write_text(_CLOVER_JEST_CONTAINER_REPORT) + result = normalize_coverage_file(str(report), ["src/a.js"]) + assert result != str(report) + files = ET.parse(result).findall(".//file") # noqa: S314 + assert [f.get("path") for f in files] == ["src/a.js", "src/lib/b.js"] + assert "stripped prefix '/var/www/app/'" in capsys.readouterr().out + + def test_real_jest_report_from_container_is_normalized(self, capsys): + # Regression, Jest clover, container path, MateCat PR #4927: the real + # report of the 4 changed files, reduced from CI run 39423022273. + report = Path(__file__).parent / "fixtures" / "jest_clover_container_paths.xml" + diff_files = [ + "public/js/actions/ModalsActions.js", + "public/js/api/confirmSplitRequest/confirmSplitRequest.js", + "public/js/api/mergeJobChunks/mergeJobChunks.js", + "public/js/components/modals/SplitJob.js", + ] + result = normalize_coverage_file(str(report), diff_files) + assert result != str(report) + paths = [f.get("path") for f in ET.parse(result).findall(".//file")] # noqa: S314 + assert sorted(paths) == diff_files + assert "stripped prefix '/var/www/matecat/'" in capsys.readouterr().out + + def test_jest_relative_path_returns_original(self, tmp_path): + # Regression guard, Jest clover, container path, MateCat PR #4927: nothing + # to strip when path= is already git-relative. + report = tmp_path / "js-coverage.xml" + report.write_text(_CLOVER_JEST_RELATIVE_REPORT) + assert normalize_coverage_file(str(report), ["src/a.js"]) == str(report) + + def test_jest_path_without_shared_prefix_returns_original(self, tmp_path): + # Regression guard, Jest clover, container path, MateCat PR #4927: a path= + # that matches no diff file yields no prefix, so the report is untouched. + report = tmp_path / "js-coverage.xml" + report.write_text(_CLOVER_JEST_CONTAINER_REPORT) + assert normalize_coverage_file(str(report), ["lib/other.js"]) == str(report) + + def test_phpunit_name_still_wins_and_is_copied_to_path(self, tmp_path): + # Regression guard, Jest clover, container path, MateCat PR #4927: PHPUnit + # keeps reading name=, and _rewrite_clover() still copies it into path=. + report = tmp_path / "clover.xml" + report.write_text(_CLOVER_PHPUNIT_REPORT) + result = normalize_coverage_file(str(report), ["lib/AuthCookie.php"]) + file_elem = ET.parse(result).find(".//file") # noqa: S314 + assert file_elem is not None + assert file_elem.get("name") == "lib/AuthCookie.php" + assert file_elem.get("path") == "lib/AuthCookie.php" + + def test_mixed_name_and_path_shapes_share_one_prefix(self, tmp_path, capsys): + # Regression, Jest clover, container path, MateCat PR #4927: one report + # with an absolute name= file and a path=-only file gets one prefix. + report = tmp_path / "clover.xml" + report.write_text(_CLOVER_MIXED_REPORT) + result = normalize_coverage_file(str(report), ["lib/Auth.php", "src/a.js"]) + files = ET.parse(result).findall(".//file") # noqa: S314 + assert [f.get("path") for f in files] == ["lib/Auth.php", "src/a.js"] + assert files[0].get("name") == "lib/Auth.php" + assert files[1].get("name") == "a.js" + assert "stripped prefix '/var/www/app/'" in capsys.readouterr().out diff --git a/tests/test_layer1_coverage.py b/tests/test_layer1_coverage.py index 18451f3..572d133 100644 --- a/tests/test_layer1_coverage.py +++ b/tests/test_layer1_coverage.py @@ -3,6 +3,7 @@ import os import subprocess from unittest.mock import patch +from xml.etree import ElementTree as ET from src.layer1_coverage import ( _DIFF_COVER_TIMEOUT, @@ -47,6 +48,27 @@ """ +# Jest clover run from a subdirectory: path= is relative to it, not to the repo root. +_CLOVER_JEST_SUBDIR_STYLE = """ + + + + + + +""" + +# b.js sits under a container root the prefix detector strips; a.js under another root. +_CLOVER_TWO_ROOTS = """ + + + + + + +""" + + class TestConstants: def test_diff_cover_timeout_value(self): assert _DIFF_COVER_TIMEOUT == 60 @@ -205,6 +227,61 @@ def test_absent_but_in_jest_clover_report_passes(self, mock_cov, tmp_path): assert result.short_circuit is True assert result.unmeasurable_files == {"src/bruno/types.ts"} + @patch("src.layer1_coverage._compute_diff_coverage") + def test_suffix_only_report_path_warns_of_mismatch(self, mock_cov, tmp_path, capsys): + # Regression, Jest clover, container path, MateCat PR #4927: path= is + # relative to a subdirectory, so diff-cover cannot match it to the git + # path even though the report mentions the file. + cov = tmp_path / "js-coverage.xml" + cov.write_text(_CLOVER_JEST_SUBDIR_STYLE) + mock_cov.return_value = (100.0, {"app/src/ok.js": 100.0}, "") + result = run_layer1( + coverage_files=[str(cov)], + threshold=80, + diff_files=["app/src/ok.js", "app/src/a.js"], + ) + out = capsys.readouterr().out + assert "::warning::Coverage path mismatch: app/src/a.js" in out + assert "'src/a.js'" in out + assert result.unmeasurable_files == {"app/src/a.js"} + + @patch("src.layer1_coverage._compute_diff_coverage") + def test_exact_report_path_does_not_warn(self, mock_cov, tmp_path, capsys): + # Regression guard, Jest clover, container path, MateCat PR #4927: a + # report path equal to the git path is genuinely "nothing to measure". + cov = tmp_path / "clover.xml" + cov.write_text(_CLOVER_WITH_TYPES) + mock_cov.return_value = (100.0, {"src/bruno/request.ts": 100.0}, "") + run_layer1( + coverage_files=[str(cov)], + threshold=95, + diff_files=["src/bruno/request.ts", "src/bruno/types.ts"], + ) + assert "path mismatch" not in capsys.readouterr().out + + @patch("src.layer1_coverage._compute_diff_coverage") + def test_unstripped_foreign_root_warns(self, mock_cov, tmp_path, capsys): + # Regression, Jest clover, container path, MateCat PR #4927: a second + # container root survives prefix stripping and never reaches diff-cover. + cov = tmp_path / "js-coverage.xml" + cov.write_text(_CLOVER_TWO_ROOTS.format(other_root="/srv/other")) + mock_cov.return_value = (100.0, {"src/b.js": 100.0}, "") + run_layer1(coverage_files=[str(cov)], threshold=80, diff_files=["src/b.js", "src/a.js"]) + assert "'/srv/other/src/a.js'" in capsys.readouterr().out + + @patch("src.layer1_coverage._compute_diff_coverage") + def test_report_path_under_checkout_does_not_warn( + self, mock_cov, tmp_path, monkeypatch, capsys + ): + # Regression guard, Jest clover, container path, MateCat PR #4927: an + # absolute path under the checkout resolves in diff-cover, so no warning. + monkeypatch.chdir(tmp_path) + cov = tmp_path / "js-coverage.xml" + cov.write_text(_CLOVER_TWO_ROOTS.format(other_root=tmp_path.resolve().as_posix())) + mock_cov.return_value = (100.0, {"src/b.js": 100.0}, "") + run_layer1(coverage_files=[str(cov)], threshold=80, diff_files=["src/b.js", "src/a.js"]) + assert "path mismatch" not in capsys.readouterr().out + @patch("src.layer1_coverage._compute_diff_coverage") def test_absent_and_missing_from_report_still_fails(self, mock_cov, tmp_path): # Same absence, different cause: the file is nowhere in the report, so @@ -640,3 +717,41 @@ def test_already_relative_paths_use_original_file(self, mock_run, tmp_path): cmd = mock_run.call_args[0][0] # Should use original file (no rewriting) assert cmd[1] == str(cov) + + @patch("src.layer1_coverage.subprocess.run") + def test_jest_container_paths_reach_diff_cover_normalized(self, mock_run, tmp_path): + """Regression, Jest clover, container path, MateCat PR #4927: diff-cover + must receive path= with the container prefix stripped, so the changed + file gets its real verdict instead of 'no executable lines changed'.""" + cov = tmp_path / "js-coverage.xml" + cov.write_text(""" + + + + + + +""") + received: list[str | None] = [] + + def fake_run(cmd, **kwargs): + # The normalized temp file is deleted once run_layer1 returns. + file_elem = ET.parse(cmd[1]).find(".//file") # noqa: S314 + received.append(None if file_elem is None else file_elem.get("path")) + return subprocess.CompletedProcess( + args=cmd, + returncode=0, + stdout=( + '{"total_percent_covered": 90.0, "src_stats": ' + '{"src/a.js": {"percent_covered": 90.0}}}' + ), + stderr="", + ) + + mock_run.side_effect = fake_run + result = run_layer1([str(cov)], threshold=80, diff_files=["src/a.js"]) + + assert received == ["src/a.js"] + fv = next(f for f in result.file_verdicts if f.file == "src/a.js") + assert fv.verdict == Verdict.PASS + assert "no executable lines changed" not in fv.reason