Skip to content

Commit ae8ad1d

Browse files
committed
Corrected handling of empty/whitespace-only .gitignore files
1 parent 75ce8f3 commit ae8ad1d

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

defn_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ Documentation/**/p*.pdf
169169
// define the expected number of errors during repository matching
170170
_GITREPOSITORYERRORS = 38
171171
_GITREPOSITORYERRORSFALSE = 1
172+
173+
// define a .gitignore file the contains just whitespace & comments
174+
_GITIGNORE_WHITESPACE = `
175+
# this is an empty .gitignore file
176+
# - the following lines contains just whitespace
177+
178+
179+
`
172180
)
173181

174182
var (

gitignore_test.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,29 @@ func TestNewFromFile(t *testing.T) {
2424
}
2525

2626
// perform the gitignore test
27-
withfile(t, _test)
27+
withfile(t, _test, _GITIGNORE)
2828
} // TestNewFromFile()
2929

30+
func TestNewFromWhitespaceFile(t *testing.T) {
31+
_test := &gitignoretest{}
32+
_test.instance = func(file string) (gitignore.GitIgnore, error) {
33+
return gitignore.NewFromFile(file)
34+
}
35+
36+
// perform the gitignore test
37+
withfile(t, _test, _GITIGNORE_WHITESPACE)
38+
} // TestNewFromWhitespaceFile()
39+
40+
func TestNewFromEmptyFile(t *testing.T) {
41+
_test := &gitignoretest{}
42+
_test.instance = func(file string) (gitignore.GitIgnore, error) {
43+
return gitignore.NewFromFile(file)
44+
}
45+
46+
// perform the gitignore test
47+
withfile(t, _test, "")
48+
} // TestNewFromEmptyFile()
49+
3050
func TestNewWithErrors(t *testing.T) {
3151
_test := &gitignoretest{}
3252
_test.error = func(e gitignore.Error) bool {
@@ -57,10 +77,10 @@ func TestNewWithErrors(t *testing.T) {
5777
}
5878

5979
// perform the gitignore test
60-
withfile(t, _test)
80+
withfile(t, _test, _GITIGNORE)
6181

6282
_test.error = nil
63-
withfile(t, _test)
83+
withfile(t, _test, _GITIGNORE)
6484
} // TestNewWithErrors()
6585

6686
func TestNewWithCache(t *testing.T) {
@@ -92,14 +112,14 @@ func TestNewWithCache(t *testing.T) {
92112
}
93113

94114
// perform the gitignore test
95-
withfile(t, _test)
115+
withfile(t, _test, _GITIGNORE)
96116

97117
// repeat the tests while accumulating errors
98118
_test.error = func(e gitignore.Error) bool {
99119
_test.errors = append(_test.errors, e)
100120
return true
101121
}
102-
withfile(t, _test)
122+
withfile(t, _test, _GITIGNORE)
103123

104124
// create a temporary .gitignore
105125
_file, _err := file(_GITIGNORE)
@@ -185,9 +205,9 @@ func TestNew(t *testing.T) {
185205
}
186206
} // TestNew()
187207

188-
func withfile(t *testing.T, test *gitignoretest) {
208+
func withfile(t *testing.T, test *gitignoretest, content string) {
189209
// create a temporary .gitignore
190-
_file, _err := file(_GITIGNORE)
210+
_file, _err := file(content)
191211
if _err != nil {
192212
t.Fatalf("unable to create temporary .gitignore: %s", _err.Error())
193213
}
@@ -241,7 +261,7 @@ func withfile(t *testing.T, test *gitignoretest) {
241261
}
242262
}
243263

244-
// test NewFromFile() behaves as expected if the .gtignore file does
264+
// test NewFromFile() behaves as expected if the .gitgnore file does
245265
// not exist
246266
_err = os.Remove(_file.Name())
247267
if _err != nil {
@@ -275,7 +295,7 @@ func withfile(t *testing.T, test *gitignoretest) {
275295

276296
// test NewFromFile() behaves as expected if absolute path of the
277297
// .gitignore cannot be determined
278-
_map := map[string]string{gitignore.File: _GITIGNORE}
298+
_map := map[string]string{gitignore.File: content}
279299
_dir, _err = dir(_map)
280300
if _err != nil {
281301
t.Fatalf("unable to create temporary directory: %s", _err.Error())

pattern.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ type any struct {
5353
// negated, anchored to the start of the path (relative to the base directory
5454
// of tie containing .gitignore), or match directories only.
5555
func NewPattern(tokens []*Token) Pattern {
56+
// if we have no tokens there is no pattern
57+
if len(tokens) == 0 {
58+
return nil
59+
}
60+
5661
// extract the pattern position from first token
5762
_position := tokens[0].Position
5863
_string := tokenset(tokens).String()

0 commit comments

Comments
 (0)