github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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 os.Setenv("GIT_ALLOW_PROTOCOL", "https:foo") 275 for _, test := range tests { 276 secure := test.vcs.isSecure(test.url) 277 if secure != test.secure { 278 t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure) 279 } 280 } 281 } 282 283 func TestMatchGoImport(t *testing.T) { 284 tests := []struct { 285 imports []metaImport 286 path string 287 mi metaImport 288 err error 289 }{ 290 { 291 imports: []metaImport{ 292 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 293 }, 294 path: "example.com/user/foo", 295 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 296 }, 297 { 298 imports: []metaImport{ 299 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 300 }, 301 path: "example.com/user/foo/", 302 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 303 }, 304 { 305 imports: []metaImport{ 306 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 307 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 308 }, 309 path: "example.com/user/foo", 310 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 311 }, 312 { 313 imports: []metaImport{ 314 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 315 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 316 }, 317 path: "example.com/user/fooa", 318 mi: metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 319 }, 320 { 321 imports: []metaImport{ 322 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 323 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 324 }, 325 path: "example.com/user/foo/bar", 326 err: errors.New("should not be allowed to create nested repo"), 327 }, 328 { 329 imports: []metaImport{ 330 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 331 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 332 }, 333 path: "example.com/user/foo/bar/baz", 334 err: errors.New("should not be allowed to create nested repo"), 335 }, 336 { 337 imports: []metaImport{ 338 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 339 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 340 }, 341 path: "example.com/user/foo/bar/baz/qux", 342 err: errors.New("should not be allowed to create nested repo"), 343 }, 344 { 345 imports: []metaImport{ 346 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 347 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 348 }, 349 path: "example.com/user/foo/bar/baz/", 350 err: errors.New("should not be allowed to create nested repo"), 351 }, 352 { 353 imports: []metaImport{ 354 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 355 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 356 }, 357 path: "example.com", 358 err: errors.New("pathologically short path"), 359 }, 360 { 361 imports: []metaImport{ 362 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 363 }, 364 path: "different.example.com/user/foo", 365 err: errors.New("meta tags do not match import path"), 366 }, 367 } 368 369 for _, test := range tests { 370 mi, err := matchGoImport(test.imports, test.path) 371 if mi != test.mi { 372 t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi) 373 } 374 375 got := err 376 want := test.err 377 if (got == nil) != (want == nil) { 378 t.Errorf("unexpected error; got %v, want %v", got, want) 379 } 380 } 381 }