package guards import ( "strings" "testing" ) // testOnlyPackages are the helper packages that import testing from a // non-test file. Each one's doc comment already says so — "this package // must never be imported from non-test code: doing so would drag the // testing package into the production binary" — but a comment is not a // guard. // // It is a slice rather than a single constant because there are now two: // gate 9's logtest and gate 1's sealtest. A guard that covers one of two // identical hazards is the shape of bug this repo keeps finding. var testOnlyPackages = []string{ "amadan.net/oficina/home/internal/home/logtest", "amadan.net/oficina/home/internal/home/sealtest", } // TestTestOnlyPackagesNotImportedByProductionCode fails on any // non-_test.go file that imports one of them, or anything under it: a // future subpackage must inherit the rule rather than fall outside it on // an exact match. // // An earlier version of this comment said the import registers test // flags on the default flag set. It does not: since Go 1.13 those are // registered by testing.Init, which only the generated test main calls. // The true claim is narrower and is the one Sheets' original makes — // linking testing into a deployed binary is binary hygiene we do not // want to argue about per-release, and nothing announces it at build // time. func TestTestOnlyPackagesNotImportedByProductionCode(t *testing.T) { root := repoRoot(t) for _, path := range goFiles(t, root) { if strings.HasSuffix(path, "_test.go") { continue } rel := relPath(t, root, path) for _, imp := range fileImports(t, path) { for _, want := range testOnlyPackages { if imp == want || strings.HasPrefix(imp, want+"/") { t.Errorf("%s: imports %q from non-test code (it imports testing, so it must stay test-only)", rel, want) } } } } }