1package ignore
2
3import "testing"
4
5func TestParseAndMatch(t *testing.T) {
6 g := Parse(lines("# comment", "", ".terraform/", ".terragrunt-cache/", "node_modules/", "*.log", "/build", "!keep.log"))
7
8 cases := []struct {
9 rel string
10 isDir bool
11 want bool
12 }{
13 {".terraform", true, true},
14 // A dirOnly rule matches the directory itself; the walk prunes
15 // descent on that match, so children are never tested. A file
16 // path under it is therefore not matched directly by the rule.
17 {".terraform/providers/x", false, false},
18 {".terragrunt-cache", true, true},
19 {"modules/.terragrunt-cache", true, true},
20 {"node_modules", true, true},
21 {"src/node_modules/pkg", true, true},
22 {"debug.log", false, true},
23 {"keep.log", false, false}, // re-included by negation
24 {"build", true, true}, // anchored
25 {"sub/build", true, false}, // anchored: only at root
26 {"main.tf", false, false},
27 {"src/app.go", false, false},
28 }
29 for _, c := range cases {
30 if got := g.Match(c.rel, c.isDir); got != c.want {
31 t.Errorf("Match(%q, dir=%v) = %v, want %v", c.rel, c.isDir, got, c.want)
32 }
33 }
34}
35
36func TestEmptyIgnoresNothing(t *testing.T) {
37 g := Parse("")
38 if g.Match("anything", false) || g.Match("dir", true) {
39 t.Fatal("empty matcher should ignore nothing")
40 }
41}
42
43// lines joins fixture lines with newlines for readable .gitignore
44// fixtures.
45func lines(ls ...string) string {
46 out := ""
47 for i, l := range ls {
48 if i > 0 {
49 out += "\n"
50 }
51 out += l
52 }
53 return out
54}