File: ignore_test.go

package info (click to toggle)
golang-github-sabhiram-go-gitignore 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 104 kB
  • sloc: makefile: 2
file content (324 lines) | stat: -rw-r--r-- 13,099 bytes parent folder | download
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Implement tests for the `ignore` library
package ignore

import (
	"os"

	"io/ioutil"
	"path/filepath"

	"fmt"
	"testing"

	"runtime"

	"github.com/stretchr/testify/assert"
)

const (
	TEST_DIR = "test_fixtures"
)

////////////////////////////////////////////////////////////

// Helper function to setup a test fixture dir and write to
// it a file with the name "fname" and content "content"
func writeFileToTestDir(fname, content string) {
	testDirPath := "." + string(filepath.Separator) + TEST_DIR
	testFilePath := testDirPath + string(filepath.Separator) + fname
	_ = os.MkdirAll(testDirPath, 0755)
	_ = ioutil.WriteFile(testFilePath, []byte(content), os.ModePerm)
}

func cleanupTestDir() {
	_ = os.RemoveAll(fmt.Sprintf(".%s%s", string(filepath.Separator), TEST_DIR))
}

////////////////////////////////////////////////////////////

// Validate "CompileIgnoreLines()"
func TestCompileIgnoreLines(test *testing.T) {
	lines := []string{"abc/def", "a/b/c", "b"}
	object, error := CompileIgnoreLines(lines...)
	assert.Nil(test, error, "error from CompileIgnoreLines should be nil")

	// MatchesPath
	// Paths which are targeted by the above "lines"
	assert.Equal(test, true, object.MatchesPath("abc/def/child"), "abc/def/child should match")
	assert.Equal(test, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match")

	// Paths which are not targeted by the above "lines"
	assert.Equal(test, false, object.MatchesPath("abc"), "abc should not match")
	assert.Equal(test, false, object.MatchesPath("def"), "def should not match")
	assert.Equal(test, false, object.MatchesPath("bd"), "bd should not match")

	object, error = CompileIgnoreLines("abc/def", "a/b/c", "b")
	assert.Nil(test, error, "error from CompileIgnoreLines should be nil")

	// Paths which are targeted by the above "lines"
	assert.Equal(test, true, object.MatchesPath("abc/def/child"), "abc/def/child should match")
	assert.Equal(test, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match")

	// Paths which are not targeted by the above "lines"
	assert.Equal(test, false, object.MatchesPath("abc"), "abc should not match")
	assert.Equal(test, false, object.MatchesPath("def"), "def should not match")
	assert.Equal(test, false, object.MatchesPath("bd"), "bd should not match")
}

// Validate the invalid files
func TestCompileIgnoreFile_InvalidFile(test *testing.T) {
	object, error := CompileIgnoreFile("./test_fixtures/invalid.file")
	assert.Nil(test, object, "object should be nil")
	assert.NotNil(test, error, "error should be unknown file / dir")
}

// Validate the an empty files
func TestCompileIgnoreLines_EmptyFile(test *testing.T) {
	writeFileToTestDir("test.gitignore", ``)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, false, object.MatchesPath("a"), "should not match any path")
	assert.Equal(test, false, object.MatchesPath("a/b"), "should not match any path")
	assert.Equal(test, false, object.MatchesPath(".foobar"), "should not match any path")
}

// Validate the correct handling of the negation operator "!"
func TestCompileIgnoreLines_HandleIncludePattern(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, true, object.MatchesPath("a"), "a should match")
	assert.Equal(test, true, object.MatchesPath("foo/baz"), "foo/baz should match")
	assert.Equal(test, false, object.MatchesPath("foo"), "foo should not match")
	assert.Equal(test, false, object.MatchesPath("/foo/bar"), "/foo/bar should not match")
}

// Validate the correct handling of comments and empty lines
func TestCompileIgnoreLines_HandleSpaces(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
#
# A comment

# Another comment


    # Invalid Comment

abc/def
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, 2, len(object.patterns), "should have two regex pattern")
	assert.Equal(test, false, object.MatchesPath("abc/abc"), "/abc/abc should not match")
	assert.Equal(test, true, object.MatchesPath("abc/def"), "/abc/def should match")
}

