From 0bc48d6051161405bf7316faee70083a160c0aa0 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 10 May 2023 14:18:24 +0200 Subject: [PATCH] Restore 1.x.x compatibility --- paths.go | 6 +++--- paths_test.go | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/paths.go b/paths.go index a9e752b..8b07bdc 100644 --- a/paths.go +++ b/paths.go @@ -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 diff --git a/paths_test.go b/paths_test.go index fba0481..116131f 100644 --- a/paths_test.go +++ b/paths_test.go @@ -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)