diff --git a/pkg/utils/file.go b/pkg/utils/file.go index 9e0368db7ce..21bb00697b7 100644 --- a/pkg/utils/file.go +++ b/pkg/utils/file.go @@ -276,7 +276,7 @@ func IsPathInDir(dir, pathToCheck string) bool { rel, err := filepath.Rel(dir, pathToCheck) if err == nil { - if !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + if !strings.HasPrefix(rel, "..") { // if path is the parent no separator is returned return true } } diff --git a/pkg/utils/file_linux_test.go b/pkg/utils/file_linux_test.go new file mode 100644 index 00000000000..7f106ae14a5 --- /dev/null +++ b/pkg/utils/file_linux_test.go @@ -0,0 +1,35 @@ +// +build linux + +package utils + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/assert" +) + +// Due to the way some functions work some tests need to be run +// with data for the running OS + +// linux path tests for IsPathInDir (path separator is evaluated at runtime) +var linuxTestPaths = []struct { + basePath string + path string + isInDir bool +}{ + {"/media/.previews", "/media", false}, //1 + {"/media", "/media/.previews", true}, + {"/", "/my/media/path", true}, + {"/opt/stash/media", "/opt/media", false}, + {"/opt/", "/opt/stash/media", true}, + {"/opt/stash/", "/opt/stash", true}, // 6 +} + +func TestIsPathInDirLinux(t *testing.T) { + assert := assert.New(t) + + for i, tp := range linuxTestPaths { + assert.Equal(tp.isInDir, IsPathInDir(tp.basePath, tp.path), "Test "+strconv.Itoa(i+1)+":"+tp.basePath+"... failed") + } +} diff --git a/pkg/utils/file_windows_test.go b/pkg/utils/file_windows_test.go new file mode 100644 index 00000000000..a258483dd2e --- /dev/null +++ b/pkg/utils/file_windows_test.go @@ -0,0 +1,35 @@ +// +build windows + +package utils + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/assert" +) + +// Due to the way some functions work some tests need to be run +// with data for the running OS + +// windows path tests for IsPathInDir (path separator is evaluated at runtime) +var windowsTestPaths = []struct { + basePath string + path string + isInDir bool +}{ + {"c:\\media\\.previews", "c:\\media", false}, //1 + {"c:\\media", "c:\\media\\.previews", true}, + {"c:\\", "c:\\my\\media\\path", true}, + {"\\\\netshare\\stash\\media", "\\\\netshare\\opt\\media", false}, + {"\\\\netshare", "\\\\netshare\\stash\\media", true}, + {"c:\\user\\data\\2", "c:\\user\\data\\2", true}, // 6 +} + +func TestIsPathInDirWindows(t *testing.T) { + assert := assert.New(t) + + for i, tp := range windowsTestPaths { + assert.Equal(tp.isInDir, IsPathInDir(tp.basePath, tp.path), "Test "+strconv.Itoa(i+1)+":"+tp.basePath+"... failed") + } +}