Skip to content

Commit 2d9d89e

Browse files
committed
remove fromTxtar as CL 613160
1 parent 7d6bca5 commit 2d9d89e

File tree

3 files changed

+54
-74
lines changed

3 files changed

+54
-74
lines changed

go/ssa/builder_test.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func h(error)
556556
// t8 = phi [1: t7, 3: t4] #e
557557
// ...
558558

559-
p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
559+
p := packageFromBytes(t, input, ssa.BuilderMode(0))
560560
p.Build()
561561
g := p.Func("g")
562562

@@ -606,7 +606,7 @@ func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
606606
// func init func()
607607
// var init$guard bool
608608

609-
p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
609+
p := packageFromBytes(t, input, ssa.BuilderMode(0))
610610
p.Build()
611611

612612
if load := p.Func("Load"); load.Signature.TypeParams().Len() != 1 {
@@ -759,7 +759,7 @@ func TestTypeparamTest(t *testing.T) {
759759
if err != nil {
760760
t.Fatal(err)
761761
}
762-
762+
fsys := os.DirFS(dir)
763763
for _, entry := range list {
764764
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
765765
continue // Consider standalone go files.
@@ -777,20 +777,7 @@ func TestTypeparamTest(t *testing.T) {
777777

778778
t.Logf("Input: %s\n", input)
779779

780-
pkgs, err := packages.Load(&packages.Config{
781-
Mode: packages.NeedSyntax |
782-
packages.NeedTypesInfo |
783-
packages.NeedDeps |
784-
packages.NeedName |
785-
packages.NeedFiles |
786-
packages.NeedImports |
787-
packages.NeedCompiledGoFiles |
788-
packages.NeedTypes,
789-
}, input)
790-
if err != nil {
791-
t.Fatalf("fail to load pkgs from file %s", input)
792-
}
793-
780+
pkgs := fromFS(t, fsys, input)
794781
mode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics
795782
prog, _ := ssautil.Packages(pkgs, mode)
796783
prog.Build()
@@ -815,7 +802,7 @@ func sliceMax(s []int) []int { return s[a():b():c()] }
815802
816803
`
817804

818-
p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
805+
p := packageFromBytes(t, input, ssa.BuilderMode(0))
819806
p.Build()
820807

821808
for _, item := range []struct {
@@ -1119,7 +1106,7 @@ func TestLabels(t *testing.T) {
11191106
func main() { _:println(1); _:println(2)}`,
11201107
}
11211108
for _, test := range tests {
1122-
pkg := loadPackageFromSingleFile(t, test, ssa.BuilderMode(0)).spkg
1109+
pkg := packageFromBytes(t, test, ssa.BuilderMode(0))
11231110
pkg.Build()
11241111
}
11251112
}
@@ -1155,7 +1142,7 @@ func TestIssue67079(t *testing.T) {
11551142
// Load the package.
11561143
const src = `package p; type T int; func (T) f() {}; var _ = (*T).f`
11571144

1158-
p := loadPackageFromSingleFile(t, src, ssa.BuilderMode(0)).spkg
1145+
p := packageFromBytes(t, src, ssa.BuilderMode(0))
11591146
pkg := p.Pkg
11601147
prog := p.Prog
11611148
prog.Build()

go/ssa/source_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"golang.org/x/tools/go/loader"
2424
"golang.org/x/tools/go/ssa"
2525
"golang.org/x/tools/go/ssa/ssautil"
26+
"golang.org/x/tools/txtar"
2627
)
2728

2829
func TestObjValueLookup(t *testing.T) {
@@ -34,10 +35,16 @@ func TestObjValueLookup(t *testing.T) {
3435
if err != nil {
3536
t.Fatal(err)
3637
}
37-
pkgs := fromTxtar(t, string(src))
38+
39+
fs, err := txtar.FS(txtar.Parse(src))
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
pkgs := fromFS(t, fs, ".")
3845
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))
3946

40-
info := getPkgInfo(prog, pkgs, "main")
47+
info := getPkgInfo(pkgs, "main")
4148

4249
if info == nil {
4350
t.Fatalf("fail to get package main from loaded packages")
@@ -47,7 +54,7 @@ func TestObjValueLookup(t *testing.T) {
4754
mainInfo := conf.TypesInfo
4855

4956
f := info.file
50-
mainPkg := info.spkg
57+
mainPkg := prog.Package(info.ppkg.Types)
5158

5259
readFile := func(_ string) ([]byte, error) {
5360
// split the file content to get the exact file content,
@@ -246,17 +253,22 @@ func testValueForExpr(t *testing.T, testfile string) {
246253
t.Fatal(err)
247254
}
248255

249-
pkgs := fromTxtar(t, string(src))
256+
fs, err := txtar.FS(txtar.Parse(src))
257+
if err != nil {
258+
t.Fatal(err)
259+
}
260+
261+
pkgs := fromFS(t, fs, ".")
250262
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))
251263

252-
info := getPkgInfo(prog, pkgs, "main")
264+
info := getPkgInfo(pkgs, "main")
253265

254266
if info == nil {
255267
t.Fatalf("fail to get package main from loaded packages")
256268
}
257269
mainInfo := info.ppkg.TypesInfo
258270
f := info.file
259-
mainPkg := info.spkg
271+
mainPkg := prog.Package(info.ppkg.Types)
260272

261273
mainPkg.SetDebugMode(true)
262274
mainPkg.Build()

go/ssa/testutil_test.go

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,48 @@
55
package ssa_test
66

77
import (
8-
"fmt"
98
"go/ast"
109
"go/parser"
1110
"go/token"
11+
"io/fs"
1212
"testing"
1313

14+
"golang.org/x/tools/go/packages"
1415
"golang.org/x/tools/go/ssa"
1516
"golang.org/x/tools/go/ssa/ssautil"
16-
17-
"golang.org/x/tools/go/packages"
1817
"golang.org/x/tools/internal/testfiles"
1918
"golang.org/x/tools/txtar"
2019
)
2120

22-
// loadPackageFromSingleFile is a utility function to create a package based on the content of a go file,
23-
// and returns the pkgInfo about the input go file. The package name is retrieved from content after parsing.
24-
// It's useful to create a ssa package and its packages.Package and ast.File representation.
25-
func loadPackageFromSingleFile(t *testing.T, content string, mode ssa.BuilderMode) *pkgInfo {
26-
ar := archiveFromSingleFileContent(content)
27-
pkgs := fromTxtar(t, ar)
28-
prog, _ := ssautil.Packages(pkgs, mode)
29-
30-
pkgName := packageName(t, content)
31-
pkgInfo := getPkgInfo(prog, pkgs, pkgName)
32-
if pkgInfo == nil {
33-
t.Fatalf("fail to get package %s from loaded packages", pkgName)
34-
}
35-
return pkgInfo
36-
}
37-
38-
// archiveFromSingleFileContent helps to create a go archive format string
39-
// with go module example.com, the given content is put inside main.go.
40-
// The package name depends on the package clause in the content.
41-
//
42-
// It's useful to define a package in a string variable instead of putting it inside a file.
43-
func archiveFromSingleFileContent(content string) string {
44-
return fmt.Sprintf(`
45-
-- go.mod --
46-
module example.com
47-
go 1.18
48-
-- main.go --
49-
%s`, content)
50-
}
51-
52-
// fromTxtar creates a temporary folder from the content in txtar format
53-
// and then loads the packages.
54-
func fromTxtar(t *testing.T, content string) []*packages.Package {
55-
ar := txtar.Parse([]byte(content))
56-
57-
fs, err := txtar.FS(ar)
21+
// packageFromBytes creates a package under the go module example.com from file content data,
22+
// and returns its ssa package.
23+
func packageFromBytes(t *testing.T, data string, mode ssa.BuilderMode) *ssa.Package {
24+
src, err := txtar.FS(&txtar.Archive{
25+
Files: []txtar.File{
26+
{Name: "go.mod", Data: []byte("module example.com\ngo 1.18")},
27+
{Name: "main.go", Data: []byte(data)},
28+
}})
5829
if err != nil {
5930
t.Fatal(err)
6031
}
6132

62-
dir := testfiles.CopyToTmp(t, fs)
63-
if err != nil {
64-
t.Fatal(err)
33+
pkgs := fromFS(t, src, ".")
34+
prog, _ := ssautil.Packages(pkgs, mode)
35+
36+
pkgName := packageName(t, data)
37+
for _, spkg := range prog.AllPackages() {
38+
if spkg.Pkg.Name() == pkgName {
39+
return spkg
40+
}
6541
}
42+
t.Fatalf("fail to get package %s from loaded packages", pkgName)
43+
return nil
44+
}
6645

46+
// fromFS copies the files and directories in src to a new temporary testing directory,
47+
// loads and returns the Go packages named by the given patterns.
48+
func fromFS(t *testing.T, src fs.FS, patterns ...string) []*packages.Package {
49+
dir := testfiles.CopyToTmp(t, src)
6750
var baseConfig = &packages.Config{
6851
Mode: packages.NeedSyntax |
6952
packages.NeedTypesInfo |
@@ -75,7 +58,7 @@ func fromTxtar(t *testing.T, content string) []*packages.Package {
7558
packages.NeedTypes,
7659
Dir: dir,
7760
}
78-
pkgs, err := packages.Load(baseConfig, "./...")
61+
pkgs, err := packages.Load(baseConfig, patterns...)
7962
if err != nil {
8063
t.Fatal(err)
8164
}
@@ -88,18 +71,16 @@ func fromTxtar(t *testing.T, content string) []*packages.Package {
8871
// pkgInfo holds information about a ssa package for testing purpose.
8972
// We assume ssa package only has one file in tests.
9073
type pkgInfo struct {
91-
spkg *ssa.Package // ssa representation of a package
9274
ppkg *packages.Package // packages representation of a package
9375
file *ast.File // AST representation of the first package file
9476
}
9577

96-
// getPkgInfo retrieves the package info from the program with the given name.
97-
// It's useful to test a package from a string instead of storing it inside a file.
98-
func getPkgInfo(prog *ssa.Program, pkgs []*packages.Package, pkgname string) *pkgInfo {
78+
// getPkgInfo gets the ast.File and packages.Package of a ssa package.
79+
func getPkgInfo(pkgs []*packages.Package, pkgname string) *pkgInfo {
9980
for _, pkg := range pkgs {
81+
// checking package name is enough for testing purpose
10082
if pkg.Name == pkgname {
10183
return &pkgInfo{
102-
spkg: prog.Package(pkg.Types),
10384
ppkg: pkg,
10485
file: pkg.Syntax[0], // we assume the test package is only consisted by one file
10586
}

0 commit comments

Comments
 (0)