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