Skip to content

Commit 28ffb9f

Browse files
committed
internal/proxydatasource: use standard proxy test modules
Change (most of) the proxydatasource tests to use the new, standard test modules. Change-Id: I98231adc41f52d5e6be54876b72a388785b90d9d Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/295190 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent d2ece35 commit 28ffb9f

File tree

1 file changed

+65
-76
lines changed

1 file changed

+65
-76
lines changed

internal/proxydatasource/datasource_test.go

Lines changed: 65 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11-
"strings"
11+
"os"
1212
"testing"
1313
"time"
1414

@@ -21,42 +21,19 @@ import (
2121
"golang.org/x/pkgsite/internal/licenses"
2222
"golang.org/x/pkgsite/internal/proxy"
2323
"golang.org/x/pkgsite/internal/testing/sample"
24-
"golang.org/x/pkgsite/internal/testing/testhelper"
2524
)
2625

27-
func setup(t *testing.T) (context.Context, *DataSource, func()) {
28-
t.Helper()
29-
dochtml.LoadTemplates(template.TrustedSourceFromConstant("../../content/static/html/doc"))
26+
var testModules []*proxy.Module
3027

31-
contents := map[string]string{
32-
"go.mod": "module foo.com/bar",
33-
"LICENSE": testhelper.MITLicense,
34-
"baz/baz.go": "//Package baz provides a helpful constant.\npackage baz\nimport \"net/http\"\nconst OK = http.StatusOK",
35-
}
36-
// nrContents is the same as contents, except the license is non-redistributable.
37-
nrContents := map[string]string{
38-
"go.mod": "module foo.com/nr",
39-
"LICENSE": "unknown",
40-
"baz/baz.go": "//Package baz provides a helpful constant.\npackage baz\nimport \"net/http\"\nconst OK = http.StatusOK",
41-
}
28+
func TestMain(m *testing.M) {
29+
dochtml.LoadTemplates(template.TrustedSourceFromConstant("../../content/static/html/doc"))
30+
testModules = proxy.LoadTestModules("../proxy/testdata")
31+
licenses.OmitExceptions = true
32+
os.Exit(m.Run())
33+
}
4234

43-
testModules := []*proxy.Module{
44-
{
45-
ModulePath: "foo.com/bar",
46-
Version: "v1.1.0",
47-
Files: contents,
48-
},
49-
{
50-
ModulePath: "foo.com/bar",
51-
Version: "v1.2.0",
52-
Files: contents,
53-
},
54-
{
55-
ModulePath: "foo.com/nr",
56-
Version: "v1.1.0",
57-
Files: nrContents,
58-
},
59-
}
35+
func setup(t *testing.T) (context.Context, *DataSource, func()) {
36+
t.Helper()
6037
client, teardownProxy := proxy.SetupTestClient(t, testModules)
6138
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
6239
return ctx, NewForTesting(client), func() {
@@ -84,31 +61,32 @@ var (
8461
GOARCH: "amd64",
8562
}},
8663
}
87-
wantModuleInfo = internal.ModuleInfo{
88-
ModulePath: "foo.com/bar",
89-
Version: "v1.2.0",
90-
CommitTime: time.Date(2019, 1, 30, 0, 0, 0, 0, time.UTC),
91-
IsRedistributable: true,
92-
HasGoMod: true,
93-
}
9464
cmpOpts = append([]cmp.Option{
9565
cmpopts.IgnoreFields(licenses.License{}, "Contents"),
66+
cmpopts.IgnoreFields(internal.ModuleInfo{}, "SourceInfo"),
9667
}, sample.LicenseCmpOpts...)
9768
)
9869

