Skip to content

Commit

Permalink
Fix path in dir (stashapp#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkai committed Mar 10, 2021
1 parent 698e21a commit 607ab36
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/utils/file_linux_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
35 changes: 35 additions & 0 deletions pkg/utils/file_windows_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 607ab36

Please sign in to comment.