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): test-quality markers read the stamped test flag, not the file name (#3898)

Raghav Chamadiyaefbb8df7dd83
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py b/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py
index ad84d1dd4..d7ffc99a2 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/assertion_free_test.py
@@ -38,7 +38,6 @@ per-language precision and the false positives it does not separate.
from __future__ import annotations
from ..asserts.predicate import checks_something
-from ..coverage import is_test_file
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -62,7 +61,7 @@ class AssertionFreeTestDetector:
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
if ctx.language not in SHIPPING_LANGUAGES:
return []
- if not is_test_file(ctx.file_path):
+ if not ctx.is_test_material:
return []
out: list[BiomarkerResult] = []
oracles = _asserting_names(ctx)
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/duplicated_assertion_block.py b/packages/core/src/repowise/core/analysis/health/biomarkers/duplicated_assertion_block.py
index 7846e009e..ff384c0fd 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/duplicated_assertion_block.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/duplicated_assertion_block.py
@@ -28,7 +28,6 @@ co-change): this one is scoped to test assertions and lands in the milder
from __future__ import annotations
from ..asserts.lexicon import is_flag_check
-from ..coverage import is_test_file
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -79,7 +78,7 @@ class DuplicatedAssertionBlockDetector:
category = "test_quality"
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
- if not is_test_file(ctx.file_path) or not ctx.clones:
+ if not ctx.is_test_material or not ctx.clones:
return []
own_lines = ctx.clone_sources.get(ctx.file_path) or []
blocks = [
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/large_assertion_block.py b/packages/core/src/repowise/core/analysis/health/biomarkers/large_assertion_block.py
index 11ca2057c..ff5ef640d 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/large_assertion_block.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/large_assertion_block.py
@@ -13,7 +13,6 @@ each ``FunctionComplexity``.
from __future__ import annotations
-from ..coverage import is_test_file
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -25,7 +24,7 @@ class LargeAssertionBlockDetector:
_MIN_COUNT = 15
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
- if not is_test_file(ctx.file_path):
+ if not ctx.is_test_material:
return []
out: list[BiomarkerResult] = []
for fn in ctx.all_functions:
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/mock_saturated_test.py b/packages/core/src/repowise/core/analysis/health/biomarkers/mock_saturated_test.py
index db394664a..dc9b34720 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/mock_saturated_test.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/mock_saturated_test.py
@@ -23,7 +23,6 @@ this one; never quote it for this marker.
from __future__ import annotations
-from ..coverage import is_test_file
from ..mocks.lexicon import mock_dialect
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -43,7 +42,7 @@ class MockSaturatedTestDetector:
_HIGH_RATIO = 8.0 # setup:assertion at which the test is nearly all scaffolding
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
- if not is_test_file(ctx.file_path):
+ if not ctx.is_test_material:
return []
prefixes = _name_prefixes(ctx.language)
out: list[BiomarkerResult] = []
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
index 8d6021ebc..c26428b22 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
@@ -18,7 +18,6 @@ it sits on the first append and lists every appended line in ``details["lines"]`
from __future__ import annotations
from ..complexity import PerfHit
-from ..coverage import is_test_file
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -30,7 +29,7 @@ class StringConcatInLoopDetector:
category = "performance"
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
- if is_test_file(ctx.file_path):
+ if ctx.is_test_material:
return []
groups: dict[tuple[str | None, int, int, str | int], list[PerfHit]] = {}
for index, hit in enumerate(ctx.perf_hits):
diff --git a/tests/unit/health/test_assertion_free_test.py b/tests/unit/health/test_assertion_free_test.py
index 541478844..64a0d7095 100644
--- a/tests/unit/health/test_assertion_free_test.py
+++ b/tests/unit/health/test_assertion_free_test.py
@@ -958,3 +958,32 @@ def test_a_bare_python_reraise_is_not_an_oracle() -> None:
" raise\n"
)
assert _flagged(src) == ["test_reraises"]
+
+
+# --------------------------------------------------------------------------
+# Which files are test files: the stamped flag, not the name
+# --------------------------------------------------------------------------
+
+
+def _detect_with_flag(source: str, path: str, is_test: bool | None):
+ fc = walk_file(path, "python", source.encode("utf-8"))
+ ctx = FileContext(
+ file_path=path,
+ language="python",
+ nloc=fc.file_nloc,
+ has_test_file=True,
+ module=None,
+ is_test=is_test,
+ all_functions=tuple(fc.functions),
+ )
+ return [f.function_name for f in BIOMARKER.detect(ctx)]
+
+
+def test_production_module_named_like_a_test_is_not_judged() -> None:
+ # ``core/test_reachability.py`` is production code that pytest does not
+ # collect. Ingestion stamps it is_test=False; its ``tests_reaching``-style
+ # functions are not test cases that forgot to assert.
+ _require("python")
+ source = "def tests_reaching(graph):\n return {}\n"
+ assert _detect_with_flag(source, "pkg/core/test_reachability.py", False) == []
+ assert _detect_with_flag(source, "tests/test_sample.py", True) == ["tests_reaching"]