v0.3.52 cad135d

gitignore_test.go RawHistoryFolder

1package ignore
2
3import (
4	"os"
5	"path/filepath"
6	"testing"
7)
8
9func TestParseAndMatch(t *testing.T) {
10	g := Parse(lines("# comment", "", ".terraform/", ".terragrunt-cache/", "node_modules/", "*.log", "/build", "!keep.log"))
11
12	cases := []struct {
13		rel   string
14		isDir bool
15		want  bool
16	}{
17		{".terraform", true, true},
18		// A dirOnly rule matches the directory itself; the walk prunes
19		// descent on that match, so children are never tested. A file
20		// path under it is therefore not matched directly by the rule.
21		{".terraform/providers/x", false, false},
22		{".terragrunt-cache", true, true},
23		{"modules/.terragrunt-cache", true, true},
24		{"node_modules", true, true},
25		{"src/node_modules/pkg", true, true},
26		{"debug.log", false, true},
27		{"keep.log", false, false}, // re-included by negation
28		{"build", true, true},      // anchored
29		{"sub/build", true, false}, // anchored: only at root
30		{"main.tf", false, false},
31		{"src/app.go", false, false},
32	}
33	for _, c := range cases {
34		if got := g.Match(c.rel, c.isDir); got != c.want {
35			t.Errorf("Match(%q, dir=%v) = %v, want %v", c.rel, c.isDir, got, c.want)
36		}
37	}
38}
39
40func TestEmptyIgnoresNothing(t *testing.T) {
41	g := Parse("")
42	if g.Match("anything", false) || g.Match("dir", true) {
43		t.Fatal("empty matcher should ignore nothing")
44	}
45}
46
47// TestStackHonorsNestedGitignore pins the recursive-picker bug: a
48// .gitignore living inside a subdirectory (here .opencode/.gitignore
49// ignoring node_modules, exactly the layout that flooded the @-picker)
50// must prune that subdirectory's node_modules even though the root
51// .gitignore says nothing about it.
52func TestStackHonorsNestedGitignore(t *testing.T) {
53	root := t.TempDir()
54	// Root .gitignore: only build/ at root, nothing about node_modules.
55	if err := os.WriteFile(filepath.Join(root, ".gitignore"), []byte("build/\n"), 0o644); err != nil {
56		t.Fatal(err)
57	}
58	opencode := filepath.Join(root, ".opencode")
59	if err := os.MkdirAll(opencode, 0o755); err != nil {
60		t.Fatal(err)
61	}
62	if err := os.WriteFile(filepath.Join(opencode, ".gitignore"), []byte("node_modules\n"), 0o644); err != nil {
63		t.Fatal(err)
64	}
65
66	s := NewStack(root)
67	// Before descending into .opencode the nested rule is not in scope,
68	// so a same-named path elsewhere stays visible.
69	if s.Match("node_modules", true) {
70		t.Fatal("root-level node_modules should not be ignored by an unloaded nested rule")
71	}
72	// Descend into .opencode: push its .gitignore.
73	s.Push(opencode, ".opencode")
74	if !s.Match(".opencode/node_modules", true) {
75		t.Fatal("nested .opencode/.gitignore should ignore .opencode/node_modules")
76	}
77	if !s.Match(".opencode/node_modules/zod/src/v3/tests/pipeline.test.ts", false) {
78		t.Fatal("files under nested-ignored node_modules should be ignored")
79	}
80	// A sibling source file inside .opencode is still visible.
81	if s.Match(".opencode/config.json", false) {
82		t.Fatal(".opencode/config.json should not be ignored")
83	}
84	// Root build/ rule still applies through the stack.
85	if !s.Match("build", true) {
86		t.Fatal("root build/ rule should still apply while nested frame is pushed")
87	}
88	// Pop the nested frame: its rule no longer applies.
89	s.Pop()
90	if s.Match(".opencode/node_modules", true) {
91		t.Fatal("after popping, the nested rule should no longer be in scope")
92	}
93}
94
95// TestStackNestedNegationReincludes verifies a nested !pattern can
96// re-include a path a parent .gitignore excluded.
97func TestStackNestedNegationReincludes(t *testing.T) {
98	root := t.TempDir()
99	if err := os.WriteFile(filepath.Join(root, ".gitignore"), []byte("*.log\n"), 0o644); err != nil {
100		t.Fatal(err)
101	}
102	sub := filepath.Join(root, "sub")
103	if err := os.MkdirAll(sub, 0o755); err != nil {
104		t.Fatal(err)
105	}
106	if err := os.WriteFile(filepath.Join(sub, ".gitignore"), []byte("!keep.log\n"), 0o644); err != nil {
107		t.Fatal(err)
108	}
109	s := NewStack(root)
110	s.Push(sub, "sub")
111	if s.Match("sub/keep.log", false) {
112		t.Fatal("nested !keep.log should re-include a *.log excluded by root")
113	}
114	if !s.Match("sub/other.log", false) {
115		t.Fatal("sub/other.log should still be excluded by root *.log")
116	}
117}
118
119// lines joins fixture lines with newlines for readable .gitignore
120// fixtures.
121func lines(ls ...string) string {
122	out := ""
123	for i, l := range ls {
124		if i > 0 {
125			out += "\n"
126		}
127		out += l
128	}
129	return out
130}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close