8
8
"context"
9
9
"errors"
10
10
"fmt"
11
- "strings "
11
+ "os "
12
12
"testing"
13
13
"time"
14
14
@@ -21,42 +21,19 @@ import (
21
21
"golang.org/x/pkgsite/internal/licenses"
22
22
"golang.org/x/pkgsite/internal/proxy"
23
23
"golang.org/x/pkgsite/internal/testing/sample"
24
- "golang.org/x/pkgsite/internal/testing/testhelper"
25
24
)
26
25
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
30
27
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.\n package baz\n import \" net/http\" \n const 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.\n package baz\n import \" net/http\" \n const 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
+ }
42
34
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 ()
60
37
client , teardownProxy := proxy .SetupTestClient (t , testModules )
61
38
ctx , cancel := context .WithTimeout (context .Background (), 20 * time .Second )
62
39
return ctx , NewForTesting (client ), func () {
@@ -84,31 +61,32 @@ var (
84
61
GOARCH : "amd64" ,
85
62
}},
86
63
}
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
- }
94
64
cmpOpts = append ([]cmp.Option {
95
65
cmpopts .IgnoreFields (licenses.License {}, "Contents" ),
66
+ cmpopts .IgnoreFields (internal.ModuleInfo {}, "SourceInfo" ),
96
67
}, sample .LicenseCmpOpts ... )
97
68
)
98
69
99
- func TestDataSource_GetModuleInfo (t * testing.T ) {
70
+ func TestGetModuleInfo (t * testing.T ) {
100
71
ctx , ds , teardown := setup (t )
101
72
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" )
103
74
if err != nil {
104
75
t .Fatal (err )
105
76
}
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
+ }
106
84
if diff := cmp .Diff (& wantModuleInfo , got , cmpOpts ... ); diff != "" {
107
85
t .Errorf ("GetModuleInfo diff (-want +got):\n %s" , diff )
108
86
}
109
87
}
110
88
111
- func TestDataSource_GetUnitMeta (t * testing.T ) {
89
+ func TestGetUnitMeta (t * testing.T ) {
112
90
ctx , ds , teardown := setup (t )
113
91
defer teardown ()
114
92
@@ -117,45 +95,45 @@ func TestDataSource_GetUnitMeta(t *testing.T) {
117
95
want * internal.UnitMeta
118
96
}{
119
97
{
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" ,
123
101
want : & internal.UnitMeta {
124
- ModulePath : "foo .com/bar " ,
125
- Version : "v1.1 .0" ,
102
+ ModulePath : "example .com/single " ,
103
+ Version : "v1.0 .0" ,
126
104
IsRedistributable : true ,
127
105
},
128
106
},
129
107
{
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" ,
133
111
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" ,
137
115
IsRedistributable : true ,
138
116
},
139
117
},
140
118
{
141
- path : "foo .com/bar/baz " ,
119
+ path : "example .com/single/pkg " ,
142
120
modulePath : internal .UnknownModulePath ,
143
- version : "v1.1 .0" ,
121
+ version : "v1.0 .0" ,
144
122
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" ,
148
126
IsRedistributable : true ,
149
127
},
150
128
},
151
129
{
152
- path : "foo .com/bar/baz " ,
130
+ path : "example .com/basic " ,
153
131
modulePath : internal .UnknownModulePath ,
154
132
version : internal .LatestVersion ,
155
133
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" ,
159
137
IsRedistributable : true ,
160
138
},
161
139
},
@@ -173,31 +151,42 @@ func TestDataSource_GetUnitMeta(t *testing.T) {
173
151
}
174
152
}
175
153
176
- func TestDataSource_Bypass (t * testing.T ) {
154
+ func TestBypass (t * testing.T ) {
177
155
for _ , bypass := range []bool {false , true } {
178
156
t .Run (fmt .Sprintf ("bypass=%t" , bypass ), func (t * testing.T ) {
179
157
// re-create the data source to get around caching
180
158
ctx , ds , teardown := setup (t )
181
159
defer teardown ()
182
160
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
+ }
185
177
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
+ })
195
184
}
196
185
})
197
186
}
198
187
}
199
188
200
- func TestDataSource_GetLatestInfo (t * testing.T ) {
189
+ func TestGetLatestInfo (t * testing.T ) {
201
190
t .Helper ()
202
191
testModules := []* proxy.Module {
203
192
{
0 commit comments