Skip to content

Commit

Permalink
detector/file: pwd symlink should be determined with EvalSymlinks
Browse files Browse the repository at this point in the history
We were using `ReadLink` before which read the exact link.
Unfortunately, we want to evaluate this link relative to the pwd if it
is an absolute path otherwise we evaluate the symlink incorrectly.

For example, if the pwd is set to "/foo/fake" and the value is
"../real", but we're evaluating go-getter from the real shell pwd of
"/bar", then we'd turn "/foo/fake" into "/bar/real". The behavior of
shells is to take it relative to its location, not your pwd. This
preserves that.
  • Loading branch information
mitchellh committed Sep 5, 2017
1 parent 3942d21 commit 4751e7c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion detect_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
return "", true, err
}
if fi.Mode()&os.ModeSymlink != 0 {
pwd, err = os.Readlink(pwd)
pwd, err = filepath.EvalSymlinks(pwd)
if err != nil {
return "", true, err
}
Expand Down
50 changes: 40 additions & 10 deletions detect_file_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package getter

import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)

Expand All @@ -17,6 +21,9 @@ var fileTests = []fileTest{
}

var unixFileTests = []fileTest{
{"./foo", "test-fixtures/detect-file-symlink-pwd/syml/pwd",
"test-fixtures/detect-file-symlink-pwd/real/foo", false},

{"/foo", "/pwd", "file:///foo", false},
{"/foo?bar=baz", "/pwd", "file:///foo?bar=baz", false},
}
Expand All @@ -34,19 +41,42 @@ func TestFileDetector(t *testing.T) {
fileTests = append(fileTests, unixFileTests...)
}

// Get the pwd
pwdRoot, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
pwdRoot, err = filepath.Abs(pwdRoot)
if err != nil {
t.Fatalf("err: %s", err)
}

f := new(FileDetector)
for i, tc := range fileTests {
out, ok, err := f.Detect(tc.in, tc.pwd)
if err != nil {
t.Fatalf("err: %s", err)
}
if !ok {
t.Fatal("not ok")
}
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
pwd := tc.pwd
if !filepath.IsAbs(pwd) {
pwd = filepath.Join(pwdRoot, pwd)
}

if out != tc.out {
t.Fatalf("%d: bad: %#v", i, out)
}
out, ok, err := f.Detect(tc.in, pwd)
if err != nil {
t.Fatalf("err: %s", err)
}
if !ok {
t.Fatal("not ok")
}

expected := tc.out
if !strings.HasPrefix(expected, "file://") {
expected = "file://" + filepath.Join(pwdRoot, expected)
}

if out != expected {
t.Fatalf("input: %q\npwd: %q\nexpected: %q\nbad output: %#v",
tc.in, pwd, expected, out)
}
})
}
}

Expand Down
Empty file.
1 change: 1 addition & 0 deletions test-fixtures/detect-file-symlink-pwd/syml/pwd

0 comments on commit 4751e7c

Please sign in to comment.