зеркало из https://github.com/golang/tools.git
go/vcs: match go-import package prefixes by slash
This is related to golang/go#15947. This ports https://golang.org/cl/23732 (June 2016) from cmd/go to this repository. Change-Id: I9960556bd19775b433d4eecc0ecca3f09ce1368b Reviewed-on: https://go-review.googlesource.com/119935 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Родитель
53440098a4
Коммит
fcb3cb6c24
|
@ -603,15 +603,28 @@ type metaImport struct {
|
|||
// errNoMatch is returned from matchGoImport when there's no applicable match.
|
||||
var errNoMatch = errors.New("no import match")
|
||||
|
||||
// pathPrefix reports whether sub is a prefix of s,
|
||||
// only considering entire path components.
|
||||
func pathPrefix(s, sub string) bool {
|
||||
// strings.HasPrefix is necessary but not sufficient.
|
||||
if !strings.HasPrefix(s, sub) {
|
||||
return false
|
||||
}
|
||||
// The remainder after the prefix must either be empty or start with a slash.
|
||||
rem := s[len(sub):]
|
||||
return rem == "" || rem[0] == '/'
|
||||
}
|
||||
|
||||
// matchGoImport returns the metaImport from imports matching importPath.
|
||||
// An error is returned if there are multiple matches.
|
||||
// errNoMatch is returned if none match.
|
||||
func matchGoImport(imports []metaImport, importPath string) (_ metaImport, err error) {
|
||||
match := -1
|
||||
for i, im := range imports {
|
||||
if !strings.HasPrefix(importPath, im.Prefix) {
|
||||
if !pathPrefix(importPath, im.Prefix) {
|
||||
continue
|
||||
}
|
||||
|
||||
if match != -1 {
|
||||
err = fmt.Errorf("multiple meta tags match import path %q", importPath)
|
||||
return
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package vcs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
@ -184,3 +185,96 @@ func TestValidateRepoRoot(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchGoImport(t *testing.T) {
|
||||
tests := []struct {
|
||||
imports []metaImport
|
||||
path string
|
||||
mi metaImport
|
||||
err error
|
||||
}{
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo",
|
||||
mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo/",
|
||||
mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo",
|
||||
mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/fooa",
|
||||
mi: metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo/bar",
|
||||
err: errors.New("should not be allowed to create nested repo"),
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo/bar/baz",
|
||||
err: errors.New("should not be allowed to create nested repo"),
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo/bar/baz/qux",
|
||||
err: errors.New("should not be allowed to create nested repo"),
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com/user/foo/bar/baz/",
|
||||
err: errors.New("should not be allowed to create nested repo"),
|
||||
},
|
||||
{
|
||||
imports: []metaImport{
|
||||
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||
},
|
||||
path: "example.com",
|
||||
err: errors.New("pathologically short path"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
mi, err := matchGoImport(test.imports, test.path)
|
||||
if mi != test.mi {
|
||||
t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi)
|
||||
}
|
||||
|
||||
got := err
|
||||
want := test.err
|
||||
if (got == nil) != (want == nil) {
|
||||
t.Errorf("unexpected error; got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче