forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_test.go
91 lines (78 loc) · 2.71 KB
/
check_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package fsutil_test
import (
"runtime"
"testing"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
)
//goland:noinspection GoBoolExpressions
func TestFsUtil_common(t *testing.T) {
assert.Eq(t, "", fsutil.FileExt("testdata/testjpg"))
assert.Eq(t, "", fsutil.Suffix("testdata/testjpg"))
assert.Eq(t, "", fsutil.Extname("testdata/testjpg"))
assert.Eq(t, ".jpg", fsutil.FileExt("testdata/test.jpg"))
assert.Eq(t, ".jpg", fsutil.Suffix("testdata/test.jpg"))
assert.Eq(t, "jpg", fsutil.Extname("testdata/test.jpg"))
// IsZipFile
assert.False(t, fsutil.IsZipFile("testdata/not-exists-file"))
assert.False(t, fsutil.IsZipFile("testdata/test.jpg"))
assert.Eq(t, "test.jpg", fsutil.PathName("testdata/test.jpg"))
assert.Eq(t, "test.jpg", fsutil.Name("path/to/test.jpg"))
assert.Eq(t, "", fsutil.Name(""))
assert.NotEmpty(t, fsutil.DirPath("path/to/test.jpg"))
if runtime.GOOS == "windows" {
assert.Eq(t, "path\\to", fsutil.Dir("path/to/test.jpg"))
} else {
assert.Eq(t, "path/to", fsutil.Dir("path/to/test.jpg"))
}
}
func TestPathExists(t *testing.T) {
assert.False(t, fsutil.PathExists(""))
assert.False(t, fsutil.PathExist("/not-exist"))
assert.False(t, fsutil.PathExists("/not-exist"))
assert.True(t, fsutil.PathExist("testdata/test.jpg"))
assert.True(t, fsutil.PathExists("testdata/test.jpg"))
}
func TestIsFile(t *testing.T) {
assert.False(t, fsutil.FileExists(""))
assert.False(t, fsutil.IsFile(""))
assert.False(t, fsutil.IsFile("/not-exist"))
assert.False(t, fsutil.FileExists("/not-exist"))
assert.True(t, fsutil.IsFile("testdata/test.jpg"))
assert.True(t, fsutil.FileExist("testdata/test.jpg"))
}
func TestIsDir(t *testing.T) {
assert.False(t, fsutil.IsDir(""))
assert.False(t, fsutil.DirExist(""))
assert.False(t, fsutil.IsDir("/not-exist"))
assert.True(t, fsutil.IsDir("testdata"))
assert.True(t, fsutil.DirExist("testdata"))
assert.False(t, fsutil.IsEmptyDir("testdata"))
assert.False(t, fsutil.IsEmptyDir("testdata/not-exist-dir"))
}
func TestIsAbsPath(t *testing.T) {
assert.True(t, fsutil.IsAbsPath("/data/some.txt"))
assert.False(t, fsutil.IsAbsPath(""))
assert.False(t, fsutil.IsAbsPath("some.txt"))
assert.NoErr(t, fsutil.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, fsutil.PathMatch(tt.p, tt.s), "case %v", tt)
}
assert.False(t, fsutil.PathMatch("ab", "abc"))
assert.True(t, fsutil.PathMatch("ab*", "abc"))
}