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

Commit d688a81

Browse files
committed
GOPATH detection now works when GOPATH is a symlink
1 parent 911cd22 commit d688a81

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
212212
// detectGOPATH detects the GOPATH for a given path from ctx.GOPATHs.
213213
func (c *Ctx) detectGOPATH(path string) (string, error) {
214214
for _, gp := range c.GOPATHs {
215+
// Evaluate symlinks in case the user's GOPATH contains a symlink.
216+
gp, err := filepath.EvalSymlinks(gp)
217+
if err != nil {
218+
return "", errors.New("failed to evaluate symlinks on GOPATH")
219+
}
215220
if fs.HasFilepathPrefix(path, gp) {
216221
return gp, nil
217222
}

context_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,11 @@ func TestDetectProjectGOPATH(t *testing.T) {
362362

363363
h.TempDir("go")
364364
h.TempDir("go-two")
365+
h.TempDir("go-symgopath")
366+
h.TempSymlink("go-symgopath", "go-three")
365367

366368
ctx := &Ctx{
367-
GOPATHs: []string{h.Path("go"), h.Path("go-two")},
369+
GOPATHs: []string{h.Path("go"), h.Path("go-two"), h.Path("go-three")},
368370
}
369371

370372
h.TempDir("go/src/real/path")
@@ -376,12 +378,14 @@ func TestDetectProjectGOPATH(t *testing.T) {
376378
h.TempDir(filepath.Join("go", "src", "sym", "path"))
377379
h.TempDir(filepath.Join("go", "src", " real", "path"))
378380
h.TempDir(filepath.Join("go-two", "src", "real", "path"))
381+
h.TempDir(filepath.Join("go-symgopath", "src", "real", "path"))
379382

380383
testcases := []struct {
381384
name string
382385
root string
383386
resolvedRoot string
384387
GOPATH string
388+
realGOPATH string
385389
expectErr bool
386390
}{
387391
{
@@ -432,6 +436,13 @@ func TestDetectProjectGOPATH(t *testing.T) {
432436
resolvedRoot: filepath.Join(ctx.GOPATHs[0], "src", " real", "path"),
433437
GOPATH: ctx.GOPATHs[0],
434438
},
439+
{
440+
name: "GOPATH-is-a-symlink",
441+
root: filepath.Join(h.Path("go-symgopath"), "src", "real", "path"),
442+
resolvedRoot: filepath.Join(h.Path("go-symgopath"), "src", "real", "path"),
443+
GOPATH: ctx.GOPATHs[2],
444+
realGOPATH: h.Path("go-symgopath"),
445+
},
435446
}
436447

437448
for _, tc := range testcases {
@@ -447,8 +458,11 @@ func TestDetectProjectGOPATH(t *testing.T) {
447458
} else if tc.expectErr && err == nil {
448459
t.Fatalf("expected an error, got nil and gopath %s", GOPATH)
449460
}
450-
if GOPATH != tc.GOPATH {
451-
t.Errorf("expected GOPATH %s, got %s", tc.GOPATH, GOPATH)
461+
if tc.realGOPATH == "" {
462+
tc.realGOPATH = tc.GOPATH
463+
}
464+
if GOPATH != tc.realGOPATH {
465+
t.Errorf("expected GOPATH %s, got %s", tc.realGOPATH, GOPATH)
452466
}
453467
})
454468
}

internal/test/test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,26 +497,32 @@ func (h *Helper) TempDir(path string) {
497497
}
498498
}
499499

500+
// TempSymlink adds a temporary symlink for a run of testgo.
501+
func (h *Helper) TempSymlink(old, new string) {
502+
h.makeTempdir()
503+
fullOldPath := filepath.Join(h.tempdir, old)
504+
fullNewPath := filepath.Join(h.tempdir, new)
505+
if err := os.Symlink(fullOldPath, fullNewPath); err != nil {
506+
h.t.Fatalf("%+v", errors.Errorf("Unable to create symlink from %s -> %s", fullOldPath, fullNewPath))
507+
}
508+
}
509+
500510
// Path returns the absolute pathname to file with the temporary
501511
// directory.
502512
func (h *Helper) Path(name string) string {
503513
if h.tempdir == "" {
504514
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: path(%q) with no tempdir", name))
505515
}
506516

507-
var joined string
508-
if name == "." {
509-
joined = h.tempdir
510-
} else {
511-
joined = filepath.Join(h.tempdir, name)
517+
evaled, err := filepath.EvalSymlinks(h.tempdir)
518+
if err != nil {
519+
h.t.Fatalf("%+v", errors.Wrapf(err, "internal testsuite error: could not evaluate symlinks for dir(%q)", h.tempdir))
512520
}
513521

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

522528
// MustExist fails if path does not exist.

0 commit comments

Comments
 (0)