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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package module
import "testing"
var checkTests = []struct {
path string
version string
ok bool
}{
{"rsc.io/quote", "0.1.0", false},
{"rsc io/quote", "v1.0.0", false},
{"github.com/go-yaml/yaml", "v0.8.0", true},
{"github.com/go-yaml/yaml", "v1.0.0", true},
{"github.com/go-yaml/yaml", "v2.0.0", false},
{"github.com/go-yaml/yaml", "v2.1.5", false},
{"github.com/go-yaml/yaml", "v3.0.0", false},
{"github.com/go-yaml/yaml/v2", "v1.0.0", false},
{"github.com/go-yaml/yaml/v2", "v2.0.0", true},
{"github.com/go-yaml/yaml/v2", "v2.1.5", true},
{"github.com/go-yaml/yaml/v2", "v3.0.0", false},
{"gopkg.in/yaml.v0", "v0.8.0", true},
{"gopkg.in/yaml.v0", "v1.0.0", false},
{"gopkg.in/yaml.v0", "v2.0.0", false},
{"gopkg.in/yaml.v0", "v2.1.5", false},
{"gopkg.in/yaml.v0", "v3.0.0", false},
{"gopkg.in/yaml.v1", "v0.8.0", false},
{"gopkg.in/yaml.v1", "v1.0.0", true},
{"gopkg.in/yaml.v1", "v2.0.0", false},
{"gopkg.in/yaml.v1", "v2.1.5", false},
{"gopkg.in/yaml.v1", "v3.0.0", false},
// For gopkg.in, .v1 means v1 only (not v0).
// But early versions of vgo still generated v0 pseudo-versions for it.
// Even though now we'd generate those as v1 pseudo-versions,
// we accept the old pseudo-versions to avoid breaking existing go.mod files.
// For example gopkg.in/yaml.v2@v2.2.1's go.mod requires check.v1 at a v0 pseudo-version.
{"gopkg.in/check.v1", "v0.0.0", false},
{"gopkg.in/check.v1", "v0.0.0-20160102150405-abcdef123456", true},
{"gopkg.in/yaml.v2", "v1.0.0", false},
{"gopkg.in/yaml.v2", "v2.0.0", true},
{"gopkg.in/yaml.v2", "v2.1.5", true},
{"gopkg.in/yaml.v2", "v3.0.0", false},
{"rsc.io/quote", "v17.0.0", false},
{"rsc.io/quote", "v17.0.0+incompatible", true},
}
func TestCheck(t *testing.T) {
for _, tt := range checkTests {
err := Check(tt.path, tt.version)
if tt.ok && err != nil {
t.Errorf("Check(%q, %q) = %v, wanted nil error", tt.path, tt.version, err)
} else if !tt.ok && err == nil {
t.Errorf("Check(%q, %q) succeeded, wanted error", tt.path, tt.version)
}
}
}
var checkPathTests = []struct {
path string
ok bool
importOK bool
fileOK bool
}{
{"x.y/z", true, true, true},
{"x.y", true, true, true},
{"", false, false, false},
{"x.y/\xFFz", false, false, false},
{"/x.y/z", false, false, false},
{"x./z", false, false, false},
{".x/z", false, true, true},
{"-x/z", false, false, true},
{"x..y/z", true, true, true},
{"x.y/z/../../w", false, false, false},
{"x.y//z", false, false, false},
{"x.y/z//w", false, false, false},
{"x.y/z/", false, false, false},
{"x.y/z/v0", false, true, true},
{"x.y/z/v1", false, true, true},
{"x.y/z/v2", true, true, true},
{"x.y/z/v2.0", false, true, true},
{"X.y/z", false, true, true},
{"!x.y/z", false, false, true},
{"_x.y/z", false, true, true},
{"x.y!/z", false, false, true},
{"x.y\"/z", false, false, false},
{"x.y#/z", false, false, true},
{"x.y$/z", false, false, true},
{"x.y%/z", false, false, true},
{"x.y&/z", false, false, true},
{"x.y'/z", false, false, false},
{"x.y(/z", false, false, true},
{"x.y)/z", false, false, true},
{"x.y*/z", false, false, false},
{"x.y+/z", false, true, true},
{"x.y,/z", false, false, true},
{"x.y-/z", true, true, true},
{"x.y./zt", false, false, false},
{"x.y:/z", false, false, false},
{"x.y;/z", false, false, false},
{"x.y</z", false, false, false},
{"x.y=/z", false, false, true},
{"x.y>/z", false, false, false},
{"x.y?/z", false, false, false},
{"x.y@/z", false, false, true},
{"x.y[/z", false, false, true},
{"x.y\\/z", false, false, false},
{"x.y]/z", false, false, true},
{"x.y^/z", false, false, true},
{"x.y_/z", false, true, true},
{"x.y`/z", false, false, false},
{"x.y{/z", false, false, true},
{"x.y}/z", false, false, true},
{"x.y~/z", false, true, true},
{"x.y/z!", false, false, true},
{"x.y/z\"", false, false, false},
{"x.y/z#", false, false, true},
{"x.y/z$", false, false, true},
{"x.y/z%", false, false, true},
{"x.y/z&", false, false, true},
{"x.y/z'", false, false, false},
{"x.y/z(", false, false, true},
{"x.y/z)", false, false, true},
{"x.y/z*", false, false, false},
{"x.y/z++", false, true, true},
{"x.y/z,", false, false, true},
{"x.y/z-", true, true, true},
{"x.y/z.t", true, true, true},
{"x.y/z/t", true, true, true},
{"x.y/z:", false, false, false},
{"x.y/z;", false, false, false},
{"x.y/z<", false, false, false},
{"x.y/z=", false, false, true},
{"x.y/z>", false, false, false},
{"x.y/z?", false, false, false},
{"x.y/z@", false, false, true},
{"x.y/z[", false, false, true},
{"x.y/z\\", false, false, false},
{"x.y/z]", false, false, true},
{"x.y/z^", false, false, true},
{"x.y/z_", true, true, true},
{"x.y/z`", false, false, false},
{"x.y/z{", false, false, true},
{"x.y/z}", false, false, true},
{"x.y/z~", true, true, true},
{"x.y/x.foo", true, true, true},
{"x.y/aux.foo", false, false, false},
{"x.y/prn", false, false, false},
{"x.y/prn2", true, true, true},
{"x.y/com", true, true, true},
{"x.y/com1", false, false, false},
{"x.y/com1.txt", false, false, false},
{"x.y/calm1", true, true, true},
{"x.y/z~", true, true, true},
{"x.y/z~0", false, false, true},
{"x.y/z~09", false, false, true},
{"x.y/z09", true, true, true},
{"x.y/z09~", true, true, true},
{"x.y/z09~09z", true, true, true},
{"x.y/z09~09z~09", false, false, true},
{"github.com/!123/logrus", false, false, true},
// TODO: CL 41822 allowed Unicode letters in old "go get"
// without due consideration of the implications, and only on github.com (!).
// For now, we disallow non-ASCII characters in module mode,
// in both module paths and general import paths,
// until we can get the implications right.
// When we do, we'll enable them everywhere, not just for GitHub.
{"github.com/user/unicode/испытание", false, false, true},
{"../x", false, false, false},
{"./y", false, false, false},
{"x:y", false, false, false},
{`\temp\foo`, false, false, false},
{".gitignore", false, true, true},
{".github/ISSUE_TEMPLATE", false, true, true},
{"x☺y", false, false, false},
}
func TestCheckPath(t *testing.T) {
for _, tt := range checkPathTests {
err := CheckPath(tt.path)
if tt.ok && err != nil {
t.Errorf("CheckPath(%q) = %v, wanted nil error", tt.path, err)
} else if !tt.ok && err == nil {
t.Errorf("CheckPath(%q) succeeded, wanted error", tt.path)
}
err = CheckImportPath(tt.path)
if tt.importOK && err != nil {
t.Errorf("CheckImportPath(%q) = %v, wanted nil error", tt.path, err)
} else if !tt.importOK && err == nil {
t.Errorf("CheckImportPath(%q) succeeded, wanted error", tt.path)
}
err = CheckFilePath(tt.path)
if tt.fileOK && err != nil {
t.Errorf("CheckFilePath(%q) = %v, wanted nil error", tt.path, err)
} else if !tt.fileOK && err == nil {
t.Errorf("CheckFilePath(%q) succeeded, wanted error", tt.path)
}
}
}
var splitPathVersionTests = []struct {
pathPrefix string
version string
}{
{"x.y/z", ""},
{"x.y/z", "/v2"},
{"x.y/z", "/v3"},
{"x.y/v", ""},
{"gopkg.in/yaml", ".v0"},
{"gopkg.in/yaml", ".v1"},
{"gopkg.in/yaml", ".v2"},
{"gopkg.in/yaml", ".v3"},
}
func TestSplitPathVersion(t *testing.T) {
for _, tt := range splitPathVersionTests {
pathPrefix, version, ok := SplitPathVersion(tt.pathPrefix + tt.version)
if pathPrefix != tt.pathPrefix || version != tt.version || !ok {
t.Errorf("SplitPathVersion(%q) = %q, %q, %v, want %q, %q, true", tt.pathPrefix+tt.version, pathPrefix, version, ok, tt.pathPrefix, tt.version)
}
}
for _, tt := range checkPathTests {
pathPrefix, version, ok := SplitPathVersion(tt.path)
if pathPrefix+version != tt.path {
t.Errorf("SplitPathVersion(%q) = %q, %q, %v, doesn't add to input", tt.path, pathPrefix, version, ok)
}
}
}
var escapeTests = []struct {
path string
esc string // empty means same as path
}{
{path: "ascii.com/abcdefghijklmnopqrstuvwxyz.-/~_0123456789"},
{path: "github.com/GoogleCloudPlatform/omega", esc: "github.com/!google!cloud!platform/omega"},
}
func TestEscapePath(t *testing.T) {
// Check invalid paths.
for _, tt := range checkPathTests {
if !tt.ok {
_, err := EscapePath(tt.path)
if err == nil {
t.Errorf("EscapePath(%q): succeeded, want error (invalid path)", tt.path)
}
}
}
// Check encodings.
for _, tt := range escapeTests {
esc, err := EscapePath(tt.path)
if err != nil {
t.Errorf("EscapePath(%q): unexpected error: %v", tt.path, err)
continue
}
want := tt.esc
if want == "" {
want = tt.path
}
if esc != want {
t.Errorf("EscapePath(%q) = %q, want %q", tt.path, esc, want)
}
}
}
var badUnescape = []string{
"github.com/GoogleCloudPlatform/omega",
"github.com/!google!cloud!platform!/omega",
"github.com/!0google!cloud!platform/omega",
"github.com/!_google!cloud!platform/omega",
"github.com/!!google!cloud!platform/omega",
"",
}
func TestUnescapePath(t *testing.T) {
// Check invalid decodings.
for _, bad := range badUnescape {
_, err := UnescapePath(bad)
if err == nil {
t.Errorf("UnescapePath(%q): succeeded, want error (invalid decoding)", bad)
}
}
// Check invalid paths (or maybe decodings).
for _, tt := range checkPathTests {
if !tt.ok {
path, err := UnescapePath(tt.path)
if err == nil {
t.Errorf("UnescapePath(%q) = %q, want error (invalid path)", tt.path, path)
}
}
}
// Check encodings.
for _, tt := range escapeTests {
esc := tt.esc
if esc == "" {
esc = tt.path
}
path, err := UnescapePath(esc)
if err != nil {
t.Errorf("UnescapePath(%q): unexpected error: %v", esc, err)
continue
}
if path != tt.path {
t.Errorf("UnescapePath(%q) = %q, want %q", esc, path, tt.path)
}
}
}
func TestMatchPathMajor(t *testing.T) {
for _, test := range []struct {
v, pathMajor string
want bool
}{
{"v0.0.0", "", true},
{"v0.0.0", "/v2", false},
{"v0.0.0", ".v0", true},
{"v0.0.0-20190510104115-cbcb75029529", ".v1", true},
{"v1.0.0", "/v2", false},
{"v1.0.0", ".v1", true},
{"v1.0.0", ".v1-unstable", true},
{"v2.0.0+incompatible", "", true},
{"v2.0.0", "", false},
{"v2.0.0", "/v2", true},
{"v2.0.0", ".v2", true},
} {
if got := MatchPathMajor(test.v, test.pathMajor); got != test.want {
t.Errorf("MatchPathMajor(%q, %q) = %v, want %v", test.v, test.pathMajor, got, test.want)
}
}
}
func TestMatchPrefixPatterns(t *testing.T) {
for _, test := range []struct {
globs, target string
want bool
}{
{"", "rsc.io/quote", false},
{"/", "rsc.io/quote", false},
{"*/quote", "rsc.io/quote", true},
{"*/quo", "rsc.io/quote", false},
{"*/quo??", "rsc.io/quote", true},
{"*/quo*", "rsc.io/quote", true},
{"*quo*", "rsc.io/quote", false},
{"rsc.io", "rsc.io/quote", true},
{"*.io", "rsc.io/quote", true},
{"rsc.io/", "rsc.io/quote", true},
{"rsc", "rsc.io/quote", false},
{"rsc*", "rsc.io/quote", true},
{"rsc.io", "rsc.io/quote/v3", true},
{"*/quote", "rsc.io/quote/v3", true},
{"*/quote/", "rsc.io/quote/v3", true},
{"*/quote/*", "rsc.io/quote/v3", true},
{"*/quote/*/", "rsc.io/quote/v3", true},
{"*/v3", "rsc.io/quote/v3", false},
{"*/*/v3", "rsc.io/quote/v3", true},
{"*/*/*", "rsc.io/quote/v3", true},
{"*/*/*/", "rsc.io/quote/v3", true},
{"*/*/*", "rsc.io/quote", false},
{"*/*/*/", "rsc.io/quote", false},
{"*/*/*,,", "rsc.io/quote", false},
{"*/*/*,,*/quote", "rsc.io/quote", true},
{",,*/quote", "rsc.io/quote", true},
} {
if got := MatchPrefixPatterns(test.globs, test.target); got != test.want {
t.Errorf("MatchPrefixPatterns(%q, %q) = %t, want %t", test.globs, test.target, got, test.want)
}
}
}
|