Raw
1 #!/bin/sh
2
3 test_description='wildmatch tests'
4
5 . ./test-lib.sh
6
7 should_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
73 match_with_function() {
74 text=$1
75 pattern=$2
76 match_expect=$3
77 match_function=$4
78
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'
83 "
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'
88 "
89 else
90 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
91 fi
92
93 }
94
95 match_with_ls_files() {
96 text=$1
97 pattern=$2
98 match_expect=$3
99 match_function=$4
100 ls_files_args=$5
101
102 prereqs=EXPENSIVE_ON_WINDOWS
103 case "$pattern" in
104 *\\*)
105 prereqs="$prereqs,BSLASHPSPEC"
106 ;;
107 esac
108
109 match_stdout_stderr_cmp="
110 tr -d '\0' <actual.raw >actual &&
111 test_must_be_empty actual.err &&
112 test_cmp expect actual"
113
114 if test "$match_expect" = 'E'
115 then
116 if test -e .git/created_test_file
117 then
118 test_expect_success $prereqs "$match_function (via ls-files): match dies on '$pattern' '$text'" "
119 printf '%s' '$text' >expect &&
120 test_must_fail git$ls_files_args ls-files -z -- '$pattern'
121 "
122 else
123 test_expect_failure $prereqs "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
124 fi
125 elif test "$match_expect" = 1
126 then
127 if test -e .git/created_test_file
128 then
129 test_expect_success $prereqs "$match_function (via ls-files): match '$pattern' '$text'" "
130 printf '%s' '$text' >expect &&
131 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
132 $match_stdout_stderr_cmp
133 "
134 else
135 test_expect_failure $prereqs "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
136 fi
137 elif test "$match_expect" = 0
138 then
139 if test -e .git/created_test_file
140 then
141 test_expect_success $prereqs "$match_function (via ls-files): no match '$pattern' '$text'" "
142 >expect &&
143 git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
144 $match_stdout_stderr_cmp
145 "
146 else
147 test_expect_failure $prereqs "$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
148 fi
149 else
150 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
151 fi
152 }
153
154 match() {
155 if test "$#" = 6
156 then
157 # When test-tool wildmatch and git ls-files produce the same
158 # result.
159 match_glob=$1
160 match_file_glob=$match_glob
161 match_iglob=$2
162 match_file_iglob=$match_iglob
163 match_pathmatch=$3
164 match_file_pathmatch=$match_pathmatch
165 match_pathmatchi=$4
166 match_file_pathmatchi=$match_pathmatchi
167 text=$5
168 pattern=$6
169 elif test "$#" = 10
170 then
171 match_glob=$1
172 match_iglob=$2
173 match_pathmatch=$3
174 match_pathmatchi=$4
175 match_file_glob=$5
176 match_file_iglob=$6
177 match_file_pathmatch=$7
178 match_file_pathmatchi=$8
179 text=$9
180 pattern=${10}
181 fi
182
183 test_expect_success EXPENSIVE_ON_WINDOWS 'cleanup after previous file test' '
184 if test -e .git/created_test_file
185 then
186 git reset &&
187 git clean -df
188 fi
189 '
190
191 printf '%s' "$text" >.git/expected_test_file
192
193 test_expect_success EXPENSIVE_ON_WINDOWS "setup match file test for $text" '
194 file=$(cat .git/expected_test_file) &&
195 if should_create_test_file "$file"
196 then
197 dirs=${file%/*} &&
198 if test "$file" != "$dirs"
199 then
200 mkdir -p -- "$dirs" &&
201 touch -- "./$text"
202 else
203 touch -- "./$file"
204 fi &&
205 git add -A &&
206 printf "%s" "$file" >.git/created_test_file
207 elif test -e .git/created_test_file
208 then
209 rm .git/created_test_file
210 fi
211 '
212
213 # $1: Case sensitive glob match: test-tool wildmatch & ls-files
214 match_with_function "$text" "$pattern" $match_glob "wildmatch"
215 match_with_ls_files "$text" "$pattern" $match_file_glob "wildmatch" " --glob-pathspecs"
216
217 # $2: Case insensitive glob match: test-tool wildmatch & ls-files
218 match_with_function "$text" "$pattern" $match_iglob "iwildmatch"
219 match_with_ls_files "$text" "$pattern" $match_file_iglob "iwildmatch" " --glob-pathspecs --icase-pathspecs"
220
221 # $3: Case sensitive path match: test-tool wildmatch & ls-files
222 match_with_function "$text" "$pattern" $match_pathmatch "pathmatch"
223 match_with_ls_files "$text" "$pattern" $match_file_pathmatch "pathmatch" ""
224
225 # $4: Case insensitive path match: test-tool wildmatch & ls-files
226 match_with_function "$text" "$pattern" $match_pathmatchi "ipathmatch"
227 match_with_ls_files "$text" "$pattern" $match_file_pathmatchi "ipathmatch" " --icase-pathspecs"
228 }
229
230 # Basic wildmatch features
231 match 1 1 1 1 foo foo
232 match 0 0 0 0 foo bar
233 match 1 1 1 1 '' ""
234 match 1 1 1 1 foo '???'
235 match 0 0 0 0 foo '??'
236 match 1 1 1 1 foo '*'
237 match 1 1 1 1 foo 'f*'
238 match 0 0 0 0 foo '*f'
239 match 1 1 1 1 foo '*foo*'
240 match 1 1 1 1 foobar '*ob*a*r*'
241 match 1 1 1 1 aaaaaaabababab '*ab'
242 match 1 1 1 1 'foo*' 'foo\*'
243 match 0 0 0 0 foobar 'foo\*bar'
244 match 1 1 1 1 'f\oo' 'f\\oo'
245 match 0 0 0 0 \
246 1 1 1 1 'foo\' 'foo\'
247 match 1 1 1 1 ball '*[al]?'
248 match 0 0 0 0 ten '[ten]'
249 match 1 1 1 1 ten '**[!te]'
250 match 0 0 0 0 ten '**[!ten]'
251 match 1 1 1 1 ten 't[a-g]n'
252 match 0 0 0 0 ten 't[!a-g]n'
253 match 1 1 1 1 ton 't[!a-g]n'
254 match 1 1 1 1 ton 't[^a-g]n'
255 match 1 1 1 1 'a]b' 'a[]]b'
256 match 1 1 1 1 a-b 'a[]-]b'
257 match 1 1 1 1 'a]b' 'a[]-]b'
258 match 0 0 0 0 aab 'a[]-]b'
259 match 1 1 1 1 aab 'a[]a-]b'
260 match 1 1 1 1 ']' ']'
261
262 # Extended slash-matching features
263 match 0 0 1 1 'foo/baz/bar' 'foo*bar'
264 match 0 0 1 1 'foo/baz/bar' 'foo**bar'
265 match 1 1 1 1 'foobazbar' 'foo**bar'
266 match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
267 match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
268 match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
269 match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
270 match 1 1 0 0 'foo/bar' 'foo/**/bar'
271 match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
272 match 0 0 1 1 'foo/bar' 'foo?bar'
273 match 0 0 1 1 'foo/bar' 'foo[/]bar'
274 match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
275 match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
276 match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
277 match 1 1 0 0 'foo' '**/foo'
278 match 1 1 1 1 'XXX/foo' '**/foo'
279 match 1 1 1 1 'bar/baz/foo' '**/foo'
280 match 0 0 1 1 'bar/baz/foo' '*/foo'
281 match 0 0 1 1 'foo/bar/baz' '**/bar*'
282 match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
283 match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
284 match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
285 match 0 0 0 0 'deep/foo/bar' '**/bar/*'
286 match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
287 match 0 0 1 1 'foo/bar/baz' '**/bar**'
288 match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
289 match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
290 match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
291
292 # Various additional tests
293 match 0 0 0 0 'acrt' 'a[c-c]st'
294 match 1 1 1 1 'acrt' 'a[c-c]rt'
295 match 0 0 0 0 ']' '[!]-]'
296 match 1 1 1 1 'a' '[!]-]'
297 match 0 0 0 0 '' '\'
298 match 0 0 0 0 \
299 1 1 1 1 '\' '\'
300 match 0 0 0 0 'XXX/\' '*/\'
301 match 1 1 1 1 'XXX/\' '*/\\'
302 match 1 1 1 1 'foo' 'foo'
303 match 1 1 1 1 '@foo' '@foo'
304 match 0 0 0 0 'foo' '@foo'
305 match 1 1 1 1 '[ab]' '\[ab]'
306 match 1 1 1 1 '[ab]' '[[]ab]'
307 match 1 1 1 1 '[ab]' '[[:]ab]'
308 match 0 0 0 0 '[ab]' '[[::]ab]'
309 match 1 1 1 1 '[ab]' '[[:digit]ab]'
310 match 1 1 1 1 '[ab]' '[\[:]ab]'
311 match 1 1 1 1 '?a?b' '\??\?b'
312 match 1 1 1 1 'abc' '\a\b\c'
313 match 0 0 0 0 \
314 E E E E 'foo' ''
315 match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
316
317 # Character class tests
318 match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
319 match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
320 match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
321 match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
322 match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
323 match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
324 match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
325 match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
326 match 1 1 1 1 '5' '[[:xdigit:]]'
327 match 1 1 1 1 'f' '[[:xdigit:]]'
328 match 1 1 1 1 'D' '[[:xdigit:]]'
329 match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
330 match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
331 match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
332 match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
333 match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
334 match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
335
336 # Additional tests, including some malformed wildmatch patterns
337 match 1 1 1 1 ']' '[\\-^]'
338 match 0 0 0 0 '[' '[\\-^]'
339 match 1 1 1 1 '-' '[\-_]'
340 match 1 1 1 1 ']' '[\]]'
341 match 0 0 0 0 '\]' '[\]]'
342 match 0 0 0 0 '\' '[\]]'
343 match 0 0 0 0 'ab' 'a[]b'
344 match 0 0 0 0 \
345 1 1 1 1 'a[]b' 'a[]b'
346 match 0 0 0 0 \
347 1 1 1 1 'ab[' 'ab['
348 match 0 0 0 0 'ab' '[!'
349 match 0 0 0 0 'ab' '[-'
350 match 1 1 1 1 '-' '[-]'
351 match 0 0 0 0 '-' '[a-'
352 match 0 0 0 0 '-' '[!a-'
353 match 1 1 1 1 '-' '[--A]'
354 match 1 1 1 1 '5' '[--A]'
355 match 1 1 1 1 ' ' '[ --]'
356 match 1 1 1 1 '$' '[ --]'
357 match 1 1 1 1 '-' '[ --]'
358 match 0 0 0 0 '0' '[ --]'
359 match 1 1 1 1 '-' '[---]'
360 match 1 1 1 1 '-' '[------]'
361 match 0 0 0 0 'j' '[a-e-n]'
362 match 1 1 1 1 '-' '[a-e-n]'
363 match 1 1 1 1 'a' '[!------]'
364 match 0 0 0 0 '[' '[]-a]'
365 match 1 1 1 1 '^' '[]-a]'
366 match 0 0 0 0 '^' '[!]-a]'
367 match 1 1 1 1 '[' '[!]-a]'
368 match 1 1 1 1 '^' '[a^bc]'
369 match 1 1 1 1 '-b]' '[a-]b]'
370 match 0 0 0 0 '\' '[\]'
371 match 1 1 1 1 '\' '[\\]'
372 match 0 0 0 0 '\' '[!\\]'
373 match 1 1 1 1 'G' '[A-\\]'
374 match 0 0 0 0 'aaabbb' 'b*a'
375 match 0 0 0 0 'aabcaa' '*ba*'
376 match 1 1 1 1 ',' '[,]'
377 match 1 1 1 1 ',' '[\\,]'
378 match 1 1 1 1 '\' '[\\,]'
379 match 1 1 1 1 '-' '[,-.]'
380 match 0 0 0 0 '+' '[,-.]'
381 match 0 0 0 0 '-.]' '[,-.]'
382 match 1 1 1 1 '2' '[\1-\3]'
383 match 1 1 1 1 '3' '[\1-\3]'
384 match 0 0 0 0 '4' '[\1-\3]'
385 match 1 1 1 1 '\' '[[-\]]'
386 match 1 1 1 1 '[' '[[-\]]'
387 match 1 1 1 1 ']' '[[-\]]'
388 match 0 0 0 0 '-' '[[-\]]'
389
390 # Test recursion
391 match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
392 match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
393 match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
394 match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
395 match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
396 match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
397 match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
398 match 0 0 0 0 foo '*/*/*'
399 match 0 0 0 0 foo/bar '*/*/*'
400 match 1 1 1 1 foo/bba/arr '*/*/*'
401 match 0 0 1 1 foo/bb/aa/rr '*/*/*'
402 match 1 1 1 1 foo/bb/aa/rr '**/**/**'
403 match 1 1 1 1 abcXdefXghi '*X*i'
404 match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
405 match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
406 match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
407
408 # Extra pathmatch tests
409 match 0 0 0 0 foo fo
410 match 1 1 1 1 foo/bar foo/bar
411 match 1 1 1 1 foo/bar 'foo/*'
412 match 0 0 1 1 foo/bba/arr 'foo/*'
413 match 1 1 1 1 foo/bba/arr 'foo/**'
414 match 0 0 1 1 foo/bba/arr 'foo*'
415 match 0 0 1 1 \
416 1 1 1 1 foo/bba/arr 'foo**'
417 match 0 0 1 1 foo/bba/arr 'foo/*arr'
418 match 0 0 1 1 foo/bba/arr 'foo/**arr'
419 match 0 0 0 0 foo/bba/arr 'foo/*z'
420 match 0 0 0 0 foo/bba/arr 'foo/**z'
421 match 0 0 1 1 foo/bar 'foo?bar'
422 match 0 0 1 1 foo/bar 'foo[/]bar'
423 match 0 0 1 1 foo/bar 'foo[^a-z]bar'
424 match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
425
426 # Extra case-sensitivity tests
427 match 0 1 0 1 'a' '[A-Z]'
428 match 1 1 1 1 'A' '[A-Z]'
429 match 0 1 0 1 'A' '[a-z]'
430 match 1 1 1 1 'a' '[a-z]'
431 match 0 1 0 1 'a' '[[:upper:]]'
432 match 1 1 1 1 'A' '[[:upper:]]'
433 match 0 1 0 1 'A' '[[:lower:]]'
434 match 1 1 1 1 'a' '[[:lower:]]'
435 match 0 1 0 1 'A' '[B-Za]'
436 match 1 1 1 1 'a' '[B-Za]'
437 match 0 1 0 1 'A' '[B-a]'
438 match 1 1 1 1 'a' '[B-a]'
439 match 0 1 0 1 'z' '[Z-y]'
440 match 1 1 1 1 'Z' '[Z-y]'
441
442 test_expect_success 'matching does not exhibit exponential behavior' '
443 {
444 test-tool wildmatch wildmatch \
445 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab \
446 "*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a" &
447 pid=$!
448 } &&
449 sleep 2 &&
450 ! kill $!
451 '
452
453 test_done