@@ -27,27 +27,52 @@ import (
27
27
"golang.org/x/tools/txtar"
28
28
)
29
29
30
- // TestRTASingleFile runs RTA on each testdata/*.txtar file containing a single
31
- // go file and compares the results with the expectations expressed in the WANT
30
+ // TestRTA runs RTA on each testdata/*.txtar file containing a single
31
+ // go file in a single package or multiple files in different packages,
32
+ // and compares the results with the expectations expressed in the WANT
32
33
// comment.
33
- func TestRTASingleFile (t * testing.T ) {
34
+ func TestRTA (t * testing.T ) {
34
35
archivePaths := []string {
35
36
"testdata/func.txtar" ,
36
- "testdata/pkgmaingenerics.txtar" ,
37
37
"testdata/generics.txtar" ,
38
38
"testdata/iface.txtar" ,
39
39
"testdata/reflectcall.txtar" ,
40
40
"testdata/rtype.txtar" ,
41
+ "testdata/multipkgs.txtar" ,
41
42
}
42
43
for _ , archive := range archivePaths {
43
44
t .Run (archive , func (t * testing.T ) {
44
45
pkgs := loadPackages (t , archive )
45
46
46
- f := pkgs [0 ].Syntax [0 ]
47
+ // find the file which contains the expected result
48
+ var f * ast.File
49
+ for _ , p := range pkgs {
50
+ // We assume the packages have a single file or
51
+ // the wanted result is in the first file of the main package.
52
+ if p .Name == "main" {
53
+ f = p .Syntax [0 ]
54
+ }
55
+ }
56
+ if f == nil {
57
+ t .Fatalf ("failed to find the file with expected result within main package %s" , archive )
58
+ }
59
+
60
+ prog , spkgs := ssautil .Packages (pkgs , ssa .SanityCheckFunctions | ssa .InstantiateGenerics )
61
+
62
+ // find the main package to get functions for rta analysis
63
+ var mainPkg * ssa.Package
64
+ for _ , sp := range spkgs {
65
+ if sp .Pkg .Name () == "main" {
66
+ mainPkg = sp
67
+ break
68
+ }
69
+ }
70
+ if mainPkg == nil {
71
+ t .Fatalf ("failed to find main ssa package %s" , archive )
72
+ }
47
73
48
- prog , spkg := ssautil .Packages (pkgs , ssa .SanityCheckFunctions | ssa .InstantiateGenerics )
49
74
prog .Build ()
50
- mainPkg := spkg [ 0 ]
75
+
51
76
res := rta .Analyze ([]* ssa.Function {
52
77
mainPkg .Func ("main" ),
53
78
mainPkg .Func ("init" ),
@@ -58,38 +83,6 @@ func TestRTASingleFile(t *testing.T) {
58
83
}
59
84
}
60
85
61
- // TestRTAOnPackages runs RTA on a go module which contains multiple packages to test the case
62
- // when an interface has implementations across different packages.
63
- func TestRTAOnPackages (t * testing.T ) {
64
- pkgs := loadPackages (t , "testdata/multipkgs.txtar" )
65
-
66
- var f * ast.File
67
- for _ , p := range pkgs {
68
- // We assume the packages have a single file or
69
- // the wanted result is in the first file of the main package.
70
- if p .Name == "main" {
71
- f = p .Syntax [0 ]
72
- }
73
- }
74
-
75
- prog , spkgs := ssautil .Packages (pkgs , ssa .SanityCheckFunctions | ssa .InstantiateGenerics )
76
- prog .Build ()
77
- var mainPkg * ssa.Package
78
- for _ , sp := range spkgs {
79
- if sp .Pkg .Name () == "main" {
80
- mainPkg = sp
81
- break
82
- }
83
- }
84
-
85
- res := rta .Analyze ([]* ssa.Function {
86
- mainPkg .Func ("main" ),
87
- mainPkg .Func ("init" ),
88
- }, true )
89
-
90
- check (t , f , mainPkg , res )
91
- }
92
-
93
86
// loadPackages unpacks the archive to a temporary directory and loads all packages within it.
94
87
func loadPackages (t * testing.T , archive string ) []* packages.Package {
95
88
ar , err := txtar .ParseFile (archive )
0 commit comments