interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t3070-wildmatch.sh

453 lines · 13300 bytes Raw Blame History
1#!/bin/sh
2
3test_description='wildmatch tests'
4
5. ./test-lib.sh
6
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
73match_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
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 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
154match() {
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
231match 1 1 1 1 foo foo
232match 0 0 0 0 foo bar
233match 1 1 1 1 '' ""
234match 1 1 1 1 foo '???'
235match 0 0 0 0 foo '??'
236match 1 1 1 1 foo '*'
237match 1 1 1 1 foo 'f*'
238match 0 0 0 0 foo '*f'
239match 1 1 1 1 foo '*foo*'
240match 1 1 1 1 foobar '*ob*a*r*'
241match 1 1 1 1 aaaaaaabababab '*ab'
242match 1 1 1 1 'foo*' 'foo\*'
243match 0 0 0 0 foobar 'foo\*bar'
244match 1 1 1 1 'f\oo' 'f\\oo'
245match 0 0 0 0 \
246 1 1 1 1 'foo\' 'foo\'
247match 1 1 1 1 ball '*[al]?'
248match 0 0 0 0 ten '[ten]'
249match 1 1 1 1 ten '**[!te]'
250match 0 0 0 0 ten '**[!ten]'
251match 1 1 1 1 ten 't[a-g]n'
252match 0 0 0 0 ten 't[!a-g]n'
253match 1 1 1 1 ton 't[!a-g]n'
254match 1 1 1 1 ton 't[^a-g]n'
255match 1 1 1 1 'a]b' 'a[]]b'
256match 1 1 1 1 a-b 'a[]-]b'
257match 1 1 1 1 'a]b' 'a[]-]b'
258match 0 0 0 0 aab 'a[]-]b'
259match 1 1 1 1 aab 'a[]a-]b'
260match 1 1 1 1 ']' ']'
261
262# Extended slash-matching features
263match 0 0 1 1 'foo/baz/bar' 'foo*bar'
264match 0 0 1 1 'foo/baz/bar' 'foo**bar'
265match 1 1 1 1 'foobazbar' 'foo**bar'
266match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
267match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
268match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
269match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
270match 1 1 0 0 'foo/bar' 'foo/**/bar'
271match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
272match 0 0 1 1 'foo/bar' 'foo?bar'
273match 0 0 1 1 'foo/bar' 'foo[/]bar'
274match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
275match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
276match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
277match 1 1 0 0 'foo' '**/foo'
278match 1 1 1 1 'XXX/foo' '**/foo'
279match 1 1 1 1 'bar/baz/foo' '**/foo'
280match 0 0 1 1 'bar/baz/foo' '*/foo'
281match 0 0 1 1 'foo/bar/baz' '**/bar*'
282match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
283match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
284match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
285match 0 0 0 0 'deep/foo/bar' '**/bar/*'
286match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
287match 0 0 1 1 'foo/bar/baz' '**/bar**'
288match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
289match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
290match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
291
292# Various additional tests
293match 0 0 0 0 'acrt' 'a[c-c]st'
294match 1 1 1 1 'acrt' 'a[c-c]rt'
295match 0 0 0 0 ']' '[!]-]'
296match 1 1 1 1 'a' '[!]-]'
297match 0 0 0 0 '' '\'
298match 0 0 0 0 \
299 1 1 1 1 '\' '\'
300match 0 0 0 0 'XXX/\' '*/\'
301match 1 1 1 1 'XXX/\' '*/\\'
302match 1 1 1 1 'foo' 'foo'
303match 1 1 1 1 '@foo' '@foo'
304match 0 0 0 0 'foo' '@foo'
305match 1 1 1 1 '[ab]' '\[ab]'
306match 1 1 1 1 '[ab]' '[[]ab]'
307match 1 1 1 1 '[ab]' '[[:]ab]'
308match 0 0 0 0 '[ab]' '[[::]ab]'
309match 1 1 1 1 '[ab]' '[[:digit]ab]'
310match 1 1 1 1 '[ab]' '[\[:]ab]'
311match 1 1 1 1 '?a?b' '\??\?b'
312match 1 1 1 1 'abc' '\a\b\c'
313match 0 0 0 0 \
314 E E E E 'foo' ''
315match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
316
317# Character class tests
318match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
319match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
320match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
321match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
322match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
323match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
324match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
325match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
326match 1 1 1 1 '5' '[[:xdigit:]]'
327match 1 1 1 1 'f' '[[:xdigit:]]'
328match 1 1 1 1 'D' '[[:xdigit:]]'
329match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
330match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
331match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
332match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
333match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
334match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
335
336# Additional tests, including some malformed wildmatch patterns
337match 1 1 1 1 ']' '[\\-^]'
338match 0 0 0 0 '[' '[\\-^]'
339match 1 1 1 1 '-' '[\-_]'
340match 1 1 1 1 ']' '[\]]'
341match 0 0 0 0 '\]' '[\]]'
342match 0 0 0 0 '\' '[\]]'
343match 0 0 0 0 'ab' 'a[]b'
344match 0 0 0 0 \
345 1 1 1 1 'a[]b' 'a[]b'
346match 0 0 0 0 \
347 1 1 1 1 'ab[' 'ab['
348match 0 0 0 0 'ab' '[!'
349match 0 0 0 0 'ab' '[-'
350match 1 1 1 1 '-' '[-]'
351match 0 0 0 0 '-' '[a-'
352match 0 0 0 0 '-' '[!a-'
353match 1 1 1 1 '-' '[--A]'
354match 1 1 1 1 '5' '[--A]'
355match 1 1 1 1 ' ' '[ --]'
356match 1 1 1 1 '$' '[ --]'
357match 1 1 1 1 '-' '[ --]'
358match 0 0 0 0 '0' '[ --]'
359match 1 1 1 1 '-' '[---]'
360match 1 1 1 1 '-' '[------]'
361match 0 0 0 0 'j' '[a-e-n]'
362match 1 1 1 1 '-' '[a-e-n]'
363match 1 1 1 1 'a' '[!------]'
364match 0 0 0 0 '[' '[]-a]'
365match 1 1 1 1 '^' '[]-a]'
366match 0 0 0 0 '^' '[!]-a]'
367match 1 1 1 1 '[' '[!]-a]'
368match 1 1 1 1 '^' '[a^bc]'
369match 1 1 1 1 '-b]' '[a-]b]'
370match 0 0 0 0 '\' '[\]'
371match 1 1 1 1 '\' '[\\]'
372match 0 0 0 0 '\' '[!\\]'
373match 1 1 1 1 'G' '[A-\\]'
374match 0 0 0 0 'aaabbb' 'b*a'
375match 0 0 0 0 'aabcaa' '*ba*'
376match 1 1 1 1 ',' '[,]'
377match 1 1 1 1 ',' '[\\,]'
378match 1 1 1 1 '\' '[\\,]'
379match 1 1 1 1 '-' '[,-.]'
380match 0 0 0 0 '+' '[,-.]'
381match 0 0 0 0 '-.]' '[,-.]'
382match 1 1 1 1 '2' '[\1-\3]'
383match 1 1 1 1 '3' '[\1-\3]'
384match 0 0 0 0 '4' '[\1-\3]'
385match 1 1 1 1 '\' '[[-\]]'
386match 1 1 1 1 '[' '[[-\]]'
387match 1 1 1 1 ']' '[[-\]]'
388match 0 0 0 0 '-' '[[-\]]'
389
390# Test recursion
391match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
392match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
393match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
394match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
395match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
396match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
397match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
398match 0 0 0 0 foo '*/*/*'
399match 0 0 0 0 foo/bar '*/*/*'
400match 1 1 1 1 foo/bba/arr '*/*/*'
401match 0 0 1 1 foo/bb/aa/rr '*/*/*'
402match 1 1 1 1 foo/bb/aa/rr '**/**/**'
403match 1 1 1 1 abcXdefXghi '*X*i'
404match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
405match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
406match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
407
408# Extra pathmatch tests
409match 0 0 0 0 foo fo
410match 1 1 1 1 foo/bar foo/bar
411match 1 1 1 1 foo/bar 'foo/*'
412match 0 0 1 1 foo/bba/arr 'foo/*'
413match 1 1 1 1 foo/bba/arr 'foo/**'
414match 0 0 1 1 foo/bba/arr 'foo*'
415match 0 0 1 1 \
416 1 1 1 1 foo/bba/arr 'foo**'
417match 0 0 1 1 foo/bba/arr 'foo/*arr'
418match 0 0 1 1 foo/bba/arr 'foo/**arr'
419match 0 0 0 0 foo/bba/arr 'foo/*z'
420match 0 0 0 0 foo/bba/arr 'foo/**z'
421match 0 0 1 1 foo/bar 'foo?bar'
422match 0 0 1 1 foo/bar 'foo[/]bar'
423match 0 0 1 1 foo/bar 'foo[^a-z]bar'
424match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
425
426# Extra case-sensitivity tests
427match 0 1 0 1 'a' '[A-Z]'
428match 1 1 1 1 'A' '[A-Z]'
429match 0 1 0 1 'A' '[a-z]'
430match 1 1 1 1 'a' '[a-z]'
431match 0 1 0 1 'a' '[[:upper:]]'
432match 1 1 1 1 'A' '[[:upper:]]'
433match 0 1 0 1 'A' '[[:lower:]]'
434match 1 1 1 1 'a' '[[:lower:]]'
435match 0 1 0 1 'A' '[B-Za]'
436match 1 1 1 1 'a' '[B-Za]'
437match 0 1 0 1 'A' '[B-a]'
438match 1 1 1 1 'a' '[B-a]'
439match 0 1 0 1 'z' '[Z-y]'
440match 1 1 1 1 'Z' '[Z-y]'
441
442test_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
453test_done