// Validate the correct handling of leading / chars
func TestCompileIgnoreLines_HandleLeadingSlash(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
/a/b/c
d/e/f
/g
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, 3, len(object.patterns), "should have 3 regex patterns")
	assert.Equal(test, true, object.MatchesPath("a/b/c"), "a/b/c should match")
	assert.Equal(test, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match")
	assert.Equal(test, true, object.MatchesPath("d/e/f"), "d/e/f should match")
	assert.Equal(test, true, object.MatchesPath("g"), "g should match")
}

// Validate the correct handling of files starting with # or !
func TestCompileIgnoreLines_HandleLeadingSpecialChars(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
# Comment
\#file.txt
\!file.txt
file.txt
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, true, object.MatchesPath("#file.txt"), "#file.txt should match")
	assert.Equal(test, true, object.MatchesPath("!file.txt"), "!file.txt should match")
	assert.Equal(test, true, object.MatchesPath("a/!file.txt"), "a/!file.txt should match")
	assert.Equal(test, true, object.MatchesPath("file.txt"), "file.txt should match")
	assert.Equal(test, true, object.MatchesPath("a/file.txt"), "a/file.txt should match")
	assert.Equal(test, false, object.MatchesPath("file2.txt"), "file2.txt should not match")

}

// Validate the correct handling matching files only within a given folder
func TestCompileIgnoreLines_HandleAllFilesInDir(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
Documentation/*.html
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, true, object.MatchesPath("Documentation/git.html"), "Documentation/git.html should match")
	assert.Equal(test, false, object.MatchesPath("Documentation/ppc/ppc.html"), "Documentation/ppc/ppc.html should not match")
	assert.Equal(test, false, object.MatchesPath("tools/perf/Documentation/perf.html"), "tools/perf/Documentation/perf.html should not match")
}

// Validate the correct handling of "**"
func TestCompileIgnoreLines_HandleDoubleStar(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
**/foo
bar
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, true, object.MatchesPath("foo"), "foo should match")
	assert.Equal(test, true, object.MatchesPath("baz/foo"), "baz/foo should match")
	assert.Equal(test, true, object.MatchesPath("bar"), "bar should match")
	assert.Equal(test, true, object.MatchesPath("baz/bar"), "baz/bar should match")
}

// Validate the correct handling of leading slash
func TestCompileIgnoreLines_HandleLeadingSlashPath(test *testing.T) {
	writeFileToTestDir("test.gitignore", `
/*.c
`)
	defer cleanupTestDir()

	object, error := CompileIgnoreFile("./test_fixtures/test.gitignore")
	assert.Nil(test, error, "error should be nil")
	assert.NotNil(test, object, "object should not be nil")

	assert.Equal(test, true, object.MatchesPath("hello.c"), "hello.c should match")
	assert.Equal(test, false, object.MatchesPath("foo/hello.c"), "foo/hello.c should not match")
}

func ExampleCompileIgnoreLines() {
	ignoreObject, error := CompileIgnoreLines([]string{"node_modules", "*.out", "foo/*.c"}...)
	if error != nil {
		panic("Error when compiling ignore lines: " + error.Error())
	}

	// You can test the ignoreObject against various paths using the
	// "MatchesPath()" interface method. This pretty much is up to
	// the users interpretation. In the case of a ".gitignore" file,
	// a "match" would indicate that a given path would be ignored.
	fmt.Println(ignoreObject.MatchesPath("node_modules/test/foo.js"))
	fmt.Println(ignoreObject.MatchesPath("node_modules2/test.out"))
	fmt.Println(ignoreObject.MatchesPath("test/foo.js"))

	// Output:
	// true
	// true
	// false
}

