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

Commit 4bfa359

Browse files
authored
Merge pull request #682 from jmank88/split_absolute_project_root
SplitAbsoluteProjectRoot re-name/doc
2 parents 5b45e0a + eb972b2 commit 4bfa359

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

cmd/dep/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
116116
return errors.Errorf("invalid state: manifest %q does not exist, but lock %q does", mf, lf)
117117
}
118118

119-
ip, err := ctx.SplitAbsoluteProjectRoot(root)
119+
ip, err := ctx.ImportForAbs(root)
120120
if err != nil {
121-
return errors.Wrap(err, "determineProjectRoot")
121+
return errors.Wrap(err, "root project import")
122122
}
123123
p.ImportRoot = gps.ProjectRoot(ip)
124124

context.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ func (c *Ctx) LoadProject() (*Project, error) {
113113
return nil, err
114114
}
115115

116-
ip, err := c.SplitAbsoluteProjectRoot(p.AbsRoot)
116+
ip, err := c.ImportForAbs(p.AbsRoot)
117117
if err != nil {
118-
return nil, errors.Wrap(err, "split absolute project root")
118+
return nil, errors.Wrap(err, "root project import")
119119
}
120120
p.ImportRoot = gps.ProjectRoot(ip)
121121

@@ -219,14 +219,9 @@ func (c *Ctx) detectGOPATH(path string) (string, error) {
219219
return "", errors.Errorf("%s is not within a known GOPATH", path)
220220
}
221221

222-
// SplitAbsoluteProjectRoot takes an absolute path and compares it against the detected
223-
// GOPATH to determine what portion of the input path should be treated as an
224-
// import path - as a project root.
225-
func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
226-
if c.GOPATH == "" {
227-
return "", errors.Errorf("no GOPATH detected in this context")
228-
}
229-
222+
// ImportForAbs returns the import path for an absolute project path by trimming the
223+
// `$GOPATH/src/` prefix. Returns an error for paths equal to, or without this prefix.
224+
func (c *Ctx) ImportForAbs(path string) (string, error) {
230225
srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
231226
if fs.HasFilepathPrefix(path, srcprefix) {
232227
if len(path) <= len(srcprefix) {
@@ -238,7 +233,7 @@ func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
238233
return filepath.ToSlash(path[len(srcprefix):]), nil
239234
}
240235

241-
return "", errors.Errorf("%s not in any GOPATH", path)
236+
return "", errors.Errorf("%s not in GOPATH", path)
242237
}
243238

244239
// absoluteProjectRoot determines the absolute path to the project root

context_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
discardLogger = log.New(ioutil.Discard, "", 0)
2323
)
2424

25-
func TestSplitAbsoluteProjectRoot(t *testing.T) {
25+
func TestCtx_ProjectImport(t *testing.T) {
2626
h := test.NewHelper(t)
2727
defer h.Cleanup()
2828

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

3939
for _, want := range importPaths {
4040
fullpath := filepath.Join(depCtx.GOPATH, "src", want)
41-
got, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
41+
got, err := depCtx.ImportForAbs(fullpath)
4242
if err != nil {
4343
t.Fatal(err)
4444
}
@@ -48,13 +48,13 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
4848
}
4949

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

5656
// test where it should return an error
57-
got, err = depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
57+
got, err = depCtx.ImportForAbs("tra/la/la/la")
5858
if err == nil {
5959
t.Fatalf("should have gotten an error but did not for tra/la/la/la: %s", got)
6060
}
@@ -345,7 +345,7 @@ func TestCaseInsentitiveGOPATH(t *testing.T) {
345345

346346
ip := "github.com/pkg/errors"
347347
fullpath := filepath.Join(depCtx.GOPATH, "src", ip)
348-
pr, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
348+
pr, err := depCtx.ImportForAbs(fullpath)
349349
if err != nil {
350350
t.Fatal(err)
351351
}

0 commit comments

Comments
 (0)