@@ -7,6 +7,7 @@ package fetch
7
7
import (
8
8
"context"
9
9
"errors"
10
+ "fmt"
10
11
"io"
11
12
"net/http"
12
13
"net/http/httptest"
@@ -18,7 +19,7 @@ import (
18
19
"golang.org/x/pkgsite/internal"
19
20
"golang.org/x/pkgsite/internal/derrors"
20
21
"golang.org/x/pkgsite/internal/godoc"
21
- "golang.org/x/pkgsite/internal/proxy "
22
+ "golang.org/x/pkgsite/internal/licenses "
22
23
"golang.org/x/pkgsite/internal/source"
23
24
"golang.org/x/pkgsite/internal/stdlib"
24
25
"golang.org/x/pkgsite/internal/testing/sample"
@@ -48,6 +49,7 @@ func TestFetchModule(t *testing.T) {
48
49
name string
49
50
mod * testModule
50
51
fetchVersion string
52
+ proxyOnly bool
51
53
}{
52
54
{name : "basic" , mod : moduleNoGoMod },
53
55
{name : "wasm" , mod : moduleWasm },
@@ -63,54 +65,67 @@ func TestFetchModule(t *testing.T) {
63
65
{name : "module with type example" , mod : moduleTypeExample },
64
66
{name : "module with method example" , mod : moduleMethodExample },
65
67
{name : "module with nonredistributable packages" , mod : moduleNonRedist },
66
- {name : "stdlib module" , mod : moduleStd },
67
- {name : "master version of module" , mod : moduleMaster , fetchVersion : "master" },
68
- {name : "latest version of module" , mod : moduleLatest , fetchVersion : "latest" },
68
+ // Proxy only as stdlib is not accounted for in local mode
69
+ {name : "stdlib module" , mod : moduleStd , proxyOnly : true },
70
+ // Proxy only as version is pre specified in local mode
71
+ {name : "master version of module" , mod : moduleMaster , fetchVersion : "master" , proxyOnly : true },
72
+ // Proxy only as version is pre specified in local mode
73
+ {name : "latest version of module" , mod : moduleLatest , fetchVersion : "latest" , proxyOnly : true },
69
74
} {
70
- t .Run (test .name , func (t * testing.T ) {
71
- ctx := context .Background ()
72
- ctx , cancel := context .WithTimeout (ctx , 60 * time .Second )
73
- defer cancel ()
74
-
75
- modulePath := test .mod .mod .ModulePath
76
- version := test .mod .mod .Version
77
- fetchVersion := test .fetchVersion
78
- if version == "" {
79
- version = "v1.0.0"
80
- }
81
- if fetchVersion == "" {
82
- fetchVersion = version
83
- }
84
- sourceClient := source .NewClient (sourceTimeout )
85
- proxyClient , teardownProxy := proxy .SetupTestClient (t , []* proxy.Module {{
86
- ModulePath : modulePath ,
87
- Version : version ,
88
- Files : test .mod .mod .Files ,
89
- }})
90
- defer teardownProxy ()
91
- got := FetchModule (ctx , modulePath , fetchVersion , proxyClient , sourceClient )
92
- defer got .Defer ()
93
- if got .Error != nil {
94
- t .Fatal (got .Error )
95
- }
96
- d := licenseDetector (ctx , t , modulePath , got .ResolvedVersion , proxyClient )
97
- fr := cleanFetchResult (test .mod .fr , d )
98
- sortFetchResult (fr )
99
- sortFetchResult (got )
100
- opts := []cmp.Option {
101
- cmpopts .IgnoreFields (internal.LegacyPackage {}, "DocumentationHTML" ),
102
- cmpopts .IgnoreFields (internal.Documentation {}, "HTML" ),
103
- cmpopts .IgnoreFields (internal.PackageVersionState {}, "Error" ),
104
- cmpopts .IgnoreFields (FetchResult {}, "Defer" ),
105
- cmp .AllowUnexported (source.Info {}),
106
- cmpopts .EquateEmpty (),
107
- }
108
- opts = append (opts , sample .LicenseCmpOpts ... )
109
- if diff := cmp .Diff (fr , got , opts ... ); diff != "" {
110
- t .Fatalf ("mismatch (-want +got):\n %s" , diff )
75
+ for _ , fetcher := range []struct {
76
+ name string
77
+ fetch func (t * testing.T , withLicenseDetector bool , ctx context.Context , mod * testModule , fetchVersion string ) (* FetchResult , * licenses.Detector )
78
+ }{
79
+ {name : "proxy" , fetch : proxyFetcher },
80
+ {name : "local" , fetch : localFetcher },
81
+ } {
82
+ if test .proxyOnly && fetcher .name == "local" {
83
+ continue
111
84
}
112
- validateDocumentationHTML (t , got .Module , fr .Module )
113
- })
85
+ t .Run (fmt .Sprintf ("%s:%s" , fetcher .name , test .name ), func (t * testing.T ) {
86
+ ctx := context .Background ()
87
+ ctx , cancel := context .WithTimeout (ctx , 60 * time .Second )
88
+ defer cancel ()
89
+
90
+ got , d := fetcher .fetch (t , true , ctx , test .mod , test .fetchVersion )
91
+ defer got .Defer ()
92
+ if got .Error != nil {
93
+ t .Fatal ("fetching failed: %w" , got .Error )
94
+ }
95
+
96
+ if fetcher .name == "proxy" {
97
+ test .mod .fr = cleanFetchResult (t , test .mod .fr , d )
98
+ }
99
+ fr := updateFetchResultVersions (t , test .mod .fr , fetcher .name == "local" )
100
+ sortFetchResult (fr )
101
+ sortFetchResult (got )
102
+ opts := []cmp.Option {
103
+ cmpopts .IgnoreFields (internal.LegacyPackage {}, "DocumentationHTML" ),
104
+ cmpopts .IgnoreFields (internal.Documentation {}, "HTML" ),
105
+ cmpopts .IgnoreFields (internal.PackageVersionState {}, "Error" ),
106
+ cmpopts .IgnoreFields (FetchResult {}, "Defer" ),
107
+ cmp .AllowUnexported (source.Info {}),
108
+ cmpopts .EquateEmpty (),
109
+ }
110
+ if fetcher .name == "local" {
111
+ opts = append (opts ,
112
+ []cmp.Option {
113
+ // Pre specified for all modules
114
+ cmpopts .IgnoreFields (internal.Module {}, "SourceInfo" ),
115
+ cmpopts .IgnoreFields (internal.Module {}, "Version" ),
116
+ cmpopts .IgnoreFields (FetchResult {}, "RequestedVersion" ),
117
+ cmpopts .IgnoreFields (FetchResult {}, "ResolvedVersion" ),
118
+ cmpopts .IgnoreFields (internal.Module {}, "CommitTime" ),
119
+ }... )
120
+ }
121
+
122
+ opts = append (opts , sample .LicenseCmpOpts ... )
123
+ if diff := cmp .Diff (fr , got , opts ... ); diff != "" {
124
+ t .Fatalf ("mismatch (-want +got):\n %s" , diff )
125
+ }
126
+ validateDocumentationHTML (t , got .Module , fr .Module )
127
+ })
128
+ }
114
129
}
115
130
}
116
131
func TestFetchModule_Errors (t * testing.T ) {
@@ -125,29 +140,25 @@ func TestFetchModule_Errors(t *testing.T) {
125
140
{name : "alternative" , mod : moduleAlternative , wantErr : derrors .AlternativeModule , wantGoModPath : "canonical" },
126
141
{name : "empty module" , mod : moduleEmpty , wantErr : derrors .BadModule },
127
142
} {
128
- t .Run (test .name , func (t * testing.T ) {
129
- modulePath := test .mod .mod .ModulePath
130
- version := test .mod .mod .Version
131
- if version == "" {
132
- version = "v1.0.0"
133
- }
134
- proxyClient , teardownProxy := proxy .SetupTestClient (t , []* proxy.Module {{
135
- ModulePath : modulePath ,
136
- Files : test .mod .mod .Files ,
137
- }})
138
- defer teardownProxy ()
139
-
140
- sourceClient := source .NewClient (sourceTimeout )
141
- got := FetchModule (ctx , modulePath , "v1.0.0" , proxyClient , sourceClient )
142
- defer got .Defer ()
143
- if ! errors .Is (got .Error , test .wantErr ) {
144
- t .Fatalf ("FetchModule(ctx, %q, v1.0.0, proxyClient, sourceClient): %v; wantErr = %v)" , modulePath , got .Error , test .wantErr )
145
- }
146
- if test .wantGoModPath != "" {
147
- if got == nil || got .GoModPath != test .wantGoModPath {
148
- t .Errorf ("got %+v, wanted GoModPath %q" , got , test .wantGoModPath )
143
+ for _ , fetcher := range []struct {
144
+ name string
145
+ fetch func (t * testing.T , withLicenseDetector bool , ctx context.Context , mod * testModule , fetchVersion string ) (* FetchResult , * licenses.Detector )
146
+ }{
147
+ {name : "proxy" , fetch : proxyFetcher },
148
+ {name : "local" , fetch : localFetcher },
149
+ } {
150
+ t .Run (fmt .Sprintf ("%s:%s" , fetcher .name , test .name ), func (t * testing.T ) {
151
+ got , _ := fetcher .fetch (t , false , ctx , test .mod , "" )
152
+ defer got .Defer ()
153
+ if ! errors .Is (got .Error , test .wantErr ) {
154
+ t .Fatalf ("got error = %v; wantErr = %v)" , got .Error , test .wantErr )
149
155
}
150
- }
151
- })
156
+ if test .wantGoModPath != "" {
157
+ if got == nil || got .GoModPath != test .wantGoModPath {
158
+ t .Errorf ("got %+v, wanted GoModPath %q" , got , test .wantGoModPath )
159
+ }
160
+ }
161
+ })
162
+ }
152
163
}
153
164
}
0 commit comments