Skip to content
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

Make GetPathRelativeTo() resolve symbolic links #78

Merged
merged 2 commits into from
Oct 25, 2022
Merged
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
17 changes: 14 additions & 3 deletions files/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func Grep(regex *regexp.Regexp, glob string) (bool, error) {
return false, nil
}

// Return the relative path you would have to take to get from basePath to path
// GetPathRelativeTo returns the relative path you would have to take to get from basePath to path. If either path
// or basePath are symbolic links, they will be evaluated before the relative path between them is calculated.
func GetPathRelativeTo(path string, basePath string) (string, error) {
if path == "" {
path = "."
Expand All @@ -77,12 +78,22 @@ func GetPathRelativeTo(path string, basePath string) (string, error) {
basePath = "."
infraredgirl marked this conversation as resolved.
Show resolved Hide resolved
}

inputFolderAbs, err := filepath.Abs(basePath)
basePathEvaluated, err := filepath.EvalSymlinks(basePath)
if err != nil {
return "", errors.WithStackTrace(err)
}

fileAbs, err := filepath.Abs(path)
inputFolderAbs, err := filepath.Abs(basePathEvaluated)
if err != nil {
return "", errors.WithStackTrace(err)
}

pathEvaluated, err := filepath.EvalSymlinks(path)
if err != nil {
return "", errors.WithStackTrace(err)
}

fileAbs, err := filepath.Abs(pathEvaluated)
if err != nil {
return "", errors.WithStackTrace(err)
}
Expand Down
7 changes: 6 additions & 1 deletion files/paths_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package files

import (
"path/filepath"
"regexp"
"testing"

Expand All @@ -27,8 +28,12 @@ func TestGetPathRelativeTo(t *testing.T) {
{"/root", "/other-root/sub-child/sub-sub-child", "../../../root"},
}

fixtureRoot := "../fixtures/files"

for _, testCase := range testCases {
actual, err := GetPathRelativeTo(testCase.path, testCase.basePath)
path := filepath.Join(fixtureRoot, testCase.path)
basePath := filepath.Join(fixtureRoot, testCase.basePath)
actual, err := GetPathRelativeTo(path, basePath)
assert.Nil(t, err, "Unexpected error for path %s and basePath %s: %v", testCase.path, testCase.basePath, err)
assert.Equal(t, testCase.expected, actual, "For path %s and basePath %s", testCase.path, testCase.basePath)
}
Expand Down
Empty file added fixtures/files/child/.gitkeep
Empty file.
Empty file.
Empty file.
Empty file.