]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3070-wildmatch.sh
wildmatch: change behavior of "foo**bar" in WM_PATHNAME mode
[thirdparty/git.git] / t / t3070-wildmatch.sh
CommitLineData
feabcc17
NTND
1#!/bin/sh
2
3test_description='wildmatch tests'
4
5. ./test-lib.sh
6
de8bada2
ÆAB
7should_create_test_file() {
8 file=$1
9
10 case $file in
11 # `touch .` will succeed but obviously not do what we intend
12 # here.
13 ".")
14 return 1
15 ;;
16 # We cannot create a file with an empty filename.
17 "")
18 return 1
19 ;;
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
25 ;;
26 # When testing the difference between foo/bar and foo/bar/ we
27 # can't test the latter.
28 */)
29 return 1
30 ;;
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
40 fi
41 # NOTE: The ;;& bash extension is not portable, so
42 # this test needs to be at the end of the pattern
43 # list.
44 #
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.
48 ;;
49 esac
50
51
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
60 ;;
61 *\<*|*\>*|*:*|*\"*|*\|*|*\?*|*\**)
62 # Files with various special characters aren't
63 # allowed on Windows. Sourced from
64 # https://stackoverflow.com/a/31976060
65 return 1
66 ;;
67 esac
68 fi
69
70 return 0
71}
72
91061c44
ÆAB
73match_with_function() {
74 text=$1
75 pattern=$2
76 match_expect=$3
77 match_function=$4
feabcc17 78
91061c44 79 if test "$match_expect" = 1
5008ba8c 80 then
91061c44 81 test_expect_success "$match_function: match '$text' '$pattern'" "
0489289d 82 test-tool wildmatch $match_function '$text' '$pattern'
a4a136f5 83 "
91061c44 84 elif test "$match_expect" = 0
5684c2bc 85 then
91061c44 86 test_expect_success "$match_function: no match '$text' '$pattern'" "
0489289d 87 test_must_fail test-tool wildmatch $match_function '$text' '$pattern'
a4a136f5 88 "
5684c2bc 89 else
91061c44 90 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
a4a136f5 91 fi
91061c44 92
b79c0c37
AR
93}
94
de8bada2
ÆAB
95match_with_ls_files() {
96 text=$1
97 pattern=$2
98 match_expect=$3
99 match_function=$4
100 ls_files_args=$5
101
102 match_stdout_stderr_cmp="
103 tr -d '\0' <actual.raw >actual &&
104 >expect.err &&
105 test_cmp expect.err actual.err &&
106 test_cmp expect actual"
107
108 if test "$match_expect" = 'E'
109 then
110 if test -e .git/created_test_file
111 then
8725923b 112 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match dies on '$pattern' '$text'" "
de8bada2
ÆAB
113 printf '%s' '$text' >expect &&
114 test_must_fail git$ls_files_args ls-files -z -- '$pattern'
115 "
116 else
8725923b 117 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
de8bada2
ÆAB
118 fi
119 elif test "$match_expect" = 1
120 then
121 if test -e .git/created_test_file
122 then
8725923b 123 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match '$pattern' '$text'" "
de8bada2
ÆAB
124 printf '%s' '$text' >expect &&
125 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
126 $match_stdout_stderr_cmp
127 "
128 else
8725923b 129 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
de8bada2
ÆAB
130 fi
131 elif test "$match_expect" = 0
132 then
133 if test -e .git/created_test_file
134 then
8725923b 135 test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match '$pattern' '$text'" "
de8bada2
ÆAB
136 >expect &&
137 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
138 $match_stdout_stderr_cmp
139 "
140 else
8725923b 141 test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
de8bada2
ÆAB
142 fi
143 else
144 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
145 fi
146}
147
91061c44 148match() {
de8bada2
ÆAB
149 if test "$#" = 6
150 then
0489289d 151 # When test-tool wildmatch and git ls-files produce the same
de8bada2
ÆAB
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}
175 fi
176
8725923b 177 test_expect_success EXPENSIVE_ON_WINDOWS 'cleanup after previous file test' '
de8bada2
ÆAB
178 if test -e .git/created_test_file
179 then
180 git reset &&
181 git clean -df
182 fi
183 '
184
185 printf '%s' "$text" >.git/expected_test_file
186
8725923b 187 test_expect_success EXPENSIVE_ON_WINDOWS "setup match file test for $text" '
de8bada2
ÆAB
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
204 fi
205 '
91061c44 206
0489289d 207 # $1: Case sensitive glob match: test-tool wildmatch & ls-files
91061c44 208 match_with_function "$text" "$pattern" $match_glob "wildmatch"
de8bada2 209 match_with_ls_files "$text" "$pattern" $match_file_glob "wildmatch" " --glob-pathspecs"
91061c44 210
0489289d 211 # $2: Case insensitive glob match: test-tool wildmatch & ls-files
91061c44 212 match_with_function "$text" "$pattern" $match_iglob "iwildmatch"
de8bada2 213 match_with_ls_files "$text" "$pattern" $match_file_iglob "iwildmatch" " --glob-pathspecs --icase-pathspecs"
91061c44 214
0489289d 215 # $3: Case sensitive path match: test-tool wildmatch & ls-files
91061c44 216 match_with_function "$text" "$pattern" $match_pathmatch "pathmatch"
de8bada2 217 match_with_ls_files "$text" "$pattern" $match_file_pathmatch "pathmatch" ""
91061c44 218
0489289d 219 # $4: Case insensitive path match: test-tool wildmatch & ls-files
91061c44 220 match_with_function "$text" "$pattern" $match_pathmatchi "ipathmatch"
de8bada2 221 match_with_ls_files "$text" "$pattern" $match_file_pathmatchi "ipathmatch" " --icase-pathspecs"
c41244e7
NTND
222}
223
91061c44
ÆAB
224# Basic wildmatch features
225match 1 1 1 1 foo foo
226match 0 0 0 0 foo bar
227match 1 1 1 1 '' ""
228match 1 1 1 1 foo '???'
229match 0 0 0 0 foo '??'
230match 1 1 1 1 foo '*'
231match 1 1 1 1 foo 'f*'
232match 0 0 0 0 foo '*f'
233match 1 1 1 1 foo '*foo*'
234match 1 1 1 1 foobar '*ob*a*r*'
235match 1 1 1 1 aaaaaaabababab '*ab'
236match 1 1 1 1 'foo*' 'foo\*'
237match 0 0 0 0 foobar 'foo\*bar'
238match 1 1 1 1 'f\oo' 'f\\oo'
239match 1 1 1 1 ball '*[al]?'
240match 0 0 0 0 ten '[ten]'
e5bbe09e 241match 1 1 1 1 ten '**[!te]'
91061c44
ÆAB
242match 0 0 0 0 ten '**[!ten]'
243match 1 1 1 1 ten 't[a-g]n'
244match 0 0 0 0 ten 't[!a-g]n'
245match 1 1 1 1 ton 't[!a-g]n'
246match 1 1 1 1 ton 't[^a-g]n'
247match 1 1 1 1 'a]b' 'a[]]b'
248match 1 1 1 1 a-b 'a[]-]b'
249match 1 1 1 1 'a]b' 'a[]-]b'
250match 0 0 0 0 aab 'a[]-]b'
251match 1 1 1 1 aab 'a[]a-]b'
252match 1 1 1 1 ']' ']'
feabcc17
NTND
253
254# Extended slash-matching features
91061c44
ÆAB
255match 0 0 1 1 'foo/baz/bar' 'foo*bar'
256match 0 0 1 1 'foo/baz/bar' 'foo**bar'
e5bbe09e 257match 1 1 1 1 'foobazbar' 'foo**bar'
91061c44
ÆAB
258match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
259match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
260match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
261match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
262match 1 1 0 0 'foo/bar' 'foo/**/bar'
263match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
264match 0 0 1 1 'foo/bar' 'foo?bar'
265match 0 0 1 1 'foo/bar' 'foo[/]bar'
266match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
267match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
268match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
269match 1 1 0 0 'foo' '**/foo'
270match 1 1 1 1 'XXX/foo' '**/foo'
271match 1 1 1 1 'bar/baz/foo' '**/foo'
272match 0 0 1 1 'bar/baz/foo' '*/foo'
273match 0 0 1 1 'foo/bar/baz' '**/bar*'
274match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
275match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
276match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
277match 0 0 0 0 'deep/foo/bar' '**/bar/*'
278match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
279match 0 0 1 1 'foo/bar/baz' '**/bar**'
280match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
281match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
282match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
feabcc17
NTND
283
284# Various additional tests
91061c44
ÆAB
285match 0 0 0 0 'acrt' 'a[c-c]st'
286match 1 1 1 1 'acrt' 'a[c-c]rt'
287match 0 0 0 0 ']' '[!]-]'
288match 1 1 1 1 'a' '[!]-]'
289match 0 0 0 0 '' '\'
de8bada2
ÆAB
290match 0 0 0 0 \
291 1 1 1 1 '\' '\'
91061c44
ÆAB
292match 0 0 0 0 'XXX/\' '*/\'
293match 1 1 1 1 'XXX/\' '*/\\'
294match 1 1 1 1 'foo' 'foo'
295match 1 1 1 1 '@foo' '@foo'
296match 0 0 0 0 'foo' '@foo'
297match 1 1 1 1 '[ab]' '\[ab]'
298match 1 1 1 1 '[ab]' '[[]ab]'
299match 1 1 1 1 '[ab]' '[[:]ab]'
300match 0 0 0 0 '[ab]' '[[::]ab]'
301match 1 1 1 1 '[ab]' '[[:digit]ab]'
302match 1 1 1 1 '[ab]' '[\[:]ab]'
303match 1 1 1 1 '?a?b' '\??\?b'
304match 1 1 1 1 'abc' '\a\b\c'
de8bada2
ÆAB
305match 0 0 0 0 \
306 E E E E 'foo' ''
91061c44 307match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
feabcc17
NTND
308
309# Character class tests
91061c44
ÆAB
310match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
311match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
312match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
313match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
314match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
315match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
316match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
317match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
318match 1 1 1 1 '5' '[[:xdigit:]]'
319match 1 1 1 1 'f' '[[:xdigit:]]'
320match 1 1 1 1 'D' '[[:xdigit:]]'
321match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
322match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
323match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
324match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
325match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
326match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
feabcc17 327
91061c44
ÆAB
328# Additional tests, including some malformed wildmatch patterns
329match 1 1 1 1 ']' '[\\-^]'
330match 0 0 0 0 '[' '[\\-^]'
331match 1 1 1 1 '-' '[\-_]'
332match 1 1 1 1 ']' '[\]]'
333match 0 0 0 0 '\]' '[\]]'
334match 0 0 0 0 '\' '[\]]'
335match 0 0 0 0 'ab' 'a[]b'
de8bada2
ÆAB
336match 0 0 0 0 \
337 1 1 1 1 'a[]b' 'a[]b'
338match 0 0 0 0 \
339 1 1 1 1 'ab[' 'ab['
91061c44
ÆAB
340match 0 0 0 0 'ab' '[!'
341match 0 0 0 0 'ab' '[-'
342match 1 1 1 1 '-' '[-]'
343match 0 0 0 0 '-' '[a-'
344match 0 0 0 0 '-' '[!a-'
345match 1 1 1 1 '-' '[--A]'
346match 1 1 1 1 '5' '[--A]'
347match 1 1 1 1 ' ' '[ --]'
348match 1 1 1 1 '$' '[ --]'
349match 1 1 1 1 '-' '[ --]'
350match 0 0 0 0 '0' '[ --]'
351match 1 1 1 1 '-' '[---]'
352match 1 1 1 1 '-' '[------]'
353match 0 0 0 0 'j' '[a-e-n]'
354match 1 1 1 1 '-' '[a-e-n]'
355match 1 1 1 1 'a' '[!------]'
356match 0 0 0 0 '[' '[]-a]'
357match 1 1 1 1 '^' '[]-a]'
358match 0 0 0 0 '^' '[!]-a]'
359match 1 1 1 1 '[' '[!]-a]'
360match 1 1 1 1 '^' '[a^bc]'
361match 1 1 1 1 '-b]' '[a-]b]'
362match 0 0 0 0 '\' '[\]'
363match 1 1 1 1 '\' '[\\]'
364match 0 0 0 0 '\' '[!\\]'
365match 1 1 1 1 'G' '[A-\\]'
366match 0 0 0 0 'aaabbb' 'b*a'
367match 0 0 0 0 'aabcaa' '*ba*'
368match 1 1 1 1 ',' '[,]'
369match 1 1 1 1 ',' '[\\,]'
370match 1 1 1 1 '\' '[\\,]'
371match 1 1 1 1 '-' '[,-.]'
372match 0 0 0 0 '+' '[,-.]'
373match 0 0 0 0 '-.]' '[,-.]'
374match 1 1 1 1 '2' '[\1-\3]'
375match 1 1 1 1 '3' '[\1-\3]'
376match 0 0 0 0 '4' '[\1-\3]'
377match 1 1 1 1 '\' '[[-\]]'
378match 1 1 1 1 '[' '[[-\]]'
379match 1 1 1 1 ']' '[[-\]]'
380match 0 0 0 0 '-' '[[-\]]'
feabcc17 381
91061c44
ÆAB
382# Test recursion
383match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
384match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
385match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
386match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
387match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
388match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
389match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
390match 0 0 0 0 foo '*/*/*'
391match 0 0 0 0 foo/bar '*/*/*'
392match 1 1 1 1 foo/bba/arr '*/*/*'
393match 0 0 1 1 foo/bb/aa/rr '*/*/*'
394match 1 1 1 1 foo/bb/aa/rr '**/**/**'
395match 1 1 1 1 abcXdefXghi '*X*i'
396match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
397match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
398match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
c41244e7 399
91061c44
ÆAB
400# Extra pathmatch tests
401match 0 0 0 0 foo fo
402match 1 1 1 1 foo/bar foo/bar
403match 1 1 1 1 foo/bar 'foo/*'
404match 0 0 1 1 foo/bba/arr 'foo/*'
405match 1 1 1 1 foo/bba/arr 'foo/**'
406match 0 0 1 1 foo/bba/arr 'foo*'
de8bada2
ÆAB
407match 0 0 1 1 \
408 1 1 1 1 foo/bba/arr 'foo**'
91061c44
ÆAB
409match 0 0 1 1 foo/bba/arr 'foo/*arr'
410match 0 0 1 1 foo/bba/arr 'foo/**arr'
411match 0 0 0 0 foo/bba/arr 'foo/*z'
412match 0 0 0 0 foo/bba/arr 'foo/**z'
413match 0 0 1 1 foo/bar 'foo?bar'
414match 0 0 1 1 foo/bar 'foo[/]bar'
415match 0 0 1 1 foo/bar 'foo[^a-z]bar'
416match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
b79c0c37 417
91061c44
ÆAB
418# Extra case-sensitivity tests
419match 0 1 0 1 'a' '[A-Z]'
420match 1 1 1 1 'A' '[A-Z]'
421match 0 1 0 1 'A' '[a-z]'
422match 1 1 1 1 'a' '[a-z]'
423match 0 1 0 1 'a' '[[:upper:]]'
424match 1 1 1 1 'A' '[[:upper:]]'
425match 0 1 0 1 'A' '[[:lower:]]'
426match 1 1 1 1 'a' '[[:lower:]]'
427match 0 1 0 1 'A' '[B-Za]'
428match 1 1 1 1 'a' '[B-Za]'
429match 0 1 0 1 'A' '[B-a]'
430match 1 1 1 1 'a' '[B-a]'
431match 0 1 0 1 'z' '[Z-y]'
432match 1 1 1 1 'Z' '[Z-y]'
b79c0c37 433
feabcc17 434test_done