You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package nfs_test
|
|
|
|
import (
|
|
"git.noahlan.cn/noahlan/ntool/nfs"
|
|
"git.noahlan.cn/noahlan/ntool/ntest/assert"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
//goland:noinspection GoBoolExpressions
|
|
func TestNfs_common(t *testing.T) {
|
|
assert.Eq(t, "", nfs.FileExt("testdata/testjpg"))
|
|
assert.Eq(t, "", nfs.Suffix("testdata/testjpg"))
|
|
assert.Eq(t, "", nfs.ExtName("testdata/testjpg"))
|
|
assert.Eq(t, ".txt", nfs.FileExt("testdata/test.txt"))
|
|
assert.Eq(t, ".txt", nfs.Suffix("testdata/test.txt"))
|
|
assert.Eq(t, "txt", nfs.ExtName("testdata/test.txt"))
|
|
|
|
// IsZipFile
|
|
assert.False(t, nfs.IsZipFile("testdata/not-exists-file"))
|
|
assert.False(t, nfs.IsZipFile("testdata/test.txt"))
|
|
assert.Eq(t, "test.txt", nfs.PathName("testdata/test.txt"))
|
|
|
|
assert.Eq(t, "test.txt", nfs.Name("path/to/test.txt"))
|
|
assert.Eq(t, "", nfs.Name(""))
|
|
|
|
if runtime.GOOS == "windows" {
|
|
assert.Eq(t, "path\\to", nfs.Dir("path/to/test.txt"))
|
|
} else {
|
|
assert.Eq(t, "path/to", nfs.Dir("path/to/test.txt"))
|
|
}
|
|
}
|
|
|
|
func TestPathExists(t *testing.T) {
|
|
assert.False(t, nfs.PathExists(""))
|
|
assert.False(t, nfs.PathExists("/not-exist"))
|
|
assert.False(t, nfs.PathExists("/not-exist"))
|
|
assert.True(t, nfs.PathExists("testdata/test.txt"))
|
|
assert.True(t, nfs.PathExists("testdata/test.txt"))
|
|
}
|
|
|
|
func TestIsFile(t *testing.T) {
|
|
assert.False(t, nfs.FileExists(""))
|
|
assert.False(t, nfs.IsFile(""))
|
|
assert.False(t, nfs.IsFile("/not-exist"))
|
|
assert.False(t, nfs.FileExists("/not-exist"))
|
|
assert.True(t, nfs.IsFile("testdata/test.txt"))
|
|
assert.True(t, nfs.FileExists("testdata/test.txt"))
|
|
}
|
|
|
|
func TestIsDir(t *testing.T) {
|
|
assert.False(t, nfs.IsDir(""))
|
|
assert.False(t, nfs.DirExists(""))
|
|
assert.False(t, nfs.IsDir("/not-exist"))
|
|
assert.True(t, nfs.IsDir("testdata"))
|
|
assert.True(t, nfs.DirExists("testdata"))
|
|
}
|
|
|
|
func TestIsAbsPath(t *testing.T) {
|
|
assert.True(t, nfs.IsAbsPath("/data/some.txt"))
|
|
assert.False(t, nfs.IsAbsPath(""))
|
|
assert.False(t, nfs.IsAbsPath("some.txt"))
|
|
assert.NoErr(t, nfs.DeleteIfFileExist("/not-exist"))
|
|
}
|
|
|
|
func TestGlobMatch(t *testing.T) {
|
|
tests := []struct {
|
|
p, s string
|
|
want bool
|
|
}{
|
|
{"a*", "abc", true},
|
|
{"ab.*.ef", "ab.cd.ef", true},
|
|
{"ab.*.*", "ab.cd.ef", true},
|
|
{"ab.cd.*", "ab.cd.ef", true},
|
|
{"ab.*", "ab.cd.ef", true},
|
|
{"a*/b", "a/c/b", false},
|
|
{"a*", "a/c/b", false},
|
|
{"a**", "a/c/b", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
assert.Eq(t, tt.want, nfs.PathMatch(tt.p, tt.s), "case %v", tt)
|
|
}
|
|
|
|
assert.False(t, nfs.PathMatch("ab", "abc"))
|
|
assert.True(t, nfs.PathMatch("ab*", "abc"))
|
|
}
|