Skip to content

Restore 1.x.x compatibility #22

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

Merged
merged 1 commit into from
May 10, 2023
Merged
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
6 changes: 3 additions & 3 deletions paths.go
Original file line number Diff line number Diff line change
@@ -188,17 +188,17 @@ func (p *Path) Clean() *Path {

// IsInsideDir returns true if the current path is inside the provided
// dir
func (p *Path) IsInsideDir(dir *Path) bool {
func (p *Path) IsInsideDir(dir *Path) (bool, error) {
rel, err := filepath.Rel(dir.path, p.path)
if err != nil {
// If the dir cannot be made relative to this path it means
// that it belong to a different filesystems, so it cannot be
// inside this path.
return false
return false, nil
}
return !strings.Contains(rel, ".."+string(os.PathSeparator)) &&
rel != ".." &&
rel != "."
rel != ".", nil
}

// Parent returns all but the last element of path, typically the path's
12 changes: 9 additions & 3 deletions paths_test.go
Original file line number Diff line number Diff line change
@@ -155,11 +155,15 @@ func TestResetStatCacheWhenFollowingSymlink(t *testing.T) {

func TestIsInsideDir(t *testing.T) {
notInside := func(a, b *Path) {
require.False(t, a.IsInsideDir(b), "%s is inside %s", a, b)
isInside, err := a.IsInsideDir(b)
require.NoError(t, err)
require.False(t, isInside, "%s is inside %s", a, b)
}

inside := func(a, b *Path) {
require.True(t, a.IsInsideDir(b), "%s is inside %s", a, b)
isInside, err := a.IsInsideDir(b)
require.NoError(t, err)
require.True(t, isInside, "%s is inside %s", a, b)
notInside(b, a)
}

@@ -377,7 +381,9 @@ func TestWriteToTempFile(t *testing.T) {
defer tmp.Remove()
require.NoError(t, err)
require.True(t, strings.HasPrefix(tmp.Base(), "prefix"))
require.True(t, tmp.IsInsideDir(tmpDir))
isInside, err := tmp.IsInsideDir(tmpDir)
require.NoError(t, err)
require.True(t, isInside)
data, err := tmp.ReadFile()
require.NoError(t, err)
require.Equal(t, tmpData, data)