@@ -16,10 +16,10 @@ import (
16
16
17
17
"github.com/google/go-cmp/cmp"
18
18
"golang.org/x/net/html"
19
- "golang.org/x/pkgsite/internal"
20
19
"golang.org/x/pkgsite/internal/proxy"
21
20
"golang.org/x/pkgsite/internal/proxy/proxytest"
22
21
"golang.org/x/pkgsite/internal/testing/htmlcheck"
22
+ "golang.org/x/pkgsite/internal/testing/testhelper"
23
23
)
24
24
25
25
var (
@@ -51,48 +51,45 @@ func TestBuildGetters(t *testing.T) {
51
51
prox , teardown := proxytest .SetupTestClient (t , testModules )
52
52
defer teardown ()
53
53
54
- localGetter := "Dir(example.com/testmod, " + abs (localModule ) + ")"
54
+ localGetter := "Dir(" + abs (localModule ) + ")"
55
55
cacheGetter := "FSProxy(" + abs (cacheDir ) + ")"
56
56
for _ , test := range []struct {
57
57
name string
58
- paths []string
59
- cmods []internal.Modver
58
+ dirs []string
60
59
cacheDir string
61
- prox * proxy.Client
60
+ proxy * proxy.Client
62
61
want []string
63
62
}{
64
63
{
65
- name : "local only" ,
66
- paths : []string {localModule },
67
- want : []string {localGetter },
64
+ name : "local only" ,
65
+ dirs : []string {localModule },
66
+ want : []string {localGetter },
68
67
},
69
68
{
70
69
name : "cache" ,
71
70
cacheDir : cacheDir ,
72
71
want : []string {cacheGetter },
73
72
},
74
73
{
75
- name : "proxy" ,
76
- prox : prox ,
77
- want : []string {"Proxy" },
74
+ name : "proxy" ,
75
+ proxy : prox ,
76
+ want : []string {"Proxy" },
78
77
},
79
78
{
80
79
name : "all three" ,
81
- paths : []string {localModule },
80
+ dirs : []string {localModule },
82
81
cacheDir : cacheDir ,
83
- prox : prox ,
82
+ proxy : prox ,
84
83
want : []string {localGetter , cacheGetter , "Proxy" },
85
84
},
86
- {
87
- name : "list" ,
88
- paths : []string {localModule },
89
- cacheDir : cacheDir ,
90
- cmods : []internal.Modver {{Path : "foo" , Version : "v1.2.3" }},
91
- want : []string {localGetter , "FSProxy(" + abs (cacheDir ) + ", foo@v1.2.3)" },
92
- },
93
85
} {
94
86
t .Run (test .name , func (t * testing.T ) {
95
- getters , err := buildGetters (ctx , test .paths , false , test .cacheDir , test .cmods , test .prox )
87
+ getters , err := buildGetters (ctx , getterConfig {
88
+ dirs : test .dirs ,
89
+ pattern : "./..." ,
90
+ modCacheDir : test .cacheDir ,
91
+ proxy : test .proxy ,
92
+ })
96
93
if err != nil {
97
94
t .Fatal (err )
98
95
}
@@ -119,56 +116,91 @@ func TestServer(t *testing.T) {
119
116
return a
120
117
}
121
118
122
- localModule := repoPath ("internal/fetch/testdata/has_go_mod" )
119
+ localModule , _ := testhelper .WriteTxtarToTempDir (t , `
120
+ -- go.mod --
121
+ module example.com/testmod
122
+ -- a.go --
123
+ package a
124
+ ` )
123
125
cacheDir := repoPath ("internal/fetch/testdata/modcache" )
124
126
testModules := proxytest .LoadTestModules (repoPath ("internal/proxy/testdata" ))
125
127
prox , teardown := proxytest .SetupTestClient (t , testModules )
126
128
defer teardown ()
127
129
128
- getters , err := buildGetters ( context . Background (), [] string { localModule }, false , cacheDir , nil , prox )
129
- if err != nil {
130
- t . Fatal ( err )
131
- }
132
- server , err := newServer ( getters , prox )
133
- if err != nil {
134
- t . Fatal ( err )
130
+ defaultConfig := serverConfig {
131
+ paths : [] string { localModule },
132
+ gopathMode : false ,
133
+ useListedMods : true ,
134
+ useCache : true ,
135
+ cacheDir : cacheDir ,
136
+ proxy : prox ,
135
137
}
136
- mux := http .NewServeMux ()
137
- server .Install (mux .Handle , nil , nil )
138
138
139
139
modcacheChecker := in ("" ,
140
140
in (".Documentation" , hasText ("var V = 1" )),
141
141
sourceLinks (path .Join (abs (cacheDir ), "modcache.com@v1.0.0" ), "a.go" ))
142
142
143
+ ctx := context .Background ()
143
144
for _ , test := range []struct {
144
145
name string
146
+ cfg serverConfig
145
147
url string
146
148
want htmlcheck.Checker
147
149
}{
148
150
{
149
151
"local" ,
152
+ defaultConfig ,
150
153
"example.com/testmod" ,
151
154
in ("" ,
152
155
in (".Documentation" , hasText ("There is no documentation for this package." )),
153
156
sourceLinks (path .Join (abs (localModule ), "example.com/testmod" ), "a.go" )),
154
157
},
155
158
{
156
159
"modcache" ,
160
+ defaultConfig ,
157
161
"modcache.com@v1.0.0" ,
158
162
modcacheChecker ,
159
163
},
160
164
{
161
165
"modcache latest" ,
166
+ defaultConfig ,
162
167
"modcache.com" ,
163
168
modcacheChecker ,
164
169
},
165
170
{
166
171
"proxy" ,
172
+ defaultConfig ,
167
173
"example.com/single/pkg" ,
168
174
hasText ("G is new in v1.1.0" ),
169
175
},
176
+ {
177
+ "search" ,
178
+ defaultConfig ,
179
+ "search?q=a" ,
180
+ in (".SearchResults" ,
181
+ hasText ("example.com/testmod" ),
182
+ ),
183
+ },
184
+ {
185
+ "search" ,
186
+ defaultConfig ,
187
+ "search?q=zzz" ,
188
+ in (".SearchResults" ,
189
+ hasText ("no matches" ),
190
+ ),
191
+ },
192
+ // TODO(rfindley): add more tests, including a test for the standard
193
+ // library once it doesn't go through the stdlib package.
194
+ // See also golang/go#58923.
170
195
} {
171
196
t .Run (test .name , func (t * testing.T ) {
197
+ server , err := buildServer (ctx , test .cfg )
198
+ if err != nil {
199
+ t .Fatal (err )
200
+ }
201
+ mux := http .NewServeMux ()
202
+ server .Install (mux .Handle , nil , nil )
203
+
172
204
w := httptest .NewRecorder ()
173
205
mux .ServeHTTP (w , httptest .NewRequest ("GET" , "/" + test .url , nil ))
174
206
if w .Code != http .StatusOK {
@@ -203,36 +235,3 @@ func TestCollectPaths(t *testing.T) {
203
235
t .Errorf ("got %v, want %v" , got , want )
204
236
}
205
237
}
206
-
207
- func TestListModsForPaths (t * testing.T ) {
208
- listModules = func (string ) ([]listedMod , error ) {
209
- return []listedMod {
210
- {
211
- internal.Modver {Path : "m1" , Version : "v1.2.3" },
212
- "/dir/cache/download/m1/@v/v1.2.3.mod" ,
213
- false ,
214
- },
215
- {
216
- internal.Modver {Path : "m2" , Version : "v1.0.0" },
217
- "/repos/m2/go.mod" ,
218
- false ,
219
- },
220
- {
221
- internal.Modver {Path : "indir" , Version : "v2.3.4" },
222
- "" ,
223
- true ,
224
- },
225
- }, nil
226
- }
227
- defer func () { listModules = _listModules }()
228
-
229
- gotPaths , gotCacheMods , err := listModsForPaths ([]string {"m1" }, "/dir" )
230
- if err != nil {
231
- t .Fatal (err )
232
- }
233
- wantPaths := []string {"/repos/m2" }
234
- wantCacheMods := []internal.Modver {{Path : "m1" , Version : "v1.2.3" }}
235
- if ! cmp .Equal (gotPaths , wantPaths ) || ! cmp .Equal (gotCacheMods , wantCacheMods ) {
236
- t .Errorf ("got\n %v, %v\n want\n %v, %v" , gotPaths , gotCacheMods , wantPaths , wantCacheMods )
237
- }
238
- }
0 commit comments