@@ -33,6 +33,7 @@ import (
33
33
"os"
34
34
"path/filepath"
35
35
"strings"
36
+ "testing"
36
37
37
38
"golang.org/x/mod/module"
38
39
"golang.org/x/mod/semver"
@@ -45,26 +46,43 @@ type Server struct {
45
46
server * http.Server
46
47
URL string
47
48
dir string
49
+ logf func (string , ... any )
48
50
modList []module.Version
49
51
zipCache par.Cache
50
52
archiveCache par.Cache
51
53
}
52
54
53
- // StartProxy starts the Go module proxy listening on the given
55
+ // NewTestServer is a wrapper around [NewServer] for use in Go tests.
56
+ // Failure to start the server stops the test via [testing.TB.Fatalf],
57
+ // all server logs go through [testing.TB.Logf],
58
+ // and the server is closed when the test finishes via [testing.TB.Cleanup].
59
+ func NewTestServer (tb testing.TB , dir , addr string ) * Server {
60
+ srv , err := newServer (dir , addr , tb .Logf )
61
+ if err != nil {
62
+ tb .Fatalf ("cannot start Go proxy: %v" , err )
63
+ }
64
+ tb .Cleanup (srv .Close )
65
+ return srv
66
+ }
67
+
68
+ // NewServer starts the Go module proxy listening on the given
54
69
// network address. It serves modules taken from the given directory
55
70
// name. If addr is empty, it will listen on an arbitrary
56
71
// localhost port. If dir is empty, "testmod" will be used.
57
72
//
58
73
// The returned Server should be closed after use.
59
74
func NewServer (dir , addr string ) (* Server , error ) {
60
- var srv Server
75
+ return newServer (dir , addr , log .Printf )
76
+ }
77
+
78
+ func newServer (dir , addr string , logf func (string , ... any )) (* Server , error ) {
61
79
if addr == "" {
62
80
addr = "localhost:0"
63
81
}
64
82
if dir == "" {
65
83
dir = "testmod"
66
84
}
67
- srv . dir = dir
85
+ srv := Server { dir : dir , logf : logf }
68
86
if err := srv .readModList (); err != nil {
69
87
return nil , fmt .Errorf ("cannot read modules: %v" , err )
70
88
}
@@ -79,7 +97,7 @@ func NewServer(dir, addr string) (*Server, error) {
79
97
srv .URL = "http://" + addr + "/mod"
80
98
go func () {
81
99
if err := srv .server .Serve (l ); err != nil && err != http .ErrServerClosed {
82
- log . Printf ("go proxy: http.Serve: %v" , err )
100
+ srv . logf ("go proxy: http.Serve: %v" , err )
83
101
}
84
102
}()
85
103
return & srv , nil
@@ -141,7 +159,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
141
159
enc , file := path [:i ], path [i + len ("/@v/" ):]
142
160
path , err := module .UnescapePath (enc )
143
161
if err != nil {
144
- fmt . Fprintf ( os . Stderr , "go proxy_test: %v\n " , err )
162
+ srv . logf ( "go proxy_test: %v\n " , err )
145
163
http .NotFound (w , r )
146
164
return
147
165
}
@@ -169,7 +187,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
169
187
encVers , ext := file [:i ], file [i + 1 :]
170
188
vers , err := module .UnescapeVersion (encVers )
171
189
if err != nil {
172
- fmt . Fprintf ( os . Stderr , "go proxy_test: %v\n " , err )
190
+ srv . logf ( "go proxy_test: %v\n " , err )
173
191
http .NotFound (w , r )
174
192
return
175
193
}
@@ -204,7 +222,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
204
222
// to resolve github.com, github.com/hello and github.com/hello/world.
205
223
// cmd/go expects a 404/410 response if there is nothing there. Hence we
206
224
// cannot return with a 500.
207
- fmt . Fprintf ( os . Stderr , "go proxy: no archive %s %s\n " , path , vers )
225
+ srv . logf ( "go proxy: no archive %s %s\n " , path , vers )
208
226
http .NotFound (w , r )
209
227
return
210
228
}
@@ -246,7 +264,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
246
264
}).(cached )
247
265
248
266
if c .err != nil {
249
- fmt . Fprintf ( os . Stderr , "go proxy: %v\n " , c .err )
267
+ srv . logf ( "go proxy: %v\n " , c .err )
250
268
http .Error (w , c .err .Error (), 500 )
251
269
return
252
270
}
@@ -277,12 +295,12 @@ func (srv *Server) findHash(m module.Version) string {
277
295
func (srv * Server ) readArchive (path , vers string ) * txtar.Archive {
278
296
enc , err := module .EscapePath (path )
279
297
if err != nil {
280
- fmt . Fprintf ( os . Stderr , "go proxy: %v\n " , err )
298
+ srv . logf ( "go proxy: %v\n " , err )
281
299
return nil
282
300
}
283
301
encVers , err := module .EscapeVersion (vers )
284
302
if err != nil {
285
- fmt . Fprintf ( os . Stderr , "go proxy: %v\n " , err )
303
+ srv . logf ( "go proxy: %v\n " , err )
286
304
return nil
287
305
}
288
306
@@ -324,7 +342,7 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
324
342
}
325
343
if err != nil {
326
344
if ! os .IsNotExist (err ) {
327
- fmt . Fprintf ( os . Stderr , "go proxy: %v\n " , err )
345
+ srv . logf ( "go proxy: %v\n " , err )
328
346
}
329
347
a = nil
330
348
}
0 commit comments