Skip to content

Commit

Permalink
Resolve symlinked PWD in FileDetector.Detect
Browse files Browse the repository at this point in the history
When a relative symlink is provided for the pwd in FileDetector.Detect,
it can result in an invalid URL relative to the file root, e.g.
`file:///../modules/foo`. Resolve the absolute path after reading a
symlink in Detect.
  • Loading branch information
jbardin committed Sep 12, 2016
1 parent 0eb633d commit 0c1391c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions detect_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
if err != nil {
return "", true, err
}

// The symlink itself might be a relative path, so we have to
// resolve this to have a correctly rooted URL.
pwd, err = filepath.Abs(pwd)
if err != nil {
return "", true, err
}
}
}

Expand Down
70 changes: 70 additions & 0 deletions detect_file_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// +build test unix

package getter

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

// If a relative symlink is passed in as the pwd to Detect, the resulting URL
// can have an invalid path.
func TestFileDetector_relativeSymlink(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "go-getter")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(tmpDir)

// We may have a symlinked tmp dir,
// e.g. OSX uses /var -> /private/var
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}

err = os.Mkdir(filepath.Join(tmpDir, "realPWD"), 0755)
if err != nil {
t.Fatal(err)
}

subdir := filepath.Join(tmpDir, "subdir")
err = os.Mkdir(subdir, 0755)
if err != nil {
t.Fatal(err)
}

prevDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(prevDir)

err = os.Chdir(subdir)
if err != nil {
t.Fatal(err)
}

err = os.Symlink("../realPWD", "linkedPWD")
if err != nil {
t.Fatal(err)
}

// if detech doesn't fully resolve the pwd symlink, the output will be the
// invalid path: "file:///../modules/foo"
f := new(FileDetector)
out, ok, err := f.Detect("../modules/foo", "./linkedPWD")
if err != nil {
t.Fatalf("err: %v", err)
}
if !ok {
t.Fatal("not ok")
}
if out != "file://"+filepath.Join(tmpDir, "modules/foo") {
t.Logf("expected: %v", "file://"+filepath.Join(tmpDir, "modules/foo"))
t.Fatalf("bad: %v", out)
}
}

0 comments on commit 0c1391c

Please sign in to comment.