Skip to content

Commit 4751e7c

Browse files
committed
detector/file: pwd symlink should be determined with EvalSymlinks
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.
1 parent 3942d21 commit 4751e7c

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

detect_file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
3232
return "", true, err
3333
}
3434
if fi.Mode()&os.ModeSymlink != 0 {
35-
pwd, err = os.Readlink(pwd)
35+
pwd, err = filepath.EvalSymlinks(pwd)
3636
if err != nil {
3737
return "", true, err
3838
}

detect_file_test.go

+40-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package getter
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
47
"runtime"
8+
"strings"
59
"testing"
610
)
711

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

1923
var unixFileTests = []fileTest{
24+
{"./foo", "test-fixtures/detect-file-symlink-pwd/syml/pwd",
25+
"test-fixtures/detect-file-symlink-pwd/real/foo", false},
26+
2027
{"/foo", "/pwd", "file:///foo", false},
2128
{"/foo?bar=baz", "/pwd", "file:///foo?bar=baz", false},
2229
}
@@ -34,19 +41,42 @@ func TestFileDetector(t *testing.T) {
3441
fileTests = append(fileTests, unixFileTests...)
3542
}
3643

44+
// Get the pwd
45+
pwdRoot, err := os.Getwd()
46+
if err != nil {
47+
t.Fatalf("err: %s", err)
48+
}
49+
pwdRoot, err = filepath.Abs(pwdRoot)
50+
if err != nil {
51+
t.Fatalf("err: %s", err)
52+
}
53+
3754
f := new(FileDetector)
3855
for i, tc := range fileTests {
39-
out, ok, err := f.Detect(tc.in, tc.pwd)
40-
if err != nil {
41-
t.Fatalf("err: %s", err)
42-
}
43-
if !ok {
44-
t.Fatal("not ok")
45-
}
56+
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
57+
pwd := tc.pwd
58+
if !filepath.IsAbs(pwd) {
59+
pwd = filepath.Join(pwdRoot, pwd)
60+
}
4661

47-
if out != tc.out {
48-
t.Fatalf("%d: bad: %#v", i, out)
49-
}
62+
out, ok, err := f.Detect(tc.in, pwd)
63+
if err != nil {
64+
t.Fatalf("err: %s", err)
65+
}
66+
if !ok {
67+
t.Fatal("not ok")
68+
}
69+
70+
expected := tc.out
71+
if !strings.HasPrefix(expected, "file://") {
72+
expected = "file://" + filepath.Join(pwdRoot, expected)
73+
}
74+
75+
if out != expected {
76+
t.Fatalf("input: %q\npwd: %q\nexpected: %q\nbad output: %#v",
77+
tc.in, pwd, expected, out)
78+
}
79+
})
5080
}
5181
}
5282

test-fixtures/detect-file-symlink-pwd/real/hello.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../real

0 commit comments

Comments
 (0)