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