t/helper: teach test-regex to report pattern errors (like REG_ILLSEQ)
[git/debian.git] / t / t7816-grep-binary-pattern.sh
blob60bab291e49c61ede2926deca6224b988ad32746
1 #!/bin/sh
3 test_description='git grep with a binary pattern files'
5 . ./lib-gettext.sh
7 nul_match_internal () {
8 matches=$1
9 prereqs=$2
10 lc_all=$3
11 extra_flags=$4
12 flags=$5
13 pattern=$6
14 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
16 if test "$matches" = 1
17 then
18 test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
19 printf '$pattern' | q_to_nul >f &&
20 LC_ALL='$lc_all' git grep $extra_flags -f f $flags a
22 elif test "$matches" = 0
23 then
24 test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
25 >stderr &&
26 printf '$pattern' | q_to_nul >f &&
27 test_must_fail env LC_ALL=\"$lc_all\" git grep $extra_flags -f f $flags a 2>stderr &&
28 test_i18ngrep ! 'This is only supported with -P under PCRE v2' stderr
30 elif test "$matches" = P
31 then
32 test_expect_success $prereqs "error, PCRE v2 only: LC_ALL='$lc_all' git grep -f f $flags '$pattern_human' a" "
33 >stderr &&
34 printf '$pattern' | q_to_nul >f &&
35 test_must_fail env LC_ALL=\"$lc_all\" git grep -f f $flags a 2>stderr &&
36 test_i18ngrep 'This is only supported with -P under PCRE v2' stderr
38 else
39 test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
43 nul_match () {
44 matches=$1
45 matches_pcre2=$2
46 matches_pcre2_locale=$3
47 flags=$4
48 pattern=$5
49 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
51 nul_match_internal "$matches" "" "C" "" "$flags" "$pattern"
52 nul_match_internal "$matches_pcre2" "LIBPCRE2" "C" "-P" "$flags" "$pattern"
53 nul_match_internal "$matches_pcre2_locale" "LIBPCRE2,GETTEXT_LOCALE" "$is_IS_locale" "-P" "$flags" "$pattern"
56 test_expect_success 'setup' "
57 echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
58 git add a &&
59 git commit -m.
62 # Simple fixed-string matching that can use kwset (no -i && non-ASCII)
63 nul_match P P P '-F' 'yQf'
64 nul_match P P P '-F' 'yQx'
65 nul_match P P P '-Fi' 'YQf'
66 nul_match P P P '-Fi' 'YQx'
67 nul_match P P 1 '' 'yQf'
68 nul_match P P 0 '' 'yQx'
69 nul_match P P 1 '' 'æQð'
70 nul_match P P P '-F' 'eQm[*]c'
71 nul_match P P P '-Fi' 'EQM[*]C'
73 # Regex patterns that would match but shouldn't with -F
74 nul_match P P P '-F' 'yQ[f]'
75 nul_match P P P '-F' '[y]Qf'
76 nul_match P P P '-Fi' 'YQ[F]'
77 nul_match P P P '-Fi' '[Y]QF'
78 nul_match P P P '-F' 'æQ[ð]'
79 nul_match P P P '-F' '[æ]Qð'
81 # The -F kwset codepath can't handle -i && non-ASCII...
82 nul_match P 1 1 '-i' '[æ]Qð'
84 # ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
85 # semantics
86 nul_match P P P '-Fi' 'ÆQ[Ð]'
87 nul_match P 0 1 '-i' 'ÆQ[Ð]'
88 nul_match P 0 1 '-i' '[Æ]QÐ'
89 nul_match P 0 1 '-i' '[Æ]Qð'
90 nul_match P 0 1 '-i' 'ÆQÐ'
92 # \0 in regexes can only work with -P & PCRE v2
93 nul_match P P 1 '' 'yQ[f]'
94 nul_match P P 1 '' '[y]Qf'
95 nul_match P P 1 '-i' 'YQ[F]'
96 nul_match P P 1 '-i' '[Y]Qf'
97 nul_match P P 1 '' 'æQ[ð]'
98 nul_match P P 1 '' '[æ]Qð'
99 nul_match P P 1 '-i' 'ÆQ[Ð]'
100 nul_match P P 1 '' 'eQm.*cQ'
101 nul_match P P 1 '-i' 'EQM.*cQ'
102 nul_match P P 0 '' 'eQm[*]c'
103 nul_match P P 0 '-i' 'EQM[*]C'
105 # Assert that we're using REG_STARTEND and the pattern doesn't match
106 # just because it's cut off at the first \0.
107 nul_match P P 0 '-i' 'NOMATCHQð'
108 nul_match P P 0 '-i' '[Æ]QNOMATCH'
109 nul_match P P 0 '-i' '[æ]QNOMATCH'
111 # Ensure that the matcher doesn't regress to something that stops at
112 # \0
113 nul_match P P P '-F' 'yQ[f]'
114 nul_match P P P '-Fi' 'YQ[F]'
115 nul_match P P 0 '' 'yQNOMATCH'
116 nul_match P P 0 '' 'QNOMATCH'
117 nul_match P P 0 '-i' 'YQNOMATCH'
118 nul_match P P 0 '-i' 'QNOMATCH'
119 nul_match P P P '-F' 'æQ[ð]'
120 nul_match P P P '-Fi' 'ÆQ[Ð]'
121 nul_match P P 1 '-i' 'ÆQ[Ð]'
122 nul_match P P 0 '' 'yQNÓMATCH'
123 nul_match P P 0 '' 'QNÓMATCH'
124 nul_match P P 0 '-i' 'YQNÓMATCH'
125 nul_match P P 0 '-i' 'QNÓMATCH'
127 test_done