| 1 |
// Copyright 2024 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// This file defines helper functions for SSA tests. |
| 6 |
|
| 7 |
package ir_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"go/parser" |
| 12 |
"go/token" |
| 13 |
"io/fs" |
| 14 |
"os" |
| 15 |
"testing" |
| 16 |
"testing/fstest" |
| 17 |
|
| 18 |
"honnef.co/go/tools/go/ir" |
| 19 |
"honnef.co/go/tools/go/ir/irutil" |
| 20 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 21 |
"honnef.co/go/tools/internal/xtools-internal/testfiles" |
| 22 |
|
| 23 |
"golang.org/x/tools/go/packages" |
| 24 |
"golang.org/x/tools/txtar" |
| 25 |
) |
| 26 |
|
| 27 |
// goMod returns a go.mod file containing a name and a go directive |
| 28 |
// for the major version. If major < 0, use the current go toolchain |
| 29 |
// version. |
| 30 |
func goMod(name string, major int) []byte { |
| 31 |
if major < 0 { |
| 32 |
major = testenv.Go1Point() |
| 33 |
} |
| 34 |
return fmt.Appendf(nil, "module %s\ngo 1.%d", name, major) |
| 35 |
} |
| 36 |
|
| 37 |
// overlayFS returns a simple in-memory filesystem. |
| 38 |
func overlayFS(overlay map[string][]byte) fstest.MapFS { |
| 39 |
// taking: Maybe loadPackages should take an overlay instead? |
| 40 |
fs := make(fstest.MapFS) |
| 41 |
for name, data := range overlay { |
| 42 |
fs[name] = &fstest.MapFile{Data: data} |
| 43 |
} |
| 44 |
return fs |
| 45 |
} |
| 46 |
|
| 47 |
// openTxtar opens a txtar file as a filesystem. |
| 48 |
func openTxtar(t testing.TB, file string) fs.FS { |
| 49 |
// TODO(taking): Move to testfiles? |
| 50 |
t.Helper() |
| 51 |
|
| 52 |
ar, err := txtar.ParseFile(file) |
| 53 |
if err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
|
| 57 |
fs, err := txtar.FS(ar) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
|
| 62 |
return fs |
| 63 |
} |
| 64 |
|
| 65 |
// loadPackages copies the files in a source file system to a unique temporary |
| 66 |
// directory and loads packages matching the given patterns from the temporary directory. |
| 67 |
// |
| 68 |
// TODO(69556): Migrate loader tests to loadPackages. |
| 69 |
func loadPackages(t testing.TB, src fs.FS, patterns ...string) []*packages.Package { |
| 70 |
t.Helper() |
| 71 |
testenv.NeedsGoBuild(t) // for go/packages |
| 72 |
|
| 73 |
// TODO(taking): src and overlays are very similar. Overlays could have nicer paths. |
| 74 |
// Look into migrating src to overlays. |
| 75 |
dir := testfiles.CopyToTmp(t, src) |
| 76 |
|
| 77 |
cfg := &packages.Config{ |
| 78 |
Dir: dir, |
| 79 |
Mode: packages.NeedSyntax | |
| 80 |
packages.NeedTypesInfo | |
| 81 |
packages.NeedDeps | |
| 82 |
packages.NeedName | |
| 83 |
packages.NeedFiles | |
| 84 |
packages.NeedImports | |
| 85 |
packages.NeedCompiledGoFiles | |
| 86 |
packages.NeedTypes, |
| 87 |
Env: append(os.Environ(), |
| 88 |
"GO111MODULE=on", |
| 89 |
"GOPATH=", |
| 90 |
"GOWORK=off", |
| 91 |
"GOPROXY=off"), |
| 92 |
} |
| 93 |
pkgs, err := packages.Load(cfg, patterns...) |
| 94 |
if err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
if packages.PrintErrors(pkgs) > 0 { |
| 98 |
t.Fatal("there were errors") |
| 99 |
} |
| 100 |
return pkgs |
| 101 |
} |
| 102 |
|
| 103 |
// buildPackage builds the content of a go file into: |
| 104 |
// * a module with the same name as the package at the current go version, |
| 105 |
// * loads the *package.Package, |
| 106 |
// * checks that (*packages.Packages).Syntax contains one file, |
| 107 |
// * builds the *ir.Package (and not its dependencies), and |
| 108 |
// * returns the built *ir.Package and the loaded packages.Package. |
| 109 |
// |
| 110 |
// TODO(adonovan): factor with similar loadFile (2x) in cha/cha_test.go and vta/helpers_test.go. |
| 111 |
func buildPackage(t testing.TB, content string, mode ir.BuilderMode) (*ir.Package, *packages.Package) { |
| 112 |
name := parsePackageClause(t, content) |
| 113 |
|
| 114 |
fs := overlayFS(map[string][]byte{ |
| 115 |
"go.mod": goMod(name, -1), |
| 116 |
"input.go": []byte(content), |
| 117 |
}) |
| 118 |
ppkgs := loadPackages(t, fs, name) |
| 119 |
if len(ppkgs) != 1 { |
| 120 |
t.Fatalf("Expected to load 1 package from pattern %q. got %d", name, len(ppkgs)) |
| 121 |
} |
| 122 |
ppkg := ppkgs[0] |
| 123 |
|
| 124 |
if len(ppkg.Syntax) != 1 { |
| 125 |
t.Fatalf("Expected 1 file in package %q. got %d", ppkg, len(ppkg.Syntax)) |
| 126 |
} |
| 127 |
|
| 128 |
prog, _ := irutil.Packages(ppkgs, mode) |
| 129 |
|
| 130 |
ssapkg := prog.Package(ppkg.Types) |
| 131 |
if ssapkg == nil { |
| 132 |
t.Fatalf("Failed to find ssa package for %q", ppkg.Types) |
| 133 |
} |
| 134 |
ssapkg.Build() |
| 135 |
|
| 136 |
return ssapkg, ppkg |
| 137 |
} |
| 138 |
|
| 139 |
// parsePackageClause is a test helper to extract the package name from a string |
| 140 |
// containing the content of a go file. |
| 141 |
func parsePackageClause(t testing.TB, content string) string { |
| 142 |
f, err := parser.ParseFile(token.NewFileSet(), "", content, parser.PackageClauseOnly) |
| 143 |
if err != nil { |
| 144 |
t.Fatalf("parsing the file %q failed with error: %s", content, err) |
| 145 |
} |
| 146 |
return f.Name.Name |
| 147 |
} |