3 test_description
='wildmatch tests'
7 # Disable expensive chain-lint tests; all of the tests in this script
8 # are variants of a few trivial test-tool invocations, and there are a lot of
10 GIT_TEST_CHAIN_LINT_HARDER_DEFAULT
=0
12 should_create_test_file
() {
16 # `touch .` will succeed but obviously not do what we intend
21 # We cannot create a file with an empty filename.
25 # The tests that are testing that e.g. foo//bar is matched by
26 # foo/*/bar can't be tested on filesystems since there's no
27 # way we're getting a double slash.
31 # When testing the difference between foo/bar and foo/bar/ we
32 # can't test the latter.
36 # On Windows, \ in paths is silently converted to /, which
37 # would result in the "touch" below working, but the test
38 # itself failing. See 6fd1106aa4 ("t3700: Skip a test with
39 # backslashes in pathspec", 2009-03-13) for prior art and
42 if ! test_have_prereq BSLASHPSPEC
46 # NOTE: The ;;& bash extension is not portable, so
47 # this test needs to be at the end of the pattern
50 # If we want to add more conditional returns we either
51 # need a new case statement, or turn this whole thing
52 # into a series of "if" tests.
57 # On Windows proper (i.e. not Cygwin) many file names which
58 # under Cygwin would be emulated don't work.
59 if test_have_prereq MINGW
63 # Files called " " are forbidden on Windows
66 *\
<*|
*\
>*|
*:*|
*\"*|
*\|
*|
*\?*|
*\
**)
67 # Files with various special characters aren't
68 # allowed on Windows. Sourced from
69 # https://stackoverflow.com/a/31976060
78 match_with_function
() {
84 if test "$match_expect" = 1
86 test_expect_success
"$match_function: match '$text' '$pattern'" "
87 test-tool wildmatch $match_function '$text' '$pattern'
89 elif test "$match_expect" = 0
91 test_expect_success
"$match_function: no match '$text' '$pattern'" "
92 test_must_fail test-tool wildmatch $match_function '$text' '$pattern'
95 test_expect_success
"PANIC: Test framework error. Unknown matches value $match_expect" 'false'
100 match_with_ls_files
() {
107 match_stdout_stderr_cmp
="
108 tr -d '\0' <actual.raw >actual &&
109 test_must_be_empty actual.err &&
110 test_cmp expect actual"
112 if test "$match_expect" = 'E'
114 if test -e .git
/created_test_file
116 test_expect_success EXPENSIVE_ON_WINDOWS
"$match_function (via ls-files): match dies on '$pattern' '$text'" "
117 printf '%s' '$text' >expect &&
118 test_must_fail git$ls_files_args ls-files -z -- '$pattern'
121 test_expect_failure EXPENSIVE_ON_WINDOWS
"$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
123 elif test "$match_expect" = 1
125 if test -e .git
/created_test_file
127 test_expect_success EXPENSIVE_ON_WINDOWS
"$match_function (via ls-files): match '$pattern' '$text'" "
128 printf '%s' '$text' >expect &&
129 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
130 $match_stdout_stderr_cmp
133 test_expect_failure EXPENSIVE_ON_WINDOWS
"$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
135 elif test "$match_expect" = 0
137 if test -e .git
/created_test_file
139 test_expect_success EXPENSIVE_ON_WINDOWS
"$match_function (via ls-files): no match '$pattern' '$text'" "
141 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
142 $match_stdout_stderr_cmp
145 test_expect_failure EXPENSIVE_ON_WINDOWS
"$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
148 test_expect_success
"PANIC: Test framework error. Unknown matches value $match_expect" 'false'
155 # When test-tool wildmatch and git ls-files produce the same
158 match_file_glob
=$match_glob
160 match_file_iglob
=$match_iglob
162 match_file_pathmatch
=$match_pathmatch
164 match_file_pathmatchi
=$match_pathmatchi
175 match_file_pathmatch
=$7
176 match_file_pathmatchi
=$8
181 test_expect_success EXPENSIVE_ON_WINDOWS
'cleanup after previous file test' '
182 if test -e .git/created_test_file
189 printf '%s' "$text" >.git
/expected_test_file
191 test_expect_success EXPENSIVE_ON_WINDOWS
"setup match file test for $text" '
192 file=$(cat .git/expected_test_file) &&
193 if should_create_test_file "$file"
196 if test "$file" != "$dirs"
198 mkdir -p -- "$dirs" &&
204 printf "%s" "$file" >.git/created_test_file
205 elif test -e .git/created_test_file
207 rm .git/created_test_file
211 # $1: Case sensitive glob match: test-tool wildmatch & ls-files
212 match_with_function
"$text" "$pattern" $match_glob "wildmatch"
213 match_with_ls_files
"$text" "$pattern" $match_file_glob "wildmatch" " --glob-pathspecs"
215 # $2: Case insensitive glob match: test-tool wildmatch & ls-files
216 match_with_function
"$text" "$pattern" $match_iglob "iwildmatch"
217 match_with_ls_files
"$text" "$pattern" $match_file_iglob "iwildmatch" " --glob-pathspecs --icase-pathspecs"
219 # $3: Case sensitive path match: test-tool wildmatch & ls-files
220 match_with_function
"$text" "$pattern" $match_pathmatch "pathmatch"
221 match_with_ls_files
"$text" "$pattern" $match_file_pathmatch "pathmatch" ""
223 # $4: Case insensitive path match: test-tool wildmatch & ls-files
224 match_with_function
"$text" "$pattern" $match_pathmatchi "ipathmatch"
225 match_with_ls_files
"$text" "$pattern" $match_file_pathmatchi "ipathmatch" " --icase-pathspecs"
228 # Basic wildmatch features
229 match
1 1 1 1 foo foo
230 match
0 0 0 0 foo bar
232 match
1 1 1 1 foo
'???'
233 match
0 0 0 0 foo
'??'
234 match
1 1 1 1 foo
'*'
235 match
1 1 1 1 foo
'f*'
236 match
0 0 0 0 foo
'*f'
237 match
1 1 1 1 foo
'*foo*'
238 match
1 1 1 1 foobar
'*ob*a*r*'
239 match
1 1 1 1 aaaaaaabababab
'*ab'
240 match
1 1 1 1 'foo*' 'foo\*'
241 match
0 0 0 0 foobar
'foo\*bar'
242 match
1 1 1 1 'f\oo' 'f\\oo'
243 match
1 1 1 1 ball
'*[al]?'
244 match
0 0 0 0 ten
'[ten]'
245 match
1 1 1 1 ten
'**[!te]'
246 match
0 0 0 0 ten
'**[!ten]'
247 match
1 1 1 1 ten
't[a-g]n'
248 match
0 0 0 0 ten
't[!a-g]n'
249 match
1 1 1 1 ton
't[!a-g]n'
250 match
1 1 1 1 ton
't[^a-g]n'
251 match
1 1 1 1 'a]b' 'a[]]b'
252 match
1 1 1 1 a-b
'a[]-]b'
253 match
1 1 1 1 'a]b' 'a[]-]b'
254 match
0 0 0 0 aab
'a[]-]b'
255 match
1 1 1 1 aab
'a[]a-]b'
256 match
1 1 1 1 ']' ']'
258 # Extended slash-matching features
259 match
0 0 1 1 'foo/baz/bar' 'foo*bar'
260 match
0 0 1 1 'foo/baz/bar' 'foo**bar'
261 match
1 1 1 1 'foobazbar' 'foo**bar'
262 match
1 1 1 1 'foo/baz/bar' 'foo/**/bar'
263 match
1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
264 match
1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
265 match
1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
266 match
1 1 0 0 'foo/bar' 'foo/**/bar'
267 match
1 1 0 0 'foo/bar' 'foo/**/**/bar'
268 match
0 0 1 1 'foo/bar' 'foo?bar'
269 match
0 0 1 1 'foo/bar' 'foo[/]bar'
270 match
0 0 1 1 'foo/bar' 'foo[^a-z]bar'
271 match
0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
272 match
1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
273 match
1 1 0 0 'foo' '**/foo'
274 match
1 1 1 1 'XXX/foo' '**/foo'
275 match
1 1 1 1 'bar/baz/foo' '**/foo'
276 match
0 0 1 1 'bar/baz/foo' '*/foo'
277 match
0 0 1 1 'foo/bar/baz' '**/bar*'
278 match
1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
279 match
0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
280 match
1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
281 match
0 0 0 0 'deep/foo/bar' '**/bar/*'
282 match
1 1 1 1 'deep/foo/bar/' '**/bar/**'
283 match
0 0 1 1 'foo/bar/baz' '**/bar**'
284 match
1 1 1 1 'foo/bar/baz/x' '*/bar/**'
285 match
0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
286 match
1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
288 # Various additional tests
289 match
0 0 0 0 'acrt' 'a[c-c]st'
290 match
1 1 1 1 'acrt' 'a[c-c]rt'
291 match
0 0 0 0 ']' '[!]-]'
292 match
1 1 1 1 'a' '[!]-]'
296 match 0 0 0 0 'XXX
/\' '*/\'
297 match 1 1 1 1 'XXX
/\' '*/\\'
298 match
1 1 1 1 'foo' 'foo'
299 match
1 1 1 1 '@foo' '@foo'
300 match
0 0 0 0 'foo' '@foo'
301 match
1 1 1 1 '[ab]' '\[ab]'
302 match
1 1 1 1 '[ab]' '[[]ab]'
303 match
1 1 1 1 '[ab]' '[[:]ab]'
304 match
0 0 0 0 '[ab]' '[[::]ab]'
305 match
1 1 1 1 '[ab]' '[[:digit]ab]'
306 match
1 1 1 1 '[ab]' '[\[:]ab]'
307 match
1 1 1 1 '?a?b' '\??\?b'
308 match
1 1 1 1 'abc' '\a\b\c'
311 match
1 1 1 1 'foo/bar/baz/to' '**/t[o]'
313 # Character class tests
314 match
1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
315 match
0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
316 match
1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
317 match
1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
318 match
0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
319 match
1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
320 match
0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
321 match
1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
322 match
1 1 1 1 '5' '[[:xdigit:]]'
323 match
1 1 1 1 'f' '[[:xdigit:]]'
324 match
1 1 1 1 'D' '[[:xdigit:]]'
325 match
1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
326 match
1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
327 match
1 1 1 1 '5' '[a-c[:digit:]x-z]'
328 match
1 1 1 1 'b' '[a-c[:digit:]x-z]'
329 match
1 1 1 1 'y' '[a-c[:digit:]x-z]'
330 match
0 0 0 0 'q' '[a-c[:digit:]x-z]'
332 # Additional tests, including some malformed wildmatch patterns
333 match
1 1 1 1 ']' '[\\-^]'
334 match
0 0 0 0 '[' '[\\-^]'
335 match
1 1 1 1 '-' '[\-_]'
336 match
1 1 1 1 ']' '[\]]'
337 match
0 0 0 0 '\]' '[\]]'
338 match
0 0 0 0 '\' '[\
]]'
339 match 0 0 0 0 'ab
' 'a
[]b
'
341 1 1 1 1 'a
[]b
' 'a
[]b
'
344 match 0 0 0 0 'ab
' '[!'
345 match 0 0 0 0 'ab
' '[-'
346 match 1 1 1 1 '-' '[-]'
347 match 0 0 0 0 '-' '[a-
'
348 match 0 0 0 0 '-' '[!a-
'
349 match 1 1 1 1 '-' '[--A]'
350 match 1 1 1 1 '5' '[--A]'
351 match 1 1 1 1 ' ' '[ --]'
352 match 1 1 1 1 '$
' '[ --]'
353 match 1 1 1 1 '-' '[ --]'
354 match 0 0 0 0 '0' '[ --]'
355 match 1 1 1 1 '-' '[---]'
356 match 1 1 1 1 '-' '[------]'
357 match 0 0 0 0 'j
' '[a-e-n
]'
358 match 1 1 1 1 '-' '[a-e-n
]'
359 match 1 1 1 1 'a
' '[!------]'
360 match 0 0 0 0 '[' '[]-a]'
361 match 1 1 1 1 '^
' '[]-a]'
362 match 0 0 0 0 '^
' '[!]-a]'
363 match 1 1 1 1 '[' '[!]-a]'
364 match 1 1 1 1 '^
' '[a^
bc]'
365 match 1 1 1 1 '-b]' '[a-
]b
]'
366 match 0 0 0 0 '\' '[\]'
367 match
1 1 1 1 '\' '[\\]'
368 match 0 0 0 0 '\' '[!\\]'
369 match
1 1 1 1 'G' '[A-\\]'
370 match
0 0 0 0 'aaabbb' 'b*a'
371 match
0 0 0 0 'aabcaa' '*ba*'
372 match
1 1 1 1 ',' '[,]'
373 match
1 1 1 1 ',' '[\\,]'
374 match
1 1 1 1 '\' '[\\,]'
375 match 1 1 1 1 '-' '[,-.
]'
376 match 0 0 0 0 '+' '[,-.
]'
377 match 0 0 0 0 '-.
]' '[,-.
]'
378 match 1 1 1 1 '2' '[\
1-\
3]'
379 match 1 1 1 1 '3' '[\
1-\
3]'
380 match 0 0 0 0 '4' '[\
1-\
3]'
381 match 1 1 1 1 '\' '[[-\]]'
382 match
1 1 1 1 '[' '[[-\]]'
383 match
1 1 1 1 ']' '[[-\]]'
384 match
0 0 0 0 '-' '[[-\]]'
387 match
1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
388 match
0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
389 match
0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
390 match
1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
391 match
0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
392 match
1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
393 match
0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
394 match
0 0 0 0 foo
'*/*/*'
395 match
0 0 0 0 foo
/bar
'*/*/*'
396 match
1 1 1 1 foo
/bba
/arr
'*/*/*'
397 match
0 0 1 1 foo
/bb
/aa
/rr
'*/*/*'
398 match
1 1 1 1 foo
/bb
/aa
/rr
'**/**/**'
399 match
1 1 1 1 abcXdefXghi
'*X*i'
400 match
0 0 1 1 ab
/cXd
/efXg
/hi
'*X*i'
401 match
1 1 1 1 ab
/cXd
/efXg
/hi
'*/*X*/*/*i'
402 match
1 1 1 1 ab
/cXd
/efXg
/hi
'**/*X*/**/*i'
404 # Extra pathmatch tests
406 match
1 1 1 1 foo
/bar foo
/bar
407 match
1 1 1 1 foo
/bar
'foo/*'
408 match
0 0 1 1 foo
/bba
/arr
'foo/*'
409 match
1 1 1 1 foo
/bba
/arr
'foo/**'
410 match
0 0 1 1 foo
/bba
/arr
'foo*'
412 1 1 1 1 foo
/bba
/arr
'foo**'
413 match
0 0 1 1 foo
/bba
/arr
'foo/*arr'
414 match
0 0 1 1 foo
/bba
/arr
'foo/**arr'
415 match
0 0 0 0 foo
/bba
/arr
'foo/*z'
416 match
0 0 0 0 foo
/bba
/arr
'foo/**z'
417 match
0 0 1 1 foo
/bar
'foo?bar'
418 match
0 0 1 1 foo
/bar
'foo[/]bar'
419 match
0 0 1 1 foo
/bar
'foo[^a-z]bar'
420 match
0 0 1 1 ab
/cXd
/efXg
/hi
'*Xg*i'
422 # Extra case-sensitivity tests
423 match
0 1 0 1 'a' '[A-Z]'
424 match
1 1 1 1 'A' '[A-Z]'
425 match
0 1 0 1 'A' '[a-z]'
426 match
1 1 1 1 'a' '[a-z]'
427 match
0 1 0 1 'a' '[[:upper:]]'
428 match
1 1 1 1 'A' '[[:upper:]]'
429 match
0 1 0 1 'A' '[[:lower:]]'
430 match
1 1 1 1 'a' '[[:lower:]]'
431 match
0 1 0 1 'A' '[B-Za]'
432 match
1 1 1 1 'a' '[B-Za]'
433 match
0 1 0 1 'A' '[B-a]'
434 match
1 1 1 1 'a' '[B-a]'
435 match
0 1 0 1 'z' '[Z-y]'
436 match
1 1 1 1 'Z' '[Z-y]'