grep: add a test helper function for less verbose -f \0 tests
[git.git] / t / t7008-grep-binary.sh
blobdf93d8e44c5616c2e011001a54cea639fe393f29
1 #!/bin/sh
3 test_description='git grep in binary files'
5 . ./test-lib.sh
7 nul_match () {
8 matches=$1
9 flags=$2
10 pattern=$3
11 pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
13 if test "$matches" = 1
14 then
15 test_expect_success "git grep -f f $flags '$pattern_human' a" "
16 printf '$pattern' | q_to_nul >f &&
17 git grep -f f $flags a
19 elif test "$matches" = 0
20 then
21 test_expect_success "git grep -f f $flags '$pattern_human' a" "
22 printf '$pattern' | q_to_nul >f &&
23 test_must_fail git grep -f f $flags a
25 else
26 test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
30 test_expect_success 'setup' "
31 echo 'binaryQfile' | q_to_nul >a &&
32 git add a &&
33 git commit -m.
36 test_expect_success 'git grep ina a' '
37 echo Binary file a matches >expect &&
38 git grep ina a >actual &&
39 test_cmp expect actual
42 test_expect_success 'git grep -ah ina a' '
43 git grep -ah ina a >actual &&
44 test_cmp a actual
47 test_expect_success 'git grep -I ina a' '
48 : >expect &&
49 test_must_fail git grep -I ina a >actual &&
50 test_cmp expect actual
53 test_expect_success 'git grep -c ina a' '
54 echo a:1 >expect &&
55 git grep -c ina a >actual &&
56 test_cmp expect actual
59 test_expect_success 'git grep -l ina a' '
60 echo a >expect &&
61 git grep -l ina a >actual &&
62 test_cmp expect actual
65 test_expect_success 'git grep -L bar a' '
66 echo a >expect &&
67 git grep -L bar a >actual &&
68 test_cmp expect actual
71 test_expect_success 'git grep -q ina a' '
72 : >expect &&
73 git grep -q ina a >actual &&
74 test_cmp expect actual
77 test_expect_success 'git grep -F ile a' '
78 git grep -F ile a
81 test_expect_success 'git grep -Fi iLE a' '
82 git grep -Fi iLE a
85 # This test actually passes on platforms where regexec() supports the
86 # flag REG_STARTEND.
87 test_expect_success 'git grep ile a' '
88 git grep ile a
91 test_expect_failure 'git grep .fi a' '
92 git grep .fi a
95 nul_match 1 '-F' 'yQf'
96 nul_match 0 '-F' 'yQx'
97 nul_match 1 '-Fi' 'YQf'
98 nul_match 0 '-Fi' 'YQx'
99 nul_match 1 '' 'yQf'
100 nul_match 0 '' 'yQx'
102 test_expect_success 'grep respects binary diff attribute' '
103 echo text >t &&
104 git add t &&
105 echo t:text >expect &&
106 git grep text t >actual &&
107 test_cmp expect actual &&
108 echo "t -diff" >.gitattributes &&
109 echo "Binary file t matches" >expect &&
110 git grep text t >actual &&
111 test_cmp expect actual
114 test_expect_success 'grep --cached respects binary diff attribute' '
115 git grep --cached text t >actual &&
116 test_cmp expect actual
119 test_expect_success 'grep --cached respects binary diff attribute (2)' '
120 git add .gitattributes &&
121 rm .gitattributes &&
122 git grep --cached text t >actual &&
123 test_when_finished "git rm --cached .gitattributes" &&
124 test_when_finished "git checkout .gitattributes" &&
125 test_cmp expect actual
128 test_expect_success 'grep revision respects binary diff attribute' '
129 git commit -m new &&
130 echo "Binary file HEAD:t matches" >expect &&
131 git grep text HEAD -- t >actual &&
132 test_when_finished "git reset HEAD^" &&
133 test_cmp expect actual
136 test_expect_success 'grep respects not-binary diff attribute' '
137 echo binQary | q_to_nul >b &&
138 git add b &&
139 echo "Binary file b matches" >expect &&
140 git grep bin b >actual &&
141 test_cmp expect actual &&
142 echo "b diff" >.gitattributes &&
143 echo "b:binQary" >expect &&
144 git grep bin b >actual.raw &&
145 nul_to_q <actual.raw >actual &&
146 test_cmp expect actual
149 cat >nul_to_q_textconv <<'EOF'
150 #!/bin/sh
151 "$PERL_PATH" -pe 'y/\000/Q/' < "$1"
153 chmod +x nul_to_q_textconv
155 test_expect_success 'setup textconv filters' '
156 echo a diff=foo >.gitattributes &&
157 git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
160 test_expect_success 'grep does not honor textconv' '
161 test_must_fail git grep Qfile
164 test_expect_success 'grep --textconv honors textconv' '
165 echo "a:binaryQfile" >expect &&
166 git grep --textconv Qfile >actual &&
167 test_cmp expect actual
170 test_expect_success 'grep --no-textconv does not honor textconv' '
171 test_must_fail git grep --no-textconv Qfile
174 test_expect_success 'grep --textconv blob honors textconv' '
175 echo "HEAD:a:binaryQfile" >expect &&
176 git grep --textconv Qfile HEAD:a >actual &&
177 test_cmp expect actual
180 test_done