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.
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
)
27 "github.com/golang/groupcache",
30 repo
: "https://github.com/golang/groupcache",
33 // IBM DevOps Services tests
35 "hub.jazz.net/git/user1/pkgname",
38 repo
: "https://hub.jazz.net/git/user1/pkgname",
42 "hub.jazz.net/git/user1/pkgname/submodule/submodule/submodule",
45 repo
: "https://hub.jazz.net/git/user1/pkgname",
57 "hub.jazz.net/someotherprefix",
61 "hub.jazz.net/someotherprefix/user1/pkgname",
64 // Spaces are not valid in user names or package names
66 "hub.jazz.net/git/User 1/pkgname",
70 "hub.jazz.net/git/user1/pkg name",
73 // Dots are not valid in user names
75 "hub.jazz.net/git/user.1/pkgname",
79 "hub.jazz.net/git/user/pkg.name",
82 repo
: "https://hub.jazz.net/git/user/pkg.name",
85 // User names cannot have uppercase letters
87 "hub.jazz.net/git/USER/pkgname",
92 "git.openstack.org/openstack/swift",
95 repo
: "https://git.openstack.org/openstack/swift",
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
102 "git.openstack.org/openstack/swift.git",
105 repo
: "https://git.openstack.org/openstack/swift.git",
109 "git.openstack.org/openstack/swift/go/hummingbird",
112 repo
: "https://git.openstack.org/openstack/swift",
120 "git.openstack.org/openstack",
123 // Spaces are not valid in package name
125 "git.apache.org/package name/path/to/lib",
128 // Should have ".git" suffix
130 "git.apache.org/package-name/path/to/lib",
134 "git.apache.org/package-name.git",
137 repo
: "https://git.apache.org/package-name.git",
141 "git.apache.org/package-name_2.x.git/path/to/lib",
144 repo
: "https://git.apache.org/package-name_2.x.git",
149 for _
, test
:= range tests
{
150 got
, err
:= repoRootForImportPath(test
.path
, secure
)
155 t
.Errorf("RepoRootForImport(%q): Error expected but not received", test
.path
)
160 t
.Errorf("RepoRootForImport(%q): %v", test
.path
, err
)
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
)
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")
175 defer os
.RemoveAll(tempDir
)
177 for j
, vcs
:= range vcsList
{
178 dir
:= filepath
.Join(tempDir
, "example.com", vcs
.name
, "."+vcs
.cmd
)
180 err
:= os
.MkdirAll(dir
, 0755)
185 err
:= os
.MkdirAll(filepath
.Dir(dir
), 0755)
189 f
, err
:= os
.Create(dir
)
198 root
: path
.Join("example.com", vcs
.name
),
201 got
.vcs
, got
.root
, err
= vcsFromDir(dir
, tempDir
)
203 t
.Errorf("FromDir(%q, %q): %v", dir
, tempDir
, err
)
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
)
212 func TestIsSecure(t
*testing
.T
) {
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},
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
)
244 func TestIsSecureGitAllowProtocol(t
*testing
.T
) {
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},
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},
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
)
284 func TestMatchGoImport(t
*testing
.T
) {
292 imports
: []metaImport
{
293 {Prefix
: "example.com/user/foo", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
295 path
: "example.com/user/foo",
296 mi
: metaImport
{Prefix
: "example.com/user/foo", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
299 imports
: []metaImport
{
300 {Prefix
: "example.com/user/foo", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
302 path
: "example.com/user/foo/",
303 mi
: metaImport
{Prefix
: "example.com/user/foo", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
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"},
310 path
: "example.com/user/foo",
311 mi
: metaImport
{Prefix
: "example.com/user/foo", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
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"},
318 path
: "example.com/user/fooa",
319 mi
: metaImport
{Prefix
: "example.com/user/fooa", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
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"},
326 path
: "example.com/user/foo/bar",
327 err
: errors
.New("should not be allowed to create nested repo"),
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"},
334 path
: "example.com/user/foo/bar/baz",
335 err
: errors
.New("should not be allowed to create nested repo"),
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"},
342 path
: "example.com/user/foo/bar/baz/qux",
343 err
: errors
.New("should not be allowed to create nested repo"),
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"},
350 path
: "example.com/user/foo/bar/baz/",
351 err
: errors
.New("should not be allowed to create nested repo"),
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"},
359 err
: errors
.New("pathologically short path"),
362 imports
: []metaImport
{
363 {Prefix
: "example.com/user/foo", VCS
: "git", RepoRoot
: "https://example.com/repo/target"},
365 path
: "different.example.com/user/foo",
366 err
: errors
.New("meta tags do not match import path"),
370 for _
, test
:= range tests
{
371 mi
, err
:= matchGoImport(test
.imports
, test
.path
)
373 t
.Errorf("unexpected metaImport; got %v, want %v", mi
, test
.mi
)
378 if (got
== nil) != (want
== nil) {
379 t
.Errorf("unexpected error; got %v, want %v", got
, want
)