github.com/euank/go@v0.0.0-20160829210321-495514729181/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", 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 _, vcs := range vcsList { 178 dir := filepath.Join(tempDir, "example.com", vcs.name, "."+vcs.cmd) 179 err := os.MkdirAll(dir, 0755) 180 if err != nil { 181 t.Fatal(err) 182 } 183 184 want := repoRoot{ 185 vcs: vcs, 186 root: path.Join("example.com", vcs.name), 187 } 188 var got repoRoot 189 got.vcs, got.root, err = vcsFromDir(dir, tempDir) 190 if err != nil { 191 t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err) 192 continue 193 } 194 if got.vcs.name != want.vcs.name || got.root != want.root { 195 t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.vcs, got.root, want.vcs, want.root) 196 } 197 } 198 } 199 200 func TestIsSecure(t *testing.T) { 201 tests := []struct { 202 vcs *vcsCmd 203 url string 204 secure bool 205 }{ 206 {vcsGit, "http://example.com/foo.git", false}, 207 {vcsGit, "https://example.com/foo.git", true}, 208 {vcsBzr, "http://example.com/foo.bzr", false}, 209 {vcsBzr, "https://example.com/foo.bzr", true}, 210 {vcsSvn, "http://example.com/svn", false}, 211 {vcsSvn, "https://example.com/svn", true}, 212 {vcsHg, "http://example.com/foo.hg", false}, 213 {vcsHg, "https://example.com/foo.hg", true}, 214 {vcsGit, "ssh://user@example.com/foo.git", true}, 215 {vcsGit, "user@server:path/to/repo.git", false}, 216 {vcsGit, "user@server:", false}, 217 {vcsGit, "server:repo.git", false}, 218 {vcsGit, "server:path/to/repo.git", false}, 219 {vcsGit, "example.com:path/to/repo.git", false}, 220 {vcsGit, "path/that/contains/a:colon/repo.git", false}, 221 {vcsHg, "ssh://user@example.com/path/to/repo.hg", true}, 222 } 223 224 for _, test := range tests { 225 secure := test.vcs.isSecure(test.url) 226 if secure != test.secure { 227 t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure) 228 } 229 } 230 } 231 232 func TestMatchGoImport(t *testing.T) { 233 tests := []struct { 234 imports []metaImport 235 path string 236 mi metaImport 237 err error 238 }{ 239 { 240 imports: []metaImport{ 241 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 242 }, 243 path: "example.com/user/foo", 244 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 245 }, 246 { 247 imports: []metaImport{ 248 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 249 }, 250 path: "example.com/user/foo/", 251 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 252 }, 253 { 254 imports: []metaImport{ 255 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 256 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 257 }, 258 path: "example.com/user/foo", 259 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 260 }, 261 { 262 imports: []metaImport{ 263 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 264 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 265 }, 266 path: "example.com/user/fooa", 267 mi: metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 268 }, 269 { 270 imports: []metaImport{ 271 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 272 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 273 }, 274 path: "example.com/user/foo/bar", 275 err: errors.New("should not be allowed to create nested repo"), 276 }, 277 { 278 imports: []metaImport{ 279 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 280 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 281 }, 282 path: "example.com/user/foo/bar/baz", 283 err: errors.New("should not be allowed to create nested repo"), 284 }, 285 { 286 imports: []metaImport{ 287 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 288 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 289 }, 290 path: "example.com/user/foo/bar/baz/qux", 291 err: errors.New("should not be allowed to create nested repo"), 292 }, 293 { 294 imports: []metaImport{ 295 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 296 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 297 }, 298 path: "example.com/user/foo/bar/baz/", 299 err: errors.New("should not be allowed to create nested repo"), 300 }, 301 { 302 imports: []metaImport{ 303 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 304 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"}, 305 }, 306 path: "example.com", 307 err: errors.New("pathologically short path"), 308 }, 309 } 310 311 for _, test := range tests { 312 mi, err := matchGoImport(test.imports, test.path) 313 if mi != test.mi { 314 t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi) 315 } 316 317 got := err 318 want := test.err 319 if (got == nil) != (want == nil) { 320 t.Errorf("unexpected error; got %v, want %v", got, want) 321 } 322 } 323 }