@@ -27,16 +27,14 @@ import (
27
27
"golang.org/x/tools/txtar"
28
28
)
29
29
30
- var baseConfig = & packages.Config {
31
- Mode : packages .NeedSyntax |
32
- packages .NeedTypesInfo |
33
- packages .NeedDeps |
34
- packages .NeedName |
35
- packages .NeedFiles |
36
- packages .NeedImports |
37
- packages .NeedCompiledGoFiles |
38
- packages .NeedTypes ,
39
- }
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
40
38
41
39
// TestRTASingleFile runs RTA on each testdata/*.txtar file containing a single
42
40
// go file and compares the results with the expectations expressed in the WANT
@@ -52,11 +50,7 @@ func TestRTASingleFile(t *testing.T) {
52
50
}
53
51
for _ , archive := range archivePaths {
54
52
t .Run (archive , func (t * testing.T ) {
55
- baseConfig .Dir = restoreArchive (t , archive )
56
- pkgs , err := packages .Load (baseConfig , "./..." )
57
- if err != nil {
58
- t .Fatal (err )
59
- }
53
+ pkgs := loadPackages (t , archive )
60
54
61
55
f := pkgs [0 ].Syntax [0 ]
62
56
@@ -76,11 +70,7 @@ func TestRTASingleFile(t *testing.T) {
76
70
// TestRTAOnPackages runs RTA on a go module which contains multiple packages to test the case
77
71
// when an interface has implementations across different packages.
78
72
func TestRTAOnPackages (t * testing.T ) {
79
- baseConfig .Dir = restoreArchive (t , "testdata/multipkgs.txtar" )
80
- pkgs , err := packages .Load (baseConfig , "./..." )
81
- if err != nil {
82
- t .Fatal (err )
83
- }
73
+ pkgs := loadPackages (t , "testdata/multipkgs.txtar" )
84
74
85
75
var f * ast.File
86
76
for _ , p := range pkgs {
@@ -109,6 +99,18 @@ func TestRTAOnPackages(t *testing.T) {
109
99
check (t , f , mainPkg , res )
110
100
}
111
101
102
+ 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 , "./..." )
108
+ if err != nil {
109
+ t .Fatal (err )
110
+ }
111
+ return pkgs
112
+ }
113
+
112
114
// restoreArchive restores a go module from the archive file,
113
115
// and puts all contents in a temporary folder.
114
116
func restoreArchive (t * testing.T , achieveFilePath string ) string {
0 commit comments