github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/cmd/go/internal/get/vcs_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package get 6 7 import ( 8 "errors" 9 "internal/testenv" 10 "io/ioutil" 11 "os" 12 "path" 13 "path/filepath" 14 "testing" 15 16 "cmd/go/internal/web" 17 ) 18 19 // Test that RepoRootForImportPath creates the correct RepoRoot for a given importPath. 20 // TODO(cmang): Add tests for SVN and BZR. 21 func TestRepoRootForImportPath(t *testing.T) { 22 testenv.MustHaveExternalNetwork(t) 23 24 tests := []struct { 25 path string 26 want *repoRoot 27 }{ 28 { 29 "github.com/golang/groupcache", 30 &repoRoot{ 31 vcs: vcsGit, 32 repo: "https://github.com/golang/groupcache", 33 }, 34 }, 35 // IBM DevOps Services tests 36 { 37 "hub.jazz.net/git/user1/pkgname", 38 &repoRoot{ 39 vcs: vcsGit, 40 repo: "https://hub.jazz.net/git/user1/pkgname", 41 }, 42 }, 43 { 44 "hub.jazz.net/git/user1/pkgname/submodule/submodule/submodule", 45 &repoRoot{ 46 vcs: vcsGit, 47 repo: "https://hub.jazz.net/git/user1/pkgname", 48 }, 49 }, 50 { 51 "hub.jazz.net", 52 nil, 53 }, 54 { 55 "hub2.jazz.net", 56 nil, 57 }, 58 { 59 "hub.jazz.net/someotherprefix", 60 nil, 61 }, 62 { 63 "hub.jazz.net/someotherprefix/user1/pkgname", 64 nil, 65 }, 66 // Spaces are not valid in user names or package names 67 { 68 "hub.jazz.net/git/User 1/pkgname", 69 nil, 70 }, 71 { 72 "hub.jazz.net/git/user1/pkg name", 73 nil, 74 }, 75 // Dots are not valid in user names 76 { 77 "hub.jazz.net/git/user.1/pkgname", 78 nil, 79 }, 80 { 81 "hub.jazz.net/git/user/pkg.name", 82 &repoRoot{ 83 vcs: vcsGit, 84 repo: "https://hub.jazz.net/git/user/pkg.name", 85 }, 86 }, 87 // User names cannot have uppercase letters 88 { 89 "hub.jazz.net/git/USER/pkgname", 90 nil, 91 }, 92 // OpenStack tests 93 { 94 "git.openstack.org/openstack/swift", 95 &repoRoot{ 96 vcs: vcsGit, 97 repo: "https://git.openstack.org/openstack/swift", 98 }, 99 }, 100 // Trailing .git is less preferred but included for 101 // compatibility purposes while the same source needs to 102 // be compilable on both old and new go 103 { 104 "git.openstack.org/openstack/swift.git", 105 &repoRoot{ 106 vcs: vcsGit, 107 repo: "https://git.openstack.org/openstack/swift.git", 108 }, 109 }, 110 { 111 "git.openstack.org/openstack/swift/go/hummingbird", 112 &repoRoot{ 113 vcs: vcsGit, 114 repo: "https://git.openstack.org/openstack/swift", 115 }, 116 }, 117 { 118 "git.openstack.org", 119 nil, 120 }, 121 { 122 "git.openstack.org/openstack", 123 nil, 124 }, 125 // Spaces are not valid in package name 126 { 127 "git.apache.org/package name/path/to/lib", 128 nil, 129 }, 130 // Should have ".git" suffix 131 { 132 "git.apache.org/package-name/path/to/lib", 133 nil, 134 }, 135 { 136 "git.apache.org/package-name.git", 137 &repoRoot{ 138 vcs: vcsGit, 139 repo: "https://git.apache.org/package-name.git", 140 }, 141 }, 142 { 143 "git.apache.org/package-name_2.x.git/path/to/lib", 144 &repoRoot{ 145 vcs: vcsGit, 146 repo: "https://git.apache.org/package-name_2.x.git", 147 }, 148 }, 149 } 150 151 for _, test := range tests { 152 got, err := repoRootForImportPath(test.path, web.Secure) 153 want := test.want 154 155 if want == nil { 156 if err == nil { 157 t.Errorf("RepoRootForImport(%q): Error expected but not received", test.path) 158 } 159 continue 160 } 161 if err != nil { 162 t.Errorf("RepoRootForImport(%q): %v", test.path, err) 163 continue 164 } 165 if got.vcs.name != want.vcs.name || got.repo != want.repo { 166 t.Errorf("RepoRootForImport(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.vcs, got.repo, want.vcs, want.repo) 167 } 168 } 169 } 170 171 // Test that vcsFromDir correctly inspects a given directory and returns the right VCS and root. 172 func TestFromDir(t *testing.T) { 173 tempDir, err := ioutil.TempDir("", "vcstest") 174 if err != nil { 175 t.Fatal(err) 176 } 177 defer os.RemoveAll(tempDir) 178 179 for j, vcs := range vcsList { 180 dir := filepath.Join(tempDir, "example.com", vcs.name, "."+vcs.cmd) 181 if j&1 == 0 { 182 err := os.MkdirAll(dir, 0755) 183 if err != nil { 184 t.Fatal(err) 185 } 186 } else { 187 err := os.MkdirAll(filepath.Dir(dir), 0755) 188 if err != nil { 189 t.Fatal(err) 190 } 191 f, err := os.Create(dir) 192 if err != nil { 193 t.Fatal(err) 194 } 195 f.Close() 196 } 197 198 want := repoRoot{ 199 vcs: vcs, 200 root: path.Join("example.com", vcs.name), 201 } 202 var got repoRoot 203 got.vcs, got.root, err = vcsFromDir(dir, tempDir) 204 if err != nil { 205 t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err) 206 continue 207 } 208 if got.vcs.name != want.vcs.name || got.root != want.root { 209 t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.vcs, got.root, want.vcs, want.root) 210 } 211 } 212 } 213 214 func TestIsSecure(t *testing.T) { 215 tests := []struct { 216 vcs *vcsCmd 217 url string 218 secure bool 219 }{ 220 {vcsGit, "http://example.com/foo.git", false}, 221 {vcsGit, "https://example.com/foo.git", true}, 222 {vcsBzr, "http://example.com/foo.bzr", false}, 223 {vcsBzr, "https://example.com/foo.bzr", true}, 224 {vcsSvn, "http://example.com/svn", false}, 225 {vcsSvn, "https://example.com/svn", true}, 226 {vcsHg, "http://example.com/foo.hg", false}, 227 {vcsHg, "https://example.com/foo.hg", true}, 228 {vcsGit, "ssh://user@example.com/foo.git", true}, 229 {vcsGit, "user@server:path/to/repo.git", false}, 230 {vcsGit, "user@server:", false}, 231 {vcsGit, "server:repo.git", false}, 232 {vcsGit, "server:path/to/repo.git", false}, 233 {vcsGit, "example.com:path/to/repo.git", false}, 234 {vcsGit, "path/that/contains/a:colon/repo.git", false}, 235 {vcsHg, "ssh://user@example.com/path/to/repo.hg", true}, 236 } 237 238 for _, test := range tests { 239 secure := test.vcs.isSecure(test.url) 240 if secure != test.secure { 241 t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure) 242 } 243 } 244 } 245 246 func TestIsSecureGitAllowProtocol(t *testing.T) { 247 tests := []struct { 248 vcs *vcsCmd 249 url string 250 secure bool 251 }{ 252 // Same as TestIsSecure to verify same behavior. 253 {vcsGit, "http://example.com/foo.git", false}, 254 {vcsGit, "https://example.com/foo.git", true}, 255 {vcsBzr, "http://example.com/foo.bzr", false}, 256 {vcsBzr, "https://example.com/foo.bzr", true}, 257 {vcsSvn, "http://example.com/svn", false}, 258 {vcsSvn, "https://example.com/svn", true}, 259 {vcsHg, "http://example.com/foo.hg", false}, 260 {vcsHg, "https://example.com/foo.hg", true}, 261 {vcsGit, "user@server:path/to/repo.git", false}, 262 {vcsGit, "user@server:", false}, 263 {vcsGit, "server:repo.git", false}, 264 {vcsGit, "server:path/to/repo.git", false}, 265 {vcsGit, "example.com:path/to/repo.git", false}, 266 {vcsGit, "path/that/contains/a:colon/repo.git", false}, 267 {vcsHg, "ssh://user@example.com/path/to/repo.hg", true}, 268 // New behavior. 269 {vcsGit, "ssh://user@example.com/foo.git", false}, 270 {vcsGit, "foo://example.com/bar.git", true}, 271 {vcsHg, "foo://example.com/bar.hg", false}, 272 {vcsSvn, "foo://example.com/svn", false}, 273 {vcsBzr, "foo://example.com/bar.bzr", false}, 274 } 275 276 defer os.Unsetenv("GIT_ALLOW_PROTOCOL") 277 os.Setenv("GIT_ALLOW_PROTOCOL", "https:foo") 278 for _, test := range tests { 279 secure := test.vcs.isSecure(test.url) 280 if secure != test.secure { 281 t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure) 282 } 283 } 284 } 285 286 func TestMatchGoImport(t *testing.T) { 287 tests := []struct { 288 imports []metaImport 289 path string 290 mi metaImport 291 err error 292 }{ 293 { 294 imports: []metaImport{ 295 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 296 }, 297 path: "example.com/user/foo", 298 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 299 }, 300 { 301 imports: []metaImport{ 302 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 303 }, 304 path: "example.com/user/foo/", 305 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 306 }, 307 { 308 imports: []metaImport{ 309 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 310 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 311 }, 312 path: "example.com/user/foo", 313 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 314 }, 315 { 316 imports: []metaImport{ 317 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 318 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 319 }, 320 path: "example.com/user/fooa", 321 mi: metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 322 }, 323 { 324 imports: []metaImport{ 325 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 326 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 327 }, 328 path: "example.com/user/foo/bar", 329 err: errors.New("should not be allowed to create nested repo"), 330 }, 331 { 332 imports: []metaImport{ 333 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 334 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 335 }, 336 path: "example.com/user/foo/bar/baz", 337 err: errors.New("should not be allowed to create nested repo"), 338 }, 339 { 340 imports: []metaImport{ 341 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 342 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 343 }, 344 path: "example.com/user/foo/bar/baz/qux", 345 err: errors.New("should not be allowed to create nested repo"), 346 }, 347 { 348 imports: []metaImport{ 349 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 350 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 351 }, 352 path: "example.com/user/foo/bar/baz/", 353 err: errors.New("should not be allowed to create nested repo"), 354 }, 355 { 356 imports: []metaImport{ 357 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 358 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 359 }, 360 path: "example.com", 361 err: errors.New("pathologically short path"), 362 }, 363 { 364 imports: []metaImport{ 365 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 366 }, 367 path: "different.example.com/user/foo", 368 err: errors.New("meta tags do not match import path"), 369 }, 370 } 371 372 for _, test := range tests { 373 mi, err := matchGoImport(test.imports, test.path) 374 if mi != test.mi { 375 t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi) 376 } 377 378 got := err 379 want := test.err 380 if (got == nil) != (want == nil) { 381 t.Errorf("unexpected error; got %v, want %v", got, want) 382 } 383 } 384 }