Skip to content

Commit 82a6a80

Browse files
committed
add LoadProxies function
1 parent 4ccd091 commit 82a6a80

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

internal/modproxy/modproxy_test.go

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
11
package modproxy
22

33
import (
4-
"net/http"
5-
"net/http/httptest"
6-
"os"
74
"reflect"
85
"testing"
96

107
"github.com/icholy/gomajor/internal/modproxy/testmodproxy"
118
)
129

13-
type testProxy struct {
14-
name string
15-
url string
16-
}
17-
18-
func loadTestProxies(t *testing.T) []testProxy {
19-
proxyfs, err := testmodproxy.LoadFS("testdata/modules")
20-
if err != nil {
21-
t.Fatal(err)
22-
}
23-
server := httptest.NewServer(http.FileServer(http.FS(proxyfs)))
24-
t.Cleanup(func() { server.Close() })
25-
proxydir := t.TempDir()
26-
if err := os.CopyFS(proxydir, proxyfs); err != nil {
27-
t.Fatal(err)
28-
}
29-
return []testProxy{
30-
{name: "http", url: server.URL},
31-
{name: "file", url: "file://" + proxydir},
32-
}
33-
}
34-
3510
func TestModule(t *testing.T) {
3611
tests := []struct {
3712
mod *Module
@@ -280,10 +255,10 @@ func TestQuery(t *testing.T) {
280255
exist: false,
281256
},
282257
}
283-
proxies := loadTestProxies(t)
258+
proxies := testmodproxy.LoadProxies(t, "testdata/modules")
284259
for _, proxy := range proxies {
285-
t.Run(proxy.name, func(t *testing.T) {
286-
t.Setenv("GOPROXY", proxy.url)
260+
t.Run(proxy.Name, func(t *testing.T) {
261+
t.Setenv("GOPROXY", proxy.URL)
287262
for _, tt := range tests {
288263
t.Run(tt.name, func(t *testing.T) {
289264
mod, ok, err := Query(tt.modpath, false)
@@ -337,10 +312,10 @@ func TestLatest(t *testing.T) {
337312
},
338313
},
339314
}
340-
proxies := loadTestProxies(t)
315+
proxies := testmodproxy.LoadProxies(t, "testdata/modules")
341316
for _, proxy := range proxies {
342-
t.Run(proxy.name, func(t *testing.T) {
343-
t.Setenv("GOPROXY", proxy.url)
317+
t.Run(proxy.Name, func(t *testing.T) {
318+
t.Setenv("GOPROXY", proxy.URL)
344319
for _, tt := range tests {
345320
t.Run(tt.name, func(t *testing.T) {
346321
mod, err := Latest(tt.modpath, false, tt.pre)
@@ -379,10 +354,10 @@ func TestQueryPackage(t *testing.T) {
379354
},
380355
},
381356
}
382-
proxies := loadTestProxies(t)
357+
proxies := testmodproxy.LoadProxies(t, "testdata/modules")
383358
for _, proxy := range proxies {
384-
t.Run(proxy.name, func(t *testing.T) {
385-
t.Setenv("GOPROXY", proxy.url)
359+
t.Run(proxy.Name, func(t *testing.T) {
360+
t.Setenv("GOPROXY", proxy.URL)
386361
for _, tt := range tests {
387362
t.Run(tt.name, func(t *testing.T) {
388363
mod, err := QueryPackage(tt.pkgpath, false)

internal/modproxy/testmodproxy/testmodproxy.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,44 @@ import (
77
"io"
88
"io/fs"
99
"maps"
10+
"net/http"
11+
"net/http/httptest"
1012
"os"
1113
"path/filepath"
1214
"slices"
1315
"strings"
16+
"testing"
1417
"testing/fstest"
1518

1619
"golang.org/x/mod/module"
1720
"golang.org/x/mod/semver"
1821
)
1922

23+
// Proxy is a named test proxy url.
24+
type Proxy struct {
25+
Name string
26+
URL string
27+
}
28+
29+
// LoadProxies creates an http:// and file:// proxy for testing.
30+
// See LoadFS for input directory format.
31+
func LoadProxies(t *testing.T, rootDir string) []Proxy {
32+
proxyfs, err := LoadFS(rootDir)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
server := httptest.NewServer(http.FileServer(http.FS(proxyfs)))
37+
t.Cleanup(func() { server.Close() })
38+
proxydir := t.TempDir()
39+
if err := os.CopyFS(proxydir, proxyfs); err != nil {
40+
t.Fatal(err)
41+
}
42+
return []Proxy{
43+
{Name: "http", URL: server.URL},
44+
{Name: "file", URL: "file://" + proxydir},
45+
}
46+
}
47+
2048
// LoadFS creates a virtual filesystem that implements the Go module proxy protocol.
2149
// It scans a directory of module source code and automatically generates all the
2250
// necessary proxy files: /@v/list, /@v/{version}.mod, /@v/{version}.zip, and /@v/{version}.info.

0 commit comments

Comments
 (0)