forked from bmatcuk/doublestar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
243 lines (229 loc) · 8.26 KB
/
utils_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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package doublestar
import (
"errors"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
"time"
)
var filepathGlobTests = []string{
".",
"././.",
"..",
"../.",
".././././",
"../..",
"/",
"./",
"/.",
"/././././",
"nopermission/.",
}
func TestSpecialFilepathGlobCases(t *testing.T) {
for idx, pattern := range filepathGlobTests {
testSpecialFilepathGlobCasesWith(t, idx, pattern)
}
}
func testSpecialFilepathGlobCasesWith(t *testing.T, idx int, pattern string) {
defer func() {
if r := recover(); r != nil {
t.Errorf("#%v. FilepathGlob(%#q) panicked with: %#v", idx, pattern, r)
}
}()
pattern = filepath.FromSlash(pattern)
matches, err := FilepathGlob(pattern)
results, stdErr := filepath.Glob(pattern)
// doublestar.FilepathGlob Cleans the path
for idx, result := range results {
results[idx] = filepath.Clean(result)
}
if !compareSlices(matches, results) || !compareErrors(err, stdErr) {
t.Errorf("#%v. FilepathGlob(%#q) != filepath.Glob(%#q). Got %#v, %v want %#v, %v", idx, pattern, pattern, matches, err, results, stdErr)
}
}
func TestFilepathGlobWithGlobOptions(t *testing.T) {
// creating temp file to test on
baseDir, err := createTestingDirs()
if err != nil {
t.Error(err)
}
defer func(path string) { _ = os.RemoveAll(path) }(baseDir)
type args struct {
pattern string
opts []GlobOption
}
tests := []struct {
name string
args args
wantMatches []string
wantErr bool
}{
{
name: "no wild card file",
args: args{
pattern: baseDir + "/old1_level1/old1_level2/file2.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(),
},
},
wantMatches: []string{
filepath.Join(baseDir, "old1_level1/old1_level2/file2.log"),
},
wantErr: false,
},
{
name: "no wild card old file",
args: args{
pattern: baseDir + "/old1_level1/old1_level2/file2.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(), WithMaxAge(time.Hour * 12),
},
},
wantMatches: nil,
wantErr: false,
},
{
name: "{x,y} file",
args: args{
pattern: baseDir + "/old1_level1/old1_level2/file{1,3}.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(),
},
},
wantMatches: []string{
filepath.Join(baseDir, "old1_level1/old1_level2/file1.log"),
filepath.Join(baseDir, "old1_level1/old1_level2/file3.log"),
},
wantErr: false,
},
{
name: "{x,y} old file",
args: args{
pattern: baseDir + "/*/*/file{1,3}.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(), WithMaxAge(time.Hour * 12),
},
},
wantMatches: []string{
filepath.Join(baseDir, "new1_level1/new1_level2/file1.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file3.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file1.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file3.log"),
},
wantErr: false,
},
{
name: "fetch all data",
args: args{
pattern: baseDir + "/*/*/*.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(), // WithMaxAge(maxAge),
},
},
wantMatches: []string{
filepath.Join(baseDir, "new1_level1/new1_level2/file1.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file2.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file3.log"),
filepath.Join(baseDir, "old1_level1/old1_level2/file1.log"),
filepath.Join(baseDir, "old1_level1/old1_level2/file2.log"),
filepath.Join(baseDir, "old1_level1/old1_level2/file3.log"),
filepath.Join(baseDir, "old1_level1/old1_level2/file4.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file1.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file2.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file3.log"),
},
wantErr: false,
},
{
name: "ignore old data",
args: args{
pattern: baseDir + "/*/*/*.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(), WithMaxAge(time.Hour * 12),
},
},
wantMatches: []string{
filepath.Join(baseDir, "new1_level1/new1_level2/file1.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file2.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file3.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file1.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file2.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file3.log"),
},
wantErr: false,
},
{
name: "new data in old & new folders",
args: args{
pattern: baseDir + "/**/*/*.log",
opts: []GlobOption{
WithFilesOnly(), WithFailOnIOErrors(), WithMaxAge(time.Hour * 12),
},
},
wantMatches: []string{
filepath.Join(baseDir, "new1_level1/new1_level2/file1.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file2.log"),
filepath.Join(baseDir, "new1_level1/new1_level2/file3.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file1.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file2.log"),
filepath.Join(baseDir, "old2_level1/old2_level2/file3.log"),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotMatches, err := FilepathGlob(tt.args.pattern, tt.args.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("FilepathGlob() error = %v, wantErr %v", err, tt.wantErr)
return
}
sort.Slice(gotMatches, func(i, j int) bool { return gotMatches[i] < gotMatches[j] })
sort.Slice(tt.wantMatches, func(i, j int) bool { return tt.wantMatches[i] < tt.wantMatches[j] })
if !reflect.DeepEqual(gotMatches, tt.wantMatches) {
t.Errorf("FilepathGlob() gotMatches = %v, want %v", gotMatches, tt.wantMatches)
}
})
}
}
func createTestingDirs() (string, error) {
var err error
baseDir := "./unit_test_files"
err = os.RemoveAll(baseDir)
if err != nil {
return baseDir, err
}
// old files in old directories
err = errors.Join(err, os.MkdirAll(filepath.Join(baseDir, "old1_level1/old1_level2"), os.ModePerm))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old1_level1/old1_level2/file1.log")))
err = errors.Join(err, os.Chtimes(filepath.Join(baseDir, "old1_level1/old1_level2/file1.log"), time.Now().Add(-time.Hour*24), time.Now().Add(-time.Hour*24)))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old1_level1/old1_level2/file2.log")))
err = errors.Join(err, os.Chtimes(filepath.Join(baseDir, "old1_level1/old1_level2/file2.log"), time.Now().Add(-time.Hour*24), time.Now().Add(-time.Hour*24)))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old1_level1/old1_level2/file3.log")))
err = errors.Join(err, os.Chtimes(filepath.Join(baseDir, "old1_level1/old1_level2/file3.log"), time.Now().Add(-time.Hour*24), time.Now().Add(-time.Hour*24)))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old1_level1/old1_level2/file4.log")))
err = errors.Join(err, os.Chtimes(filepath.Join(baseDir, "old1_level1/old1_level2/file4.log"), time.Now().Add(-time.Hour*24), time.Now().Add(-time.Hour*24)))
// new files in old directories
err = errors.Join(err, os.MkdirAll(filepath.Join(baseDir, "old2_level1/old2_level2"), os.ModePerm))
err = errors.Join(err, os.Chtimes(filepath.Join(baseDir, "old2_level1"), time.Now().Add(-time.Hour*24), time.Now().Add(-time.Hour*24)))
err = errors.Join(err, os.Chtimes(filepath.Join(baseDir, "old2_level1/old2_level2"), time.Now().Add(-time.Hour*24), time.Now().Add(-time.Hour*24)))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old2_level1/old2_level2/file1.log")))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old2_level1/old2_level2/file2.log")))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "old2_level1/old2_level2/file3.log")))
// new files in new directories
err = errors.Join(err, os.MkdirAll(filepath.Join(baseDir, "new1_level1/new1_level2"), os.ModePerm))
err = errors.Join(err, os.MkdirAll(filepath.Join(baseDir, "new2_level1/new2_level2"), os.ModePerm))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "new1_level1/new1_level2/file1.log")))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "new1_level1/new1_level2/file2.log")))
err = errors.Join(err, touchFile(filepath.Join(baseDir, "new1_level1/new1_level2/file3.log")))
return baseDir, err
}
func touchFile(name string) error {
file, err := os.OpenFile(name, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
return file.Close()
}