-
Notifications
You must be signed in to change notification settings - Fork 1
/
glob.go
58 lines (49 loc) · 1.17 KB
/
glob.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
package testutils
import (
"path/filepath"
"testing"
)
// Files calls fn for each given file.
//
// The function errors out if the pattern matches no files.
func Files(t *testing.T, files []string, fn func(*testing.T, string)) {
t.Helper()
if len(files) == 0 {
t.Fatalf("No files given")
}
for _, f := range files {
file := f // force copy
name := filepath.Base(file)
t.Run(name, func(t *testing.T) {
fn(t, file)
})
}
}
// Glob finds files matching a pattern.
//
// The pattern should may include full path. Excludes use the same syntax as
// pattern, but are only applied to the basename instead of the full path.
func Glob(tb testing.TB, pattern string, excludes ...string) []string {
tb.Helper()
files, err := filepath.Glob(pattern)
if err != nil {
tb.Fatal("Can't glob files:", err)
}
if len(excludes) == 0 {
return files
}
var filtered []string
nextFile:
for _, file := range files {
base := filepath.Base(file)
for _, exclude := range excludes {
if matched, err := filepath.Match(exclude, base); err != nil {
tb.Fatal(err)
} else if matched {
continue nextFile
}
}
filtered = append(filtered, file)
}
return filtered
}