Skip to content

Commit

Permalink
remove fromTxtar as CL 613160
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuschen committed Sep 20, 2024
1 parent 7d6bca5 commit 2d9d89e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 74 deletions.
27 changes: 7 additions & 20 deletions go/ssa/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func h(error)
// t8 = phi [1: t7, 3: t4] #e
// ...

p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
p := packageFromBytes(t, input, ssa.BuilderMode(0))
p.Build()
g := p.Func("g")

Expand Down Expand Up @@ -606,7 +606,7 @@ func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
// func init func()
// var init$guard bool

p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
p := packageFromBytes(t, input, ssa.BuilderMode(0))
p.Build()

if load := p.Func("Load"); load.Signature.TypeParams().Len() != 1 {
Expand Down Expand Up @@ -759,7 +759,7 @@ func TestTypeparamTest(t *testing.T) {
if err != nil {
t.Fatal(err)
}

fsys := os.DirFS(dir)
for _, entry := range list {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
continue // Consider standalone go files.
Expand All @@ -777,20 +777,7 @@ func TestTypeparamTest(t *testing.T) {

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

pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedDeps |
packages.NeedName |
packages.NeedFiles |
packages.NeedImports |
packages.NeedCompiledGoFiles |
packages.NeedTypes,
}, input)
if err != nil {
t.Fatalf("fail to load pkgs from file %s", input)
}

pkgs := fromFS(t, fsys, input)
mode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics
prog, _ := ssautil.Packages(pkgs, mode)
prog.Build()
Expand All @@ -815,7 +802,7 @@ func sliceMax(s []int) []int { return s[a():b():c()] }
`

p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
p := packageFromBytes(t, input, ssa.BuilderMode(0))
p.Build()

for _, item := range []struct {
Expand Down Expand Up @@ -1119,7 +1106,7 @@ func TestLabels(t *testing.T) {
func main() { _:println(1); _:println(2)}`,
}
for _, test := range tests {
pkg := loadPackageFromSingleFile(t, test, ssa.BuilderMode(0)).spkg
pkg := packageFromBytes(t, test, ssa.BuilderMode(0))
pkg.Build()
}
}
Expand Down Expand Up @@ -1155,7 +1142,7 @@ func TestIssue67079(t *testing.T) {
// Load the package.
const src = `package p; type T int; func (T) f() {}; var _ = (*T).f`

p := loadPackageFromSingleFile(t, src, ssa.BuilderMode(0)).spkg
p := packageFromBytes(t, src, ssa.BuilderMode(0))
pkg := p.Pkg
prog := p.Prog
prog.Build()
Expand Down
24 changes: 18 additions & 6 deletions go/ssa/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/txtar"
)

func TestObjValueLookup(t *testing.T) {
Expand All @@ -34,10 +35,16 @@ func TestObjValueLookup(t *testing.T) {
if err != nil {
t.Fatal(err)
}
pkgs := fromTxtar(t, string(src))

fs, err := txtar.FS(txtar.Parse(src))
if err != nil {
t.Fatal(err)
}

pkgs := fromFS(t, fs, ".")
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))

info := getPkgInfo(prog, pkgs, "main")
info := getPkgInfo(pkgs, "main")

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

f := info.file
mainPkg := info.spkg
mainPkg := prog.Package(info.ppkg.Types)

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

pkgs := fromTxtar(t, string(src))
fs, err := txtar.FS(txtar.Parse(src))
if err != nil {
t.Fatal(err)
}

pkgs := fromFS(t, fs, ".")
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))

info := getPkgInfo(prog, pkgs, "main")
info := getPkgInfo(pkgs, "main")

if info == nil {
t.Fatalf("fail to get package main from loaded packages")
}
mainInfo := info.ppkg.TypesInfo
f := info.file
mainPkg := info.spkg
mainPkg := prog.Package(info.ppkg.Types)

mainPkg.SetDebugMode(true)
mainPkg.Build()
Expand Down
77 changes: 29 additions & 48 deletions go/ssa/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,48 @@
package ssa_test

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"testing"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/testfiles"
"golang.org/x/tools/txtar"
)

// loadPackageFromSingleFile is a utility function to create a package based on the content of a go file,
// and returns the pkgInfo about the input go file. The package name is retrieved from content after parsing.
// It's useful to create a ssa package and its packages.Package and ast.File representation.
func loadPackageFromSingleFile(t *testing.T, content string, mode ssa.BuilderMode) *pkgInfo {
ar := archiveFromSingleFileContent(content)
pkgs := fromTxtar(t, ar)
prog, _ := ssautil.Packages(pkgs, mode)

pkgName := packageName(t, content)
pkgInfo := getPkgInfo(prog, pkgs, pkgName)
if pkgInfo == nil {
t.Fatalf("fail to get package %s from loaded packages", pkgName)
}
return pkgInfo
}

// archiveFromSingleFileContent helps to create a go archive format string
// with go module example.com, the given content is put inside main.go.
// The package name depends on the package clause in the content.
//
// It's useful to define a package in a string variable instead of putting it inside a file.
func archiveFromSingleFileContent(content string) string {
return fmt.Sprintf(`
-- go.mod --
module example.com
go 1.18
-- main.go --
%s`, content)
}

// fromTxtar creates a temporary folder from the content in txtar format
// and then loads the packages.
func fromTxtar(t *testing.T, content string) []*packages.Package {
ar := txtar.Parse([]byte(content))

fs, err := txtar.FS(ar)
// packageFromBytes creates a package under the go module example.com from file content data,
// and returns its ssa package.
func packageFromBytes(t *testing.T, data string, mode ssa.BuilderMode) *ssa.Package {
src, err := txtar.FS(&txtar.Archive{
Files: []txtar.File{
{Name: "go.mod", Data: []byte("module example.com\ngo 1.18")},
{Name: "main.go", Data: []byte(data)},
}})
if err != nil {
t.Fatal(err)
}

dir := testfiles.CopyToTmp(t, fs)
if err != nil {
t.Fatal(err)
pkgs := fromFS(t, src, ".")
prog, _ := ssautil.Packages(pkgs, mode)

pkgName := packageName(t, data)
for _, spkg := range prog.AllPackages() {
if spkg.Pkg.Name() == pkgName {
return spkg
}
}
t.Fatalf("fail to get package %s from loaded packages", pkgName)
return nil
}

// fromFS copies the files and directories in src to a new temporary testing directory,
// loads and returns the Go packages named by the given patterns.
func fromFS(t *testing.T, src fs.FS, patterns ...string) []*packages.Package {
dir := testfiles.CopyToTmp(t, src)
var baseConfig = &packages.Config{
Mode: packages.NeedSyntax |
packages.NeedTypesInfo |
Expand All @@ -75,7 +58,7 @@ func fromTxtar(t *testing.T, content string) []*packages.Package {
packages.NeedTypes,
Dir: dir,
}
pkgs, err := packages.Load(baseConfig, "./...")
pkgs, err := packages.Load(baseConfig, patterns...)
if err != nil {
t.Fatal(err)
}
Expand All @@ -88,18 +71,16 @@ func fromTxtar(t *testing.T, content string) []*packages.Package {
// pkgInfo holds information about a ssa package for testing purpose.
// We assume ssa package only has one file in tests.
type pkgInfo struct {
spkg *ssa.Package // ssa representation of a package
ppkg *packages.Package // packages representation of a package
file *ast.File // AST representation of the first package file
}

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

0 comments on commit 2d9d89e

Please sign in to comment.