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