support ignoring file or directories

use the doublestar library to support pattern matching of files or
directories to ignore. This replaces (and deprecates) the previous
-skip flag which only supported file extensions.
This commit is contained in:
Will Norris
2021-07-28 02:41:40 -07:00
parent 99ebc9c9db
commit 4f7a460357
6 changed files with 106 additions and 12 deletions

View File

@@ -397,3 +397,63 @@ func TestHasLicense(t *testing.T) {
}
}
}
func TestFileMatches(t *testing.T) {
tests := []struct {
pattern string
path string
wantMatch bool
}{
// basic single directory patterns
{"", "file.c", false},
{"*.c", "file.h", false},
{"*.c", "file.c", true},
// subdirectory patterns
{"*.c", "vendor/file.c", false},
{"**/*.c", "vendor/file.c", true},
{"vendor/**", "vendor/file.c", true},
{"vendor/**/*.c", "vendor/file.c", true},
{"vendor/**/*.c", "vendor/a/b/file.c", true},
// single character "?" match
{"*.?", "file.c", true},
{"*.?", "file.go", false},
{"*.??", "file.c", false},
{"*.??", "file.go", true},
// character classes - sets and ranges
{"*.[ch]", "file.c", true},
{"*.[ch]", "file.h", true},
{"*.[ch]", "file.ch", false},
{"*.[a-z]", "file.c", true},
{"*.[a-z]", "file.h", true},
{"*.[a-z]", "file.go", false},
{"*.[a-z]", "file.R", false},
// character classes - negations
{"*.[^ch]", "file.c", false},
{"*.[^ch]", "file.h", false},
{"*.[^ch]", "file.R", true},
{"*.[!ch]", "file.c", false},
{"*.[!ch]", "file.h", false},
{"*.[!ch]", "file.R", true},
// comma-separated alternative matches
{"*.{c,go}", "file.c", true},
{"*.{c,go}", "file.go", true},
{"*.{c,go}", "file.h", false},
// negating alternative matches
{"*.[^{c,go}]", "file.c", false},
{"*.[^{c,go}]", "file.go", false},
{"*.[^{c,go}]", "file.h", true},
}
for _, tt := range tests {
patterns := []string{tt.pattern}
if got := fileMatches(tt.path, patterns); got != tt.wantMatch {
t.Errorf("fileMatches(%q, %q) returned %v, want %v", tt.path, patterns, got, tt.wantMatch)
}
}
}