diff --git a/util/normalizename_test.go b/util/normalizename_test.go index fe1f77b2..c6f4d02f 100644 --- a/util/normalizename_test.go +++ b/util/normalizename_test.go @@ -5,17 +5,44 @@ import ( ) func TestNormalizeName(t *testing.T) { - packages := map[string]string{ - "github.com/Masterminds/cookoo/web/io/foo": "github.com/Masterminds/cookoo", - "golang.org/x/crypto/ssh": "golang.org/x/crypto", - "incomplete/example": "incomplete/example", - "net": "net", + packages := []struct { + input string + root string + extra string + }{ + { + input: "github.com/Masterminds/cookoo/web/io/foo", + root: "github.com/Masterminds/cookoo", + extra: "web/io/foo", + }, + { + input: `github.com\Masterminds\cookoo\web\io\foo`, + root: "github.com/Masterminds/cookoo", + extra: "web/io/foo", + }, + { + input: "golang.org/x/crypto/ssh", + root: "golang.org/x/crypto", + extra: "ssh", + }, + { + input: "incomplete/example", + root: "incomplete/example", + extra: "", + }, + { + input: "net", + root: "net", + extra: "", + }, } - for start, expected := range packages { - if finish, extra := NormalizeName(start); expected != finish { - t.Errorf("Expected '%s', got '%s'", expected, finish) - } else if start != finish && start != finish+"/"+extra { - t.Errorf("Expected %s to end with %s", finish, extra) + for _, test := range packages { + root, extra := NormalizeName(test.input) + if root != test.root { + t.Errorf("%s: Expected root '%s', got '%s'", test.input, test.root, root) + } + if extra != test.extra { + t.Errorf("%s: Expected extra '%s', got '%s'", test.input, test.extra, extra) } } } diff --git a/util/util.go b/util/util.go index e32fffaf..a5b346f9 100644 --- a/util/util.go +++ b/util/util.go @@ -29,13 +29,17 @@ func init() { } } +func toSlash(v string) string { + return strings.Replace(v, "\\", "/", -1) +} + // GetRootFromPackage retrives the top level package from a name. // // From a package name find the root repo. For example, // the package github.com/Masterminds/cookoo/io has a root repo // at github.com/Masterminds/cookoo func GetRootFromPackage(pkg string) string { - pkg = filepath.ToSlash(pkg) + pkg = toSlash(pkg) for _, v := range vcsList { m := v.regex.FindStringSubmatch(pkg) if m == nil { @@ -307,10 +311,11 @@ func NormalizeName(name string) (string, string) { if err == nil { p := filepath.Join(b.GOROOT, "src", name) if _, err := os.Stat(p); err == nil { - return filepath.ToSlash(name), "" + return toSlash(name), "" } } + name = toSlash(name) root := GetRootFromPackage(name) extra := strings.TrimPrefix(name, root) if len(extra) > 0 && extra != "/" {