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(risk): pair FooTest.java, FooTests.cs and FooSpec.kt with their source (#3096)

Raghav Chamadiyae69ef194141c
diff --git a/packages/core/src/repowise/core/test_paths.py b/packages/core/src/repowise/core/test_paths.py
index 87e600f25..e6d04020c 100644
--- a/packages/core/src/repowise/core/test_paths.py
+++ b/packages/core/src/repowise/core/test_paths.py
@@ -462,6 +462,14 @@ def is_test_to_production_pair(
_PASCAL_UNIT_SUFFIXES = frozenset({".pas", ".pp", ".dpr", ".dpk", ".lpr"})
+# JVM and .NET tests are a PascalCase class named for the class under test.
+_CLASS_TEST_SUFFIXES: dict[str, tuple[str, ...]] = {
+ ".java": ("Test", "Tests"),
+ ".cs": ("Test", "Tests"),
+ ".kt": ("Test", "Tests", "Spec"),
+ ".scala": ("Spec", "Suite", "Test"),
+}
+
def paired_test_names(rel_path: str) -> frozenset[str]:
"""Filenames a test for *rel_path* would conventionally carry, any directory."""
@@ -486,4 +494,6 @@ def paired_test_names(rel_path: str) -> frozenset[str]:
# Delphi pairs ``uFoo.pas`` with a ``TestFoo.dpr`` program; only a
# lowercase ``u`` is the unit prefix (``Utils.pas`` keeps its U).
names.add(f"Test{stem[1:] if stem[:1] == 'u' else stem}.dpr")
+ for suffix in _CLASS_TEST_SUFFIXES.get(p.suffix, ()):
+ names.add(f"{stem}{suffix}{p.suffix}")
return frozenset(names)
diff --git a/tests/unit/health/test_paired_test_basenames.py b/tests/unit/health/test_paired_test_basenames.py
index f30868320..47963dddb 100644
--- a/tests/unit/health/test_paired_test_basenames.py
+++ b/tests/unit/health/test_paired_test_basenames.py
@@ -12,6 +12,8 @@ from __future__ import annotations
from pathlib import Path
from typing import ClassVar
+import pytest
+
from repowise.core.analysis.health.engine import (
_has_paired_test_file,
_path_basenames,
@@ -173,3 +175,46 @@ def test_unnamed_tests_rank_by_the_directories_they_share_with_the_target():
ranked = rank_tests("pkg/a/b/mod.py", ["pkg/x/test_other.py", "pkg/a/b/test_sibling.py"])
assert ranked == ["pkg/a/b/test_sibling.py", "pkg/x/test_other.py"]
+
+
+class TestClassNamedPairing:
+ """JVM and .NET tests are a class named ``<Stem>Test``/``<Stem>Tests``/``<Stem>Spec``."""
+
+ @pytest.mark.parametrize(
+ ("source", "test"),
+ [
+ ("src/main/java/a/Foo.java", "src/test/java/a/FooTest.java"),
+ ("src/main/java/a/Foo.java", "src/test/java/a/FooTests.java"),
+ ("src/App/Foo.cs", "tests/App.Tests/FooTest.cs"),
+ ("src/App/Foo.cs", "tests/App.Tests/FooTests.cs"),
+ ("src/main/kotlin/Foo.kt", "src/test/kotlin/FooTest.kt"),
+ ("src/main/kotlin/Foo.kt", "src/test/kotlin/FooSpec.kt"),
+ ("src/main/scala/Foo.scala", "src/test/scala/FooSpec.scala"),
+ ("src/main/scala/Foo.scala", "src/test/scala/FooSuite.scala"),
+ ("src/main/scala/Foo.scala", "src/test/scala/FooTest.scala"),
+ ],
+ )
+ def test_pairs(self, source: str, test: str) -> None:
+ assert _has_paired_test_file(source, _path_basenames({source, test}))
+
+ @pytest.mark.parametrize(
+ ("source", "test"),
+ [
+ # Suffix belongs to the language: a C# test does not pair a Java class.
+ ("src/Foo.java", "tests/FooTests.cs"),
+ ("src/Foo.cs", "tests/FooSpec.cs"),
+ ("src/Foo.java", "tests/FooSuite.java"),
+ # Case matters: ``Footest`` is not a test class for ``Foo``.
+ ("src/Foo.java", "tests/Footest.java"),
+ ],
+ )
+ def test_near_misses(self, source: str, test: str) -> None:
+ assert not _has_paired_test_file(source, _path_basenames({source, test}))
+
+ def test_name_match_tier_finds_a_java_test(self) -> None:
+ from repowise.core.analysis.test_reachability import tests_matching_by_name
+
+ found = tests_matching_by_name(
+ ["src/main/java/a/Foo.java"], {"src/test/java/a/FooTest.java"}
+ )
+ assert found["src/main/java/a/Foo.java"].tests == ["src/test/java/a/FooTest.java"]