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

GOPATH detection now works when GOPATH is a symlink #834

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
// detectGOPATH detects the GOPATH for a given path from ctx.GOPATHs.
func (c *Ctx) detectGOPATH(path string) (string, error) {
for _, gp := range c.GOPATHs {
// Evaluate symlinks in case the user's GOPATH contains a symlink.
gp, err := filepath.EvalSymlinks(gp)
if err != nil {
return "", errors.New("failed to evaluate symlinks on GOPATH")
}
if fs.HasFilepathPrefix(path, gp) {
return gp, nil
}
Expand Down
20 changes: 17 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,11 @@ func TestDetectProjectGOPATH(t *testing.T) {

h.TempDir("go")
h.TempDir("go-two")
h.TempDir("go-symgopath")
h.TempSymlink("go-symgopath", "go-three")

ctx := &Ctx{
GOPATHs: []string{h.Path("go"), h.Path("go-two")},
GOPATHs: []string{h.Path("go"), h.Path("go-two"), h.Path("go-three")},
}

h.TempDir("go/src/real/path")
Expand All @@ -376,12 +378,14 @@ func TestDetectProjectGOPATH(t *testing.T) {
h.TempDir(filepath.Join("go", "src", "sym", "path"))
h.TempDir(filepath.Join("go", "src", " real", "path"))
h.TempDir(filepath.Join("go-two", "src", "real", "path"))
h.TempDir(filepath.Join("go-symgopath", "src", "real", "path"))

testcases := []struct {
name string
root string
resolvedRoot string
GOPATH string
realGOPATH string
expectErr bool
}{
{
Expand Down Expand Up @@ -432,6 +436,13 @@ func TestDetectProjectGOPATH(t *testing.T) {
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", " real", "path"),
GOPATH: ctx.GOPATHs[0],
},
{
name: "GOPATH-is-a-symlink",
root: filepath.Join(h.Path("go-symgopath"), "src", "real", "path"),
resolvedRoot: filepath.Join(h.Path("go-symgopath"), "src", "real", "path"),
GOPATH: ctx.GOPATHs[2],
realGOPATH: h.Path("go-symgopath"),
},
}

for _, tc := range testcases {
Expand All @@ -447,8 +458,11 @@ func TestDetectProjectGOPATH(t *testing.T) {
} else if tc.expectErr && err == nil {
t.Fatalf("expected an error, got nil and gopath %s", GOPATH)
}
if GOPATH != tc.GOPATH {
t.Errorf("expected GOPATH %s, got %s", tc.GOPATH, GOPATH)
if tc.realGOPATH == "" {
tc.realGOPATH = tc.GOPATH
}
if GOPATH != tc.realGOPATH {
t.Errorf("expected GOPATH %s, got %s", tc.realGOPATH, GOPATH)
}
})
}
Expand Down
26 changes: 16 additions & 10 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,26 +497,32 @@ func (h *Helper) TempDir(path string) {
}
}

// TempSymlink adds a temporary symlink for a run of testgo.
func (h *Helper) TempSymlink(old, new string) {
h.makeTempdir()
fullOldPath := filepath.Join(h.tempdir, old)
fullNewPath := filepath.Join(h.tempdir, new)
if err := os.Symlink(fullOldPath, fullNewPath); err != nil {
h.t.Fatalf("%+v", errors.Errorf("Unable to create symlink from %s -> %s", fullOldPath, fullNewPath))
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
Can you add a comment for this method? As it's an exported method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this.


// Path returns the absolute pathname to file with the temporary
// directory.
func (h *Helper) Path(name string) string {
if h.tempdir == "" {
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: path(%q) with no tempdir", name))
}

var joined string
if name == "." {
joined = h.tempdir
} else {
joined = filepath.Join(h.tempdir, name)
evaled, err := filepath.EvalSymlinks(h.tempdir)
if err != nil {
h.t.Fatalf("%+v", errors.Wrapf(err, "internal testsuite error: could not evaluate symlinks for dir(%q)", h.tempdir))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is similar to the one in #812, which also adds another function. You might have to rebase this once #812 gets merged. 😊

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I can wait and fix it after #812 is completed.

}

// Ensure it's the absolute, symlink-less path we're returning
abs, err := filepath.EvalSymlinks(joined)
if err != nil {
h.t.Fatalf("%+v", errors.Wrapf(err, "internal testsuite error: could not get absolute path for dir(%q)", joined))
if name == "." {
return evaled
}
return abs
return filepath.Join(evaled, name)
}

// MustExist fails if path does not exist.
Expand Down