Skip to content

fix(go): credit package-level test coverage instead of name-pairing only#633

Open
aliscott wants to merge 1 commit into
peteromallet:mainfrom
aliscott:fix/go-package-level-test-coverage
Open

fix(go): credit package-level test coverage instead of name-pairing only#633
aliscott wants to merge 1 commit into
peteromallet:mainfrom
aliscott:fix/go-package-level-test-coverage

Conversation

@aliscott

@aliscott aliscott commented Jul 4, 2026

Copy link
Copy Markdown

Problem

Go's test-coverage mapping only pairs test files to production files by name (foo_test.go -> foo.go). But Go compiles a directory as one package and go test reports coverage package-wide: a test file can exercise any identifier in its package regardless of which file defines it.

On a real Go repo this produced a cluster of false untested_module findings: internal/ingest/ingest_test.go (a 1,000-line suite) covers plan.go (91% by go tool cover), verify.go (100%), match.go (90%) - yet all were flagged as untested because no file named plan_test.go exists. Test health scored 38% on a package with 90 passing tests.

desloppify scan --path .   # Go repo, package-level test suites
# -> 23 untested_module findings, most false

A blind-review subagent independently flagged the same thing: "the detector flags files like plan.go, verify.go, match.go as 'untested modules' because they lack matching *_test.go filenames, but Go package-level tests cover them well".

Fix

  • Add an optional plural map_test_to_sources(test_path, production_set) -> set[str] language hook, consumed by naming_based_mapping and get_test_files_for_prod. Languages that don't define it are unaffected.
  • Implement it for Go: a _test.go file maps to every production .go file in its directory. This mirrors the language's own semantics - both in-package and external (package foo_test) test files compile and run against exactly that package, and go test -cover attributes coverage package-wide.

Verified against the same Go repo: false positives drop from 25 to 11 findings (the remainder are genuinely untested packages), test health 43.8% -> 81.1%.

Tests added:

  • tests/lang/go/test_go_test_coverage.py: package-wide mapping, non-test files ignored
  • tests/detectors/coverage/test_test_coverage_mapping_import_and_logic.py: engine credits whole Go package; absent hook is a no-op for other languages

pytest desloppify/tests/lang/go/ desloppify/tests/detectors/coverage/ - 200 passed. Full suite: 2916 passed; the 3 test_bash_unused_imports.py failures also occur on a clean checkout in this environment (macOS bash) and are unrelated.

Go compiles a directory as one package and 'go test' reports coverage
package-wide: a test file can exercise any identifier in its package
regardless of which file defines it. The Go mapper only paired
foo_test.go -> foo.go by name, so files covered by a package's suite
under a different test filename (e.g. ingest_test.go covering plan.go,
verify.go, run.go) were flagged untested_module.

Adds an optional plural map_test_to_sources language hook consumed by
naming_based_mapping and get_test_files_for_prod, and implements it for
Go as same-directory production files. Languages without the hook are
unaffected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant