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
|
package filepathfilter
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPatternMatch(t *testing.T) {
assertPatternMatch(t, "*",
"a",
"a.a",
"a/b",
"a/b.b",
"a/b/c",
"a/b/c.c",
)
assertPatternMatch(t, "filename.txt", "filename.txt")
assertPatternMatch(t, "*.txt", "filename.txt")
refutePatternMatch(t, "*.tx", "filename.txt")
assertPatternMatch(t, "f*.txt", "filename.txt")
refutePatternMatch(t, "g*.txt", "filename.txt")
assertPatternMatch(t, "file*", "filename.txt")
refutePatternMatch(t, "file", "filename.txt")
// With no path separators, should match in subfolders
assertPatternMatch(t, "*.txt", "sub/filename.txt")
refutePatternMatch(t, "*.tx", "sub/filename.txt")
assertPatternMatch(t, "f*.txt", "sub/filename.txt")
refutePatternMatch(t, "g*.txt", "sub/filename.txt")
assertPatternMatch(t, "file*", "sub/filename.txt")
refutePatternMatch(t, "file", "sub/filename.txt")
// matches only in subdir
assertPatternMatch(t, "sub/*.txt", "sub/filename.txt")
refutePatternMatch(t, "sub/*.txt",
"top/sub/filename.txt",
"sub/filename.dat",
"other/filename.txt",
)
// Needs wildcard for exact filename
assertPatternMatch(t, "**/filename.txt", "sub/sub/sub/filename.txt")
// Should not match dots to subparts
refutePatternMatch(t, "*.ign", "sub/shouldignoreme.txt")
// Path specific
assertPatternMatch(t, "sub",
"sub",
"top/sub",
)
refutePatternMatch(t, "sub",
"subfilename.txt",
"sub/filename.txt",
"top/sub/filename.txt",
)
assertPatternMatch(t, "/sub",
"sub",
)
refutePatternMatch(t, "/sub",
"subfilename.txt",
"sub/filename.txt",
"top/sub",
"top/sub/filename.txt",
)
refutePatternMatch(t, "sub/",
"sub",
"subfilename.txt",
"sub/filename.txt",
"top/sub",
"top/sub/filename.txt",
)
assertPatternMatchIgnore(t, "sub/",
"sub/",
"sub/filename.txt",
"top/sub/",
"top/sub/filename.txt",
)
refutePatternMatchIgnore(t, "sub/",
"subfilename.txt",
)
refutePatternMatch(t, "/sub/",
"sub",
"subfilename.txt",
"sub/filename.txt",
"top/sub",
"top/sub/filename.txt",
)
assertPatternMatchIgnore(t, "/sub/",
"sub/",
"sub/filename.txt",
)
refutePatternMatchIgnore(t, "/sub/",
"subfilename.txt",
"top/sub",
"top/sub/filename.txt",
)
// Absolute
assertPatternMatch(t, "*.dat", "/path/to/sub/.git/test.dat")
assertPatternMatch(t, "**/.git", "/path/to/sub/.git")
}
func assertPatternMatch(t *testing.T, pattern string, filenames ...string) {
p := NewPattern(pattern, GitAttributes)
for _, filename := range filenames {
assert.True(t, p.Match(filename), "%q should match pattern %q", filename, pattern)
}
}
func assertPatternMatchIgnore(t *testing.T, pattern string, filenames ...string) {
p := NewPattern(pattern, GitIgnore)
for _, filename := range filenames {
assert.True(t, p.Match(filename), "%q should match pattern %q", filename, pattern)
}
}
func refutePatternMatch(t *testing.T, pattern string, filenames ...string) {
p := NewPattern(pattern, GitAttributes)
for _, filename := range filenames {
assert.False(t, p.Match(filename), "%q should not match pattern %q", filename, pattern)
}
}
func refutePatternMatchIgnore(t *testing.T, pattern string, filenames ...string) {
p := NewPattern(pattern, GitIgnore)
for _, filename := range filenames {
assert.False(t, p.Match(filename), "%q should not match pattern %q", filename, pattern)
}
}
type filterTest struct {
expectedResult bool
expectedPattern string
includes []string
excludes []string
}
func TestFilterReportsIncludePatterns(t *testing.T) {
filter := New([]string{"*.foo", "*.bar"}, nil, GitAttributes)
assert.Equal(t, []string{"*.foo", "*.bar"}, filter.Include())
}
func TestFilterReportsExcludePatterns(t *testing.T) {
filter := New(nil, []string{"*.baz", "*.quux"}, GitAttributes)
assert.Equal(t, []string{"*.baz", "*.quux"}, filter.Exclude())
}
|