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