Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

SplitAbsoluteProjectRoot re-name/doc #682

Merged
merged 1 commit into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
return errors.Errorf("invalid state: manifest %q does not exist, but lock %q does", mf, lf)
}

ip, err := ctx.SplitAbsoluteProjectRoot(root)
ip, err := ctx.ImportForAbs(root)
if err != nil {
return errors.Wrap(err, "determineProjectRoot")
return errors.Wrap(err, "root project import")
}
p.ImportRoot = gps.ProjectRoot(ip)

Expand Down
17 changes: 6 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func (c *Ctx) LoadProject() (*Project, error) {
return nil, err
}

ip, err := c.SplitAbsoluteProjectRoot(p.AbsRoot)
ip, err := c.ImportForAbs(p.AbsRoot)
if err != nil {
return nil, errors.Wrap(err, "split absolute project root")
return nil, errors.Wrap(err, "root project import")
}
p.ImportRoot = gps.ProjectRoot(ip)

Expand Down Expand Up @@ -237,14 +237,9 @@ func (c *Ctx) detectGOPATH(path string) (string, error) {
return "", errors.Errorf("%s is not within a known GOPATH", path)
}

// SplitAbsoluteProjectRoot takes an absolute path and compares it against the detected
// GOPATH to determine what portion of the input path should be treated as an
// import path - as a project root.
func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
if c.GOPATH == "" {
return "", errors.Errorf("no GOPATH detected in this context")
}

// ImportForAbs returns the import path for an absolute project path by trimming the
// `$GOPATH/src/` prefix. Returns an error for paths equal to, or without this prefix.
func (c *Ctx) ImportForAbs(path string) (string, error) {
srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
if fs.HasFilepathPrefix(path, srcprefix) {
if len(path) <= len(srcprefix) {
Expand All @@ -256,7 +251,7 @@ func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
return filepath.ToSlash(path[len(srcprefix):]), nil
}

return "", errors.Errorf("%s not in any GOPATH", path)
return "", errors.Errorf("%s not in GOPATH", path)
}

// absoluteProjectRoot determines the absolute path to the project root
Expand Down
10 changes: 5 additions & 5 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
discardLogger = log.New(ioutil.Discard, "", 0)
)

func TestSplitAbsoluteProjectRoot(t *testing.T) {
func TestCtx_ProjectImport(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

Expand All @@ -38,7 +38,7 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {

for _, want := range importPaths {
fullpath := filepath.Join(depCtx.GOPATH, "src", want)
got, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
got, err := depCtx.ImportForAbs(fullpath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -48,13 +48,13 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
}

// test where it should return an error when directly within $GOPATH/src
got, err := depCtx.SplitAbsoluteProjectRoot(filepath.Join(depCtx.GOPATH, "src"))
got, err := depCtx.ImportForAbs(filepath.Join(depCtx.GOPATH, "src"))
if err == nil || !strings.Contains(err.Error(), "GOPATH/src") {
t.Fatalf("should have gotten an error for use directly in GOPATH/src, but got %s", got)
}

// test where it should return an error
got, err = depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
got, err = depCtx.ImportForAbs("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 Expand Up @@ -345,7 +345,7 @@ func TestCaseInsentitiveGOPATH(t *testing.T) {

ip := "github.com/pkg/errors"
fullpath := filepath.Join(depCtx.GOPATH, "src", ip)
pr, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
pr, err := depCtx.ImportForAbs(fullpath)
if err != nil {
t.Fatal(err)
}
Expand Down