diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-12 01:22:12 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-12 01:22:12 +0200 |
| commit | 3b36a48b7ce5a53a9366f3b31f94311f178e2553 (patch) | |
| tree | ecbb277ff916b719f2ee45fba017792b85d5faf9 /internal/ignore/ignore_test.go | |
| parent | 42b02c47be9b285099203e44a2570636d4ca6f03 (diff) | |
| download | krino-3b36a48b7ce5a53a9366f3b31f94311f178e2553.tar.gz krino-3b36a48b7ce5a53a9366f3b31f94311f178e2553.zip | |
krino: matching — scan, ignore, conditions, extraction, duplicates, explain, dry run
Diffstat (limited to 'internal/ignore/ignore_test.go')
| -rw-r--r-- | internal/ignore/ignore_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/ignore/ignore_test.go b/internal/ignore/ignore_test.go new file mode 100644 index 0000000..04b60c4 --- /dev/null +++ b/internal/ignore/ignore_test.go @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package ignore + +import "testing" + +func TestMatch(t *testing.T) { + type c struct { + rel string + isDir bool + want bool + } + tests := []struct { + patterns []string + cases []c + }{ + {[]string{"*.log"}, []c{{"a.log", false, true}, {"dir/b.log", false, true}, {"a.txt", false, false}}}, + {[]string{"/a.txt"}, []c{{"a.txt", false, true}, {"dir/a.txt", false, false}}}, + {[]string{"build/"}, []c{{"build", true, true}, {"build", false, false}, {"src/build", true, true}, {"build/x.bin", false, true}}}, + {[]string{"doc/*.md"}, []c{{"doc/r.md", false, true}, {"doc/sub/r.md", false, false}, {"x/doc/r.md", false, false}}}, + {[]string{"**/readme.md"}, []c{{"readme.md", false, true}, {"a/b/readme.md", false, true}}}, + {[]string{"deep/**"}, []c{{"deep", true, false}, {"deep/a", true, true}, {"deep/a/b/f.tmp", false, true}}}, + {[]string{"x/**/z.txt"}, []c{{"x/z.txt", false, true}, {"x/y/z.txt", false, true}, {"x/y/w/z.txt", false, true}, {"q/x/z.txt", false, false}}}, + {[]string{"*.part", "!keep.part"}, []c{{"a.part", false, true}, {"keep.part", false, false}}}, + {[]string{"dir/", "!dir/c.txt"}, []c{{"dir/c.txt", false, true}}}, + {[]string{".*"}, []c{{".hidden", false, true}, {"a/.cfg", false, true}, {"visible", false, false}}}, + {[]string{"a?.txt", "[bc].txt"}, []c{{"a1.txt", false, true}, {"a12.txt", false, false}, {"b.txt", false, true}, {"d.txt", false, false}}}, + {[]string{"[!a]*.txt"}, []c{{"b.txt", false, true}, {"a.txt", false, false}}}, + {[]string{"sub"}, []c{{"sub", true, true}, {"x/sub", true, true}, {"x/sub/f", false, true}, {"subx", false, false}}}, + {[]string{"\\#notes", "\\!bang"}, []c{{"#notes", false, true}, {"!bang", false, true}}}, + {[]string{"", "# comment"}, []c{{"# comment", false, false}}}, + {nil, []c{{"anything", false, false}}}, + {[]string{"x[]a]y"}, []c{{"x]y", false, true}, {"xay", false, true}, {"xby", false, false}}}, + {[]string{"a[/]b"}, []c{{"a/b", false, false}}}, + } + for _, tt := range tests { + m, err := New(tt.patterns) + if err != nil { + t.Fatalf("New(%q): %v", tt.patterns, err) + } + for _, cs := range tt.cases { + if got := m.Match(cs.rel, cs.isDir); got != cs.want { + t.Errorf("patterns %q: Match(%q, dir=%v) = %v, want %v", tt.patterns, cs.rel, cs.isDir, got, cs.want) + } + } + } +} + +func TestBadPattern(t *testing.T) { + if _, err := New([]string{"[abc"}); err == nil || err.Error() != `bad ignore pattern "[abc": unterminated [` { + t.Fatalf("got %v", err) + } + // "\]" escapes the only "]" to a literal member, leaving the class + // with no terminator. + if _, err := New([]string{"a[\\]d"}); err == nil || err.Error() != `bad ignore pattern "a[\\]d": unterminated [` { + t.Fatalf("got %v", err) + } +} |
