Skip to content

Commit deda7b7

Browse files
committed
refine function loadPackages
1 parent 4b40bbc commit deda7b7

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

go/callgraph/rta/rta_test.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ import (
2727
"golang.org/x/tools/txtar"
2828
)
2929

30-
const packageConfigMode = packages.NeedSyntax |
31-
packages.NeedTypesInfo |
32-
packages.NeedDeps |
33-
packages.NeedName |
34-
packages.NeedFiles |
35-
packages.NeedImports |
36-
packages.NeedCompiledGoFiles |
37-
packages.NeedTypes
38-
3930
// TestRTASingleFile runs RTA on each testdata/*.txtar file containing a single
4031
// go file and compares the results with the expectations expressed in the WANT
4132
// comment.
@@ -99,31 +90,38 @@ func TestRTAOnPackages(t *testing.T) {
9990
check(t, f, mainPkg, res)
10091
}
10192

93+
// loadPackages unpacks the archive to a temporary directory and loads all packages within it.
10294
func loadPackages(t *testing.T, archive string) []*packages.Package {
103-
var baseConfig = &packages.Config{
104-
Mode: packageConfigMode,
105-
Dir: restoreArchive(t, archive),
106-
}
107-
pkgs, err := packages.Load(baseConfig, "./...")
95+
ar, err := txtar.ParseFile(archive)
10896
if err != nil {
10997
t.Fatal(err)
11098
}
111-
return pkgs
112-
}
11399

114-
// restoreArchive restores a go module from the archive file,
115-
// and puts all contents in a temporary folder.
116-
func restoreArchive(t *testing.T, achieveFilePath string) string {
117-
ar, err := txtar.ParseFile(achieveFilePath)
100+
fs, err := txtar.FS(ar)
118101
if err != nil {
119102
t.Fatal(err)
120103
}
104+
dir := testfiles.CopyToTmp(t, fs)
121105

122-
fs, err := txtar.FS(ar)
106+
var baseConfig = &packages.Config{
107+
Mode: packages.NeedSyntax |
108+
packages.NeedTypesInfo |
109+
packages.NeedDeps |
110+
packages.NeedName |
111+
packages.NeedFiles |
112+
packages.NeedImports |
113+
packages.NeedCompiledGoFiles |
114+
packages.NeedTypes,
115+
Dir: dir,
116+
}
117+
pkgs, err := packages.Load(baseConfig, "./...")
123118
if err != nil {
124119
t.Fatal(err)
125120
}
126-
return testfiles.CopyToTmp(t, fs)
121+
if num := packages.PrintErrors(pkgs); num > 0 {
122+
t.Fatalf("packages contained %d errors", num)
123+
}
124+
return pkgs
127125
}
128126

129127
// check tests the RTA analysis results against the test expectations

0 commit comments

Comments
 (0)