@@ -22,12 +22,15 @@ import (
22
22
"golang.org/x/mod/semver"
23
23
"golang.org/x/pkgsite/internal"
24
24
"golang.org/x/pkgsite/internal/derrors"
25
+ "golang.org/x/pkgsite/internal/frontend"
25
26
"golang.org/x/pkgsite/internal/proxy"
26
27
"golang.org/x/pkgsite/internal/symbol"
27
28
"golang.org/x/pkgsite/internal/version"
28
29
)
29
30
30
31
var (
32
+ frontendHost = flag .String ("frontend" , "http://localhost:8080" ,
33
+ "Use the frontend host referred to by this URL for comparing data" )
31
34
proxyURL = flag .String ("proxy" , "https://proxy.golang.org" ,
32
35
"Use the module proxy referred to by this URL for fetching packages" )
33
36
)
@@ -37,6 +40,8 @@ func main() {
37
40
out := flag .CommandLine .Output ()
38
41
fmt .Fprintln (out , "api [cmd] [module path]:[package path suffix]" )
39
42
fmt .Fprintf (out , " generate: generates the API history for a package and writes to %s\n " , testdataDir )
43
+ fmt .Fprintf (out , " compare: compares the API history for a package in %s to %s\n " , testdataDir , * frontendHost )
44
+ fmt .Fprintln (out )
40
45
flag .PrintDefaults ()
41
46
}
42
47
flag .Parse ()
@@ -48,7 +53,7 @@ func main() {
48
53
ctx := context .Background ()
49
54
cmd := flag .Args ()[0 ]
50
55
pkgPath , modulePath := parsePath (flag .Args ()[1 ])
51
- if err := run (ctx , cmd , pkgPath , modulePath , * proxyURL ); err != nil {
56
+ if err := run (ctx , cmd , pkgPath , modulePath , * frontendHost , * proxyURL ); err != nil {
52
57
log .Fatal (err )
53
58
}
54
59
}
@@ -61,10 +66,15 @@ func parsePath(arg string) (pkgPath, modulePath string) {
61
66
return strings .Join (parts , "/" ), parts [0 ]
62
67
}
63
68
64
- const tmpDir = "/tmp/api"
69
+ const (
70
+ testdataDir = "tests/api/testdata"
71
+ tmpDir = "/tmp/api"
72
+ )
65
73
66
- func run (ctx context.Context , cmd , pkgPath , modulePath , proxyURL string ) error {
74
+ func run (ctx context.Context , cmd , pkgPath , modulePath , frontendHost , proxyURL string ) error {
67
75
switch cmd {
76
+ case "compare" :
77
+ return compare (frontendHost , pkgPath )
68
78
case "generate" :
69
79
return generate (ctx , pkgPath , modulePath , tmpDir , proxyURL )
70
80
}
@@ -115,6 +125,46 @@ func generate(ctx context.Context, pkgPath, modulePath, tmpPath, proxyURL string
115
125
return nil
116
126
}
117
127
128
+ // compare compares data from the testdata directory with the frontend.
129
+ func compare (frontendHost , pkgPath string ) (err error ) {
130
+ defer derrors .Wrap (& err , "compare(ctx, %q, %q, %q)" , frontendHost , pkgPath , testdataDir )
131
+ files , err := symbol .LoadAPIFiles (pkgPath , testdataDir )
132
+ if err != nil {
133
+ return err
134
+ }
135
+ apiVersions , err := symbol .ParsePackageAPIInfo (files )
136
+ if err != nil {
137
+ return err
138
+ }
139
+
140
+ // Parse API data from the frontend versions page.
141
+ client := frontend .NewClient (frontendHost )
142
+ vd , err := client .GetVersions (pkgPath )
143
+ if err != nil {
144
+ return err
145
+ }
146
+
147
+ sh , err := frontend .ParseVersionsDetails (vd )
148
+ if err != nil {
149
+ return err
150
+ }
151
+
152
+ // Compare the output of these two data sources.
153
+ errors , err := symbol .CompareAPIVersions (pkgPath , apiVersions [pkgPath ], sh )
154
+ if err != nil {
155
+ return err
156
+ }
157
+ if len (errors ) == 0 {
158
+ fmt .Printf ("The APIs match for %s!\n " , pkgPath )
159
+ return nil
160
+ }
161
+ fmt .Printf ("---------- Errors for %s\n " , pkgPath )
162
+ for _ , e := range errors {
163
+ fmt .Print (e )
164
+ }
165
+ return nil
166
+ }
167
+
118
168
func fetchFeatureContext (ctx context.Context , proxyClient * proxy.Client ,
119
169
modulePath , pkgPath , ver , dirPath string ) (_ map [string ]map [string ]bool , err error ) {
120
170
defer derrors .Wrap (& err , "fetchFeatureContext(ctx, proxyClient, %q, %q, %q, %q)" ,
@@ -149,8 +199,6 @@ func fetchFeatureContext(ctx context.Context, proxyClient *proxy.Client,
149
199
return symbol .GenerateFeatureContexts (ctx , pkgPath , pkgDir )
150
200
}
151
201
152
- const testdataDir = "tests/api/testdata"
153
-
154
202
func writeFeatures (features []string , pkgPath , ver , outDir string ) (err error ) {
155
203
defer derrors .Wrap (& err , "writeFeatures(%v, %q, %q, %q)" , features , pkgPath , ver , outDir )
156
204
if outDir == "" {
0 commit comments