aboutsummaryrefslogtreecommitdiffstats
path: root/gitignore_test.go
blob: db9f25b47ba161ef66a997ba31ed4c6199fb5b0f (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main

import (
	"testing"

	ignore "github.com/sabhiram/go-gitignore"
)

func TestGitignore_SimplePatterns(t *testing.T) {
	tests := []struct {
		name     string
		patterns []string
		path     string
		expected bool
	}{
		{"exact file", []string{"foo.txt"}, "foo.txt", true},
		{"exact file no match", []string{"foo.txt"}, "bar.txt", false},
		{"wildcard extension", []string{"*.log"}, "debug.log", true},
		{"wildcard no match", []string{"*.log"}, "debug.txt", false},
		{"directory pattern with content", []string{"build/"}, "build/output.o", true},
		{"leading slash anchored", []string{"/foo"}, "foo", true},
		{"leading slash anchored no match", []string{"/foo"}, "bar/foo", false},
		{"comment line ignored", []string{"# this is a comment"}, "foo", false},
		{"empty line ignored", []string{""}, "foo", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gi := ignore.CompileIgnoreLines(tt.patterns...)
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}
}

func TestGitignore_Negation(t *testing.T) {
	gi := ignore.CompileIgnoreLines("*.log", "!important.log")

	tests := []struct {
		path     string
		expected bool
	}{
		{"debug.log", true},
		{"important.log", false},
		{"test.log", true},
		{"readme.txt", false},
	}

	for _, tt := range tests {
		t.Run(tt.path, func(t *testing.T) {
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}
}

func TestGitignore_DoubleStar(t *testing.T) {
	tests := []struct {
		name     string
		patterns []string
		path     string
		expected bool
	}{
		{"double star prefix", []string{"**/temp"}, "temp", true},
		{"double star prefix nested", []string{"**/temp"}, "foo/temp", true},
		{"double star prefix deep nested", []string{"**/temp"}, "foo/bar/baz/temp", true},
		{"double star suffix", []string{"temp/**"}, "temp/file.txt", true},
		{"double star suffix deep", []string{"temp/**"}, "temp/sub/file.txt", true},
		{"double star middle", []string{"foo/**/bar"}, "foo/bar", true},
		{"double star middle nested", []string{"foo/**/bar"}, "foo/x/y/bar", true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gi := ignore.CompileIgnoreLines(tt.patterns...)
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}
}

func TestGitignore_QuestionMark(t *testing.T) {
	// NOTE: go-gitignore does not support ? wildcard
	// This test documents the limitation
	gi := ignore.CompileIgnoreLines("?.txt")

	result := gi.MatchesPath("a.txt")
	if result {
		t.Log("go-gitignore now supports ? wildcard - update docs")
	} else {
		t.Log("go-gitignore does NOT support ? wildcard - known limitation")
	}
}

func TestGitignore_BracketRange(t *testing.T) {
	gi := ignore.CompileIgnoreLines("[abc].txt")

	tests := []struct {
		path     string
		expected bool
	}{
		{"a.txt", true},
		{"b.txt", true},
		{"c.txt", true},
		{"d.txt", false},
	}

	for _, tt := range tests {
		t.Run(tt.path, func(t *testing.T) {
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}
}

func TestGitignore_NestedGitignore(t *testing.T) {
	rules := map[string][]string{
		".gitignore":             {"*.log"},
		"subdir/.gitignore":      {"*.tmp"},
		"subdir/deep/.gitignore": {"*.bak"},
	}

	gi := ignore.CompileIgnoreLines(rules[".gitignore"]...)

	tests := []struct {
		name     string
		path     string
		expected bool
	}{
		{"root log", "debug.log", true},
		{"root txt", "readme.txt", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}

	// Nested gitignore needs separate handling per directory
	subGi := ignore.CompileIgnoreLines(rules["subdir/.gitignore"]...)
	if subGi.MatchesPath("file.tmp") != true {
		t.Error("subdir gitignore should match *.tmp")
	}
}

func TestGitignore_CombinedPatterns(t *testing.T) {
	gi := ignore.CompileIgnoreLines(
		"*.o",
		"*.a",
		"build/",
		"!lib/special.o",
	)

	tests := []struct {
		path     string
		expected bool
	}{
		{"foo.o", true},
		{"lib.a", true},
		{"build/output.o", true},
		{"lib/special.o", false},
		{"src/main.c", false},
	}

	for _, tt := range tests {
		t.Run(tt.path, func(t *testing.T) {
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}
}

func TestGitignore_HashInFilename(t *testing.T) {
	gi := ignore.CompileIgnoreLines("*.txt")

	tests := []struct {
		path     string
		expected bool
	}{
		{"file#1.txt", true},
		{"file#2.txt", true},
		{"readme.md", false},
	}

	for _, tt := range tests {
		t.Run(tt.path, func(t *testing.T) {
			result := gi.MatchesPath(tt.path)
			if result != tt.expected {
				t.Errorf("MatchesPath(%q) = %v, want %v", tt.path, result, tt.expected)
			}
		})
	}
}

func TestGitignore_TrailingSpace(t *testing.T) {
	// NOTE: go-gitignore does not handle trailing spaces in patterns
	// This test documents the limitation
	gi := ignore.CompileIgnoreLines("foo.txt   ")

	result := gi.MatchesPath("foo.txt")
	if result {
		t.Log("go-gitignore handles trailing spaces correctly")
	} else {
		t.Log("go-gitignore does NOT handle trailing spaces - known limitation")
	}
}