Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(refactoring): the characterization step names the changed function and the tests that reach its file (#4232)
Raghav Chamadiyabfcdb9cf959d
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.pyindex 9904a43f1..e6da37844 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py@@ -678,12 +678,33 @@ def _runs_reason(test: MappedTest, symbol: str) -> str:return f"calls {label}" if test.hops == 1 else f"reaches {label} in {test.hops} calls"-def _characterization_step(suggestion: RefactoringSuggestion) -> str:- """The step before an edit no test reaches: pin today's behaviour first."""- symbol = suggestion.target_symbol+def _characterization_step(suggestion: RefactoringSuggestion, file_tests: int = 0) -> str:+ """The step before an edit no test reaches: pin today's behaviour first.++ *file_tests* counts the listed tests that reach the file but not the+ changed function, so the step and the list state the same fact.+ """+ symbol = (suggestion.target_symbol or "").rsplit("::", 1)[-1]if not symbol or not symbol.replace(".", "_").isidentifier():symbol = suggestion.file_path.rsplit("/", 1)[-1]- return f"No test reaches this; add a characterization test for `{symbol}` before the edit."+ if file_tests:+ files = "its files" if len(affected_files(suggestion)) > 1 else "its file"+ reach = f"{file_tests} reach {files}" if file_tests > 1 else f"1 reaches {files}"+ return f"No test reaches `{symbol}` ({reach}); add a characterization test first."+ return f"No test reaches `{symbol}`; add a characterization test before the edit."+++def _prerequisite(+ suggestion: RefactoringSuggestion,+ basis: ValidationBasis,+ nearest: list[tuple[MappedTest, str]] | None,+ file_tests: int,+) -> str | None:+ """The characterization step when no test reaches the change, also when+ tests reach the file (*file_tests* of them) but none a function it changes."""+ if nearest == []:+ return _characterization_step(suggestion, file_tests)+ return _characterization_step(suggestion) if basis == "unknown" else Nonedef _line_ranges(suggestion: RefactoringSuggestion) -> dict[str, set[int] | None]:@@ -936,11 +957,12 @@ def build_validation_plan(targets=target_rows,reasons=reasons if order_tests else {},test_runs=plan_runs(shown, facts.test_runs),- # Also when tests reach the file but none reaches a function it changes.- prerequisite=(- _characterization_step(suggestion)- if aggregate_basis == "unknown" or nearest == []- else None+ prerequisite=_prerequisite(+ suggestion,+ aggregate_basis,+ nearest,+ # Summed per file, a test reaching two files counts twice: no count then.+ aggregate_total if identities_complete or len(target_rows) == 1 else 0,),)diff --git a/tests/unit/health/test_function_test_map.py b/tests/unit/health/test_function_test_map.pyindex 90479965c..814fa0ee5 100644--- a/tests/unit/health/test_function_test_map.py+++ b/tests/unit/health/test_function_test_map.py@@ -260,7 +260,7 @@ def test_a_function_no_test_reaches_asks_for_a_characterization_test_in_a_testedplan = build_validation_plan(_extract(), {}, _FILE_REACH, evidence=_evidence(()))assert plan.tests == ["tests/test_core.py", "tests/test_misc.py"]assert plan.prerequisite == (- "No test reaches this; add a characterization test for `parse` before the edit."+ "No test reaches `parse` (2 reach its file); add a characterization test first.")diff --git a/tests/unit/health/test_recommendations.py b/tests/unit/health/test_recommendations.pyindex 1a9987df4..3137e8556 100644--- a/tests/unit/health/test_recommendations.py+++ b/tests/unit/health/test_recommendations.py@@ -434,12 +434,15 @@ def test_a_plan_no_test_reaches_has_no_command_and_asks_for_a_characterization_tassert plan.basis == "unknown"assert plan.commands == [], pathassert plan.prerequisite == (- "No test reaches this; add a characterization test for `target` before the edit."+ "No test reaches `target`; add a characterization test before the edit.")assert plan.as_dict()["prerequisite"] == plan.prerequisite# A target that is not a symbol name is named by its file.split = build_validation_plan(_plan("core.py -> 3 files", rtype="split_file"), {}, {})- assert "characterization test for `core.py` before" in split.prerequisite+ assert "reaches `core.py`;" in split.prerequisite+ # A graph id names its function, not the file.+ method = build_validation_plan(_plan("src/core.py::Store::_open"), {}, {})+ assert "reaches `_open`;" in method.prerequisitedef test_a_reached_plan_carries_no_prerequisite() -> None:diff --git a/tests/unit/persistence/test_fix_first_loader.py b/tests/unit/persistence/test_fix_first_loader.pyindex 57a80c306..9d249ef1b 100644--- a/tests/unit/persistence/test_fix_first_loader.py+++ b/tests/unit/persistence/test_fix_first_loader.py@@ -25,7 +25,7 @@ def no_tests(path, function, start, end) -> dict:"""What the loader's test lookup answers for a finding no seeded test reaches."""return {"prerequisite": (- f"No test reaches this; add a characterization test for `{function}` before the edit."+ f"No test reaches `{function}`; add a characterization test before the edit.")}@@ -351,7 +351,7 @@ async def test_a_finding_item_with_no_tests_stays_unknown(async_session) -> Noneassert (walk.verify.tests, walk.verify.basis, walk.verify.command) == ((), "unknown", None)# The step to take before the edit, since no test pins today's behaviour.assert walk.verify.prerequisite == (- "No test reaches this; add a characterization test for `walk` before the edit."+ "No test reaches `walk`; add a characterization test before the edit.")diff --git a/tests/unit/persistence/test_test_reachability_walk.py b/tests/unit/persistence/test_test_reachability_walk.pyindex 46068f453..507b5fb54 100644--- a/tests/unit/persistence/test_test_reachability_walk.py+++ b/tests/unit/persistence/test_test_reachability_walk.py@@ -594,7 +594,7 @@ async def test_a_hub_plan_lists_only_tests_reaching_the_changed_symbol(async_sesassert lonely.tests == []assert lonely.commands == []assert lonely.prerequisite == (- "No test reaches this; add a characterization test for `lonely` before the edit."+ "No test reaches `lonely`; add a characterization test before the edit.")