Files
magpie/lazyre_test.go
T
yetone 500f39886e testenv: a package's own run fails on a package-level regexp it compiles at launch
TestNoEagerPackageRegexps lives in the root package, and a change runs
its own packages, so the guard went red on main on six days: #6020,
#6145, 4847a087f, 10e783080, 7869a454c and today 8f3f7a46a (fixed in
022ffd09e). LESSONS.md asked on 11-12 for the check to run where the
work already runs; writing the rule again hadn't held.

testenv.RunIn (every testenv.Main/Run TestMain, 65 packages, provider,
gui, update, library and catalog among them) now checks the package's
own non-test files after its tests and exits 1 naming each pattern. It
runs on a `-run` of one test too. The parsing moved into
testenv.EagerRegexps, which the root guard now calls, so both read the
same rule. A child test binary (Marker set) doesn't check again.

Run on mote at this tree (origin/main 022ffd09e + this):
- go test -count=1 ./internal/testenv .: ok (TestEagerRegexps new:
  lines 5 and 8 listed; a func's, a func literal's and lazyre.New not)
- Broken by hand: zz_probe.go with `var eagerProbe =
  regexp.MustCompile("x")` in internal/update; `go test -run
  TestNothingMatchesThis ./internal/update` FAIL with "testenv: a
  package-level regexp is compiled at launch; use lazyre.New:
  zz_probe.go:5:18"; removed, ok.
- GOOS=windows go vet and GOOS=linux go vet -tags nogui of
  ./internal/testenv and .: clean
- gofmt -l: clean
Not run: every testenv package; the root guard passing at this tree
says none of them has such a pattern, so none fails on the new check.
2026-11-16 02:42:56 +08:00

53 lines
1.6 KiB
Go

package main
import (
"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
// compile on first use (internal/lazyre), so a bad one fails here and not
// on a user's machine.
func TestAllLazyPatternsCompile(t *testing.T) {
if n := lazyre.MustAll(); n < 500 {
t.Fatalf("only %d lazy patterns registered; the binary's packages hold more", n)
}
}
// TestNoEagerPackageRegexps fails on a package-level regexp.MustCompile
// (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. 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 {
if err != nil {
return err
}
if d.IsDir() {
if path != "." && (strings.HasPrefix(d.Name(), ".") || d.Name() == "testdata" || d.Name() == "node_modules" || d.Name() == "site") {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
bad = append(bad, testenv.EagerRegexps(path)...)
return nil
})
if err != nil {
t.Fatal(err)
}
for _, b := range bad {
t.Errorf("%s: a package-level regexp is compiled at launch; use lazyre.New", b)
}
}