1 // Copyright 2013 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.
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 if runtime.GOOS == "android" {
23 t.Skipf("incomplete source tree on %s", runtime.GOOS)
31 "github.com/golang/groupcache",
34 Repo: "https://github.com/golang/groupcache",
37 // Unicode letters in directories (issue 18660).
39 "github.com/user/unicode/испытание",
42 Repo: "https://github.com/user/unicode",
47 for _, test := range tests {
48 got, err := RepoRootForImportPath(test.path, false)
50 t.Errorf("RepoRootForImportPath(%q): %v", test.path, err)
54 if got.VCS.Name != want.VCS.Name || got.Repo != want.Repo {
55 t.Errorf("RepoRootForImportPath(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.VCS, got.Repo, want.VCS, want.Repo)
60 // Test that FromDir correctly inspects a given directory and returns the right VCS and root.
61 func TestFromDir(t *testing.T) {
62 tempDir, err := ioutil.TempDir("", "vcstest")
66 defer os.RemoveAll(tempDir)
68 for j, vcs := range vcsList {
69 dir := filepath.Join(tempDir, "example.com", vcs.Name, "."+vcs.Cmd)
71 err := os.MkdirAll(dir, 0755)
76 err := os.MkdirAll(filepath.Dir(dir), 0755)
80 f, err := os.Create(dir)
89 Root: path.Join("example.com", vcs.Name),
92 got.VCS, got.Root, err = FromDir(dir, tempDir)
94 t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err)
97 if got.VCS.Name != want.VCS.Name || got.Root != want.Root {
98 t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.VCS, got.Root, want.VCS, want.Root)
103 var parseMetaGoImportsTests = []struct {
108 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
109 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
112 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
113 <meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
115 {"foo/bar", "git", "https://github.com/rsc/foo/bar"},
116 {"baz/quux", "git", "http://github.com/rsc/baz/quux"},
120 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
121 <meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`,
123 {"foo/bar", "git", "https://github.com/rsc/foo/bar"},
127 `<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
128 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
130 {"foo/bar", "git", "https://github.com/rsc/foo/bar"},
135 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
137 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
140 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
142 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
145 `<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
146 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
149 // XML doesn't like <div style=position:relative>.
150 `<!doctype html><title>Page Not Found</title><meta name=go-import content="chitin.io/chitin git https://github.com/chitin-io/chitin"><div style=position:relative>DRAFT</div>`,
151 []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}},
154 `<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
155 <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
157 []metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x"}},
161 func TestParseMetaGoImports(t *testing.T) {
162 for i, tt := range parseMetaGoImportsTests {
163 out, err := parseMetaGoImports(strings.NewReader(tt.in))
165 t.Errorf("test#%d: %v", i, err)
168 if !reflect.DeepEqual(out, tt.out) {
169 t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
174 func TestValidateRepoRoot(t *testing.T) {
205 for _, test := range tests {
206 err := validateRepoRoot(test.root)
213 t.Errorf("validateRepoRoot(%q) = %q, want %s", test.root, err, want)
218 func TestMatchGoImport(t *testing.T) {
226 imports: []metaImport{
227 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
229 path: "example.com/user/foo",
230 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
233 imports: []metaImport{
234 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
236 path: "example.com/user/foo/",
237 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
240 imports: []metaImport{
241 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
242 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
244 path: "example.com/user/foo",
245 mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
248 imports: []metaImport{
249 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
250 {Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
252 path: "example.com/user/fooa",
253 mi: metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
256 imports: []metaImport{
257 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
258 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
260 path: "example.com/user/foo/bar",
261 err: errors.New("should not be allowed to create nested repo"),
264 imports: []metaImport{
265 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
266 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
268 path: "example.com/user/foo/bar/baz",
269 err: errors.New("should not be allowed to create nested repo"),
272 imports: []metaImport{
273 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
274 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
276 path: "example.com/user/foo/bar/baz/qux",
277 err: errors.New("should not be allowed to create nested repo"),
280 imports: []metaImport{
281 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
282 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
284 path: "example.com/user/foo/bar/baz/",
285 err: errors.New("should not be allowed to create nested repo"),
288 imports: []metaImport{
289 {Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
290 {Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
293 err: errors.New("pathologically short path"),
297 for _, test := range tests {
298 mi, err := matchGoImport(test.imports, test.path)
300 t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi)
305 if (got == nil) != (want == nil) {
306 t.Errorf("unexpected error; got %v, want %v", got, want)