Skip to content

Commit 361e7d2

Browse files
committed
goproxytest: add test wrapper API
I found that cmd/cue's tests were chatty because goproxytest was complaining about missing modules which did not matter. Because those logs always went directly to os.Stderr, I couldn't fix it without changing the API in this package. Rather than adding a complex API that takes a logger, since nearly all downstream use cases come from Go tests, add an API on top of testing.TB which makes its use much nicer.
1 parent ccf4b43 commit 361e7d2

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

Diff for: goproxytest/proxy.go

+29-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"os"
3434
"path/filepath"
3535
"strings"
36+
"testing"
3637

3738
"golang.org/x/mod/module"
3839
"golang.org/x/mod/semver"
@@ -45,26 +46,43 @@ type Server struct {
4546
server *http.Server
4647
URL string
4748
dir string
49+
logf func(string, ...any)
4850
modList []module.Version
4951
zipCache par.Cache
5052
archiveCache par.Cache
5153
}
5254

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
5469
// network address. It serves modules taken from the given directory
5570
// name. If addr is empty, it will listen on an arbitrary
5671
// localhost port. If dir is empty, "testmod" will be used.
5772
//
5873
// The returned Server should be closed after use.
5974
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) {
6179
if addr == "" {
6280
addr = "localhost:0"
6381
}
6482
if dir == "" {
6583
dir = "testmod"
6684
}
67-
srv.dir = dir
85+
srv := Server{dir: dir, logf: logf}
6886
if err := srv.readModList(); err != nil {
6987
return nil, fmt.Errorf("cannot read modules: %v", err)
7088
}
@@ -79,7 +97,7 @@ func NewServer(dir, addr string) (*Server, error) {
7997
srv.URL = "http://" + addr + "/mod"
8098
go func() {
8199
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)
83101
}
84102
}()
85103
return &srv, nil
@@ -141,7 +159,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
141159
enc, file := path[:i], path[i+len("/@v/"):]
142160
path, err := module.UnescapePath(enc)
143161
if err != nil {
144-
fmt.Fprintf(os.Stderr, "go proxy_test: %v\n", err)
162+
srv.logf("go proxy_test: %v\n", err)
145163
http.NotFound(w, r)
146164
return
147165
}
@@ -169,7 +187,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
169187
encVers, ext := file[:i], file[i+1:]
170188
vers, err := module.UnescapeVersion(encVers)
171189
if err != nil {
172-
fmt.Fprintf(os.Stderr, "go proxy_test: %v\n", err)
190+
srv.logf("go proxy_test: %v\n", err)
173191
http.NotFound(w, r)
174192
return
175193
}
@@ -204,7 +222,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
204222
// to resolve github.com, github.com/hello and github.com/hello/world.
205223
// cmd/go expects a 404/410 response if there is nothing there. Hence we
206224
// 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)
208226
http.NotFound(w, r)
209227
return
210228
}
@@ -246,7 +264,7 @@ func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
246264
}).(cached)
247265

248266
if c.err != nil {
249-
fmt.Fprintf(os.Stderr, "go proxy: %v\n", c.err)
267+
srv.logf("go proxy: %v\n", c.err)
250268
http.Error(w, c.err.Error(), 500)
251269
return
252270
}
@@ -277,12 +295,12 @@ func (srv *Server) findHash(m module.Version) string {
277295
func (srv *Server) readArchive(path, vers string) *txtar.Archive {
278296
enc, err := module.EscapePath(path)
279297
if err != nil {
280-
fmt.Fprintf(os.Stderr, "go proxy: %v\n", err)
298+
srv.logf("go proxy: %v\n", err)
281299
return nil
282300
}
283301
encVers, err := module.EscapeVersion(vers)
284302
if err != nil {
285-
fmt.Fprintf(os.Stderr, "go proxy: %v\n", err)
303+
srv.logf("go proxy: %v\n", err)
286304
return nil
287305
}
288306

@@ -324,7 +342,7 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
324342
}
325343
if err != nil {
326344
if !os.IsNotExist(err) {
327-
fmt.Fprintf(os.Stderr, "go proxy: %v\n", err)
345+
srv.logf("go proxy: %v\n", err)
328346
}
329347
a = nil
330348
}

Diff for: goproxytest/proxy_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import (
1414
)
1515

1616
func TestScripts(t *testing.T) {
17-
srv, err := goproxytest.NewServer(filepath.Join("testdata", "mod"), "")
18-
if err != nil {
19-
t.Fatalf("cannot start proxy: %v", err)
20-
}
17+
srv := goproxytest.NewTestServer(t, filepath.Join("testdata", "mod"), "")
2118
p := testscript.Params{
2219
Dir: "testdata",
2320
Setup: func(e *testscript.Env) error {

0 commit comments

Comments
 (0)