oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package guards
2
3import (
4 "strings"
5 "testing"
6)
7
8// testOnlyPackages are the helper packages that import testing from a
9// non-test file. Each one's doc comment already says so — "this package
10// must never be imported from non-test code: doing so would drag the
11// testing package into the production binary" — but a comment is not a
12// guard.
13//
14// It is a slice rather than a single constant because there are now two:
15// gate 9's logtest and gate 1's sealtest. A guard that covers one of two
16// identical hazards is the shape of bug this repo keeps finding.
17var testOnlyPackages = []string{
18 "amadan.net/oficina/home/internal/home/logtest",
19 "amadan.net/oficina/home/internal/home/sealtest",
20}
21
22// TestTestOnlyPackagesNotImportedByProductionCode fails on any
23// non-_test.go file that imports one of them, or anything under it: a
24// future subpackage must inherit the rule rather than fall outside it on
25// an exact match.
26//
27// An earlier version of this comment said the import registers test
28// flags on the default flag set. It does not: since Go 1.13 those are
29// registered by testing.Init, which only the generated test main calls.
30// The true claim is narrower and is the one Sheets' original makes —
31// linking testing into a deployed binary is binary hygiene we do not
32// want to argue about per-release, and nothing announces it at build
33// time.
34func TestTestOnlyPackagesNotImportedByProductionCode(t *testing.T) {
35 root := repoRoot(t)
36 for _, path := range goFiles(t, root) {
37 if strings.HasSuffix(path, "_test.go") {
38 continue
39 }
40 rel := relPath(t, root, path)
41 for _, imp := range fileImports(t, path) {
42 for _, want := range testOnlyPackages {
43 if imp == want || strings.HasPrefix(imp, want+"/") {
44 t.Errorf("%s: imports %q from non-test code (it imports testing, so it must stay test-only)", rel, want)
45 }
46 }
47 }
48 }
49}
50