File: glob.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (77 lines) | stat: -rw-r--r-- 1,560 bytes parent folder | download
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
package testutils

import (
	"fmt"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/sys/cpu"
)

// 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
}

// NativeFile substitutes %s with an abbreviation of the host endianness.
func NativeFile(tb testing.TB, path string) string {
	tb.Helper()

	if !strings.Contains(path, "%s") {
		tb.Fatalf("File %q doesn't contain %%s", path)
	}

	if cpu.IsBigEndian {
		return fmt.Sprintf(path, "eb")
	}

	return fmt.Sprintf(path, "el")
}