Skip to content

Commit 7d92dd6

Browse files
xieyuschengopherbot
authored andcommitted
go/ssa/ssautil: use go/packages to load packages in switch_test.go
Updates golang/go#69556 Change-Id: I5459a835b96f6f79d193538fdbc85a3e1c7324cc Reviewed-on: https://go-review.googlesource.com/c/tools/+/615035 LUCI-TryBot-Result: Go LUCI <[email protected]> Commit-Queue: Tim King <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent 18bc032 commit 7d92dd6

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

go/ssa/ssautil/switch_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,43 @@
1010
package ssautil_test
1111

1212
import (
13+
"go/ast"
1314
"go/parser"
15+
"path/filepath"
1416
"strings"
1517
"testing"
1618

1719
"golang.org/x/tools/go/loader"
1820
"golang.org/x/tools/go/ssa"
1921
"golang.org/x/tools/go/ssa/ssautil"
22+
"golang.org/x/tools/internal/testfiles"
23+
"golang.org/x/tools/txtar"
2024
)
2125

2226
func TestSwitches(t *testing.T) {
27+
archive, err := txtar.ParseFile("testdata/switches.txtar")
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
ppkgs := testfiles.LoadPackages(t, archive, ".")
32+
if len(ppkgs) != 1 {
33+
t.Fatalf("Expected to load one package but got %d", len(ppkgs))
34+
}
35+
36+
prog, _ := ssautil.Packages(ppkgs, ssa.BuilderMode(0))
37+
mainPkg := prog.Package(ppkgs[0].Types)
38+
mainPkg.Build()
39+
testSwitches(t, ppkgs[0].Syntax[0], mainPkg)
40+
}
41+
42+
// TestCreateProgram uses loader and ssautil.CreateProgram to create an *ssa.Program.
43+
// It has the same testing logic with TestSwitches.
44+
// CreateProgram is deprecated, but it is a part of the public API.
45+
// For now keep a test that exercises it.
46+
func TestCreateProgram(t *testing.T) {
47+
dir := testfiles.ExtractTxtarFileToTmp(t, "testdata/switches.txtar")
2348
conf := loader.Config{ParserMode: parser.ParseComments}
24-
f, err := conf.ParseFile("testdata/switches.go", nil)
49+
f, err := conf.ParseFile(filepath.Join(dir, "switches.go"), nil)
2550
if err != nil {
2651
t.Error(err)
2752
return
@@ -33,11 +58,13 @@ func TestSwitches(t *testing.T) {
3358
t.Error(err)
3459
return
3560
}
36-
3761
prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0))
3862
mainPkg := prog.Package(iprog.Created[0].Pkg)
3963
mainPkg.Build()
64+
testSwitches(t, f, mainPkg)
65+
}
4066

67+
func testSwitches(t *testing.T, f *ast.File, mainPkg *ssa.Package) {
4168
for _, mem := range mainPkg.Members {
4269
if fn, ok := mem.(*ssa.Function); ok {
4370
if fn.Synthetic != "" {

go/ssa/ssautil/testdata/switches.go renamed to go/ssa/ssautil/testdata/switches.txtar

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// +build ignore
1+
-- go.mod --
2+
module example.com
3+
go 1.22
24

5+
-- switches.go --
36
package main
47

58
// This file is the input to TestSwitches in switch_test.go.

0 commit comments

Comments
 (0)