unpack-trees: resolve sparse-directory/file conflicts
[git/debian.git] / t / t3070-wildmatch.sh
blob56ea4bda133d7443ba0d67c082d157cd491bd9cf
1 #!/bin/sh
3 test_description='wildmatch tests'
5 . ./test-lib.sh
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
9 # them.
10 GIT_TEST_CHAIN_LINT_HARDER_DEFAULT=0
12 should_create_test_file() {
13 file=$1
15 case $file in
16 # `touch .` will succeed but obviously not do what we intend
17 # here.
18 ".")
19 return 1
21 # We cannot create a file with an empty filename.
22 "")
23 return 1
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.
28 *//*)
29 return 1
31 # When testing the difference between foo/bar and foo/bar/ we
32 # can't test the latter.
33 */)
34 return 1
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
40 # details.
41 *\\*)
42 if ! test_have_prereq BSLASHPSPEC
43 then
44 return 1
46 # NOTE: The ;;& bash extension is not portable, so
47 # this test needs to be at the end of the pattern
48 # list.
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.
54 esac
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
60 then
61 case $file in
62 " ")
63 # Files called " " are forbidden on Windows
64 return 1
66 *\<*|*\>*|*:*|*\"*|*\|*|*\?*|*\**)
67 # Files with various special characters aren't
68 # allowed on Windows. Sourced from
69 # https://stackoverflow.com/a/31976060
70 return 1
72 esac
75 return 0
78 match_with_function() {
79 text=$1
80 pattern=$2
81 match_expect=$3
82 match_function=$4
84 if test "$match_expect" = 1
85 then
86 test_expect_success "$match_function: match '$text' '$pattern'" "
87 test-tool wildmatch $match_function '$text' '$pattern'
89 elif test "$match_expect" = 0
90 then
91 test_expect_success "$match_function: no match '$text' '$pattern'" "
92 test_must_fail test-tool wildmatch $match_function '$text' '$pattern'
94 else
95 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
100 match_with_ls_files() {
101 text=$1
102 pattern=$2
103 match_expect=$3
104 match_function=$4
105 ls_files_args=$5
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'
113 then
114 if test -e .git/created_test_file
115 then
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'
120 else
121 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
123 elif test "$match_expect" = 1
124 then
125 if test -e .git/created_test_file
126 then
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
132 else
133 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
135 elif test "$match_expect" = 0
136 then
137 if test -e .git/created_test_file
138 then
139 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match '$pattern' '$text'" "
140 >expect &&
141 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
142 $match_stdout_stderr_cmp
144 else
145 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
147 else
148 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
152 match() {
153 if test "$#" = 6
154 then
155 # When test-tool wildmatch and git ls-files produce the same
156 # result.
157 match_glob=$1
158 match_file_glob=$match_glob
159 match_iglob=$2
160 match_file_iglob=$match_iglob
161 match_pathmatch=$3
162 match_file_pathmatch=$match_pathmatch
163 match_pathmatchi=$4
164 match_file_pathmatchi=$match_pathmatchi
165 text=$5
166 pattern=$6
167 elif test "$#" = 10
168 then
169 match_glob=$1
170 match_iglob=$2
171 match_pathmatch=$3
172 match_pathmatchi=$4
173 match_file_glob=$5
174 match_file_iglob=$6
175 match_file_pathmatch=$7
176 match_file_pathmatchi=$8
177 text=$9
178 pattern=${10}
181 test_expect_success EXPENSIVE_ON_WINDOWS 'cleanup after previous file test' '
182 if test -e .git/created_test_file
183 then
184 git reset &&
185 git clean -df
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"
194 then
195 dirs=${file%/*}
196 if test "$file" != "$dirs"
197 then
198 mkdir -p -- "$dirs" &&
199 touch -- "./$text"
200 else
201 touch -- "./$file"
202 fi &&
203 git add -A &&
204 printf "%s" "$file" >.git/created_test_file
205 elif test -e .git/created_test_file
206 then
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
231 match 1 1 1 1 '' ""
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' '[!]-]'
293 match 0 0 0 0 '' '\'
294 match 0 0 0 0 \
295 1 1 1 1 '\' '\'
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'
309 match 0 0 0 0 \
310 E E E E 'foo' ''
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'
340 match 0 0 0 0 \
341 1 1 1 1 'a[]b' 'a[]b'
342 match 0 0 0 0 \
343 1 1 1 1 'ab[' 'ab['
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 '-' '[[-\]]'
386 # Test recursion
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
405 match 0 0 0 0 foo fo
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*'
411 match 0 0 1 1 \
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]'
438 test_done