-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry_test.go
More file actions
222 lines (182 loc) · 6.56 KB
/
registry_test.go
File metadata and controls
222 lines (182 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package purl
import (
"testing"
)
func TestRegistryURL(t *testing.T) {
tests := []struct {
purl string
want string
wantErr bool
}{
// npm
{"pkg:npm/lodash", "https://www.npmjs.com/package/lodash", false},
{"pkg:npm/%40babel/core", "https://www.npmjs.com/package/@babel/core", false},
// pypi
{"pkg:pypi/requests", "https://pypi.org/project/requests/", false},
// cargo
{"pkg:cargo/serde", "https://crates.io/crates/serde", false},
// gem
{"pkg:gem/rails", "https://rubygems.org/gems/rails", false},
// maven
{"pkg:maven/org.apache.commons/commons-lang3", "https://mvnrepository.com/artifact/org.apache.commons/commons-lang3", false},
// composer
{"pkg:composer/symfony/console", "https://packagist.org/packages/symfony/console", false},
// hex
{"pkg:hex/phoenix", "https://hex.pm/packages/phoenix", false},
// hackage
{"pkg:hackage/aeson", "https://hackage.haskell.org/package/aeson", false},
// pub
{"pkg:pub/http", "https://pub.dev/packages/http", false},
// nuget
{"pkg:nuget/Newtonsoft.Json", "https://www.nuget.org/packages/Newtonsoft.Json", false},
// conda
{"pkg:conda/numpy", "https://anaconda.org/conda-forge/numpy", false},
// homebrew
{"pkg:homebrew/wget", "https://formulae.brew.sh/formula/wget", false},
// deno
{"pkg:deno/oak", "https://deno.land/x/oak", false},
// No registry config
{"pkg:deb/debian/curl", "", true},
{"pkg:apk/alpine/curl", "", true},
}
for _, tt := range tests {
t.Run(tt.purl, func(t *testing.T) {
p, err := Parse(tt.purl)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
got, err := p.RegistryURL()
if (err != nil) != tt.wantErr {
t.Errorf("RegistryURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("RegistryURL() = %q, want %q", got, tt.want)
}
})
}
}
func TestRegistryURLWithVersion(t *testing.T) {
tests := []struct {
purl string
want string
wantErr bool
}{
// npm with version
{"pkg:npm/lodash@4.17.21", "https://www.npmjs.com/package/lodash/v/4.17.21", false},
{"pkg:npm/%40babel/core@7.24.0", "https://www.npmjs.com/package/@babel/core/v/7.24.0", false},
// npm without version falls back
{"pkg:npm/lodash", "https://www.npmjs.com/package/lodash", false},
// pypi with version
{"pkg:pypi/requests@2.28.1", "https://pypi.org/project/requests/2.28.1/", false},
// gem with version
{"pkg:gem/rails@7.0.4", "https://rubygems.org/gems/rails/versions/7.0.4", false},
// nuget with version
{"pkg:nuget/Newtonsoft.Json@13.0.1", "https://www.nuget.org/packages/Newtonsoft.Json/13.0.1", false},
// maven with version
{"pkg:maven/org.apache.commons/commons-lang3@3.12.0", "https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.12.0", false},
// hackage with version
{"pkg:hackage/aeson@2.1.1.0", "https://hackage.haskell.org/package/aeson-2.1.1.0", false},
// deno with version
{"pkg:deno/oak@12.0.0", "https://deno.land/x/oak@12.0.0", false},
// elm with version
{"pkg:elm/elm/http@2.0.0", "https://package.elm-lang.org/packages/elm/http/2.0.0", false},
// cargo without version support in URL
{"pkg:cargo/serde@1.0.152", "https://crates.io/crates/serde", false},
// hex without version support in URL
{"pkg:hex/phoenix@1.7.0", "https://hex.pm/packages/phoenix", false},
}
for _, tt := range tests {
t.Run(tt.purl, func(t *testing.T) {
p, err := Parse(tt.purl)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
got, err := p.RegistryURLWithVersion()
if (err != nil) != tt.wantErr {
t.Errorf("RegistryURLWithVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("RegistryURLWithVersion() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseRegistryURLWithType(t *testing.T) {
tests := []struct {
url string
purlType string
wantPURL string
wantErr bool
}{
// npm
{"https://www.npmjs.com/package/lodash", "npm", "pkg:npm/lodash", false},
{"https://npmjs.com/package/lodash", "npm", "pkg:npm/lodash", false},
{"https://www.npmjs.com/package/@babel/core", "npm", "pkg:npm/%40babel/core", false},
{"https://www.npmjs.com/package/lodash/v/4.17.21", "npm", "pkg:npm/lodash@4.17.21", false},
// pypi
{"https://pypi.org/project/requests/", "pypi", "pkg:pypi/requests", false},
{"https://pypi.org/project/requests/2.28.1/", "pypi", "pkg:pypi/requests@2.28.1", false},
// cargo
{"https://crates.io/crates/serde", "cargo", "pkg:cargo/serde", false},
// gem
{"https://rubygems.org/gems/rails", "gem", "pkg:gem/rails", false},
{"https://rubygems.org/gems/rails/versions/7.0.4", "gem", "pkg:gem/rails@7.0.4", false},
// maven
{"https://mvnrepository.com/artifact/org.apache.commons/commons-lang3", "maven", "pkg:maven/org.apache.commons/commons-lang3", false},
{"https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.12.0", "maven", "pkg:maven/org.apache.commons/commons-lang3@3.12.0", false},
// nuget
{"https://www.nuget.org/packages/Newtonsoft.Json", "nuget", "pkg:nuget/Newtonsoft.Json", false},
{"https://nuget.org/packages/Newtonsoft.Json/13.0.1", "nuget", "pkg:nuget/Newtonsoft.Json@13.0.1", false},
// composer
{"https://packagist.org/packages/symfony/console", "composer", "pkg:composer/symfony/console", false},
// No match
{"https://example.com/package", "npm", "", true},
// No registry config
{"https://example.com/package", "deb", "", true},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
p, err := ParseRegistryURLWithType(tt.url, tt.purlType)
if (err != nil) != tt.wantErr {
t.Errorf("ParseRegistryURLWithType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
if got := p.String(); got != tt.wantPURL {
t.Errorf("ParseRegistryURLWithType() = %q, want %q", got, tt.wantPURL)
}
})
}
}
func TestParseRegistryURL(t *testing.T) {
tests := []struct {
url string
wantType string
wantErr bool
}{
{"https://www.npmjs.com/package/lodash", "npm", false},
{"https://pypi.org/project/requests/", "pypi", false},
{"https://crates.io/crates/serde", "cargo", false},
{"https://rubygems.org/gems/rails", "gem", false},
{"https://example.com/unknown", "", true},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
p, err := ParseRegistryURL(tt.url)
if (err != nil) != tt.wantErr {
t.Errorf("ParseRegistryURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
if p.Type != tt.wantType {
t.Errorf("ParseRegistryURL() type = %q, want %q", p.Type, tt.wantType)
}
})
}
}