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(core): test directories by whole words, filename rules on source only (#2676)
Swati Ahujac6fc49daec0c
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 075b3b6f0..1e3e4c656 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -103,7 +103,14 @@ log = structlog.get_logger(__name__)# Not a licence to move a calibrated scoring weight — those are frozen# independently of this stamp.#-# Current stamp: a multi-item ``with`` header is classified on its oracle rather+# Current stamp: which files are tests changed (``repowise.core.test_paths``).+# Compound directories headed by a test word (``e2e-tests/``, ``pkg_tests/``,+# ``integration_test/``) became test trees, and ``test``/``.test.`` filenames+# stopped counting on configuration (``tsconfig.test.json``, a workflow's+# ``*.test.yml``) and on a bare non-Python ``test.ts``. Every biomarker that+# exempts or targets tests reads that flag, so stored findings move.+#+# v35 (also): a multi-item ``with`` header is classified on its oracle rather# than on whichever context manager the author happened to type first.# ``assert_call_kinds`` is the language's plain call node, not an assertion# shape, so the header scan returned the first call it met and classified only@@ -321,7 +328,7 @@ log = structlog.get_logger(__name__)# forms. Files that were counted untested and are not become tested, which# moves untested-hotspot findings and the scores that carry them, on every# language with a prefix or spec convention rather than Ruby alone.-HEALTH_ANALYZER_VERSION = 35+HEALTH_ANALYZER_VERSION = 36def walked_functions(diff --git a/packages/core/src/repowise/core/analysis/knowledge_graph.py b/packages/core/src/repowise/core/analysis/knowledge_graph.pyindex cf9889ad5..e14766cfa 100644--- a/packages/core/src/repowise/core/analysis/knowledge_graph.py+++ b/packages/core/src/repowise/core/analysis/knowledge_graph.py@@ -216,7 +216,9 @@ def _slugify(text: str) -> str:# macros reach the export instead of being dropped.# "4": `framework_binds` joined the map, so a container-wired symbol pair# reaches the export instead of being dropped.-KG_BUILDER_VERSION = "4"+# "5": which files are tests changed (``repowise.core.test_paths``), moving the+# ``test`` tag on file nodes without moving a node or edge count.+KG_BUILDER_VERSION = "5"# An unmapped type is dropped from the export entirely (see the# `if not kg_type: continue` below), which is silent. Six real types used to bediff --git a/packages/core/src/repowise/core/ingestion/languages/specs/typescript.py b/packages/core/src/repowise/core/ingestion/languages/specs/typescript.pyindex bb6ff97f2..a0e306c3f 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/typescript.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/typescript.py@@ -56,7 +56,9 @@ SPEC = LanguageSpec(tag="typescript",display_name="TypeScript",import_support="full",- test_infixes=(".test.", ".spec."),+ # ``.test-d.`` is the type-test convention tsd and vitest's typecheck mode+ # collect (``index.test-d.ts``): assertions on types, run by the test runner.+ test_infixes=(".test.", ".spec.", ".test-d."),extensions=frozenset({".ts", ".tsx", ".mts", ".cts"}),grammar_package="tree_sitter_typescript",grammar_loader="language_typescript",diff --git a/packages/core/src/repowise/core/test_paths.py b/packages/core/src/repowise/core/test_paths.pyindex 9332f16c5..0c251c2b3 100644--- a/packages/core/src/repowise/core/test_paths.py+++ b/packages/core/src/repowise/core/test_paths.py@@ -32,10 +32,16 @@ are registry *infixes*, so every extension that will ever exist is covered byconstruction. The suffix-list copies that had to be edited per extension arewhat let #288 regress twice.-**Matching is anchored.** Directory rules compare whole path segments and-filename rules compare stems, never substrings. An unanchored ``test[s_/]``-substring is what made ``src/latest/api.py`` and ``protest/main.py`` read as-tests to community assignment.+**Matching is anchored.** Directory rules compare whole words of a path+segment and filename rules compare stems, never substrings. An unanchored+``test[s_/]`` substring is what made ``src/latest/api.py`` and+``protest/main.py`` read as tests to community assignment. A segment's words are+what ``-``, ``_`` and ``.`` separate, so ``e2e-tests/`` and ``integration_test/``+are test trees while ``latest/`` and ``contest/`` stay single words that are not.++**Filename rules are source rules.** ``test_``, ``_test`` and ``.test.`` name a+test only on a source-language file: ``tsconfig.test.json`` and+``workflows/release.test.yml`` are configuration that happens to share the word."""from __future__ import annotations@@ -46,8 +52,27 @@ from functools import cachefrom pathlib import PurePath, PurePosixPath# Directory segments that mark every file beneath them as test material,-# whatever the filename. ``__test__`` is the Jest variant of ``__tests__``.-_TEST_DIR_TOKENS: frozenset[str] = frozenset({"test", "tests", "__tests__", "__test__", "e2e"})+# whatever the filename. ``__test__`` is the Jest variant of ``__tests__``;+# ``integration_test`` is the directory Flutter's integration tests must live in.+_TEST_DIR_TOKENS: frozenset[str] = frozenset(+ {"test", "tests", "__tests__", "__test__", "e2e", "integration_test"}+)++# A compound segment (``e2e-tests``, ``pkg_tests``, ``client-e2e``) is a test+# tree when its last word, the head of the compound, is one of these. Only the+# head counts: ``test-api/`` (reference docs), ``test-tools/`` and+# ``e2e-project/`` (a generator) are *about* testing. Singular ``test`` is not a+# head word in any spelling, because a compound ending in it names a single+# thing - ``unit-test/`` an executor, ``component-test/`` a generator,+# ``assets_smoke_test/`` an example project, ``svg_test/`` a helper package -+# far more often than it names a suite.+_TEST_DIR_HEAD_WORDS: frozenset[str] = frozenset({"tests", "e2e"})++# GitHub's repository-metadata directory; see ``_classify``.+_REPO_METADATA_DIR = ".github"++# The separators a directory segment splits into words on.+_SEGMENT_WORD_SEPARATORS = re.compile(r"[-_.]+")# Tokens that also name non-test directories in the wild: "spec(s)" is as often# OpenAPI/language specifications as it is RSpec. These count only when the@@ -57,12 +82,15 @@ _TEST_DIR_TOKENS: frozenset[str] = frozenset({"test", "tests", "__tests__", "__t_AMBIGUOUS_TEST_DIR_TOKENS: frozenset[str] = frozenset({"spec", "specs"})# A whole module named for testing and nothing else: Django's per-app-# ``myapp/tests.py``, which no prefix/suffix/infix rule catches. Matched against+# ``myapp/tests.py`` and Rust's ``#[cfg(test)] mod tests;`` in ``tests.rs``,+# which no prefix/suffix/infix rule catches. Those two languages only: elsewhere+# a bare ``test.ts`` or ``test.sh`` is as often an example or a script that+# exercises something by hand as it is a suite. Matched against# the filename's own case, deliberately: a lowercase ``tests.py`` is the# snake_case-module convention, while a capitalised ``Test.java`` is a class# named Test and may be production code - which is why the registry's camel rule-# requires a lowercase boundary and excludes it. Ceiling: if one language ever-# needs to disagree, this moves onto the language specs like everything else here.+# requires a lowercase boundary and excludes it. Ceiling: if a third language+# ever needs it, this moves onto the language specs like everything else here._TEST_EXACT_STEMS: frozenset[str] = frozenset({"test", "tests"})# Directories that hold the scaffolding rather than the tests. These only count@@ -84,12 +112,19 @@ _SUPPORT_DIR_TOKENS: frozenset[str] = frozenset(# the Go convention the toolchain itself reserves - ``go build`` ignores any# directory of that name - which is why it needs no Go file to corroborate it:# the golden files inside are JSON and YAML, and asking the file's own language-# would never fire on them.+# would never fire on them. ``__snapshots__`` is where Jest and Vitest write the+# ``.snap`` output a snapshot test compares against; the ``.test.`` infix used to+# catch those files by accident, and stopped once filename rules became source+# rules.## Support rather than test, deliberately: golden data is what a test reads, not# a test. So the union counts it (#1103's reporter asked for exactly that) while# search, which uses ``is_test_path``, still surfaces the golden file by name.-_SUPPORT_DIR_TOKENS_ANYWHERE: frozenset[str] = frozenset({"__fixtures__", "testdata"})+# Matched on the segment's words run together too, so ``test-data/`` and+# ``test_data/`` are the same golden data rather than a test tree.+_SUPPORT_DIR_TOKENS_ANYWHERE: frozenset[str] = frozenset(+ {"__fixtures__", "__snapshots__", "testdata"}+)@dataclass(frozen=True, slots=True)@@ -99,6 +134,8 @@ class _Conventions:stem_prefixes: tuple[str, ...]stem_suffixes: tuple[str, ...]infixes: tuple[str, ...]+ source_exts: frozenset[str]+ exact_stem_exts: frozenset[str]camel_res: dict[str, re.Pattern[str]]camel_prefix_res: dict[str, re.Pattern[str]]support_stems: frozenset[str]@@ -126,6 +163,8 @@ def _conventions() -> _Conventions:stem_prefixes=tuple(sorted({*REGISTRY.test_stem_prefixes(), "spec_"})),stem_suffixes=REGISTRY.test_stem_suffixes(),infixes=REGISTRY.test_infixes(),+ source_exts=REGISTRY.all_code_extensions(),+ exact_stem_exts=REGISTRY.extensions_for(("python", "rust")),camel_res=REGISTRY.camel_test_res_by_extension(),camel_prefix_res=REGISTRY.camel_test_prefix_res_by_extension(),support_stems=REGISTRY.test_fixture_stems(),@@ -155,14 +194,29 @@ def _parts(path: str) -> tuple[list[str], list[str]]:return original, [seg.lower() for seg in original]+def _words(segment: str) -> list[str]:+ """A lowercased directory segment's words (``e2e-tests`` -> e2e, tests)."""+ return [word for word in _SEGMENT_WORD_SEPARATORS.split(segment) if word]+++def _is_support_anywhere_dir(segment: str) -> bool:+ return (+ segment in _SUPPORT_DIR_TOKENS_ANYWHERE+ or "".join(_words(segment)) in _SUPPORT_DIR_TOKENS_ANYWHERE+ )++def _is_test_name(filename: str) -> bool:"""Whether the filename alone marks a test (test_x.py, x_test.go, x.spec.ts)."""if not filename:return Falserules = _conventions()- if PurePosixPath(filename).stem in _TEST_EXACT_STEMS:- return Truelowered = filename.lower()+ ext = PurePosixPath(lowered).suffix+ if ext not in rules.source_exts:+ return False+ if ext in rules.exact_stem_exts and PurePosixPath(filename).stem in _TEST_EXACT_STEMS:+ return Truestem = PurePosixPath(lowered).stemif (stem.startswith(rules.stem_prefixes)@@ -170,7 +224,6 @@ def _is_test_name(filename: str) -> bool:or any(infix in lowered for infix in rules.infixes)):return True- ext = PurePosixPath(lowered).suffixoriginal_stem = PurePosixPath(filename).stemcamel_re = rules.camel_res.get(ext)if camel_re is not None and camel_re.search(original_stem) is not None:@@ -209,7 +262,11 @@ def _is_test_dir(for seg in segments:if seg in _TEST_DIR_TOKENS:return True- if seg in _AMBIGUOUS_TEST_DIR_TOKENS and (seg in lang_tokens or corroborated):+ words = _words(seg)+ head = words[-1] if words else ""+ if head in _TEST_DIR_HEAD_WORDS:+ return True+ if head in _AMBIGUOUS_TEST_DIR_TOKENS and (head in lang_tokens or corroborated):return Truefor needle in rules.dir_paths:@@ -252,11 +309,18 @@ def _classify(path: str, language: str | None) -> str:named_test = _is_test_name(filename)# A scaffolding directory that needs no test tree around it settles the- # question before the tree rules run. A test-shaped filename still wins, so- # ``testdata/build_test.go`` stays a test.- if not named_test and any(seg in _SUPPORT_DIR_TOKENS_ANYWHERE for seg in segments):+ # question before the tree rules run, ``.github/`` included. A test-shaped+ # filename still wins, so ``testdata/build_test.go`` stays a test.+ if not named_test and any(_is_support_anywhere_dir(seg) for seg in segments):return "support"+ # ``.github/`` holds CI workflows, actions, issue templates and agent+ # instructions, so no directory there makes a test tree, however it is named+ # (``skills/unit-tests/SKILL.md``). Only a test-shaped source file counts:+ # the suite for a custom action's script is still a test.+ if _REPO_METADATA_DIR in segments:+ return "test" if named_test else ""+if not named_test and not _is_test_dir(segments, original_segments, filename, language):return ""diff --git a/tests/unit/analysis/test_kg_skip_logic.py b/tests/unit/analysis/test_kg_skip_logic.pyindex 7bd496e29..dce342555 100644--- a/tests/unit/analysis/test_kg_skip_logic.py+++ b/tests/unit/analysis/test_kg_skip_logic.py@@ -210,7 +210,7 @@ class TestFingerprintDeterminism:one: whoever bumps the constant updates this line and sees, in thediff, that they are asking every existing store to re-curate."""- assert knowledge_graph.KG_BUILDER_VERSION == "4"+ assert knowledge_graph.KG_BUILDER_VERSION == "5"# ---------------------------------------------------------------------------diff --git a/tests/unit/generation/test_layers.py b/tests/unit/generation/test_layers.pyindex c827e4840..ae9e4fa1a 100644--- a/tests/unit/generation/test_layers.py+++ b/tests/unit/generation/test_layers.py@@ -202,8 +202,12 @@ def test_infer_layer_dotnet_test_project_dirs():# Sibling Foo.Tests/ projects are test roots for everything inside.assert infer_layer("Billing.Tests/InvoiceFixture.cs") == "Test"assert infer_layer("src/Billing.Tests/data/sample.json") == "Test"- # Case matters: a lowercase "billing.tests" dir is not the convention.- assert infer_layer("billing.tests/notes.md") != "Test"+ # Case matters for the .NET convention: a lowercase "billing.specs" dir is+ # an ambiguous spec folder, not a test project. (A lowercase+ # "billing.tests" still is a test tree, because "tests" heads the name.)+ assert infer_layer("Billing.Specs/notes.md") == "Test"+ assert infer_layer("billing.specs/notes.md") != "Test"+ assert infer_layer("billing.tests/notes.md") == "Test"# ---------------------------------------------------------------------------diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.pyindex 4e9703d41..94437190a 100644--- a/tests/unit/health/test_model_version_coupling.py+++ b/tests/unit/health/test_model_version_coupling.py@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (# The three stamps as they stand together. Moving either identity model means# moving the analyzer with it, and updating this tuple in the same commit.-_STAMPS = (35, 2, 2)+_STAMPS = (36, 2, 2)def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None:diff --git a/tests/unit/ingestion/test_language_capabilities.py b/tests/unit/ingestion/test_language_capabilities.pyindex fe8f1a99c..c8886034b 100644--- a/tests/unit/ingestion/test_language_capabilities.py+++ b/tests/unit/ingestion/test_language_capabilities.py@@ -57,7 +57,8 @@ class TestParityGoldens:assert set(REGISTRY.test_stem_suffixes()) == {"_test", "_spec", "_unittest"}def test_test_infixes_match_historical_set(self) -> None:- assert set(REGISTRY.test_infixes()) == {".test.", ".spec."}+ # `.test-d.` is the TypeScript type-test infix (`*.test-d.ts`).+ assert set(REGISTRY.test_infixes()) == {".test.", ".spec.", ".test-d."}def test_test_fixture_stems_match_historical_set(self) -> None:assert frozenset(diff --git a/tests/unit/test_test_paths.py b/tests/unit/test_test_paths.pyindex 74027cec0..7126e5b95 100644--- a/tests/unit/test_test_paths.py+++ b/tests/unit/test_test_paths.py@@ -92,6 +92,74 @@ _CORPUS: tuple[tuple[str, str | None, str], ...] = (("app/fixtures/premier_league.py", None, ""),("src/testdata_loader.py", None, ""),("tests/testdata/golden.json", None, "support"),+ # compound directory names: a segment's words split on - _ . and its last+ # word, the head of the compound, says what the directory holds+ ("e2e-tests/pages/login.ts", None, "test"),+ ("e2e-tests/playwright.config.ts", None, "test"),+ ("integration_test/driver.dart", None, "test"), # flutter+ ("mylib/mylib_tests/core/__init__.py", None, "test"),+ ("extensions/api-tests/package.json", None, "test"),+ ("extensions/api-tests/workspace/image.png", None, "test"),+ ("apps/client-e2e/src/app.cy.ts", None, "test"),+ ("apps/client-e2e/src/support/commands.ts", None, "support"),+ ("mylib/unit.tests/run.py", None, "test"),+ # ...so a word that merely contains a test token, or a compound that is+ # *about* testing, is not a test tree+ ("src/latest-release/api.py", None, ""),+ ("contest_results/main.py", None, ""),+ ("docs/test-api/class-test.md", None, ""),+ ("docs/test-tools/index.md", None, ""),+ ("src/generators/e2e-project/index.ts", None, ""),+ # singular `test` heads a compound naming one thing - a generator, an+ # executor, a package, an example project - in either spelling+ ("src/generators/component-test/index.ts", None, ""),+ ("src/executors/unit-test/schema.json", None, ""),+ ("packages/runner-test/cli.js", None, ""),+ ("examples/assets_smoke_test/setup.py", None, ""),+ ("third_party/packages/svg_test/lib/src/finders.dart", None, ""),+ ("example/integration_test/app_test.dart", None, "test"),+ # a library for writing tests is that library's production code+ ("packages/testing-library/src/index.ts", None, ""),+ ("src/testing_library/render.py", None, ""),+ ("src/test-utils/render.ts", None, ""),+ # `test-data` / `test_data` are golden data, the same as Go's `testdata`+ ("test-data/users.json", None, "support"),+ ("pkg/test_data/input.csv", None, "support"),+ ("tests/test-data/expected.yaml", None, "support"),+ # `spec` still needs corroboration when it heads a compound+ ("openapi-specs/users.yaml", None, ""),+ ("acceptance-specs/users.rb", "ruby", "test"),+ # filename rules are source rules: configuration that shares the word is+ # not a test+ ("tsconfig.test.json", None, ""),+ ("packages/app/tsconfig.spec.json", None, ""),+ ("config/app.test.yaml", None, ""),+ ("resources/rpm/app.spec.template", None, ""),+ ("src/generators/files/__name__.spec.ts__tmpl__", None, ""),+ ("conf/app_template/tests.py-tpl", None, ""),+ # ...except snapshot output, which Jest/Vitest's `__snapshots__` owns+ ("src/components/__snapshots__/Button.test.tsx.snap", None, "support"),+ # tsd / vitest type tests+ ("src/types/index.test-d.ts", None, "test"),+ # a bare `test`/`tests` stem is a suite in Python and Rust only; `test.ts` in an+ # examples folder is an example, and `scripts/test.sh` runs the suite+ ("examples/basic/src/test.ts", None, ""),+ ("src/common/test.ts", None, ""),+ ("scripts/test.sh", None, ""),+ ("examples/basic/tests/test.ts", None, "test"),+ ("scripts/test.py", None, "test"),+ ("crates/ide/src/tests.rs", None, "test"),+ ("lib/plug/test.ex", None, ""),+ # repository metadata is never test material, whatever it is named+ (".github/workflows/ci.test.yml", None, ""),+ (".github/workflows/tests.yml", None, ""),+ (".github/skills/unit-tests/SKILL.md", None, ""),+ (".github/workflows/data/test_settings.py.tpl", None, ""),+ # ...but a test of an action's own script is still a test+ (".github/actions/notify/notify.test.mjs", None, "test"),+ # Support directories count anywhere, .github included.+ (".github/test-data/expected.json", None, "support"),+ (".github/actions/notify/__snapshots__/Button.snap", None, "support"),)