forked from rfyiamcool/pgcacher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgcacher_test.go
More file actions
162 lines (133 loc) · 4.59 KB
/
Copy pathpgcacher_test.go
File metadata and controls
162 lines (133 loc) · 4.59 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"os"
"path/filepath"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
// --- wildcardMatch tests ---
func TestWildcardMatch(t *testing.T) {
assert.True(t, wildcardMatch("xiaorui.cc", "*rui*"))
assert.True(t, wildcardMatch("xiaorui.cc", "xiaorui?cc"))
assert.True(t, wildcardMatch("xiaorui.cc", "xiaorui?cc*"))
assert.True(t, wildcardMatch("xiaorui.cc", "*xiaorui?cc*"))
assert.True(t, wildcardMatch("github.com/rfyiamcool", "rfy"))
// exact match
assert.True(t, wildcardMatch("hello", "hello"))
// no match
assert.False(t, wildcardMatch("hello", "world"))
// wildcard only
assert.True(t, wildcardMatch("anything", "*"))
// single char wildcard
assert.True(t, wildcardMatch("ab", "a?"))
assert.False(t, wildcardMatch("abc", "a?"))
}
// --- wildcardMatchMulti tests ---
func TestWildcardMatchMulti(t *testing.T) {
// comma-separated: match second pattern
assert.True(t, wildcardMatchMulti("rfyiamcool", "*xiaorui*,rfyiamcool"))
// comma-separated: match first pattern
assert.True(t, wildcardMatchMulti("foo.log", "*.log,*.txt"))
// comma-separated: no match
assert.False(t, wildcardMatchMulti("foo.doc", "*.log,*.txt"))
// single pattern (no comma)
assert.True(t, wildcardMatchMulti("hello.txt", "*.txt"))
assert.False(t, wildcardMatchMulti("hello.txt", "*.go"))
// empty patterns string
assert.False(t, wildcardMatchMulti("anything", ""))
// spaces around comma
assert.True(t, wildcardMatchMulti("test.go", "*.txt , *.go"))
}
// --- walkDir / walkDirs tests ---
func TestWalkDir_Depth0(t *testing.T) {
// depth=0 should list immediate files only, not recurse into subdirs
tmpDir := t.TempDir()
os.WriteFile(filepath.Join(tmpDir, "a.txt"), []byte("a"), 0644)
os.WriteFile(filepath.Join(tmpDir, "b.txt"), []byte("b"), 0644)
os.Mkdir(filepath.Join(tmpDir, "sub"), 0755)
os.WriteFile(filepath.Join(tmpDir, "sub", "c.txt"), []byte("c"), 0644)
files := walkDirs([]string{tmpDir}, 0)
sort.Strings(files)
assert.Len(t, files, 2)
assert.Contains(t, files[0], "a.txt")
assert.Contains(t, files[1], "b.txt")
}
func TestWalkDir_Depth1(t *testing.T) {
tmpDir := t.TempDir()
os.WriteFile(filepath.Join(tmpDir, "a.txt"), []byte("a"), 0644)
os.Mkdir(filepath.Join(tmpDir, "sub"), 0755)
os.WriteFile(filepath.Join(tmpDir, "sub", "b.txt"), []byte("b"), 0644)
os.Mkdir(filepath.Join(tmpDir, "sub", "deep"), 0755)
os.WriteFile(filepath.Join(tmpDir, "sub", "deep", "c.txt"), []byte("c"), 0644)
files := walkDirs([]string{tmpDir}, 1)
sort.Strings(files)
// depth=1: should get a.txt and sub/b.txt but NOT sub/deep/c.txt
assert.Len(t, files, 2)
assert.Contains(t, files[0], "a.txt")
assert.Contains(t, files[1], "b.txt")
}
func TestWalkDirs_FileArg(t *testing.T) {
// a plain file argument should be returned as-is
tmpDir := t.TempDir()
fpath := filepath.Join(tmpDir, "test.txt")
os.WriteFile(fpath, []byte("test"), 0644)
files := walkDirs([]string{fpath}, 0)
assert.Equal(t, []string{fpath}, files)
}
func TestWalkDirs_NonExistent(t *testing.T) {
// non-existent path is returned as-is (the caller will handle the error)
files := walkDirs([]string{"/nonexistent/path/xyz"}, 0)
assert.Equal(t, []string{"/nonexistent/path/xyz"}, files)
}
func TestWalkDirs_Empty(t *testing.T) {
files := walkDirs(nil, 5)
assert.Nil(t, files)
}
// --- filterFiles tests ---
func TestFilterFiles(t *testing.T) {
pg := &pgcacher{
files: []string{"/a.txt", "/b.log", "/c.txt", "/a.txt"},
option: &option{},
}
pg.filterFiles()
// should deduplicate
assert.Len(t, pg.files, 3)
}
func TestFilterFiles_Exclude(t *testing.T) {
pg := &pgcacher{
files: []string{"/a.txt", "/b.log", "/c.txt"},
option: &option{excludeFiles: "*.log"},
}
pg.filterFiles()
assert.Len(t, pg.files, 2)
for _, f := range pg.files {
assert.NotContains(t, f, ".log")
}
}
func TestFilterFiles_Include(t *testing.T) {
pg := &pgcacher{
files: []string{"/a.txt", "/b.log", "/c.txt"},
option: &option{includeFiles: "*.txt"},
}
pg.filterFiles()
assert.Len(t, pg.files, 2)
for _, f := range pg.files {
assert.Contains(t, f, ".txt")
}
}
// --- ignoreFile tests ---
func TestIgnoreFile(t *testing.T) {
// no filters: nothing ignored
pg := &pgcacher{option: &option{}}
assert.False(t, pg.ignoreFile("/some/file.txt"))
// exclude matches
pg = &pgcacher{option: &option{excludeFiles: "*.log,*.tmp"}}
assert.True(t, pg.ignoreFile("/some/file.log"))
assert.True(t, pg.ignoreFile("/some/file.tmp"))
assert.False(t, pg.ignoreFile("/some/file.txt"))
// include: only *.go files pass
pg = &pgcacher{option: &option{includeFiles: "*.go"}}
assert.False(t, pg.ignoreFile("/main.go"))
assert.True(t, pg.ignoreFile("/readme.md"))
}