diff --git a/LESSONS.md b/LESSONS.md index d233653ac..5b65b3d71 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -1436,6 +1436,14 @@ each one. f97415bc0. Its body lists the gui tests and vet it ran, and neither guard's package. A new package-level var runs `go test .`; a new route runs internal/cli's parity test. + 11-15: 8f3f7a46a (Qianfan Token Plan) put two package-level regexps + in internal/provider and ran provider, gui and cli, so the root guard + was red on ubuntu CI until 022ffd09e. Done since, as 11-12 asked: + testenv's TestMain checks the package's own files after its tests + (testenv.eagerHere), so the 65 packages that run through testenv, + every one this guard caught before among them, fail their own run, + a `-run` of one test included. Broken by hand: a probe file in + internal/update failed `go test -run NoSuchTest ./internal/update`. - **11-13, criteria still missed.** - A budget's pass recorded at a tree it wasn't run at: 5a0e0378c's body says TestCoreScriptBudget passed "rebased on 7583ed65c", but diff --git a/internal/testenv/eager.go b/internal/testenv/eager.go new file mode 100644 index 000000000..382979c8a --- /dev/null +++ b/internal/testenv/eager.go @@ -0,0 +1,63 @@ +package testenv + +import ( + "fmt" + "go/ast" + "go/parser" + "go/token" + "path/filepath" + "strings" +) + +// EagerRegexps lists each package-level regexp.MustCompile (or Compile, +// MustCompilePOSIX) outside a function in the Go file at path, as +// file:line:col. Every package's init runs on every launch, so such a +// pattern is compiled for a path most launches never take; lazyre.New +// compiles on first use. A file that doesn't parse lists nothing: that is +// the compiler's to judge. +func EagerRegexps(path string) []string { + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) + if err != nil { + return nil + } + var bad []string + for _, d := range f.Decls { + gd, ok := d.(*ast.GenDecl) + if !ok || gd.Tok != token.VAR { + continue + } + ast.Inspect(gd, func(n ast.Node) bool { + if _, ok := n.(*ast.FuncLit); ok { + return false + } + if se, ok := n.(*ast.SelectorExpr); ok { + if id, ok := se.X.(*ast.Ident); ok && id.Name == "regexp" && (se.Sel.Name == "MustCompile" || se.Sel.Name == "Compile" || se.Sel.Name == "MustCompilePOSIX") { + bad = append(bad, fset.Position(se.Pos()).String()) + } + } + return true + }) + } + return bad +} + +// eagerHere is EagerRegexps over the package being tested (go test runs a +// package's tests in its own folder). The root package's +// TestNoEagerPackageRegexps checks every package, but a change runs its +// own packages, and that guard went red on main on six days (#6020, +// #6145, 4847a087f, 10e783080, 7869a454c, 022ffd09e): here it fails the +// run of the package that added the pattern, a -run of one test included. +func eagerHere() error { + files, _ := filepath.Glob("*.go") + var bad []string + for _, f := range files { + if !strings.HasSuffix(f, "_test.go") { + bad = append(bad, EagerRegexps(f)...) + } + } + if len(bad) == 0 { + return nil + } + return fmt.Errorf("a package-level regexp is compiled at launch; use lazyre.New:\n\t%s", strings.Join(bad, "\n\t")) +} diff --git a/internal/testenv/eager_test.go b/internal/testenv/eager_test.go new file mode 100644 index 000000000..bda84231e --- /dev/null +++ b/internal/testenv/eager_test.go @@ -0,0 +1,35 @@ +package testenv + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// A package-level pattern is listed; one inside a function, a func +// literal's, or a lazyre.New is not. +func TestEagerRegexps(t *testing.T) { + f := filepath.Join(t.TempDir(), "x.go") + src := `package x + +import "regexp" + +var a = regexp.MustCompile("a") + +var ( + b, c = regexp.Compile("b") + d = func() *regexp.Regexp { return regexp.MustCompile("d") } + e = lazyre.New("e") +) + +func g() { _ = regexp.MustCompile("g") } +` + if err := os.WriteFile(f, []byte(src), 0o644); err != nil { + t.Fatal(err) + } + got := EagerRegexps(f) + if len(got) != 2 || !strings.HasSuffix(got[0], ":5:9") || !strings.HasSuffix(got[1], ":8:9") { + t.Fatalf("got %q, want lines 5 and 8", got) + } +} diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 5c6ffd77a..253494fc5 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -99,7 +99,12 @@ func RunIn(m *testing.M, setup func(home string)) int { if setup != nil { setup(home) } - return m.Run() + code := m.Run() + if err := eagerHere(); err != nil { + fmt.Fprintln(os.Stderr, "testenv:", err) + return 1 + } + return code } // Isolate points the home and the config, cache and data folders into a diff --git a/lazyre_test.go b/lazyre_test.go index 89213ad48..dcc515919 100644 --- a/lazyre_test.go +++ b/lazyre_test.go @@ -1,15 +1,13 @@ package main import ( - "go/ast" - "go/parser" - "go/token" "io/fs" "path/filepath" "strings" "testing" "github.com/yetone/magpie/internal/lazyre" + "github.com/yetone/magpie/internal/testenv" ) // TestAllLazyPatternsCompile compiles every regexp the binary's packages @@ -25,7 +23,8 @@ func TestAllLazyPatternsCompile(t *testing.T) { // (or regexp.Compile) outside a function: every package's init runs on // every launch, `magpie statusline` on each Claude Code message included, // so such a pattern is compiled for a path most launches never take. Use -// lazyre.New, which compiles on first use. +// lazyre.New, which compiles on first use. Each package that runs its +// tests through testenv checks its own files too (testenv.eagerHere). func TestNoEagerPackageRegexps(t *testing.T) { var bad []string err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error { @@ -41,28 +40,7 @@ func TestNoEagerPackageRegexps(t *testing.T) { if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { return nil } - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) - if err != nil { - return nil // not this test's to judge - } - for _, d := range f.Decls { - gd, ok := d.(*ast.GenDecl) - if !ok || gd.Tok != token.VAR { - continue - } - ast.Inspect(gd, func(n ast.Node) bool { - if _, ok := n.(*ast.FuncLit); ok { - return false - } - if se, ok := n.(*ast.SelectorExpr); ok { - if id, ok := se.X.(*ast.Ident); ok && id.Name == "regexp" && (se.Sel.Name == "MustCompile" || se.Sel.Name == "Compile" || se.Sel.Name == "MustCompilePOSIX") { - bad = append(bad, fset.Position(se.Pos()).String()) - } - } - return true - }) - } + bad = append(bad, testenv.EagerRegexps(path)...) return nil }) if err != nil {