| 1 |
// Copyright 2019 The go-git authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
// This file was originally part of https://github.com/go-git/go-git/blob/main/plumbing/format/gitattributes |
| 16 |
|
| 17 |
package gitignore |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
func TestSimpleMatch_inclusion(t *testing.T) { |
| 24 |
p := ParsePattern("!vul?ano", nil) |
| 25 |
r := p.Match([]string{"value", "vulkano", "tail"}, false) |
| 26 |
if r != Include { |
| 27 |
t.Errorf("Expected Include, got %v", r) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestMatch_domainLonger_mismatch(t *testing.T) { |
| 32 |
p := ParsePattern("value", []string{"head", "middle", "tail"}) |
| 33 |
r := p.Match([]string{"head", "middle"}, false) |
| 34 |
if r != NoMatch { |
| 35 |
t.Errorf("Expected NoMatch, got %v", r) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestMatch_domainSameLength_mismatch(t *testing.T) { |
| 40 |
p := ParsePattern("value", []string{"head", "middle", "tail"}) |
| 41 |
r := p.Match([]string{"head", "middle", "tail"}, false) |
| 42 |
if r != NoMatch { |
| 43 |
t.Errorf("Expected NoMatch, got %v", r) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestMatch_domainMismatch_mismatch(t *testing.T) { |
| 48 |
p := ParsePattern("value", []string{"head", "middle", "tail"}) |
| 49 |
r := p.Match([]string{"head", "middle", "_tail_", "value"}, false) |
| 50 |
if r != NoMatch { |
| 51 |
t.Errorf("Expected NoMatch, got %v", r) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestSimpleMatch_withDomain(t *testing.T) { |
| 56 |
p := ParsePattern("middle/", []string{"value", "volcano"}) |
| 57 |
r := p.Match([]string{"value", "volcano", "middle", "tail"}, false) |
| 58 |
if r != Exclude { |
| 59 |
t.Errorf("Expected Exclude, got %v", r) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestSimpleMatch_onlyMatchInDomain_mismatch(t *testing.T) { |
| 64 |
p := ParsePattern("volcano/", []string{"value", "volcano"}) |
| 65 |
r := p.Match([]string{"value", "volcano", "tail"}, true) |
| 66 |
expected := NoMatch |
| 67 |
if r != expected { |
| 68 |
t.Errorf("Expected %v, got %v", expected, r) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestSimpleMatch_atStart(t *testing.T) { |
| 73 |
p := ParsePattern("value", nil) |
| 74 |
r := p.Match([]string{"value", "tail"}, false) |
| 75 |
expected := Exclude |
| 76 |
if r != expected { |
| 77 |
t.Errorf("Expected %v, got %v", expected, r) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestSimpleMatch_inTheMiddle(t *testing.T) { |
| 82 |
p := ParsePattern("value", nil) |
| 83 |
r := p.Match([]string{"head", "value", "tail"}, false) |
| 84 |
expected := Exclude |
| 85 |
if r != expected { |
| 86 |
t.Errorf("Expected %v, got %v", expected, r) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestSimpleMatch_atEnd(t *testing.T) { |
| 91 |
p := ParsePattern("value", nil) |
| 92 |
r := p.Match([]string{"head", "value"}, false) |
| 93 |
expected := Exclude |
| 94 |
if r != expected { |
| 95 |
t.Errorf("Expected %v, got %v", expected, r) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestSimpleMatch_atStart_dirWanted(t *testing.T) { |
| 100 |
p := ParsePattern("value/", nil) |
| 101 |
r := p.Match([]string{"value", "tail"}, false) |
| 102 |
expected := Exclude |
| 103 |
if r != expected { |
| 104 |
t.Errorf("Expected %v, got %v", expected, r) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestSimpleMatch_inTheMiddle_dirWanted(t *testing.T) { |
| 109 |
p := ParsePattern("value/", nil) |
| 110 |
r := p.Match([]string{"head", "value", "tail"}, false) |
| 111 |
expected := Exclude |
| 112 |
if r != expected { |
| 113 |
t.Errorf("Expected %v, got %v", expected, r) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestSimpleMatch_atEnd_dirWanted(t *testing.T) { |
| 118 |
p := ParsePattern("value/", nil) |
| 119 |
r := p.Match([]string{"head", "value"}, true) |
| 120 |
expected := Exclude |
| 121 |
if r != expected { |
| 122 |
t.Errorf("Expected %v, got %v", expected, r) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestSimpleMatch_atEnd_dirWanted_notADir_mismatch(t *testing.T) { |
| 127 |
p := ParsePattern("value/", nil) |
| 128 |
r := p.Match([]string{"head", "value"}, false) |
| 129 |
expected := NoMatch |
| 130 |
if r != expected { |
| 131 |
t.Errorf("Expected %v, got %v", expected, r) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestSimpleMatch_mismatch(t *testing.T) { |
| 136 |
p := ParsePattern("value", nil) |
| 137 |
r := p.Match([]string{"head", "val", "tail"}, false) |
| 138 |
expected := NoMatch |
| 139 |
if r != expected { |
| 140 |
t.Errorf("Expected %v, got %v", expected, r) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func TestSimpleMatch_valueLonger_mismatch(t *testing.T) { |
| 145 |
p := ParsePattern("val", nil) |
| 146 |
r := p.Match([]string{"head", "value", "tail"}, false) |
| 147 |
expected := NoMatch |
| 148 |
if r != expected { |
| 149 |
t.Errorf("Expected %v, got %v", expected, r) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func TestSimpleMatch_withAsterisk(t *testing.T) { |
| 154 |
p := ParsePattern("v*o", nil) |
| 155 |
r := p.Match([]string{"value", "vulkano", "tail"}, false) |
| 156 |
expected := Exclude |
| 157 |
if r != expected { |
| 158 |
t.Errorf("Expected %v, got %v", expected, r) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func TestSimpleMatch_withQuestionMark(t *testing.T) { |
| 163 |
p := ParsePattern("vul?ano", nil) |
| 164 |
r := p.Match([]string{"value", "vulkano", "tail"}, false) |
| 165 |
expected := Exclude |
| 166 |
if r != expected { |
| 167 |
t.Errorf("Expected %v, got %v", expected, r) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestSimpleMatch_magicChars(t *testing.T) { |
| 172 |
p := ParsePattern("v[ou]l[kc]ano", nil) |
| 173 |
r := p.Match([]string{"value", "volcano"}, false) |
| 174 |
expected := Exclude |
| 175 |
if r != expected { |
| 176 |
t.Errorf("Expected %v, got %v", expected, r) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func TestSimpleMatch_wrongPattern_mismatch(t *testing.T) { |
| 181 |
p := ParsePattern("v[ou]l[", nil) |
| 182 |
r := p.Match([]string{"value", "vol["}, false) |
| 183 |
expected := NoMatch |
| 184 |
if r != expected { |
| 185 |
t.Errorf("Expected %v, got %v", expected, r) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestGlobMatch_fromRootWithSlash(t *testing.T) { |
| 190 |
p := ParsePattern("/value/vul?ano", nil) |
| 191 |
r := p.Match([]string{"value", "vulkano", "tail"}, false) |
| 192 |
expected := Exclude |
| 193 |
if r != expected { |
| 194 |
t.Errorf("Expected %v, got %v", expected, r) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
func TestGlobMatch_withDomain(t *testing.T) { |
| 199 |
p := ParsePattern("middle/tail/", []string{"value", "volcano"}) |
| 200 |
r := p.Match([]string{"value", "volcano", "middle", "tail"}, true) |
| 201 |
expected := Exclude |
| 202 |
if r != expected { |
| 203 |
t.Errorf("Expected %v, got %v", expected, r) |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func TestGlobMatch_onlyMatchInDomain_mismatch(t *testing.T) { |
| 208 |
p := ParsePattern("volcano/tail", []string{"value", "volcano"}) |
| 209 |
r := p.Match([]string{"value", "volcano", "tail"}, false) |
| 210 |
expected := NoMatch |
| 211 |
if r != expected { |
| 212 |
t.Errorf("Expected %v, got %v", expected, r) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
func TestGlobMatch_fromRootWithoutSlash(t *testing.T) { |
| 217 |
p := ParsePattern("value/vul?ano", nil) |
| 218 |
r := p.Match([]string{"value", "vulkano", "tail"}, false) |
| 219 |
expected := Exclude |
| 220 |
if r != expected { |
| 221 |
t.Errorf("Expected %v, got %v", expected, r) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
func TestGlobMatch_fromRoot_mismatch(t *testing.T) { |
| 226 |
p := ParsePattern("value/vulkano", nil) |
| 227 |
r := p.Match([]string{"value", "volcano"}, false) |
| 228 |
expected := NoMatch |
| 229 |
if r != expected { |
| 230 |
t.Errorf("Expected %v, got %v", expected, r) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func TestGlobMatch_fromRoot_tooShort_mismatch(t *testing.T) { |
| 235 |
p := ParsePattern("value/vul?ano", nil) |
| 236 |
r := p.Match([]string{"value"}, false) |
| 237 |
expected := NoMatch |
| 238 |
if r != expected { |
| 239 |
t.Errorf("Expected %v, got %v", expected, r) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
func TestGlobMatch_fromRoot_notAtRoot_mismatch(t *testing.T) { |
| 244 |
p := ParsePattern("/value/volcano", nil) |
| 245 |
r := p.Match([]string{"value", "value", "volcano"}, false) |
| 246 |
expected := NoMatch |
| 247 |
if r != expected { |
| 248 |
t.Errorf("Expected %v, got %v", expected, r) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func TestGlobMatch_leadingAsterisks_atStart(t *testing.T) { |
| 253 |
p := ParsePattern("**/*lue/vol?ano", nil) |
| 254 |
r := p.Match([]string{"value", "volcano", "tail"}, false) |
| 255 |
expected := Exclude |
| 256 |
if r != expected { |
| 257 |
t.Errorf("Expected %v, got %v", expected, r) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func TestGlobMatch_leadingAsterisks_notAtStart(t *testing.T) { |
| 262 |
p := ParsePattern("**/*lue/vol?ano", nil) |
| 263 |
r := p.Match([]string{"head", "value", "volcano", "tail"}, false) |
| 264 |
expected := Exclude |
| 265 |
if r != expected { |
| 266 |
t.Errorf("Expected %v, got %v", expected, r) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
func TestGlobMatch_leadingAsterisks_mismatch(t *testing.T) { |
| 271 |
p := ParsePattern("**/*lue/vol?ano", nil) |
| 272 |
r := p.Match([]string{"head", "value", "Volcano", "tail"}, false) |
| 273 |
expected := NoMatch |
| 274 |
if r != expected { |
| 275 |
t.Errorf("Expected %v, got %v", expected, r) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func TestGlobMatch_leadingAsterisks_isDir(t *testing.T) { |
| 280 |
p := ParsePattern("**/*lue/vol?ano/", nil) |
| 281 |
r := p.Match([]string{"head", "value", "volcano", "tail"}, false) |
| 282 |
expected := Exclude |
| 283 |
if r != expected { |
| 284 |
t.Errorf("Expected %v, got %v", expected, r) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
func TestGlobMatch_leadingAsterisks_isDirAtEnd(t *testing.T) { |
| 289 |
p := ParsePattern("**/*lue/vol?ano/", nil) |
| 290 |
r := p.Match([]string{"head", "value", "volcano"}, true) |
| 291 |
expected := Exclude |
| 292 |
if r != expected { |
| 293 |
t.Errorf("Expected %v, got %v", expected, r) |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func TestGlobMatch_leadingAsterisks_isDir_mismatch(t *testing.T) { |
| 298 |
p := ParsePattern("**/*lue/vol?ano/", nil) |
| 299 |
r := p.Match([]string{"head", "value", "Colcano"}, true) |
| 300 |
expected := NoMatch |
| 301 |
if r != expected { |
| 302 |
t.Errorf("Expected %v, got %v", expected, r) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
func TestGlobMatch_leadingAsterisks_isDirNoDirAtEnd_mismatch(t *testing.T) { |
| 307 |
p := ParsePattern("**/*lue/vol?ano/", nil) |
| 308 |
r := p.Match([]string{"head", "value", "volcano"}, false) |
| 309 |
expected := NoMatch |
| 310 |
if r != expected { |
| 311 |
t.Errorf("Expected %v, got %v", expected, r) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func TestGlobMatch_tailingAsterisks(t *testing.T) { |
| 316 |
p := ParsePattern("/*lue/vol?ano/**", nil) |
| 317 |
r := p.Match([]string{"value", "volcano", "tail", "moretail"}, false) |
| 318 |
expected := Exclude |
| 319 |
if r != expected { |
| 320 |
t.Errorf("Expected %v, got %v", expected, r) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestGlobMatch_tailingAsterisks_exactMatch(t *testing.T) { |
| 325 |
p := ParsePattern("/*lue/vol?ano/**", nil) |
| 326 |
r := p.Match([]string{"value", "volcano"}, false) |
| 327 |
expected := NoMatch |
| 328 |
if r != expected { |
| 329 |
t.Errorf("Expected %v, got %v", expected, r) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
func TestGlobMatch_middleAsterisks_emptyMatch(t *testing.T) { |
| 334 |
p := ParsePattern("/*lue/**/vol?ano", nil) |
| 335 |
r := p.Match([]string{"value", "volcano"}, false) |
| 336 |
expected := Exclude |
| 337 |
if r != expected { |
| 338 |
t.Errorf("Expected %v, got %v", expected, r) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func TestGlobMatch_middleAsterisks_oneMatch(t *testing.T) { |
| 343 |
p := ParsePattern("/*lue/**/vol?ano", nil) |
| 344 |
r := p.Match([]string{"value", "middle", "volcano"}, false) |
| 345 |
expected := Exclude |
| 346 |
if r != expected { |
| 347 |
t.Errorf("Expected %v, got %v", expected, r) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
func TestGlobMatch_middleAsterisks_multiMatch(t *testing.T) { |
| 352 |
p := ParsePattern("/*lue/**/vol?ano", nil) |
| 353 |
r := p.Match([]string{"value", "middle1", "middle2", "volcano"}, false) |
| 354 |
expected := Exclude |
| 355 |
if r != expected { |
| 356 |
t.Errorf("Expected %v, got %v", expected, r) |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func TestGlobMatch_middleAsterisks_isDir_trailing(t *testing.T) { |
| 361 |
p := ParsePattern("/*lue/**/vol?ano/", nil) |
| 362 |
r := p.Match([]string{"value", "middle1", "middle2", "volcano"}, true) |
| 363 |
expected := Exclude |
| 364 |
if r != expected { |
| 365 |
t.Errorf("Expected %v, got %v", expected, r) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestGlobMatch_middleAsterisks_isDir_trailing_mismatch(t *testing.T) { |
| 370 |
p := ParsePattern("/*lue/**/vol?ano/", nil) |
| 371 |
r := p.Match([]string{"value", "middle1", "middle2", "volcano"}, false) |
| 372 |
expected := NoMatch |
| 373 |
if r != expected { |
| 374 |
t.Errorf("Expected %v, got %v", expected, r) |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
func TestGlobMatch_middleAsterisks_isDir(t *testing.T) { |
| 379 |
p := ParsePattern("/*lue/**/vol?ano/", nil) |
| 380 |
r := p.Match([]string{"value", "middle1", "middle2", "volcano", "tail"}, false) |
| 381 |
expected := Exclude |
| 382 |
if r != expected { |
| 383 |
t.Errorf("Expected %v, got %v", expected, r) |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
func TestGlobMatch_wrongDoubleAsterisk_mismatch(t *testing.T) { |
| 388 |
p := ParsePattern("/*lue/**foo/vol?ano", nil) |
| 389 |
r := p.Match([]string{"value", "foo", "volcano", "tail"}, false) |
| 390 |
expected := NoMatch |
| 391 |
if r != expected { |
| 392 |
t.Errorf("Expected %v, got %v", expected, r) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
func TestGlobMatch_magicChars(t *testing.T) { |
| 397 |
p := ParsePattern("**/head/v[ou]l[kc]ano", nil) |
| 398 |
r := p.Match([]string{"value", "head", "volcano"}, false) |
| 399 |
expected := Exclude |
| 400 |
if r != expected { |
| 401 |
t.Errorf("Expected %v, got %v", expected, r) |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
func TestGlobMatch_wrongPattern_noTraversal_mismatch(t *testing.T) { |
| 406 |
p := ParsePattern("**/head/v[ou]l[", nil) |
| 407 |
r := p.Match([]string{"value", "head", "vol["}, false) |
| 408 |
expected := NoMatch |
| 409 |
if r != expected { |
| 410 |
t.Errorf("Expected %v, got %v", expected, r) |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func TestGlobMatch_wrongPattern_onTraversal_mismatch(t *testing.T) { |
| 415 |
p := ParsePattern("/value/**/v[ou]l[", nil) |
| 416 |
r := p.Match([]string{"value", "head", "vol["}, false) |
| 417 |
expected := NoMatch |
| 418 |
if r != expected { |
| 419 |
t.Errorf("Expected %v, got %v", expected, r) |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
func TestGlobMatch_issue_923(t *testing.T) { |
| 424 |
p := ParsePattern("**/android/**/GeneratedPluginRegistrant.java", nil) |
| 425 |
r := p.Match([]string{"packages", "flutter_tools", "lib", "src", "android", "gradle.dart"}, false) |
| 426 |
expected := NoMatch |
| 427 |
if r != expected { |
| 428 |
t.Errorf("Expected %v, got %v", expected, r) |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
func TestGlobMatch_folderVersusFile(t *testing.T) { |
| 433 |
p := ParsePattern("/a*/**", nil) |
| 434 |
r := p.Match([]string{"ab"}, false) |
| 435 |
expected := NoMatch |
| 436 |
if r != expected { |
| 437 |
t.Errorf("Expected %v, got %v", expected, r) |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
func TestGlobMatch_folderVersusFileAgain(t *testing.T) { |
| 442 |
p := ParsePattern("/a*/**/a*", nil) |
| 443 |
r := p.Match([]string{"ab", "ab"}, false) |
| 444 |
expected := Exclude |
| 445 |
if r != expected { |
| 446 |
t.Errorf("Expected %v, got %v", expected, r) |
| 447 |
} |
| 448 |
} |