...
1 package testutils
2
3 import (
4 "path/filepath"
5 "testing"
6 )
7
8
9
10
11 func Files(t *testing.T, files []string, fn func(*testing.T, string)) {
12 t.Helper()
13
14 if len(files) == 0 {
15 t.Fatalf("No files given")
16 }
17
18 for _, f := range files {
19 file := f
20 name := filepath.Base(file)
21 t.Run(name, func(t *testing.T) {
22 fn(t, file)
23 })
24 }
25 }
26
27
28
29
30
31 func Glob(tb testing.TB, pattern string, excludes ...string) []string {
32 tb.Helper()
33
34 files, err := filepath.Glob(pattern)
35 if err != nil {
36 tb.Fatal("Can't glob files:", err)
37 }
38
39 if len(excludes) == 0 {
40 return files
41 }
42
43 var filtered []string
44 nextFile:
45 for _, file := range files {
46 base := filepath.Base(file)
47 for _, exclude := range excludes {
48 if matched, err := filepath.Match(exclude, base); err != nil {
49 tb.Fatal(err)
50 } else if matched {
51 continue nextFile
52 }
53 }
54 filtered = append(filtered, file)
55 }
56
57 return filtered
58 }
59
View as plain text