| 1 |
|
| 2 |
package ignore |
| 3 |
|
| 4 |
import ( |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
) |
| 9 |
|
| 10 |
func TestSimple(test *testing.T) { |
| 11 |
lines := []string{"foo"} |
| 12 |
object := CompileIgnoreLines(lines...) |
| 13 |
|
| 14 |
shouldMatch(test, object, "foo") |
| 15 |
shouldMatch(test, object, "foo/") |
| 16 |
shouldMatch(test, object, "/foo") |
| 17 |
shouldNotMatch(test, object, "fooo") |
| 18 |
shouldNotMatch(test, object, "ofoo") |
| 19 |
} |
| 20 |
|
| 21 |
func TestAnywhere(test *testing.T) { |
| 22 |
lines := []string{"**/foo"} |
| 23 |
object := CompileIgnoreLines(lines...) |
| 24 |
|
| 25 |
shouldMatch(test, object, "foo") |
| 26 |
shouldMatch(test, object, "foo/") |
| 27 |
shouldMatch(test, object, "/foo") |
| 28 |
shouldNotMatch(test, object, "fooo") |
| 29 |
shouldNotMatch(test, object, "ofoo") |
| 30 |
} |
| 31 |
|
| 32 |
func TestAnywhereFromRoot(test *testing.T) { |
| 33 |
lines := []string{"/**/foo"} |
| 34 |
object := CompileIgnoreLines(lines...) |
| 35 |
|
| 36 |
shouldMatch(test, object, "foo") |
| 37 |
shouldMatch(test, object, "foo/") |
| 38 |
shouldMatch(test, object, "/foo") |
| 39 |
shouldNotMatch(test, object, "fooo") |
| 40 |
shouldNotMatch(test, object, "ofoo") |
| 41 |
} |
| 42 |
|
| 43 |
func TestSimpleDir(test *testing.T) { |
| 44 |
lines := []string{"foo/"} |
| 45 |
object := CompileIgnoreLines(lines...) |
| 46 |
|
| 47 |
shouldMatch(test, object, "foo/") |
| 48 |
shouldMatch(test, object, "foo/a") |
| 49 |
shouldMatch(test, object, "/foo/") |
| 50 |
shouldNotMatch(test, object, "foo") |
| 51 |
shouldNotMatch(test, object, "/foo") |
| 52 |
} |
| 53 |
|
| 54 |
func TestRootExtensionOnly(test *testing.T) { |
| 55 |
lines := []string{"/.js"} |
| 56 |
object := CompileIgnoreLines(lines...) |
| 57 |
|
| 58 |
shouldMatch(test, object, ".js") |
| 59 |
shouldMatch(test, object, ".js/") |
| 60 |
shouldMatch(test, object, ".js/a") |
| 61 |
|
| 62 |
|
| 63 |
shouldNotMatch(test, object, ".jsa") |
| 64 |
} |
| 65 |
|
| 66 |
func TestRootExtension(test *testing.T) { |
| 67 |
lines := []string{"/*.js"} |
| 68 |
object := CompileIgnoreLines(lines...) |
| 69 |
|
| 70 |
shouldMatch(test, object, ".js") |
| 71 |
shouldMatch(test, object, ".js/") |
| 72 |
shouldMatch(test, object, ".js/a") |
| 73 |
shouldMatch(test, object, "a.js/a") |
| 74 |
shouldMatch(test, object, "a.js/a.js") |
| 75 |
|
| 76 |
|
| 77 |
shouldNotMatch(test, object, ".jsa") |
| 78 |
} |
| 79 |
|
| 80 |
func TestExtension(test *testing.T) { |
| 81 |
lines := []string{"*.js"} |
| 82 |
object := CompileIgnoreLines(lines...) |
| 83 |
|
| 84 |
shouldMatch(test, object, ".js") |
| 85 |
shouldMatch(test, object, ".js/") |
| 86 |
shouldMatch(test, object, ".js/a") |
| 87 |
shouldMatch(test, object, "a.js/a") |
| 88 |
shouldMatch(test, object, "a.js/a.js") |
| 89 |
shouldMatch(test, object, "/.js") |
| 90 |
shouldNotMatch(test, object, ".jsa") |
| 91 |
} |
| 92 |
|
| 93 |
func TestStarExtension(test *testing.T) { |
| 94 |
lines := []string{".js*"} |
| 95 |
object := CompileIgnoreLines(lines...) |
| 96 |
|
| 97 |
shouldMatch(test, object, ".js") |
| 98 |
shouldMatch(test, object, ".js/") |
| 99 |
shouldMatch(test, object, ".js/a") |
| 100 |
shouldNotMatch(test, object, "a.js/a") |
| 101 |
shouldNotMatch(test, object, "a.js/a.js") |
| 102 |
shouldMatch(test, object, "/.js") |
| 103 |
shouldMatch(test, object, ".jsa") |
| 104 |
} |
| 105 |
|
| 106 |
func TestDoubleStar(test *testing.T) { |
| 107 |
lines := []string{"foo/**/"} |
| 108 |
object := CompileIgnoreLines(lines...) |
| 109 |
|
| 110 |
shouldMatch(test, object, "foo/") |
| 111 |
shouldMatch(test, object, "foo/abc/") |
| 112 |
shouldMatch(test, object, "foo/x/y/z/") |
| 113 |
shouldNotMatch(test, object, "foo") |
| 114 |
shouldNotMatch(test, object, "/foo") |
| 115 |
} |
| 116 |
|
| 117 |
func TestStars(test *testing.T) { |
| 118 |
lines := []string{"foo/**/*.bar"} |
| 119 |
object := CompileIgnoreLines(lines...) |
| 120 |
|
| 121 |
shouldNotMatch(test, object, "foo/") |
| 122 |
shouldNotMatch(test, object, "abc.bar") |
| 123 |
shouldMatch(test, object, "foo/abc.bar") |
| 124 |
shouldMatch(test, object, "foo/abc.bar/") |
| 125 |
shouldMatch(test, object, "foo/x/y/z.bar") |
| 126 |
shouldMatch(test, object, "foo/x/y/z.bar/") |
| 127 |
} |
| 128 |
|
| 129 |
func TestCases_Comment(test *testing.T) { |
| 130 |
lines := []string{"#abc"} |
| 131 |
object := CompileIgnoreLines(lines...) |
| 132 |
|
| 133 |
shouldNotMatch(test, object, "#abc") |
| 134 |
} |
| 135 |
|
| 136 |
func TestCases_EscapedComment(test *testing.T) { |
| 137 |
lines := []string{`\#abc`} |
| 138 |
object := CompileIgnoreLines(lines...) |
| 139 |
|
| 140 |
shouldMatch(test, object, "#abc") |
| 141 |
} |
| 142 |
|
| 143 |
func TestCases_CouldFilterPaths(test *testing.T) { |
| 144 |
lines := []string{"abc", "!abc/b"} |
| 145 |
object := CompileIgnoreLines(lines...) |
| 146 |
|
| 147 |
shouldMatch(test, object, "abc/a.js") |
| 148 |
shouldNotMatch(test, object, "abc/b/b.js") |
| 149 |
} |
| 150 |
|
| 151 |
func TestCases_IgnoreSelect(test *testing.T) { |
| 152 |
lines := []string{"abc", "!abc/b", "#e", `\#f`} |
| 153 |
object := CompileIgnoreLines(lines...) |
| 154 |
|
| 155 |
shouldMatch(test, object, "abc/a.js") |
| 156 |
shouldNotMatch(test, object, "abc/b/b.js") |
| 157 |
shouldNotMatch(test, object, "#e") |
| 158 |
shouldMatch(test, object, "#f") |
| 159 |
} |
| 160 |
|
| 161 |
func TestCases_EscapeRegexMetacharacters(test *testing.T) { |
| 162 |
lines := []string{"*.js", `!\*.js`, "!a#b.js", "!?.js", "#abc", `\#abc`} |
| 163 |
object := CompileIgnoreLines(lines...) |
| 164 |
|
| 165 |
shouldNotMatch(test, object, "*.js") |
| 166 |
shouldMatch(test, object, "abc.js") |
| 167 |
shouldNotMatch(test, object, "a#b.js") |
| 168 |
shouldNotMatch(test, object, "abc") |
| 169 |
shouldMatch(test, object, "#abc") |
| 170 |
shouldNotMatch(test, object, "?.js") |
| 171 |
} |
| 172 |
|
| 173 |
func TestCases_QuestionMark(test *testing.T) { |
| 174 |
lines := []string{"/.project", "thumbs.db", "*.swp", ".sonar/*", ".*.sw?"} |
| 175 |
object := CompileIgnoreLines(lines...) |
| 176 |
|
| 177 |
shouldMatch(test, object, ".project") |
| 178 |
shouldNotMatch(test, object, "abc/.project") |
| 179 |
shouldNotMatch(test, object, ".a.sw") |
| 180 |
shouldMatch(test, object, ".a.sw?") |
| 181 |
shouldMatch(test, object, "thumbs.db") |
| 182 |
} |
| 183 |
|
| 184 |
func TestCases_DirEndedWithStar(test *testing.T) { |
| 185 |
lines := []string{"abc/*"} |
| 186 |
object := CompileIgnoreLines(lines...) |
| 187 |
|
| 188 |
shouldNotMatch(test, object, "abc") |
| 189 |
} |
| 190 |
|
| 191 |
func TestCases_FileEndedWithStar(test *testing.T) { |
| 192 |
lines := []string{"abc.js*"} |
| 193 |
object := CompileIgnoreLines(lines...) |
| 194 |
|
| 195 |
shouldMatch(test, object, "abc.js/") |
| 196 |
shouldMatch(test, object, "abc.js/abc") |
| 197 |
shouldMatch(test, object, "abc.jsa/") |
| 198 |
shouldMatch(test, object, "abc.jsa/abc") |
| 199 |
} |
| 200 |
|
| 201 |
func TestCases_WildcardAsFilename(test *testing.T) { |
| 202 |
lines := []string{"*.b"} |
| 203 |
object := CompileIgnoreLines(lines...) |
| 204 |
|
| 205 |
shouldMatch(test, object, "b/a.b") |
| 206 |
shouldMatch(test, object, "b/.b") |
| 207 |
shouldNotMatch(test, object, "b/.ba") |
| 208 |
shouldMatch(test, object, "b/c/a.b") |
| 209 |
} |
| 210 |
|
| 211 |
func TestCases_SlashAtBeginningAndComeWithWildcard(test *testing.T) { |
| 212 |
lines := []string{"/*.c"} |
| 213 |
object := CompileIgnoreLines(lines...) |
| 214 |
|
| 215 |
shouldMatch(test, object, ".c") |
| 216 |
shouldMatch(test, object, "c.c") |
| 217 |
shouldNotMatch(test, object, "c/c.c") |
| 218 |
shouldNotMatch(test, object, "c/d") |
| 219 |
} |
| 220 |
|
| 221 |
func TestCases_DotFile(test *testing.T) { |
| 222 |
lines := []string{".d"} |
| 223 |
object := CompileIgnoreLines(lines...) |
| 224 |
|
| 225 |
shouldMatch(test, object, ".d") |
| 226 |
shouldNotMatch(test, object, ".dd") |
| 227 |
shouldNotMatch(test, object, "d.d") |
| 228 |
shouldMatch(test, object, "d/.d") |
| 229 |
shouldNotMatch(test, object, "d/d.d") |
| 230 |
shouldNotMatch(test, object, "d/e") |
| 231 |
} |
| 232 |
|
| 233 |
func TestCases_DotDir(test *testing.T) { |
| 234 |
lines := []string{".e"} |
| 235 |
object := CompileIgnoreLines(lines...) |
| 236 |
|
| 237 |
shouldMatch(test, object, ".e/") |
| 238 |
shouldNotMatch(test, object, ".ee/") |
| 239 |
shouldNotMatch(test, object, "e.e/") |
| 240 |
shouldMatch(test, object, ".e/e") |
| 241 |
shouldMatch(test, object, "e/.e") |
| 242 |
shouldNotMatch(test, object, "e/e.e") |
| 243 |
shouldNotMatch(test, object, "e/f") |
| 244 |
} |
| 245 |
|
| 246 |
func TestCases_PatternOnce(test *testing.T) { |
| 247 |
lines := []string{"node_modules/"} |
| 248 |
object := CompileIgnoreLines(lines...) |
| 249 |
|
| 250 |
shouldMatch(test, object, "node_modules/gulp/node_modules/abc.md") |
| 251 |
shouldMatch(test, object, "node_modules/gulp/node_modules/abc.json") |
| 252 |
} |
| 253 |
|
| 254 |
func TestCases_PatternTwice(test *testing.T) { |
| 255 |
lines := []string{"node_modules/", "node_modules/"} |
| 256 |
object := CompileIgnoreLines(lines...) |
| 257 |
|
| 258 |
shouldMatch(test, object, "node_modules/gulp/node_modules/abc.md") |
| 259 |
shouldMatch(test, object, "node_modules/gulp/node_modules/abc.json") |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
func shouldMatch(test *testing.T, object *GitIgnore, path string) { |
| 265 |
assert.Equal(test, true, object.MatchesPath(path), path+" should match") |
| 266 |
} |
| 267 |
|
| 268 |
func shouldNotMatch(test *testing.T, object *GitIgnore, path string) { |
| 269 |
assert.Equal(test, false, object.MatchesPath(path), path+" should not match") |
| 270 |
} |
| 271 |
|
| 272 |
|