func TestCompileIgnoreLines_CheckNestedDotFiles(test *testing.T) {
	lines := []string{
		"**/external/**/*.md",
		"**/external/**/*.json",
		"**/external/**/*.gzip",
		"**/external/**/.*ignore",

		"**/external/foobar/*.css",
		"**/external/barfoo/less",
		"**/external/barfoo/scss",
	}
	object, error := CompileIgnoreLines(lines...)
	assert.Nil(test, error, "error from CompileIgnoreLines should be nil")
	assert.NotNil(test, object, "returned object should not be nil")

	assert.Equal(test, true, object.MatchesPath("external/foobar/angular.foo.css"), "external/foobar/angular.foo.css")
	assert.Equal(test, true, object.MatchesPath("external/barfoo/.gitignore"), "external/barfoo/.gitignore")
	assert.Equal(test, true, object.MatchesPath("external/barfoo/.bower.json"), "external/barfoo/.bower.json")
}

func TestCompileIgnoreLines_CarriageReturn(test *testing.T) {
	lines := []string{"abc/def\r", "a/b/c\r", "b\r"}
	object, error := CompileIgnoreLines(lines...)
	assert.Nil(test, error, "error from CompileIgnoreLines should be nil")

	assert.Equal(test, true, object.MatchesPath("abc/def/child"), "abc/def/child should match")
	assert.Equal(test, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match")

	assert.Equal(test, false, object.MatchesPath("abc"), "abc should not match")
	assert.Equal(test, false, object.MatchesPath("def"), "def should not match")
	assert.Equal(test, false, object.MatchesPath("bd"), "bd should not match")
}

func TestCompileIgnoreLines_WindowsPath(test *testing.T) {
	if runtime.GOOS != "windows" {
		return
	}
	lines := []string{"abc/def", "a/b/c", "b"}
	object, error := CompileIgnoreLines(lines...)
	assert.Nil(test, error, "error from CompileIgnoreLines should be nil")

	assert.Equal(test, true, object.MatchesPath("abc\\def\\child"), "abc\\def\\child should match")
	assert.Equal(test, true, object.MatchesPath("a\\b\\c\\d"), "a\\b\\c\\d should match")
}

func TestWildCardFiles(test *testing.T) {
	gitIgnore := []string{"*.swp", "/foo/*.wat", "bar/*.txt"}
	object, err := CompileIgnoreLines(gitIgnore...)
	assert.Nil(test, err, "error from CompileIgnoreLines should be nil")

	// Paths which are targeted by the above "lines"
	assert.Equal(test, true, object.MatchesPath("yo.swp"), "should ignore all swp files")
	assert.Equal(test, true, object.MatchesPath("something/else/but/it/hasyo.swp"), "should ignore all swp files in other directories")

	assert.Equal(test, true, object.MatchesPath("foo/bar.wat"), "should ignore all wat files in foo - nonpreceding /")
	assert.Equal(test, true, object.MatchesPath("/foo/something.wat"), "should ignore all wat files in foo - preceding /")

	assert.Equal(test, true, object.MatchesPath("bar/something.txt"), "should ignore all txt files in bar - nonpreceding /")
	assert.Equal(test, true, object.MatchesPath("/bar/somethingelse.txt"), "should ignore all txt files in bar - preceding /")

	// Paths which are not targeted by the above "lines"
	assert.Equal(test, false, object.MatchesPath("something/not/infoo/wat.wat"), "wat files should only be ignored in foo")
	assert.Equal(test, false, object.MatchesPath("something/not/infoo/wat.txt"), "txt files should only be ignored in bar")
}

func TestPrecedingSlash(test *testing.T) {
	gitIgnore := []string{"/foo", "bar/"}
	object, err := CompileIgnoreLines(gitIgnore...)
	assert.Nil(test, err, "error from CompileIgnoreLines should be nil")

	assert.Equal(test, true, object.MatchesPath("foo/bar.wat"), "should ignore all files in foo - nonpreceding /")
	assert.Equal(test, true, object.MatchesPath("/foo/something.txt"), "should ignore all files in foo - preceding /")

	assert.Equal(test, true, object.MatchesPath("bar/something.txt"), "should ignore all files in bar - nonpreceding /")
	assert.Equal(test, true, object.MatchesPath("/bar/somethingelse.go"), "should ignore all files in bar - preceding /")
	assert.Equal(test, true, object.MatchesPath("/boo/something/bar/boo.txt"), "should block all files if bar is a sub directory")

	assert.Equal(test, false, object.MatchesPath("something/foo/something.txt"), "should only ignore top level foo directories- not nested")
}