8000 Resolve root directory if symlinked by caleb-fringer · Pull Request #742 · air-verse/air · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Resolve root directory if symlinked #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8000
Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,18 @@ func (c *Config) preprocess(args map[string]TomlInfo) error {
if err = os.Chdir(cwd); err != nil {
return err
}
c.Root = cwd
c.Root = "."
}
c.Root, err = expandPath(c.Root)
if err != nil {
return err
}
// Dereference the root node if it is a symbolic link
c.Root, err = derefLink(c.Root)
if err != nil {
return err
}

if c.TmpDir == "" {
c.TmpDir = "tmp"
}
Expand Down
54 changes: 44 additions & 10 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,57 @@ func (e *Engine) logWithLock(f func()) {
e.ll.Unlock()
}

// Checks if a directory is a symbolic link
func isSymlink(path string) (bool, error) {
pathInfo, err := os.Lstat(path)
if err != nil {
return false, err
}
if (pathInfo.Mode() & os.ModeSymlink) != 0 {
return true, nil
}
return false, nil
}

// Dereferences a symbolic link to its relative path.
// If the path is not a symlink, simply returns the path.
// If the file does not exist, return the path and no err.
func derefLink(path string) (string, error) {
ok, err := isSymlink(path)
if err != nil {
if os.IsNotExist(err) {
return path, nil
}
return "", err
}
if !ok {
return path, nil
}

targetPath, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
absTargetPath, err := filepath.Abs(targetPath)
if err != nil {
return "", err
}
return absTargetPath, nil
}

// expandPath takes a path string (which may be absolute, relative, or tilde
// expanded) and returns an absolute path to that file.
func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
home := os.Getenv("HOME")
return home + path[1:], nil
path = filepath.Join(home, path[1:])
}
var err error
wd, err := os.Getwd()

absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
if path == "." {
return wd, nil
}
if strings.HasPrefix(path, "./") {
return wd + path[1:], nil
}
return path, nil
return absPath, nil
}

func isDir(path string) bool {
Expand Down
65 changes: 65 additions & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"runtime"
Expand Down Expand Up @@ -39,6 +40,21 @@ func TestIsDirFileNot(t *testing.T) {
}
}

func TestExpandPathWithRelPath(t *testing.T) {
relPath := path.Join("_testdata", "toml", ".air.toml")

wd, err := os.Getwd()
if err != nil {
t.Fatalf("Error getting cwd: %v", err)
}

expandedPath, _ := expandPath(relPath)
expected := path.Join(wd, relPath)
if expandedPath != expected {
t.Errorf("expected %s got %s", expected, expandedPath)
}
}

func TestExpandPathWithDot(t *testing.T) {
path, _ := expandPath(".")
wd, _ := os.Getwd()
Expand Down Expand Up @@ -402,3 +418,52 @@ func TestFormatPath(t *testing.T) {
runTests(t, tests)
})
}

func TestIsSymlink(t *testing.T) {
type testCase struct {
name string
path string
expected bool
}

tmp := path.Join("_testdata/tmp")
_, err := os.Create(tmp)
defer os.Remove(tmp)

if err != nil {
t.Fatalf("Error creating temporary file for testing: %v", err)
}

tmpTarget := path.Join("_testdata/tmp.link")
err = os.Symlink(tmp, tmpTarget)
defer os.Remove(tmpTarget)
if err != nil {
t.Fatalf("Error creating symlink to cwd for testing: %v", err)
}

testCases := []testCase{
{
name: "Symlink to current directory",
path: tmpTarget,
expected: true,
},
{
name: "Absolute path to current directory",
path: tmp,
expected: false,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(*testing.T) {
ok, err := isSymlink(testCase.path)
if err != nil {
t.Errorf("isSymlink(%s): isSymlink returned an error: %v", testCase.path, err)
}

if ok != testCase.expected {
t.Errorf("isSymlink(%s): Expected %t, got %t", testCase.path, testCase.expected, ok)
}
})
}
}
0