@@ -27,15 +27,6 @@ import (
27
27
"golang.org/x/tools/txtar"
28
28
)
29
29
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
-
39
30
// TestRTASingleFile runs RTA on each testdata/*.txtar file containing a single
40
31
// go file and compares the results with the expectations expressed in the WANT
41
32
// comment.
@@ -99,31 +90,38 @@ func TestRTAOnPackages(t *testing.T) {
99
90
check (t , f , mainPkg , res )
100
91
}
101
92
93
+ // loadPackages unpacks the archive to a temporary directory and loads all packages within it.
102
94
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 )
108
96
if err != nil {
109
97
t .Fatal (err )
110
98
}
111
- return pkgs
112
- }
113
99
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 )
118
101
if err != nil {
119
102
t .Fatal (err )
120
103
}
104
+ dir := testfiles .CopyToTmp (t , fs )
121
105
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 , "./..." )
123
118
if err != nil {
124
119
t .Fatal (err )
125
120
}
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
127
125
}
128
126
129
127
// check tests the RTA analysis results against the test expectations
0 commit comments