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): recognize Delphi's Test<Stem>.dpr test-file convention (#1706)

Laentir Valetov89e6f1e25aaa
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index b5167a5d1..ced676ee0 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -276,6 +276,9 @@ def _path_basenames(all_paths: set[str]) -> set[str]:
return {p.rsplit("/", 1)[-1] for p in all_paths}
+_PASCAL_UNIT_SUFFIXES = frozenset({".pas", ".pp", ".dpr", ".dpk", ".lpr"})
+
+
def _has_paired_test_file(rel_path: str, path_basenames: set[str]) -> bool:
"""Heuristic: does any other file look like a test for *rel_path*?
@@ -299,6 +302,20 @@ def _has_paired_test_file(rel_path: str, path_basenames: set[str]) -> bool:
f"{stem}.spec.mts",
f"{stem}.spec.cts",
}
+ if p.suffix.lower() in _PASCAL_UNIT_SUFFIXES:
+ # Delphi/FPC's lowercase "u" unit-name prefix (uFoo.pas) has no
+ # test-file convention of its own; real-world projects pair it with
+ # a standalone console test program named Test<Foo>.dpr (the "u" is
+ # dropped, the extension is .dpr since a runnable test program is a
+ # project file, not a unit). Only a lowercase "u" is stripped -- a
+ # stem that merely starts with capital "U" (Utils.pas) is a
+ # different word, not this naming convention. Confirmed against a
+ # real ~150-file Delphi codebase: uKeymap.pas <-> TestKeymap.dpr,
+ # uANSIParser.pas <-> TestANSIParser.dpr, uConsoleBuffer.pas <->
+ # TestConsoleBuffer.dpr, etc. -- src/tools/Test*.dpr, not next to
+ # the unit.
+ pascal_stem = stem[1:] if stem[:1] == "u" else stem
+ candidates.add(f"Test{pascal_stem}.dpr")
return not candidates.isdisjoint(path_basenames)
diff --git a/tests/unit/health/test_paired_test_basenames.py b/tests/unit/health/test_paired_test_basenames.py
index f72030b39..b90685eb6 100644
--- a/tests/unit/health/test_paired_test_basenames.py
+++ b/tests/unit/health/test_paired_test_basenames.py
@@ -10,6 +10,7 @@ copy of the old scan over a corpus of tricky paths.
from __future__ import annotations
from pathlib import Path
+from typing import ClassVar
from repowise.core.analysis.health.engine import (
_has_paired_test_file,
@@ -102,3 +103,41 @@ class TestPairedTestBasenames:
def test_empty_paths(self) -> None:
assert _path_basenames(set()) == set()
assert not _has_paired_test_file("anything.py", set())
+
+
+class TestPascalPairing:
+ """Delphi/FPC's ``u``-prefixed unit pairs with a standalone console test
+ program named ``Test<Stem>.dpr`` (the ``u`` dropped) -- confirmed against
+ a real ~150-file Delphi codebase's ``src/tools/Test*.dpr`` convention."""
+
+ CORPUS: ClassVar[set[str]] = {
+ "src/Core/uKeymap.pas",
+ "src/tools/TestKeymap.dpr",
+ "src/Core/uANSIParser.pas",
+ "src/tools/TestANSIParser.dpr",
+ # no leading "u" -- stem passes through unchanged
+ "src/Core/Utils.pas",
+ "src/tools/TestUtils.dpr",
+ # a unit with no paired test at all
+ "src/Core/uOrphan.pas",
+ # near-miss: same stem, wrong extension -- must NOT match
+ "src/Core/uOther.pas",
+ "src/tools/TestOther.pp",
+ }
+
+ def test_pascal_pairs_match(self) -> None:
+ basenames = _path_basenames(self.CORPUS)
+ assert _has_paired_test_file("src/Core/uKeymap.pas", basenames)
+ assert _has_paired_test_file("src/Core/uANSIParser.pas", basenames)
+ assert _has_paired_test_file("src/Core/Utils.pas", basenames)
+
+ def test_pascal_orphan_and_near_miss_do_not_match(self) -> None:
+ basenames = _path_basenames(self.CORPUS)
+ assert not _has_paired_test_file("src/Core/uOrphan.pas", basenames)
+ assert not _has_paired_test_file("src/Core/uOther.pas", basenames)
+
+ def test_non_pascal_file_is_unaffected_by_pascal_candidate(self) -> None:
+ # A non-Pascal file named e.g. "uKeymap.ts" must not spuriously pick
+ # up the Pascal Test<Stem>.dpr candidate.
+ basenames = _path_basenames({"uKeymap.ts", "TestKeymap.dpr"})
+ assert not _has_paired_test_file("uKeymap.ts", basenames)