Merge branch 'os/fetch-check-not-current-branch'
[git/debian.git] / t / t3070-wildmatch.sh
blobf9539968e4c16a53e2d0add8b448c7338925828f
1 #!/bin/sh
3 test_description='wildmatch tests'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 # Disable expensive chain-lint tests; all of the tests in this script
9 # are variants of a few trivial test-tool invocations, and there are a lot of
10 # them.
11 GIT_TEST_CHAIN_LINT_HARDER_DEFAULT=0
13 should_create_test_file() {
14 file=$1
16 case $file in
17 # `touch .` will succeed but obviously not do what we intend
18 # here.
19 ".")
20 return 1
22 # We cannot create a file with an empty filename.
23 "")
24 return 1
26 # The tests that are testing that e.g. foo//bar is matched by
27 # foo/*/bar can't be tested on filesystems since there's no
28 # way we're getting a double slash.
29 *//*)
30 return 1
32 # When testing the difference between foo/bar and foo/bar/ we
33 # can't test the latter.
34 */)
35 return 1
37 # On Windows, \ in paths is silently converted to /, which
38 # would result in the "touch" below working, but the test
39 # itself failing. See 6fd1106aa4 ("t3700: Skip a test with
40 # backslashes in pathspec", 2009-03-13) for prior art and
41 # details.
42 *\\*)
43 if ! test_have_prereq BSLASHPSPEC
44 then
45 return 1
47 # NOTE: The ;;& bash extension is not portable, so
48 # this test needs to be at the end of the pattern
49 # list.
51 # If we want to add more conditional returns we either
52 # need a new case statement, or turn this whole thing
53 # into a series of "if" tests.
55 esac
58 # On Windows proper (i.e. not Cygwin) many file names which
59 # under Cygwin would be emulated don't work.
60 if test_have_prereq MINGW
61 then
62 case $file in
63 " ")
64 # Files called " " are forbidden on Windows
65 return 1
67 *\<*|*\>*|*:*|*\"*|*\|*|*\?*|*\**)
68 # Files with various special characters aren't
69 # allowed on Windows. Sourced from
70 # https://stackoverflow.com/a/31976060
71 return 1
73 esac
76 return 0
79 match_with_function() {
80 text=$1
81 pattern=$2
82 match_expect=$3
83 match_function=$4
85 if test "$match_expect" = 1
86 then
87 test_expect_success "$match_function: match '$text' '$pattern'" "
88 test-tool wildmatch $match_function '$text' '$pattern'
90 elif test "$match_expect" = 0
91 then
92 test_expect_success "$match_function: no match '$text' '$pattern'" "
93 test_must_fail test-tool wildmatch $match_function '$text' '$pattern'
95 else
96 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
101 match_with_ls_files() {
102 text=$1
103 pattern=$2
104 match_expect=$3
105 match_function=$4
106 ls_files_args=$5
108 match_stdout_stderr_cmp="
109 tr -d '\0' <actual.raw >actual &&
110 test_must_be_empty actual.err &&
111 test_cmp expect actual"
113 if test "$match_expect" = 'E'
114 then
115 if test -e .git/created_test_file
116 then
117 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match dies on '$pattern' '$text'" "
118 printf '%s' '$text' >expect &&
119 test_must_fail git$ls_files_args ls-files -z -- '$pattern'
121 else
122 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
124 elif test "$match_expect" = 1
125 then
126 if test -e .git/created_test_file
127 then
128 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match '$pattern' '$text'" "
129 printf '%s' '$text' >expect &&
130 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
131 $match_stdout_stderr_cmp
133 else
134 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
136 elif test "$match_expect" = 0
137 then
138 if test -e .git/created_test_file
139 then
140 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match '$pattern' '$text'" "
141 >expect &&
142 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
143 $match_stdout_stderr_cmp
145 else
146 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
148 else
149 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
153 match() {
154 if test "$#" = 6
155 then
156 # When test-tool wildmatch and git ls-files produce the same
157 # result.
158 match_glob=$1
159 match_file_glob=$match_glob
160 match_iglob=$2
161 match_file_iglob=$match_iglob
162 match_pathmatch=$3
163 match_file_pathmatch=$match_pathmatch
164 match_pathmatchi=$4
165 match_file_pathmatchi=$match_pathmatchi
166 text=$5
167 pattern=$6
168 elif test "$#" = 10
169 then
170 match_glob=$1
171 match_iglob=$2
172 match_pathmatch=$3
173 match_pathmatchi=$4
174 match_file_glob=$5
175 match_file_iglob=$6
176 match_file_pathmatch=$7
177 match_file_pathmatchi=$8
178 text=$9
179 pattern=${10}
182 test_expect_success EXPENSIVE_ON_WINDOWS 'cleanup after previous file test' '
183 if test -e .git/created_test_file
184 then
185 git reset &&
186 git clean -df
190 printf '%s' "$text" >.git/expected_test_file
192 test_expect_success EXPENSIVE_ON_WINDOWS "setup match file test for $text" '
193 file=$(cat .git/expected_test_file) &&
194 if should_create_test_file "$file"
195 then
196 dirs=${file%/*} &&
197 if test "$file" != "$dirs"
198 then
199 mkdir -p -- "$dirs" &&
200 touch -- "./$text"
201 else
202 touch -- "./$file"
203 fi &&
204 git add -A &&
205 printf "%s" "$file" >.git/created_test_file
206 elif test -e .git/created_test_file
207 then
208 rm .git/created_test_file
212 # $1: Case sensitive glob match: test-tool wildmatch & ls-files
213 match_with_function "$text" "$pattern" $match_glob "wildmatch"
214 match_with_ls_files "$text" "$pattern" $match_file_glob "wildmatch" " --glob-pathspecs"
216 # $2: Case insensitive glob match: test-tool wildmatch & ls-files
217 match_with_function "$text" "$pattern" $match_iglob "iwildmatch"
218 match_with_ls_files "$text" "$pattern" $match_file_iglob "iwildmatch" " --glob-pathspecs --icase-pathspecs"
220 # $3: Case sensitive path match: test-tool wildmatch & ls-files
221 match_with_function "$text" "$pattern" $match_pathmatch "pathmatch"
222 match_with_ls_files "$text" "$pattern" $match_file_pathmatch "pathmatch" ""
224 # $4: Case insensitive path match: test-tool wildmatch & ls-files
225 match_with_function "$text" "$pattern" $match_pathmatchi "ipathmatch"
226 match_with_ls_files "$text" "$pattern" $match_file_pathmatchi "ipathmatch" " --icase-pathspecs"
229 # Basic wildmatch features
230 match 1 1 1 1 foo foo
231 match 0 0 0 0 foo bar
232 match 1 1 1 1 '' ""
233 match 1 1 1 1 foo '???'
234 match 0 0 0 0 foo '??'
235 match 1 1 1 1 foo '*'
236 match 1 1 1 1 foo 'f*'
237 match 0 0 0 0 foo '*f'
238 match 1 1 1 1 foo '*foo*'
239 match 1 1 1 1 foobar '*ob*a*r*'
240 match 1 1 1 1 aaaaaaabababab '*ab'
241 match 1 1 1 1 'foo*' 'foo\*'
242 match 0 0 0 0 foobar 'foo\*bar'
243 match 1 1 1 1 'f\oo' 'f\\oo'
244 match 1 1 1 1 ball '*[al]?'
245 match 0 0 0 0 ten '[ten]'
246 match 1 1 1 1 ten '**[!te]'
247 match 0 0 0 0 ten '**[!ten]'
248 match 1 1 1 1 ten 't[a-g]n'
249 match 0 0 0 0 ten 't[!a-g]n'
250 match 1 1 1 1 ton 't[!a-g]n'
251 match 1 1 1 1 ton 't[^a-g]n'
252 match 1 1 1 1 'a]b' 'a[]]b'
253 match 1 1 1 1 a-b 'a[]-]b'
254 match 1 1 1 1 'a]b' 'a[]-]b'
255 match 0 0 0 0 aab 'a[]-]b'
256 match 1 1 1 1 aab 'a[]a-]b'
257 match 1 1 1 1 ']' ']'
259 # Extended slash-matching features
260 match 0 0 1 1 'foo/baz/bar' 'foo*bar'
261 match 0 0 1 1 'foo/baz/bar' 'foo**bar'
262 match 1 1 1 1 'foobazbar' 'foo**bar'
263 match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
264 match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
265 match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
266 match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
267 match 1 1 0 0 'foo/bar' 'foo/**/bar'
268 match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
269 match 0 0 1 1 'foo/bar' 'foo?bar'
270 match 0 0 1 1 'foo/bar' 'foo[/]bar'
271 match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
272 match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
273 match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
274 match 1 1 0 0 'foo' '**/foo'
275 match 1 1 1 1 'XXX/foo' '**/foo'
276 match 1 1 1 1 'bar/baz/foo' '**/foo'
277 match 0 0 1 1 'bar/baz/foo' '*/foo'
278 match 0 0 1 1 'foo/bar/baz' '**/bar*'
279 match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
280 match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
281 match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
282 match 0 0 0 0 'deep/foo/bar' '**/bar/*'
283 match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
284 match 0 0 1 1 'foo/bar/baz' '**/bar**'
285 match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
286 match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
287 match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
289 # Various additional tests
290 match 0 0 0 0 'acrt' 'a[c-c]st'
291 match 1 1 1 1 'acrt' 'a[c-c]rt'
292 match 0 0 0 0 ']' '[!]-]'
293 match 1 1 1 1 'a' '[!]-]'
294 match 0 0 0 0 '' '\'
295 match 0 0 0 0 \
296 1 1 1 1 '\' '\'
297 match 0 0 0 0 'XXX/\' '*/\'
298 match 1 1 1 1 'XXX/\' '*/\\'
299 match 1 1 1 1 'foo' 'foo'
300 match 1 1 1 1 '@foo' '@foo'
301 match 0 0 0 0 'foo' '@foo'
302 match 1 1 1 1 '[ab]' '\[ab]'
303 match 1 1 1 1 '[ab]' '[[]ab]'
304 match 1 1 1 1 '[ab]' '[[:]ab]'
305 match 0 0 0 0 '[ab]' '[[::]ab]'
306 match 1 1 1 1 '[ab]' '[[:digit]ab]'
307 match 1 1 1 1 '[ab]' '[\[:]ab]'
308 match 1 1 1 1 '?a?b' '\??\?b'
309 match 1 1 1 1 'abc' '\a\b\c'
310 match 0 0 0 0 \
311 E E E E 'foo' ''
312 match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
314 # Character class tests
315 match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
316 match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
317 match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
318 match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
319 match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
320 match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
321 match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
322 match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
323 match 1 1 1 1 '5' '[[:xdigit:]]'
324 match 1 1 1 1 'f' '[[:xdigit:]]'
325 match 1 1 1 1 'D' '[[:xdigit:]]'
326 match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
327 match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
328 match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
329 match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
330 match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
331 match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
333 # Additional tests, including some malformed wildmatch patterns
334 match 1 1 1 1 ']' '[\\-^]'
335 match 0 0 0 0 '[' '[\\-^]'
336 match 1 1 1 1 '-' '[\-_]'
337 match 1 1 1 1 ']' '[\]]'
338 match 0 0 0 0 '\]' '[\]]'
339 match 0 0 0 0 '\' '[\]]'
340 match 0 0 0 0 'ab' 'a[]b'
341 match 0 0 0 0 \
342 1 1 1 1 'a[]b' 'a[]b'
343 match 0 0 0 0 \
344 1 1 1 1 'ab[' 'ab['
345 match 0 0 0 0 'ab' '[!'
346 match 0 0 0 0 'ab' '[-'
347 match 1 1 1 1 '-' '[-]'
348 match 0 0 0 0 '-' '[a-'
349 match 0 0 0 0 '-' '[!a-'
350 match 1 1 1 1 '-' '[--A]'
351 match 1 1 1 1 '5' '[--A]'
352 match 1 1 1 1 ' ' '[ --]'
353 match 1 1 1 1 '$' '[ --]'
354 match 1 1 1 1 '-' '[ --]'
355 match 0 0 0 0 '0' '[ --]'
356 match 1 1 1 1 '-' '[---]'
357 match 1 1 1 1 '-' '[------]'
358 match 0 0 0 0 'j' '[a-e-n]'
359 match 1 1 1 1 '-' '[a-e-n]'
360 match 1 1 1 1 'a' '[!------]'
361 match 0 0 0 0 '[' '[]-a]'
362 match 1 1 1 1 '^' '[]-a]'
363 match 0 0 0 0 '^' '[!]-a]'
364 match 1 1 1 1 '[' '[!]-a]'
365 match 1 1 1 1 '^' '[a^bc]'
366 match 1 1 1 1 '-b]' '[a-]b]'
367 match 0 0 0 0 '\' '[\]'
368 match 1 1 1 1 '\' '[\\]'
369 match 0 0 0 0 '\' '[!\\]'
370 match 1 1 1 1 'G' '[A-\\]'
371 match 0 0 0 0 'aaabbb' 'b*a'
372 match 0 0 0 0 'aabcaa' '*ba*'
373 match 1 1 1 1 ',' '[,]'
374 match 1 1 1 1 ',' '[\\,]'
375 match 1 1 1 1 '\' '[\\,]'
376 match 1 1 1 1 '-' '[,-.]'
377 match 0 0 0 0 '+' '[,-.]'
378 match 0 0 0 0 '-.]' '[,-.]'
379 match 1 1 1 1 '2' '[\1-\3]'
380 match 1 1 1 1 '3' '[\1-\3]'
381 match 0 0 0 0 '4' '[\1-\3]'
382 match 1 1 1 1 '\' '[[-\]]'
383 match 1 1 1 1 '[' '[[-\]]'
384 match 1 1 1 1 ']' '[[-\]]'
385 match 0 0 0 0 '-' '[[-\]]'
387 # Test recursion
388 match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
389 match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
390 match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
391 match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
392 match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
393 match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
394 match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
395 match 0 0 0 0 foo '*/*/*'
396 match 0 0 0 0 foo/bar '*/*/*'
397 match 1 1 1 1 foo/bba/arr '*/*/*'
398 match 0 0 1 1 foo/bb/aa/rr '*/*/*'
399 match 1 1 1 1 foo/bb/aa/rr '**/**/**'
400 match 1 1 1 1 abcXdefXghi '*X*i'
401 match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
402 match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
403 match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
405 # Extra pathmatch tests
406 match 0 0 0 0 foo fo
407 match 1 1 1 1 foo/bar foo/bar
408 match 1 1 1 1 foo/bar 'foo/*'
409 match 0 0 1 1 foo/bba/arr 'foo/*'
410 match 1 1 1 1 foo/bba/arr 'foo/**'
411 match 0 0 1 1 foo/bba/arr 'foo*'
412 match 0 0 1 1 \
413 1 1 1 1 foo/bba/arr 'foo**'
414 match 0 0 1 1 foo/bba/arr 'foo/*arr'
415 match 0 0 1 1 foo/bba/arr 'foo/**arr'
416 match 0 0 0 0 foo/bba/arr 'foo/*z'
417 match 0 0 0 0 foo/bba/arr 'foo/**z'
418 match 0 0 1 1 foo/bar 'foo?bar'
419 match 0 0 1 1 foo/bar 'foo[/]bar'
420 match 0 0 1 1 foo/bar 'foo[^a-z]bar'
421 match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
423 # Extra case-sensitivity tests
424 match 0 1 0 1 'a' '[A-Z]'
425 match 1 1 1 1 'A' '[A-Z]'
426 match 0 1 0 1 'A' '[a-z]'
427 match 1 1 1 1 'a' '[a-z]'
428 match 0 1 0 1 'a' '[[:upper:]]'
429 match 1 1 1 1 'A' '[[:upper:]]'
430 match 0 1 0 1 'A' '[[:lower:]]'
431 match 1 1 1 1 'a' '[[:lower:]]'
432 match 0 1 0 1 'A' '[B-Za]'
433 match 1 1 1 1 'a' '[B-Za]'
434 match 0 1 0 1 'A' '[B-a]'
435 match 1 1 1 1 'a' '[B-a]'
436 match 0 1 0 1 'z' '[Z-y]'
437 match 1 1 1 1 'Z' '[Z-y]'
439 test_done