|
@@ -277,14 +277,6 @@ func TestSingleExclamationError(t *testing.T) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// A string preceded with a ! should return true from Exclusion.
|
|
|
|
-func TestExclusion(t *testing.T) {
|
|
|
|
- exclusion := exclusion("!")
|
|
|
|
- if !exclusion {
|
|
|
|
- t.Errorf("failed to get true for a single !, got %v", exclusion)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// Matches with no patterns
|
|
// Matches with no patterns
|
|
func TestMatchesWithNoPatterns(t *testing.T) {
|
|
func TestMatchesWithNoPatterns(t *testing.T) {
|
|
matches, err := Matches("/any/path/there", []string{})
|
|
matches, err := Matches("/any/path/there", []string{})
|
|
@@ -335,7 +327,7 @@ func TestMatches(t *testing.T) {
|
|
{"dir/**", "dir/dir2/file", true},
|
|
{"dir/**", "dir/dir2/file", true},
|
|
{"dir/**", "dir/dir2/file/", true},
|
|
{"dir/**", "dir/dir2/file/", true},
|
|
{"**/dir2/*", "dir/dir2/file", true},
|
|
{"**/dir2/*", "dir/dir2/file", true},
|
|
- {"**/dir2/*", "dir/dir2/file/", false},
|
|
|
|
|
|
+ {"**/dir2/*", "dir/dir2/file/", true},
|
|
{"**/dir2/**", "dir/dir2/dir3/file", true},
|
|
{"**/dir2/**", "dir/dir2/dir3/file", true},
|
|
{"**/dir2/**", "dir/dir2/dir3/file/", true},
|
|
{"**/dir2/**", "dir/dir2/dir3/file/", true},
|
|
{"**file", "file", true},
|
|
{"**file", "file", true},
|
|
@@ -384,73 +376,82 @@ func TestMatches(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
for _, test := range tests {
|
|
for _, test := range tests {
|
|
- res, _ := regexpMatch(test.pattern, test.text)
|
|
|
|
|
|
+ pm, err := NewPatternMatcher([]string{test.pattern})
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("invalid pattern %s", test.pattern)
|
|
|
|
+ }
|
|
|
|
+ res, _ := pm.Matches(test.text)
|
|
if res != test.pass {
|
|
if res != test.pass {
|
|
t.Fatalf("Failed: %v - res:%v", test, res)
|
|
t.Fatalf("Failed: %v - res:%v", test, res)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// An empty string should return true from Empty.
|
|
|
|
-func TestEmpty(t *testing.T) {
|
|
|
|
- empty := empty("")
|
|
|
|
- if !empty {
|
|
|
|
- t.Errorf("failed to get true for an empty string, got %v", empty)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func TestCleanPatterns(t *testing.T) {
|
|
func TestCleanPatterns(t *testing.T) {
|
|
- cleaned, _, _, _ := CleanPatterns([]string{"docs", "config"})
|
|
|
|
|
|
+ patterns := []string{"docs", "config"}
|
|
|
|
+ pm, err := NewPatternMatcher(patterns)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
+ }
|
|
|
|
+ cleaned := pm.Patterns()
|
|
if len(cleaned) != 2 {
|
|
if len(cleaned) != 2 {
|
|
t.Errorf("expected 2 element slice, got %v", len(cleaned))
|
|
t.Errorf("expected 2 element slice, got %v", len(cleaned))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
|
|
func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
|
|
- cleaned, _, _, _ := CleanPatterns([]string{"docs", "config", ""})
|
|
|
|
|
|
+ patterns := []string{"docs", "config", ""}
|
|
|
|
+ pm, err := NewPatternMatcher(patterns)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
+ }
|
|
|
|
+ cleaned := pm.Patterns()
|
|
if len(cleaned) != 2 {
|
|
if len(cleaned) != 2 {
|
|
t.Errorf("expected 2 element slice, got %v", len(cleaned))
|
|
t.Errorf("expected 2 element slice, got %v", len(cleaned))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestCleanPatternsExceptionFlag(t *testing.T) {
|
|
func TestCleanPatternsExceptionFlag(t *testing.T) {
|
|
- _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md"})
|
|
|
|
- if !exceptions {
|
|
|
|
- t.Errorf("expected exceptions to be true, got %v", exceptions)
|
|
|
|
|
|
+ patterns := []string{"docs", "!docs/README.md"}
|
|
|
|
+ pm, err := NewPatternMatcher(patterns)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
+ }
|
|
|
|
+ if !pm.Exclusions() {
|
|
|
|
+ t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
|
|
func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
|
|
- _, _, exceptions, _ := CleanPatterns([]string{"docs", " !docs/README.md"})
|
|
|
|
- if !exceptions {
|
|
|
|
- t.Errorf("expected exceptions to be true, got %v", exceptions)
|
|
|
|
|
|
+ patterns := []string{"docs", " !docs/README.md"}
|
|
|
|
+ pm, err := NewPatternMatcher(patterns)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
+ }
|
|
|
|
+ if !pm.Exclusions() {
|
|
|
|
+ t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
|
|
func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
|
|
- _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md "})
|
|
|
|
- if !exceptions {
|
|
|
|
- t.Errorf("expected exceptions to be true, got %v", exceptions)
|
|
|
|
|
|
+ patterns := []string{"docs", "!docs/README.md "}
|
|
|
|
+ pm, err := NewPatternMatcher(patterns)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
+ }
|
|
|
|
+ if !pm.Exclusions() {
|
|
|
|
+ t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestCleanPatternsErrorSingleException(t *testing.T) {
|
|
func TestCleanPatternsErrorSingleException(t *testing.T) {
|
|
- _, _, _, err := CleanPatterns([]string{"!"})
|
|
|
|
|
|
+ patterns := []string{"!"}
|
|
|
|
+ _, err := NewPatternMatcher(patterns)
|
|
if err == nil {
|
|
if err == nil {
|
|
t.Errorf("expected error on single exclamation point, got %v", err)
|
|
t.Errorf("expected error on single exclamation point, got %v", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func TestCleanPatternsFolderSplit(t *testing.T) {
|
|
|
|
- _, dirs, _, _ := CleanPatterns([]string{"docs/config/CONFIG.md"})
|
|
|
|
- if dirs[0][0] != "docs" {
|
|
|
|
- t.Errorf("expected first element in dirs slice to be docs, got %v", dirs[0][1])
|
|
|
|
- }
|
|
|
|
- if dirs[0][1] != "config" {
|
|
|
|
- t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1])
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func TestCreateIfNotExistsDir(t *testing.T) {
|
|
func TestCreateIfNotExistsDir(t *testing.T) {
|
|
tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
|
|
tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -508,7 +509,7 @@ var matchTests = []matchTest{
|
|
{"*c", "abc", true, nil},
|
|
{"*c", "abc", true, nil},
|
|
{"a*", "a", true, nil},
|
|
{"a*", "a", true, nil},
|
|
{"a*", "abc", true, nil},
|
|
{"a*", "abc", true, nil},
|
|
- {"a*", "ab/c", false, nil},
|
|
|
|
|
|
+ {"a*", "ab/c", true, nil},
|
|
{"a*/b", "abc/b", true, nil},
|
|
{"a*/b", "abc/b", true, nil},
|
|
{"a*/b", "a/c/b", false, nil},
|
|
{"a*/b", "a/c/b", false, nil},
|
|
{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
|
|
{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
|
|
@@ -579,7 +580,7 @@ func TestMatch(t *testing.T) {
|
|
pattern = filepath.Clean(pattern)
|
|
pattern = filepath.Clean(pattern)
|
|
s = filepath.Clean(s)
|
|
s = filepath.Clean(s)
|
|
}
|
|
}
|
|
- ok, err := regexpMatch(pattern, s)
|
|
|
|
|
|
+ ok, err := Matches(s, []string{pattern})
|
|
if ok != tt.match || err != tt.err {
|
|
if ok != tt.match || err != tt.err {
|
|
t.Fatalf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
|
|
t.Fatalf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
|
|
}
|
|
}
|