| 1 |
|
| 2 |
package ignore |
| 3 |
|
| 4 |
import ( |
| 5 |
"os" |
| 6 |
|
| 7 |
"io/ioutil" |
| 8 |
"path/filepath" |
| 9 |
|
| 10 |
"fmt" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"runtime" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
TEST_DIR = "test_fixtures" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func writeFileToTestDir(fname, content string) { |
| 27 |
testDirPath := "." + string(filepath.Separator) + TEST_DIR |
| 28 |
testFilePath := testDirPath + string(filepath.Separator) + fname |
| 29 |
_ = os.MkdirAll(testDirPath, 0755) |
| 30 |
_ = ioutil.WriteFile(testFilePath, []byte(content), os.ModePerm) |
| 31 |
} |
| 32 |
|
| 33 |
func cleanupTestDir() { |
| 34 |
_ = os.RemoveAll(fmt.Sprintf(".%s%s", string(filepath.Separator), TEST_DIR)) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func TestCompileIgnoreLines(t *testing.T) { |
| 41 |
lines := []string{"abc/def", "a/b/c", "b"} |
| 42 |
object := CompileIgnoreLines(lines...) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
assert.Equal(t, true, object.MatchesPath("abc/def/child"), "abc/def/child should match") |
| 47 |
assert.Equal(t, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match") |
| 48 |
|
| 49 |
|
| 50 |
assert.Equal(t, false, object.MatchesPath("abc"), "abc should not match") |
| 51 |
assert.Equal(t, false, object.MatchesPath("def"), "def should not match") |
| 52 |
assert.Equal(t, false, object.MatchesPath("bd"), "bd should not match") |
| 53 |
|
| 54 |
object = CompileIgnoreLines("abc/def", "a/b/c", "b") |
| 55 |
|
| 56 |
|
| 57 |
assert.Equal(t, true, object.MatchesPath("abc/def/child"), "abc/def/child should match") |
| 58 |
assert.Equal(t, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match") |
| 59 |
|
| 60 |
|
| 61 |
assert.Equal(t, false, object.MatchesPath("abc"), "abc should not match") |
| 62 |
assert.Equal(t, false, object.MatchesPath("def"), "def should not match") |
| 63 |
assert.Equal(t, false, object.MatchesPath("bd"), "bd should not match") |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func TestCompileIgnoreFile_InvalidFile(t *testing.T) { |
| 68 |
object, err := CompileIgnoreFile("./test_fixtures/invalid.file") |
| 69 |
assert.Nil(t, object, "object should be nil") |
| 70 |
assert.NotNil(t, err, "err should be unknown file / dir") |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func TestCompileIgnoreLines_EmptyFile(t *testing.T) { |
| 75 |
writeFileToTestDir("test.gitignore", ``) |
| 76 |
defer cleanupTestDir() |
| 77 |
|
| 78 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 79 |
assert.Nil(t, err, "err should be nil") |
| 80 |
assert.NotNil(t, object, "object should not be nil") |
| 81 |
|
| 82 |
assert.Equal(t, false, object.MatchesPath("a"), "should not match any path") |
| 83 |
assert.Equal(t, false, object.MatchesPath("a/b"), "should not match any path") |
| 84 |
assert.Equal(t, false, object.MatchesPath(".foobar"), "should not match any path") |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func TestCompileIgnoreLines_HandleIncludePattern(t *testing.T) { |
| 89 |
writeFileToTestDir("test.gitignore", ` |
| 90 |
# exclude everything except directory foo/bar |
| 91 |
/* |
| 92 |
!/foo |
| 93 |
/foo/* |
| 94 |
!/foo/bar |
| 95 |
`) |
| 96 |
defer cleanupTestDir() |
| 97 |
|
| 98 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 99 |
assert.Nil(t, err, "err should be nil") |
| 100 |
assert.NotNil(t, object, "object should not be nil") |
| 101 |
|
| 102 |
assert.Equal(t, true, object.MatchesPath("a"), "a should match") |
| 103 |
assert.Equal(t, true, object.MatchesPath("foo/baz"), "foo/baz should match") |
| 104 |
assert.Equal(t, false, object.MatchesPath("foo"), "foo should not match") |
| 105 |
assert.Equal(t, false, object.MatchesPath("/foo/bar"), "/foo/bar should not match") |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func TestCompileIgnoreLines_HandleSpaces(t *testing.T) { |
| 110 |
writeFileToTestDir("test.gitignore", ` |
| 111 |
# |
| 112 |
# A comment |
| 113 |
|
| 114 |
# Another comment |
| 115 |
|
| 116 |
|
| 117 |
# Invalid Comment |
| 118 |
|
| 119 |
abc/def |
| 120 |
`) |
| 121 |
defer cleanupTestDir() |
| 122 |
|
| 123 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 124 |
assert.Nil(t, err, "err should be nil") |
| 125 |
assert.NotNil(t, object, "object should not be nil") |
| 126 |
|
| 127 |
assert.Equal(t, 2, len(object.patterns), "should have two regex pattern") |
| 128 |
assert.Equal(t, false, object.MatchesPath("abc/abc"), "/abc/abc should not match") |
| 129 |
assert.Equal(t, true, object.MatchesPath("abc/def"), "/abc/def should match") |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func TestCompileIgnoreLines_HandleLeadingSlash(t *testing.T) { |
| 134 |
writeFileToTestDir("test.gitignore", ` |
| 135 |
/a/b/c |
| 136 |
d/e/f |
| 137 |
/g |
| 138 |
`) |
| 139 |
defer cleanupTestDir() |
| 140 |
|
| 141 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 142 |
assert.Nil(t, err, "err should be nil") |
| 143 |
assert.NotNil(t, object, "object should not be nil") |
| 144 |
|
| 145 |
assert.Equal(t, 3, len(object.patterns), "should have 3 regex patterns") |
| 146 |
assert.Equal(t, true, object.MatchesPath("a/b/c"), "a/b/c should match") |
| 147 |
assert.Equal(t, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match") |
| 148 |
assert.Equal(t, true, object.MatchesPath("d/e/f"), "d/e/f should match") |
| 149 |
assert.Equal(t, true, object.MatchesPath("g"), "g should match") |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
func TestCompileIgnoreLines_HandleLeadingSpecialChars(t *testing.T) { |
| 154 |
writeFileToTestDir("test.gitignore", ` |
| 155 |
# Comment |
| 156 |
\#file.txt |
| 157 |
\!file.txt |
| 158 |
file.txt |
| 159 |
`) |
| 160 |
defer cleanupTestDir() |
| 161 |
|
| 162 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 163 |
assert.Nil(t, err, "err should be nil") |
| 164 |
assert.NotNil(t, object, "object should not be nil") |
| 165 |
|
| 166 |
assert.Equal(t, true, object.MatchesPath("#file.txt"), "#file.txt should match") |
| 167 |
assert.Equal(t, true, object.MatchesPath("!file.txt"), "!file.txt should match") |
| 168 |
assert.Equal(t, true, object.MatchesPath("a/!file.txt"), "a/!file.txt should match") |
| 169 |
assert.Equal(t, true, object.MatchesPath("file.txt"), "file.txt should match") |
| 170 |
assert.Equal(t, true, object.MatchesPath("a/file.txt"), "a/file.txt should match") |
| 171 |
assert.Equal(t, false, object.MatchesPath("file2.txt"), "file2.txt should not match") |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
func TestCompileIgnoreLines_HandleAllFilesInDir(t *testing.T) { |
| 177 |
writeFileToTestDir("test.gitignore", ` |
| 178 |
Documentation/*.html |
| 179 |
`) |
| 180 |
defer cleanupTestDir() |
| 181 |
|
| 182 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 183 |
assert.Nil(t, err, "err should be nil") |
| 184 |
assert.NotNil(t, object, "object should not be nil") |
| 185 |
|
| 186 |
assert.Equal(t, true, object.MatchesPath("Documentation/git.html"), "Documentation/git.html should match") |
| 187 |
assert.Equal(t, false, object.MatchesPath("Documentation/ppc/ppc.html"), "Documentation/ppc/ppc.html should not match") |
| 188 |
assert.Equal(t, false, object.MatchesPath("tools/perf/Documentation/perf.html"), "tools/perf/Documentation/perf.html should not match") |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
func TestCompileIgnoreLines_HandleDoubleStar(t *testing.T) { |
| 193 |
writeFileToTestDir("test.gitignore", ` |
| 194 |
**/foo |
| 195 |
bar |
| 196 |
`) |
| 197 |
defer cleanupTestDir() |
| 198 |
|
| 199 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 200 |
assert.Nil(t, err, "err should be nil") |
| 201 |
assert.NotNil(t, object, "object should not be nil") |
| 202 |
|
| 203 |
assert.Equal(t, true, object.MatchesPath("foo"), "foo should match") |
| 204 |
assert.Equal(t, true, object.MatchesPath("baz/foo"), "baz/foo should match") |
| 205 |
assert.Equal(t, true, object.MatchesPath("bar"), "bar should match") |
| 206 |
assert.Equal(t, true, object.MatchesPath("baz/bar"), "baz/bar should match") |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func TestCompileIgnoreLines_HandleLeadingSlashPath(t *testing.T) { |
| 211 |
writeFileToTestDir("test.gitignore", ` |
| 212 |
/*.c |
| 213 |
`) |
| 214 |
defer cleanupTestDir() |
| 215 |
|
| 216 |
object, err := CompileIgnoreFile("./test_fixtures/test.gitignore") |
| 217 |
assert.Nil(t, err, "err should be nil") |
| 218 |
assert.NotNil(t, object, "object should not be nil") |
| 219 |
|
| 220 |
assert.Equal(t, true, object.MatchesPath("hello.c"), "hello.c should match") |
| 221 |
assert.Equal(t, false, object.MatchesPath("foo/hello.c"), "foo/hello.c should not match") |
| 222 |
} |
| 223 |
|
| 224 |
func TestCompileIgnoreFileAndLines(t *testing.T) { |
| 225 |
writeFileToTestDir("test.gitignore", ` |
| 226 |
/*.c |
| 227 |
`) |
| 228 |
defer cleanupTestDir() |
| 229 |
|
| 230 |
object, err := CompileIgnoreFileAndLines("./test_fixtures/test.gitignore", "**/foo", "bar") |
| 231 |
assert.Nil(t, err, "err should be nil") |
| 232 |
assert.NotNil(t, object, "object should not be nil") |
| 233 |
|
| 234 |
assert.Equal(t, true, object.MatchesPath("hello.c"), "hello.c should match") |
| 235 |
assert.Equal(t, false, object.MatchesPath("baz/hello.c"), "baz/hello.c should not match") |
| 236 |
|
| 237 |
assert.Equal(t, true, object.MatchesPath("foo"), "foo should match") |
| 238 |
assert.Equal(t, true, object.MatchesPath("baz/foo"), "baz/foo should match") |
| 239 |
assert.Equal(t, true, object.MatchesPath("bar"), "bar should match") |
| 240 |
assert.Equal(t, true, object.MatchesPath("baz/bar"), "baz/bar should match") |
| 241 |
} |
| 242 |
|
| 243 |
func TestCompileIgnoreFileAndLines_InvalidFile(t *testing.T) { |
| 244 |
object, err := CompileIgnoreFileAndLines("./test_fixtures/invalid.file") |
| 245 |
assert.Nil(t, object, "object should be nil") |
| 246 |
assert.NotNil(t, err, "err should be unknown file / dir") |
| 247 |
} |
| 248 |
|
| 249 |
func ExampleCompileIgnoreLines() { |
| 250 |
ignoreObject := CompileIgnoreLines([]string{"node_modules", "*.out", "foo/*.c"}...) |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
fmt.Println(ignoreObject.MatchesPath("node_modules/test/foo.js")) |
| 257 |
fmt.Println(ignoreObject.MatchesPath("node_modules2/test.out")) |
| 258 |
fmt.Println(ignoreObject.MatchesPath("test/foo.js")) |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
func TestCompileIgnoreLines_CheckNestedDotFiles(t *testing.T) { |
| 267 |
lines := []string{ |
| 268 |
"**/external/**/*.md", |
| 269 |
"**/external/**/*.json", |
| 270 |
"**/external/**/*.gzip", |
| 271 |
"**/external/**/.*ignore", |
| 272 |
|
| 273 |
"**/external/foobar/*.css", |
| 274 |
"**/external/barfoo/less", |
| 275 |
"**/external/barfoo/scss", |
| 276 |
} |
| 277 |
object := CompileIgnoreLines(lines...) |
| 278 |
assert.NotNil(t, object, "returned object should not be nil") |
| 279 |
|
| 280 |
assert.Equal(t, true, object.MatchesPath("external/foobar/angular.foo.css"), "external/foobar/angular.foo.css") |
| 281 |
assert.Equal(t, true, object.MatchesPath("external/barfoo/.gitignore"), "external/barfoo/.gitignore") |
| 282 |
assert.Equal(t, true, object.MatchesPath("external/barfoo/.bower.json"), "external/barfoo/.bower.json") |
| 283 |
} |
| 284 |
|
| 285 |
func TestCompileIgnoreLines_CarriageReturn(t *testing.T) { |
| 286 |
lines := []string{"abc/def\r", "a/b/c\r", "b\r"} |
| 287 |
object := CompileIgnoreLines(lines...) |
| 288 |
|
| 289 |
assert.Equal(t, true, object.MatchesPath("abc/def/child"), "abc/def/child should match") |
| 290 |
assert.Equal(t, true, object.MatchesPath("a/b/c/d"), "a/b/c/d should match") |
| 291 |
|
| 292 |
assert.Equal(t, false, object.MatchesPath("abc"), "abc should not match") |
| 293 |
assert.Equal(t, false, object.MatchesPath("def"), "def should not match") |
| 294 |
assert.Equal(t, false, object.MatchesPath("bd"), "bd should not match") |
| 295 |
} |
| 296 |
|
| 297 |
func TestCompileIgnoreLines_WindowsPath(t *testing.T) { |
| 298 |
if runtime.GOOS != "windows" { |
| 299 |
return |
| 300 |
} |
| 301 |
lines := []string{"abc/def", "a/b/c", "b"} |
| 302 |
object := CompileIgnoreLines(lines...) |
| 303 |
|
| 304 |
assert.Equal(t, true, object.MatchesPath("abc\\def\\child"), "abc\\def\\child should match") |
| 305 |
assert.Equal(t, true, object.MatchesPath("a\\b\\c\\d"), "a\\b\\c\\d should match") |
| 306 |
} |
| 307 |
|
| 308 |
func TestWildCardFiles(t *testing.T) { |
| 309 |
gitIgnore := []string{"*.swp", "/foo/*.wat", "bar/*.txt"} |
| 310 |
object := CompileIgnoreLines(gitIgnore...) |
| 311 |
|
| 312 |
|
| 313 |
assert.Equal(t, true, object.MatchesPath("yo.swp"), "should ignore all swp files") |
| 314 |
assert.Equal(t, true, object.MatchesPath("something/else/but/it/hasyo.swp"), "should ignore all swp files in other directories") |
| 315 |
|
| 316 |
assert.Equal(t, true, object.MatchesPath("foo/bar.wat"), "should ignore all wat files in foo - nonpreceding /") |
| 317 |
assert.Equal(t, true, object.MatchesPath("/foo/something.wat"), "should ignore all wat files in foo - preceding /") |
| 318 |
|
| 319 |
assert.Equal(t, true, object.MatchesPath("bar/something.txt"), "should ignore all txt files in bar - nonpreceding /") |
| 320 |
assert.Equal(t, true, object.MatchesPath("/bar/somethingelse.txt"), "should ignore all txt files in bar - preceding /") |
| 321 |
|
| 322 |
|
| 323 |
assert.Equal(t, false, object.MatchesPath("something/not/infoo/wat.wat"), "wat files should only be ignored in foo") |
| 324 |
assert.Equal(t, false, object.MatchesPath("something/not/infoo/wat.txt"), "txt files should only be ignored in bar") |
| 325 |
} |
| 326 |
|
| 327 |
func TestPrecedingSlash(t *testing.T) { |
| 328 |
gitIgnore := []string{"/foo", "bar/"} |
| 329 |
object := CompileIgnoreLines(gitIgnore...) |
| 330 |
|
| 331 |
assert.Equal(t, true, object.MatchesPath("foo/bar.wat"), "should ignore all files in foo - nonpreceding /") |
| 332 |
assert.Equal(t, true, object.MatchesPath("/foo/something.txt"), "should ignore all files in foo - preceding /") |
| 333 |
|
| 334 |
assert.Equal(t, true, object.MatchesPath("bar/something.txt"), "should ignore all files in bar - nonpreceding /") |
| 335 |
assert.Equal(t, true, object.MatchesPath("/bar/somethingelse.go"), "should ignore all files in bar - preceding /") |
| 336 |
assert.Equal(t, true, object.MatchesPath("/boo/something/bar/boo.txt"), "should block all files if bar is a sub directory") |
| 337 |
|
| 338 |
assert.Equal(t, false, object.MatchesPath("something/foo/something.txt"), "should only ignore top level foo directories- not nested") |
| 339 |
} |
| 340 |
|
| 341 |
func TestMatchesLineNumbers(t *testing.T) { |
| 342 |
gitIgnore := []string{"/foo", "bar/", "*.swp"} |
| 343 |
object := CompileIgnoreLines(gitIgnore...) |
| 344 |
|
| 345 |
var matchesPath bool |
| 346 |
var reason *IgnorePattern |
| 347 |
|
| 348 |
|
| 349 |
matchesPath, reason = object.MatchesPathHow("foo/bar.wat") |
| 350 |
assert.Equal(t, true, matchesPath, "should ignore all files in foo - nonpreceding /") |
| 351 |
assert.NotNil(t, reason, "reason should not be nil") |
| 352 |
assert.Equal(t, 1, reason.LineNo, "should match with line 1") |
| 353 |
assert.Equal(t, gitIgnore[0], reason.Line, "should match with line /foo") |
| 354 |
|
| 355 |
matchesPath, reason = object.MatchesPathHow("/foo/something.txt") |
| 356 |
assert.Equal(t, true, matchesPath, "should ignore all files in foo - preceding /") |
| 357 |
assert.NotNil(t, reason, "reason should not be nil") |
| 358 |
assert.Equal(t, 1, reason.LineNo, "should match with line 1") |
| 359 |
assert.Equal(t, gitIgnore[0], reason.Line, "should match with line /foo") |
| 360 |
|
| 361 |
|
| 362 |
matchesPath, reason = object.MatchesPathHow("bar/something.txt") |
| 363 |
assert.Equal(t, true, matchesPath, "should ignore all files in bar - nonpreceding /") |
| 364 |
assert.NotNil(t, reason, "reason should not be nil") |
| 365 |
assert.Equal(t, 2, reason.LineNo, "should match with line 2") |
| 366 |
assert.Equal(t, gitIgnore[1], reason.Line, "should match with line bar/") |
| 367 |
|
| 368 |
matchesPath, reason = object.MatchesPathHow("/bar/somethingelse.go") |
| 369 |
assert.Equal(t, true, matchesPath, "should ignore all files in bar - preceding /") |
| 370 |
assert.NotNil(t, reason, "reason should not be nil") |
| 371 |
assert.Equal(t, 2, reason.LineNo, "should match with line 2") |
| 372 |
assert.Equal(t, gitIgnore[1], reason.Line, "should match with line bar/") |
| 373 |
|
| 374 |
matchesPath, reason = object.MatchesPathHow("/boo/something/bar/boo.txt") |
| 375 |
assert.Equal(t, true, matchesPath, "should block all files if bar is a sub directory") |
| 376 |
assert.NotNil(t, reason, "reason should not be nil") |
| 377 |
assert.Equal(t, 2, reason.LineNo, "should match with line 2") |
| 378 |
assert.Equal(t, gitIgnore[1], reason.Line, "should match with line bar/") |
| 379 |
|
| 380 |
|
| 381 |
matchesPath, reason = object.MatchesPathHow("yo.swp") |
| 382 |
assert.Equal(t, true, matchesPath, "should ignore all swp files") |
| 383 |
assert.NotNil(t, reason, "reason should not be nil") |
| 384 |
assert.Equal(t, 3, reason.LineNo, "should match with line 3") |
| 385 |
assert.Equal(t, gitIgnore[2], reason.Line, "should match with line *.swp") |
| 386 |
|
| 387 |
matchesPath, reason = object.MatchesPathHow("something/else/but/it/hasyo.swp") |
| 388 |
assert.Equal(t, true, matchesPath, "should ignore all swp files in other directories") |
| 389 |
assert.NotNil(t, reason, "reason should not be nil") |
| 390 |
assert.Equal(t, 3, reason.LineNo, "should match with line 3") |
| 391 |
assert.Equal(t, gitIgnore[2], reason.Line, "should match with line *.swp") |
| 392 |
|
| 393 |
|
| 394 |
matchesPath, reason = object.MatchesPathHow("something/foo/something.txt") |
| 395 |
assert.Equal(t, false, matchesPath, "should only ignore top level foo directories- not nested") |
| 396 |
assert.Nil(t, reason, "reason should be nil as no match should happen") |
| 397 |
} |