99-
func TestDataSource_GetModuleInfo(t *testing.T) {
70+
func TestGetModuleInfo(t *testing.T) {
10071
ctx, ds, teardown := setup(t)
10172
defer teardown()
102-
got, err := ds.GetModuleInfo(ctx, "foo.com/bar", "v1.2.0")
73+
got, err := ds.GetModuleInfo(ctx, "example.com/basic", "v1.1.0")
10374
if err != nil {
10475
t.Fatal(err)
10576
}
77+
wantModuleInfo := internal.ModuleInfo{
78+
ModulePath: "example.com/basic",
79+
Version: "v1.1.0",
80+
CommitTime: time.Date(2019, 1, 30, 0, 0, 0, 0, time.UTC),
81+
IsRedistributable: true,
82+
HasGoMod: true,
83+
}
10684
if diff := cmp.Diff(&wantModuleInfo, got, cmpOpts...); diff != "" {
10785
t.Errorf("GetModuleInfo diff (-want +got):\n%s", diff)
10886
}
10987
}
11088

111-
func TestDataSource_GetUnitMeta(t *testing.T) {
89+
func TestGetUnitMeta(t *testing.T) {
11290
ctx, ds, teardown := setup(t)
11391
defer teardown()
11492

@@ -117,45 +95,45 @@ func TestDataSource_GetUnitMeta(t *testing.T) {
11795
want *internal.UnitMeta
11896
}{
11997
{
120-
path: "foo.com/bar",
121-
modulePath: "foo.com/bar",
122-
version: "v1.1.0",
98+
path: "example.com/single",
99+
modulePath: "example.com/single",
100+
version: "v1.0.0",
123101
want: &internal.UnitMeta{
124-
ModulePath: "foo.com/bar",
125-
Version: "v1.1.0",
102+
ModulePath: "example.com/single",
103+
Version: "v1.0.0",
126104
IsRedistributable: true,
127105
},
128106
},
129107
{
130-
path: "foo.com/bar/baz",
131-
modulePath: "foo.com/bar",
132-
version: "v1.1.0",
108+
path: "example.com/single/pkg",
109+
modulePath: "example.com/single",
110+
version: "v1.0.0",
133111
want: &internal.UnitMeta{
134-
ModulePath: "foo.com/bar",
135-
Name: "baz",
136-
Version: "v1.1.0",
112+
ModulePath: "example.com/single",
113+
Name: "pkg",
114+
Version: "v1.0.0",
137115
IsRedistributable: true,
138116
},
139117
},
140118
{
141-
path: "foo.com/bar/baz",
119+
path: "example.com/single/pkg",
142120
modulePath: internal.UnknownModulePath,
143-
version: "v1.1.0",
121+
version: "v1.0.0",
144122
want: &internal.UnitMeta{
145-
ModulePath: "foo.com/bar",
146-
Name: "baz",
147-
Version: "v1.1.0",
123+
ModulePath: "example.com/single",
124+
Name: "pkg",
125+
Version: "v1.0.0",
148126
IsRedistributable: true,
149127
},
150128
},
151129
{
152-
path: "foo.com/bar/baz",
130+
path: "example.com/basic",
153131
modulePath: internal.UnknownModulePath,
154132
version: internal.LatestVersion,
155133
want: &internal.UnitMeta{
156-
ModulePath: "foo.com/bar",
157-
Name: "baz",
158-
Version: "v1.2.0",
134+
ModulePath: "example.com/basic",
135+
Name: "basic",
136+
Version: "v1.1.0",
159137
IsRedistributable: true,
160138
},
161139
},
@@ -173,31 +151,42 @@ func TestDataSource_GetUnitMeta(t *testing.T) {
173151
}
174152
}
175153

176-
func TestDataSource_Bypass(t *testing.T) {
154+
func TestBypass(t *testing.T) {
177155
for _, bypass := range []bool{false, true} {
178156
t.Run(fmt.Sprintf("bypass=%t", bypass), func(t *testing.T) {
179157
// re-create the data source to get around caching
180158
ctx, ds, teardown := setup(t)
181159
defer teardown()
182160
ds.bypassLicenseCheck = bypass
183-
for _, mpath := range []string{"foo.com/bar", "foo.com/nr"} {
184-
wantEmpty := !bypass && strings.HasSuffix(mpath, "/nr")
161+
for _, test := range []struct {
162+
path string
163+
wantEmpty bool
164+
}{
165+
{"example.com/basic", false},
166+
{"example.com/nonredist/unk", !bypass},
167+
} {
168+
t.Run(test.path, func(t *testing.T) {
169+
um, err := ds.GetUnitMeta(ctx, test.path, internal.UnknownModulePath, "v1.0.0")
170+
if err != nil {
171+
t.Fatal(err)
172+
}
173+
got, err := ds.GetUnit(ctx, um, 0)
174+
if err != nil {
175+
t.Fatal(err)
176+
}
185177

186-
got, err := ds.getModule(ctx, mpath, "v1.1.0")
187-
if err != nil {
188-
t.Fatal(err)
189-
}
190-
// Assume internal.Module.RemoveNonRedistributableData is correct; we just
191-
// need to check one value to confirm that it was called.
192-
if gotEmpty := (got.Licenses[0].Contents == nil); gotEmpty != wantEmpty {
193-
t.Errorf("bypass %t for %q: got empty %t, want %t", bypass, mpath, gotEmpty, wantEmpty)
194-
}
178+
// Assume internal.Module.RemoveNonRedistributableData is correct; we just
179+
// need to check one value to confirm that it was called.
180+
if gotEmpty := (got.Documentation == nil); gotEmpty != test.wantEmpty {
181+
t.Errorf("got empty %t, want %t", gotEmpty, test.wantEmpty)
182+
}
183+
})
195184
}
196185
})
197186
}
198187
}
199188

200-
func TestDataSource_GetLatestInfo(t *testing.T) {
189+
func TestGetLatestInfo(t *testing.T) {
201190
t.Helper()
202191
testModules := []*proxy.Module{
203192
{

0 commit comments

Comments
 (0)