2022-03-17 00:33:59 +00:00
|
|
|
|
package fsutil
|
2021-03-11 02:37:13 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2021-05-17 05:05:29 +00:00
|
|
|
|
"os"
|
2021-03-11 02:37:13 +00:00
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIsPathInDir(t *testing.T) {
|
|
|
|
|
type test struct {
|
|
|
|
|
dir string
|
|
|
|
|
pathToCheck string
|
|
|
|
|
expected bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parentDirName = "parentDir"
|
|
|
|
|
const subDirName = "subDir"
|
|
|
|
|
const filename = "filename"
|
|
|
|
|
subDir := filepath.Join(parentDirName, subDirName)
|
|
|
|
|
fileInSubDir := filepath.Join(subDir, filename)
|
|
|
|
|
fileInParentDir := filepath.Join(parentDirName, filename)
|
|
|
|
|
subSubSubDir := filepath.Join(parentDirName, subDirName, subDirName, subDirName)
|
|
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
|
{dir: parentDirName, pathToCheck: subDir, expected: true},
|
|
|
|
|
{dir: subDir, pathToCheck: subDir, expected: true},
|
|
|
|
|
{dir: subDir, pathToCheck: parentDirName, expected: false},
|
|
|
|
|
{dir: subDir, pathToCheck: fileInSubDir, expected: true},
|
|
|
|
|
{dir: parentDirName, pathToCheck: fileInSubDir, expected: true},
|
|
|
|
|
{dir: subDir, pathToCheck: fileInParentDir, expected: false},
|
|
|
|
|
{dir: parentDirName, pathToCheck: fileInParentDir, expected: true},
|
|
|
|
|
{dir: parentDirName, pathToCheck: filename, expected: false},
|
|
|
|
|
{dir: parentDirName, pathToCheck: subSubSubDir, expected: true},
|
|
|
|
|
{dir: subSubSubDir, pathToCheck: parentDirName, expected: false},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
|
result := IsPathInDir(tc.dir, tc.pathToCheck)
|
|
|
|
|
assert.Equal(tc.expected, result, "[%d] expected: %t for dir: %s; pathToCheck: %s", i, tc.expected, tc.dir, tc.pathToCheck)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-17 05:05:29 +00:00
|
|
|
|
|
|
|
|
|
func TestDirExists(t *testing.T) {
|
|
|
|
|
type test struct {
|
|
|
|
|
dir string
|
|
|
|
|
expected bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const st = "stash_tmp"
|
|
|
|
|
|
|
|
|
|
tmp := os.TempDir()
|
2021-09-27 00:55:23 +00:00
|
|
|
|
tmpDir, err := os.MkdirTemp(tmp, st) // create a tmp dir in the system's tmp folder
|
2021-05-17 05:05:29 +00:00
|
|
|
|
if err == nil {
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
2021-09-27 00:55:23 +00:00
|
|
|
|
tmpFile, err := os.CreateTemp(tmpDir, st)
|
2021-05-17 05:05:29 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
|
{dir: tmpDir, expected: true}, // exists
|
|
|
|
|
{dir: tmpFile.Name(), expected: false}, // not a directory
|
|
|
|
|
{dir: filepath.Join(tmpDir, st), expected: false}, // doesn't exist
|
|
|
|
|
{dir: "\000x", expected: false}, // stat error \000 (ASCII: NUL) is an invalid character in unix,ntfs file names.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
|
result, _ := DirExists(tc.dir)
|
|
|
|
|
assert.Equal(tc.expected, result, "[%d] expected: %t for dir: %s;", i, tc.expected, tc.dir)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|