package api import ( "os" "path/filepath" "regexp" "strings" "testing" ) // Review finding 6: a helper named `…ForTest` is a way around the product's rules (SetEndNoticeForTest writes „told” // with any date; SetSendFnForTest replaces the mail service). Some must live in production files because another // package's tests call them. This test reads every non-test Go file of the hub and fails when one CALLS such a // helper — a definition (`func (…) XForTest(`) is not a call. func TestReview6_NoProductionFileCallsATestOnlyHelper(t *testing.T) { root := filepath.Join("..", "..") call := regexp.MustCompile(`\b[A-Za-z0-9_]+ForTest\(`) def := regexp.MustCompile(`^func (\([^)]*\) )?[A-Za-z0-9_]+ForTest\(`) files, defs := 0, 0 err := filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error { if err != nil { return err } if d.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") { return nil } b, rerr := os.ReadFile(p) if rerr != nil { return rerr } files++ for i, line := range strings.Split(string(b), "\n") { if def.MatchString(line) { defs++ continue } if tr := strings.TrimSpace(line); strings.HasPrefix(tr, "//") { continue } if call.MatchString(line) { t.Errorf("%s:%d calls a test-only helper from production code: %s", p, i+1, strings.TrimSpace(line)) } } return nil }) if err != nil { t.Fatal(err) } if files < 100 || defs < 5 { t.Fatalf("read %d files and found %d …ForTest definitions — the scan stopped reading the hub", files, defs) } }