Skip to content

Commit

Permalink
add test and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrasho committed May 8, 2017
1 parent 3e38fb2 commit a11483a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 5 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ func (c *Ctx) resolveProjectRoot(path string) (string, error) {
//
// The second returned string indicates which GOPATH value was used.
func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
if path == filepath.Join(c.GOPATH, "src") {
return "", errors.Errorf("Initializing a project in $GOPATH/src directly is not supported currently")
}

srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
if internal.HasFilepathPrefix(path, srcprefix) {
// if path length is shorter than srcprefix then the project is directly within $GOPATH/src
if len(path) < len(srcprefix) {
return "", errors.New("projects directly within $GOPATH/src are not supported currently")
}

// filepath.ToSlash because we're dealing with an import path now,
// not an fs path
return filepath.ToSlash(path[len(srcprefix):]), nil
Expand Down
12 changes: 9 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
}
}

// test where it should return error
got, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
// test where it should return an error when directly within $GOPATH/src
got, err := depCtx.SplitAbsoluteProjectRoot(filepath.Join(depCtx.GOPATH, "src"))
if err == nil {
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", got)
t.Fatalf("should have gotten an error but did not for %s", got)
}

// test where it should return an error
got, err = depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
if err == nil {
t.Fatalf("should have gotten an error but did not for tra/la/la/la: %s", got)
}
}

Expand Down

0 comments on commit a11483a

Please sign in to comment.