Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): classify .mts/.cts tests in coverage_gradient and move_method (Refs #1103) (#1111)

Raghav Chamadiya25efeb725c1f
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py
index 75d71ad7e..c042413f6 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gradient.py
@@ -51,7 +51,19 @@ def _looks_like_test_path(path: str) -> bool:
or "/__tests__/" in p
or p.startswith(("test/", "tests/", "__tests__/"))
or p.endswith(
- ("_test.py", "_test.go", ".test.ts", ".test.tsx", ".test.js", ".spec.ts", ".spec.js")
+ (
+ "_test.py",
+ "_test.go",
+ ".test.ts",
+ ".test.tsx",
+ ".test.js",
+ ".test.mts",
+ ".test.cts",
+ ".spec.ts",
+ ".spec.js",
+ ".spec.mts",
+ ".spec.cts",
+ )
)
or p.rsplit("/", 1)[-1].startswith("test_")
)
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py b/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py
index 9614863ad..5180611fc 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py
@@ -76,8 +76,12 @@ def _is_test_path(path: str) -> bool:
".test.ts",
".test.tsx",
".test.js",
+ ".test.mts",
+ ".test.cts",
".spec.ts",
".spec.js",
+ ".spec.mts",
+ ".spec.cts",
)
)
)
diff --git a/tests/unit/health/test_mts_cts_test_classification.py b/tests/unit/health/test_mts_cts_test_classification.py
index 2b387fb4e..4656b2b5b 100644
--- a/tests/unit/health/test_mts_cts_test_classification.py
+++ b/tests/unit/health/test_mts_cts_test_classification.py
@@ -1,10 +1,28 @@
-"""`.mts`/`.cts` test files must be classified as tests across health logic (#288)."""
+"""`.mts`/`.cts` test files must be classified as tests across health logic (#288).
+
+The original version of this file asserted on four implementations, one test
+function each. Two further copies of the same predicate existed at the time and
+were not listed, so they never received the fix: ``coverage_gradient`` and
+``move_method`` still read ``foo.test.mts`` as production code. The matrix below
+covers every reachable implementation instead, so a copy that is added or edited
+without the `.mts`/`.cts` conventions fails here rather than in a user's repo.
+
+New implementations still have to be added to ``_IMPLEMENTATIONS`` by hand, which
+is the limit of this approach and the reason #1103 exists.
+"""
from __future__ import annotations
+from collections.abc import Callable
+
import pytest
-from repowise.core.analysis.health.biomarkers.coverage_gap import _looks_like_test_path
+from repowise.core.analysis.health.biomarkers.coverage_gap import (
+ _looks_like_test_path as _coverage_gap_looks_like_test_path,
+)
+from repowise.core.analysis.health.biomarkers.coverage_gradient import (
+ _looks_like_test_path as _coverage_gradient_looks_like_test_path,
+)
from repowise.core.analysis.health.biomarkers.hidden_coupling import (
_is_test_path as _coupling_is_test_path,
)
@@ -16,26 +34,57 @@ from repowise.core.analysis.health.engine import (
from repowise.core.analysis.health.engine import (
_is_test_file as _engine_is_test_file,
)
+from repowise.core.analysis.health.refactoring.extract_helper import (
+ _is_test_path as _extract_helper_is_test_path,
+)
+from repowise.core.analysis.health.refactoring.move_method import (
+ _is_test_path as _move_method_is_test_path,
+)
+from repowise.core.analysis.health.refactoring.split_file import (
+ _is_test_path as _split_file_is_test_path,
+)
+# Every implementation of "is this path a test?" reachable from health logic.
+# Consolidating them is #1103; until then they all have to agree here.
+_IMPLEMENTATIONS: tuple[tuple[str, Callable[[str], bool]], ...] = (
+ ("engine", _engine_is_test_file),
+ ("coverage_detector", is_test_file),
+ ("hidden_coupling", _coupling_is_test_path),
+ ("coverage_gap", _coverage_gap_looks_like_test_path),
+ ("coverage_gradient", _coverage_gradient_looks_like_test_path),
+ ("split_file", _split_file_is_test_path),
+ ("move_method", _move_method_is_test_path),
+ ("extract_helper", _extract_helper_is_test_path),
+)
[email protected]("path", ["src/foo.test.mts", "src/foo.test.cts", "src/foo.spec.mts", "src/foo.spec.cts"])
-def test_engine_is_test_file(path: str) -> None:
- assert _engine_is_test_file(path)
-
+_MTS_CTS_TEST_PATHS = (
+ "src/foo.test.mts",
+ "src/foo.test.cts",
+ "src/foo.spec.mts",
+ "src/foo.spec.cts",
+)
[email protected]("path", ["src/foo.test.mts", "src/foo.spec.cts"])
-def test_coverage_is_test_file(path: str) -> None:
- assert is_test_file(path)
+# Same extensions, production files: the suffix has to be the whole convention,
+# not a substring, or `.mts` sources get exempted from coverage scoring wholesale.
+_MTS_CTS_PRODUCTION_PATHS = (
+ "src/foo.mts",
+ "src/foo.cts",
+ "src/testable.mts",
+)
[email protected]("path", ["src/foo.test.mts", "src/foo.spec.cts"])
-def test_hidden_coupling_is_test_path(path: str) -> None:
- assert _coupling_is_test_path(path)
[email protected]("path", _MTS_CTS_TEST_PATHS)
[email protected](("name", "is_test"), _IMPLEMENTATIONS, ids=lambda v: v)
+def test_mts_cts_paths_are_tests(name: str, is_test: Callable[[str], bool], path: str) -> None:
+ assert is_test(path), f"{name} does not classify {path} as a test"
[email protected]("path", ["src/foo.test.mts", "src/foo.spec.cts"])
-def test_coverage_gap_looks_like_test_path(path: str) -> None:
- assert _looks_like_test_path(path)
[email protected]("path", _MTS_CTS_PRODUCTION_PATHS)
[email protected](("name", "is_test"), _IMPLEMENTATIONS, ids=lambda v: v)
+def test_mts_cts_production_paths_are_not_tests(
+ name: str, is_test: Callable[[str], bool], path: str
+) -> None:
+ assert not is_test(path), f"{name} wrongly classifies {path} as a test"
def test_paired_test_file_finds_mts_cts() -> None: