tests: read package sources without the deprecated parser.ParseDir
go/parser.ParseDir is deprecated since Go 1.25. Its suggested replacement, go/packages, applies build tags, and these guards need the files a build leaves out as well, so each one now lists the directory and parses every .go file itself. The sets of files match ParseDir's in all five directories, checked by comparing the two on this tree. The directory read still fails loudly when a directory is missing, which a glob would not.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+173
−139
internal/activator/claimgen_test.go+14 −22internal/activator/tenancies_test.go+49 −33internal/boxroles/imports_test.go+16 −8internal/console/svcroutes_test.go+16 −14internal/groupadmission/state_test.go+14 −8move_offline_run_test.go+64 −54
diff --git a/internal/activator/claimgen_test.go b/internal/activator/claimgen_test.go| index 69a692689..dace90c2b 100644 |
| --- a/internal/activator/claimgen_test.go |
| +++ b/internal/activator/claimgen_test.go |
| @@ -5,9 +5,7 @@ import ( |
| "errors" |
| "fmt" |
| "go/ast" |
| - "go/parser" |
| "go/token" |
| - "io/fs" |
| "os/exec" |
| "strings" |
| "testing" |
| @@ -426,27 +424,21 @@ func TestEveryClaimsBackedSwapIsRefusedBeforeItsSideEffects(t *testing.T) { |
| func TestClaimSpawnIsATestSeam(t *testing.T) { |
| t.Parallel() |
| fset := token.NewFileSet() |
| - pkgs, err := parser.ParseDir(fset, ".", func(fi fs.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") }, 0) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - for _, pkg := range pkgs { |
| - for name, file := range pkg.Files { |
| - ast.Inspect(file, func(n ast.Node) bool { |
| - switch x := n.(type) { |
| - case *ast.AssignStmt: |
| - for _, l := range x.Lhs { |
| - if sel, ok := l.(*ast.SelectorExpr); ok && sel.Sel.Name == "claimSpawn" { |
| - t.Errorf("%s: %s assigns claimSpawn", name, fset.Position(x.Pos())) |
| - } |
| - } |
| - case *ast.KeyValueExpr: |
| - if id, ok := x.Key.(*ast.Ident); ok && id.Name == "claimSpawn" { |
| - t.Errorf("%s: %s sets claimSpawn in a literal", name, fset.Position(x.Pos())) |
| + for name, file := range nonTestGoFiles(t, fset) { |
| + ast.Inspect(file, func(n ast.Node) bool { |
| + switch x := n.(type) { |
| + case *ast.AssignStmt: |
| + for _, l := range x.Lhs { |
| + if sel, ok := l.(*ast.SelectorExpr); ok && sel.Sel.Name == "claimSpawn" { |
| + t.Errorf("%s: %s assigns claimSpawn", name, fset.Position(x.Pos())) |
| } |
| } |
| - return true |
| - }) |
| - } |
| + case *ast.KeyValueExpr: |
| + if id, ok := x.Key.(*ast.Ident); ok && id.Name == "claimSpawn" { |
| + t.Errorf("%s: %s sets claimSpawn in a literal", name, fset.Position(x.Pos())) |
| + } |
| + } |
| + return true |
| + }) |
| } |
| } |
diff --git a/internal/activator/tenancies_test.go b/internal/activator/tenancies_test.go| index 217edd0c6..09bed7b31 100644 |
| --- a/internal/activator/tenancies_test.go |
| +++ b/internal/activator/tenancies_test.go |
| @@ -5,7 +5,7 @@ import ( |
| "go/ast" |
| "go/parser" |
| "go/token" |
| - "io/fs" |
| + "os" |
| "strings" |
| "testing" |
| "time" |
| @@ -120,49 +120,65 @@ func TestTenanciesRouteChangedInPlace(t *testing.T) { |
| } |
| } |
| +// nonTestGoFiles parses every non-test .go file in the package directory, |
| +// keyed by name. Build tags do not narrow it: a guard here reads the files |
| +// this platform's build leaves out too. |
| +func nonTestGoFiles(t *testing.T, fset *token.FileSet) map[string]*ast.File { |
| + t.Helper() |
| + entries, err := os.ReadDir(".") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + files := map[string]*ast.File{} |
| + for _, e := range entries { |
| + name := e.Name() |
| + if e.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { |
| + continue |
| + } |
| + f, err := parser.ParseFile(fset, name, nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + files[name] = f |
| + } |
| + return files |
| +} |
| + |
| // TestEntriesEnterThroughPutEntryLocked: putEntryLocked is how the host's |
| // tenants are recorded, so an entry inserted any other way would be a |
| // tenant Tenancies never saw. Fails on any other assignment into |
| // a.entries in the package's production code. |
| func TestEntriesEnterThroughPutEntryLocked(t *testing.T) { |
| fset := token.NewFileSet() |
| - pkgs, err := parser.ParseDir(fset, ".", func(fi fs.FileInfo) bool { |
| - return !strings.HasSuffix(fi.Name(), "_test.go") |
| - }, 0) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| found := 0 |
| - for _, pkg := range pkgs { |
| - for _, f := range pkg.Files { |
| - for _, d := range f.Decls { |
| - fn, ok := d.(*ast.FuncDecl) |
| - if !ok || fn.Body == nil { |
| - continue |
| + for _, f := range nonTestGoFiles(t, fset) { |
| + for _, d := range f.Decls { |
| + fn, ok := d.(*ast.FuncDecl) |
| + if !ok || fn.Body == nil { |
| + continue |
| + } |
| + ast.Inspect(fn.Body, func(n ast.Node) bool { |
| + as, ok := n.(*ast.AssignStmt) |
| + if !ok { |
| + return true |
| } |
| - ast.Inspect(fn.Body, func(n ast.Node) bool { |
| - as, ok := n.(*ast.AssignStmt) |
| + for _, lhs := range as.Lhs { |
| + ix, ok := lhs.(*ast.IndexExpr) |
| if !ok { |
| - return true |
| + continue |
| } |
| - for _, lhs := range as.Lhs { |
| - ix, ok := lhs.(*ast.IndexExpr) |
| - if !ok { |
| - continue |
| - } |
| - sel, ok := ix.X.(*ast.SelectorExpr) |
| - if !ok || sel.Sel.Name != "entries" { |
| - continue |
| - } |
| - if fn.Name.Name == "putEntryLocked" { |
| - found++ |
| - continue |
| - } |
| - t.Errorf("%s: %s assigns into entries directly; use putEntryLocked", fset.Position(as.Pos()), fn.Name.Name) |
| + sel, ok := ix.X.(*ast.SelectorExpr) |
| + if !ok || sel.Sel.Name != "entries" { |
| + continue |
| } |
| - return true |
| - }) |
| - } |
| + if fn.Name.Name == "putEntryLocked" { |
| + found++ |
| + continue |
| + } |
| + t.Errorf("%s: %s assigns into entries directly; use putEntryLocked", fset.Position(as.Pos()), fn.Name.Name) |
| + } |
| + return true |
| + }) |
| } |
| } |
| if found != 1 { |
diff --git a/internal/boxroles/imports_test.go b/internal/boxroles/imports_test.go| index 8f481bb0e..adad1417b 100644 |
| --- a/internal/boxroles/imports_test.go |
| +++ b/internal/boxroles/imports_test.go |
| @@ -3,6 +3,8 @@ package boxroles |
| import ( |
| "go/parser" |
| "go/token" |
| + "os" |
| + "path/filepath" |
| "strconv" |
| "strings" |
| "testing" |
| @@ -10,17 +12,23 @@ import ( |
| func TestAdmissionCoreImports(t *testing.T) { |
| for _, dir := range []string{".", "../groupadmission"} { |
| - pkgs, err := parser.ParseDir(token.NewFileSet(), dir, nil, parser.ImportsOnly) |
| + entries, err := os.ReadDir(dir) |
| if err != nil { |
| t.Fatal(err) |
| } |
| - for _, p := range pkgs { |
| - for name, f := range p.Files { |
| - for _, imp := range f.Imports { |
| - path, _ := strconv.Unquote(imp.Path.Value) |
| - if strings.Contains(path, "aws") || strings.HasSuffix(path, "/release") { |
| - t.Errorf("%s imports %s", name, path) |
| - } |
| + for _, e := range entries { |
| + if e.IsDir() || !strings.HasSuffix(e.Name(), ".go") { |
| + continue |
| + } |
| + name := filepath.Join(dir, e.Name()) |
| + f, err := parser.ParseFile(token.NewFileSet(), name, nil, parser.ImportsOnly) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, imp := range f.Imports { |
| + path, _ := strconv.Unquote(imp.Path.Value) |
| + if strings.Contains(path, "aws") || strings.HasSuffix(path, "/release") { |
| + t.Errorf("%s imports %s", name, path) |
| } |
| } |
| } |
diff --git a/internal/console/svcroutes_test.go b/internal/console/svcroutes_test.go| index 3493b034f..7fd82b1bd 100644 |
| --- a/internal/console/svcroutes_test.go |
| +++ b/internal/console/svcroutes_test.go |
| @@ -946,24 +946,26 @@ func scanCLISource(t *testing.T) *cliSource { |
| func parseConsoleSource(t *testing.T) (*token.FileSet, []*ast.File) { |
| t.Helper() |
| fset := token.NewFileSet() |
| - pkgs, err := parser.ParseDir(fset, ".", func(fi os.FileInfo) bool { |
| - return !strings.HasSuffix(fi.Name(), "_test.go") |
| - }, 0) |
| + entries, err := os.ReadDir(".") |
| if err != nil { |
| t.Fatalf("parse package: %v", err) |
| } |
| - pkg, ok := pkgs["console"] |
| - if !ok { |
| - t.Fatal("package console not found by the parser") |
| - } |
| - names := make([]string, 0, len(pkg.Files)) |
| - for name := range pkg.Files { |
| - names = append(names, name) |
| + var files []*ast.File // in name order: ReadDir sorts |
| + for _, e := range entries { |
| + name := e.Name() |
| + if e.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { |
| + continue |
| + } |
| + f, err := parser.ParseFile(fset, name, nil, 0) |
| + if err != nil { |
| + t.Fatalf("parse package: %v", err) |
| + } |
| + if f.Name.Name == "console" { |
| + files = append(files, f) |
| + } |
| } |
| - sort.Strings(names) |
| - files := make([]*ast.File, 0, len(names)) |
| - for _, name := range names { |
| - files = append(files, pkg.Files[name]) |
| + if len(files) == 0 { |
| + t.Fatal("package console not found by the parser") |
| } |
| return fset, files |
| } |
diff --git a/internal/groupadmission/state_test.go b/internal/groupadmission/state_test.go| index 11a63cbd1..d089ed999 100644 |
| --- a/internal/groupadmission/state_test.go |
| +++ b/internal/groupadmission/state_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "errors" |
| "go/parser" |
| "go/token" |
| + "os" |
| "strconv" |
| "strings" |
| "sync" |
| @@ -124,17 +125,22 @@ func TestAdmissionCloseWins(t *testing.T) { |
| } |
| } |
| func TestAdmissionCoreImports(t *testing.T) { |
| - pkgs, err := parser.ParseDir(token.NewFileSet(), ".", nil, parser.ImportsOnly) |
| + entries, err := os.ReadDir(".") |
| if err != nil { |
| t.Fatal(err) |
| } |
| - for _, p := range pkgs { |
| - for _, f := range p.Files { |
| - for _, imp := range f.Imports { |
| - v, _ := strconv.Unquote(imp.Path.Value) |
| - if strings.Contains(v, "aws") { |
| - t.Fatal(v) |
| - } |
| + for _, e := range entries { |
| + if e.IsDir() || !strings.HasSuffix(e.Name(), ".go") { |
| + continue |
| + } |
| + f, err := parser.ParseFile(token.NewFileSet(), e.Name(), nil, parser.ImportsOnly) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, imp := range f.Imports { |
| + v, _ := strconv.Unquote(imp.Path.Value) |
| + if strings.Contains(v, "aws") { |
| + t.Fatal(v) |
| } |
| } |
| } |
diff --git a/move_offline_run_test.go b/move_offline_run_test.go| index 93a5505ea..ce557b0b9 100644 |
| --- a/move_offline_run_test.go |
| +++ b/move_offline_run_test.go |
| @@ -1335,10 +1335,22 @@ func TestMoveOfflineRunGateBoundaries(t *testing.T) { |
| // added on this side fails here. |
| func TestRunDoorsWireIsSingleShot(t *testing.T) { |
| fset := token.NewFileSet() |
| - pkgs, err := parser.ParseDir(fset, ".", func(fi os.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") }, 0) |
| + entries, err := os.ReadDir(".") |
| if err != nil { |
| t.Fatal(err) |
| } |
| + files := map[string]*ast.File{} |
| + for _, e := range entries { |
| + name := e.Name() |
| + if e.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { |
| + continue |
| + } |
| + f, err := parser.ParseFile(fset, name, nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + files[name] = f |
| + } |
| recv := func(fn *ast.FuncDecl) string { |
| if fn.Recv == nil || len(fn.Recv.List) == 0 { |
| return "" |
| @@ -1360,66 +1372,64 @@ func TestRunDoorsWireIsSingleShot(t *testing.T) { |
| return false |
| } |
| roundTrips, httpDos := map[string]int{}, 0 |
| - for _, pkg := range pkgs { |
| - for name, f := range pkg.Files { |
| - run := name == "move_offline_run.go" |
| - for _, decl := range f.Decls { |
| - fn, ok := decl.(*ast.FuncDecl) |
| - if !ok || fn.Body == nil { |
| - continue |
| + for name, f := range files { |
| + run := name == "move_offline_run.go" |
| + for _, decl := range f.Decls { |
| + fn, ok := decl.(*ast.FuncDecl) |
| + if !ok || fn.Body == nil { |
| + continue |
| + } |
| + where := recv(fn) + "." + fn.Name.Name |
| + ast.Inspect(fn.Body, func(n ast.Node) bool { |
| + if n == nil { |
| + return true |
| } |
| - where := recv(fn) + "." + fn.Name.Name |
| - ast.Inspect(fn.Body, func(n ast.Node) bool { |
| - if n == nil { |
| + if loop(n) && ((run && where != ".ctrlC") || where == "mover.roundTrip") { |
| + t.Errorf("%s: %s has a loop", name, where) |
| + } |
| + switch n := n.(type) { |
| + case *ast.SelectStmt: |
| + if run && where != ".ctrlC" { |
| + t.Errorf("%s: %s selects: the run waits only in the Runner's loop", name, where) |
| + } |
| + case *ast.SelectorExpr: |
| + if id, ok := n.X.(*ast.Ident); run && ok && id.Name == "http" && strings.HasPrefix(n.Sel.Name, "Method") && |
| + (n.Sel.Name != "MethodGet" || (where != "runWire.Do" && where != ".probeOnce")) { |
| + t.Errorf("%s: %s names http.%s: the doors choose what is sent", name, where, n.Sel.Name) |
| + } |
| + case *ast.CallExpr: |
| + sel, ok := n.Fun.(*ast.SelectorExpr) |
| + if !ok { |
| return true |
| } |
| - if loop(n) && ((run && where != ".ctrlC") || where == "mover.roundTrip") { |
| - t.Errorf("%s: %s has a loop", name, where) |
| + if where == "mover.roundTrip" && sel.Sel.Name == "Do" { |
| + httpDos++ |
| } |
| - switch n := n.(type) { |
| - case *ast.SelectStmt: |
| - if run && where != ".ctrlC" { |
| - t.Errorf("%s: %s selects: the run waits only in the Runner's loop", name, where) |
| - } |
| - case *ast.SelectorExpr: |
| - if id, ok := n.X.(*ast.Ident); run && ok && id.Name == "http" && strings.HasPrefix(n.Sel.Name, "Method") && |
| - (n.Sel.Name != "MethodGet" || (where != "runWire.Do" && where != ".probeOnce")) { |
| - t.Errorf("%s: %s names http.%s: the doors choose what is sent", name, where, n.Sel.Name) |
| - } |
| - case *ast.CallExpr: |
| - sel, ok := n.Fun.(*ast.SelectorExpr) |
| - if !ok { |
| - return true |
| - } |
| - if where == "mover.roundTrip" && sel.Sel.Name == "Do" { |
| - httpDos++ |
| - } |
| - if !run { |
| - return true |
| - } |
| - switch sel.Sel.Name { |
| - case "roundTrip": |
| - roundTrips[where]++ |
| - case "Post", "Nudge", "Status", "Do": |
| - if where == ".probeOnce" && sel.Sel.Name == "Do" { |
| - break // the probe's one GET of the instance, not a door |
| - } |
| - t.Errorf("%s: %s calls .%s: only the Runner's gate and the doors may", name, where, sel.Sel.Name) |
| - case "sleep", "interval", "get", "getReply", "send", "mutate", "poll", "post", "follow", "readFlat", "readDetail": |
| - t.Errorf("%s: %s calls the mover's .%s, which waits or sends again", name, where, sel.Sel.Name) |
| - case "Sleep", "After", "NewTimer", "NewTicker", "Tick", "AfterFunc": |
| - if id, ok := sel.X.(*ast.Ident); ok && id.Name == "time" { |
| - t.Errorf("%s: %s calls time.%s", name, where, sel.Sel.Name) |
| - } |
| + if !run { |
| + return true |
| + } |
| + switch sel.Sel.Name { |
| + case "roundTrip": |
| + roundTrips[where]++ |
| + case "Post", "Nudge", "Status", "Do": |
| + if where == ".probeOnce" && sel.Sel.Name == "Do" { |
| + break // the probe's one GET of the instance, not a door |
| } |
| - case *ast.Ident: |
| - if n.Name == "runWire" && !run { |
| - t.Errorf("%s reaches runWire", name) |
| + t.Errorf("%s: %s calls .%s: only the Runner's gate and the doors may", name, where, sel.Sel.Name) |
| + case "sleep", "interval", "get", "getReply", "send", "mutate", "poll", "post", "follow", "readFlat", "readDetail": |
| + t.Errorf("%s: %s calls the mover's .%s, which waits or sends again", name, where, sel.Sel.Name) |
| + case "Sleep", "After", "NewTimer", "NewTicker", "Tick", "AfterFunc": |
| + if id, ok := sel.X.(*ast.Ident); ok && id.Name == "time" { |
| + t.Errorf("%s: %s calls time.%s", name, where, sel.Sel.Name) |
| } |
| } |
| - return true |
| - }) |
| - } |
| + case *ast.Ident: |
| + if n.Name == "runWire" && !run { |
| + t.Errorf("%s reaches runWire", name) |
| + } |
| + } |
| + return true |
| + }) |
| } |
| } |
| if len(roundTrips) != 1 || roundTrips["runWire.Do"] != 1 { |