aboutsummaryrefslogtreecommitdiff
path: root/internal/ignore
diff options
context:
space:
mode:
Diffstat (limited to 'internal/ignore')
-rw-r--r--internal/ignore/fuzz_test.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/ignore/fuzz_test.go b/internal/ignore/fuzz_test.go
new file mode 100644
index 0000000..0e9f0a3
--- /dev/null
+++ b/internal/ignore/fuzz_test.go
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package ignore
+
+import (
+ "strings"
+ "testing"
+)
+
+// FuzzMatch: any pattern either fails to compile or matches any walked
+// path without panicking, and gives the same answer twice. The oracle test
+// checks answers against git; this checks everything else.
+func FuzzMatch(f *testing.F) {
+ for _, p := range []string{"*.log", "/a.txt", "build/", "**/readme.md", "[!a]*.txt", "[[:digit:]]*", `\`, "[", "a[/]b", "!", "**/**/**", "*a*a*a*b"} {
+ f.Add(p, "dir/sub/a.txt")
+ }
+ f.Fuzz(func(t *testing.T, pattern, rel string) {
+ m, err := New([]string{pattern})
+ if err != nil {
+ return
+ }
+ rel = walkedRel(rel)
+ if rel == "" {
+ return
+ }
+ if m.Match(rel, false) != m.Match(rel, false) {
+ t.Fatalf("Match(%q) under %q is not deterministic", rel, pattern)
+ }
+ m.Match(rel, true)
+ })
+}
+
+// walkedRel turns any string into a path of the shape Match accepts, as a
+// directory walk produces it: slash-separated, with no empty, "." or ".."
+// components.
+func walkedRel(s string) string {
+ var parts []string
+ for _, p := range strings.Split(s, "/") {
+ if p != "" && p != "." && p != ".." {
+ parts = append(parts, p)
+ }
+ }
+ return strings.Join(parts, "/")
+}