| 1 | package filepathfilter |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | func TestPatternMatch(t *testing.T) { |
| 10 | assertPatternMatch(t, "*", |
| 11 | "a", |
| 12 | "a.a", |
| 13 | "a/b", |
| 14 | "a/b.b", |
| 15 | "a/b/c", |
| 16 | "a/b/c.c", |
| 17 | ) |
| 18 | |
| 19 | assertPatternMatch(t, "filename.txt", "filename.txt") |
| 20 | assertPatternMatch(t, "*.txt", "filename.txt") |
| 21 | refutePatternMatch(t, "*.tx", "filename.txt") |
| 22 | assertPatternMatch(t, "f*.txt", "filename.txt") |
| 23 | refutePatternMatch(t, "g*.txt", "filename.txt") |
| 24 | assertPatternMatch(t, "file*", "filename.txt") |
| 25 | refutePatternMatch(t, "file", "filename.txt") |
| 26 | |
| 27 | // With no path separators, should match in subfolders |
| 28 | assertPatternMatch(t, "*.txt", "sub/filename.txt") |
| 29 | refutePatternMatch(t, "*.tx", "sub/filename.txt") |
| 30 | assertPatternMatch(t, "f*.txt", "sub/filename.txt") |
| 31 | refutePatternMatch(t, "g*.txt", "sub/filename.txt") |
| 32 | assertPatternMatch(t, "file*", "sub/filename.txt") |
| 33 | refutePatternMatch(t, "file", "sub/filename.txt") |
| 34 | |
| 35 | // matches only in subdir |
| 36 | assertPatternMatch(t, "sub/*.txt", "sub/filename.txt") |
| 37 | refutePatternMatch(t, "sub/*.txt", |
| 38 | "top/sub/filename.txt", |
| 39 | "sub/filename.dat", |
| 40 | "other/filename.txt", |
| 41 | ) |
| 42 | |
| 43 | // Needs wildcard for exact filename |
| 44 | assertPatternMatch(t, "**/filename.txt", "sub/sub/sub/filename.txt") |
| 45 | |
| 46 | // Should not match dots to subparts |
| 47 | refutePatternMatch(t, "*.ign", "sub/shouldignoreme.txt") |
| 48 | |
| 49 | // Path specific |
| 50 | assertPatternMatch(t, "sub", |
| 51 | "sub", |
| 52 | "top/sub", |
| 53 | ) |
| 54 | refutePatternMatch(t, "sub", |
| 55 | "subfilename.txt", |
| 56 | "sub/filename.txt", |
| 57 | "top/sub/filename.txt", |
| 58 | ) |
| 59 | |
| 60 | assertPatternMatch(t, "/sub", |
| 61 | "sub", |
| 62 | ) |
| 63 | refutePatternMatch(t, "/sub", |
| 64 | "subfilename.txt", |
| 65 | "sub/filename.txt", |
| 66 | "top/sub", |
| 67 | "top/sub/filename.txt", |
| 68 | ) |
| 69 | |
| 70 | refutePatternMatch(t, "sub/", |
| 71 | "sub", |
| 72 | "subfilename.txt", |
| 73 | "sub/filename.txt", |
| 74 | "top/sub", |
| 75 | "top/sub/filename.txt", |
| 76 | ) |
| 77 | assertPatternMatchIgnore(t, "sub/", |
| 78 | "sub/", |
| 79 | "sub/filename.txt", |
| 80 | "top/sub/", |
| 81 | "top/sub/filename.txt", |
| 82 | ) |
| 83 | refutePatternMatchIgnore(t, "sub/", |
| 84 | "subfilename.txt", |
| 85 | ) |
| 86 | |
| 87 | refutePatternMatch(t, "/sub/", |
| 88 | "sub", |
| 89 | "subfilename.txt", |
| 90 | "sub/filename.txt", |
| 91 | "top/sub", |
| 92 | "top/sub/filename.txt", |
| 93 | ) |
| 94 | assertPatternMatchIgnore(t, "/sub/", |
| 95 | "sub/", |
| 96 | "sub/filename.txt", |
| 97 | ) |
| 98 | refutePatternMatchIgnore(t, "/sub/", |
| 99 | "subfilename.txt", |
| 100 | "top/sub", |
| 101 | "top/sub/filename.txt", |
| 102 | ) |
| 103 | |
| 104 | // Absolute |
| 105 | assertPatternMatch(t, "*.dat", "/path/to/sub/.git/test.dat") |
| 106 | assertPatternMatch(t, "**/.git", "/path/to/sub/.git") |
| 107 | } |
| 108 | |
| 109 | func assertPatternMatch(t *testing.T, pattern string, filenames ...string) { |
| 110 | p := NewPattern(pattern, GitAttributes, nil) |
| 111 | for _, filename := range filenames { |
| 112 | assert.True(t, p.Match(filename), "%q should match pattern %q", filename, pattern) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func assertPatternMatchIgnore(t *testing.T, pattern string, filenames ...string) { |
| 117 | p := NewPattern(pattern, GitIgnore, nil) |
| 118 | for _, filename := range filenames { |
| 119 | assert.True(t, p.Match(filename), "%q should match pattern %q", filename, pattern) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func refutePatternMatch(t *testing.T, pattern string, filenames ...string) { |
| 124 | p := NewPattern(pattern, GitAttributes, nil) |
| 125 | for _, filename := range filenames { |
| 126 | assert.False(t, p.Match(filename), "%q should not match pattern %q", filename, pattern) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func refutePatternMatchIgnore(t *testing.T, pattern string, filenames ...string) { |
| 131 | p := NewPattern(pattern, GitIgnore, nil) |
| 132 | for _, filename := range filenames { |
| 133 | assert.False(t, p.Match(filename), "%q should not match pattern %q", filename, pattern) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | type filterTest struct { |
| 138 | expectedResult bool |
| 139 | expectedPattern string |
| 140 | includes []string |
| 141 | excludes []string |
| 142 | } |
| 143 | |
| 144 | func TestFilterReportsIncludePatterns(t *testing.T) { |
| 145 | filter := New([]string{"*.foo", "*.bar"}, nil, GitAttributes, nil) |
| 146 | |
| 147 | assert.Equal(t, []string{"*.foo", "*.bar"}, filter.Include()) |
| 148 | } |
| 149 | |
| 150 | func TestFilterReportsExcludePatterns(t *testing.T) { |
| 151 | filter := New(nil, []string{"*.baz", "*.quux"}, GitAttributes, nil) |
| 152 | |
| 153 | assert.Equal(t, []string{"*.baz", "*.quux"}, filter.Exclude()) |
| 154 | } |