t4027,4041: Use test -s to test for an empty file
[git/gitweb.git] / t / t4019-diff-wserror.sh
blobf6d1f1ebab406fcd4f405178ec151149754500b0
1 #!/bin/sh
3 test_description='diff whitespace error detection'
5 . ./test-lib.sh
7 test_expect_success setup '
9 git config diff.color.whitespace "blue reverse" &&
10 >F &&
11 git add F &&
12 echo " Eight SP indent" >>F &&
13 echo " HT and SP indent" >>F &&
14 echo "With trailing SP " >>F &&
15 echo "Carriage ReturnQ" | tr Q "\015" >>F &&
16 echo "No problem" >>F &&
17 echo >>F
21 blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
23 printf "\033[%s" "$blue_grep" >check-grep
24 if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1
25 then
26 grep_a=grep
27 elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1
28 then
29 grep_a='grep -a'
30 else
31 grep_a=grep ;# expected to fail...
33 rm -f check-grep
35 prepare_output () {
36 git diff --color >output
37 $grep_a "$blue_grep" output >error
38 $grep_a -v "$blue_grep" output >normal
41 test_expect_success default '
43 prepare_output
45 grep Eight normal >/dev/null &&
46 grep HT error >/dev/null &&
47 grep With error >/dev/null &&
48 grep Return error >/dev/null &&
49 grep No normal >/dev/null
53 test_expect_success 'without -trail' '
55 git config core.whitespace -trail
56 prepare_output
58 grep Eight normal >/dev/null &&
59 grep HT error >/dev/null &&
60 grep With normal >/dev/null &&
61 grep Return normal >/dev/null &&
62 grep No normal >/dev/null
66 test_expect_success 'without -trail (attribute)' '
68 git config --unset core.whitespace
69 echo "F whitespace=-trail" >.gitattributes
70 prepare_output
72 grep Eight normal >/dev/null &&
73 grep HT error >/dev/null &&
74 grep With normal >/dev/null &&
75 grep Return normal >/dev/null &&
76 grep No normal >/dev/null
80 test_expect_success 'without -space' '
82 rm -f .gitattributes
83 git config core.whitespace -space
84 prepare_output
86 grep Eight normal >/dev/null &&
87 grep HT normal >/dev/null &&
88 grep With error >/dev/null &&
89 grep Return error >/dev/null &&
90 grep No normal >/dev/null
94 test_expect_success 'without -space (attribute)' '
96 git config --unset core.whitespace
97 echo "F whitespace=-space" >.gitattributes
98 prepare_output
100 grep Eight normal >/dev/null &&
101 grep HT normal >/dev/null &&
102 grep With error >/dev/null &&
103 grep Return error >/dev/null &&
104 grep No normal >/dev/null
108 test_expect_success 'with indent-non-tab only' '
110 rm -f .gitattributes
111 git config core.whitespace indent,-trailing,-space
112 prepare_output
114 grep Eight error >/dev/null &&
115 grep HT normal >/dev/null &&
116 grep With normal >/dev/null &&
117 grep Return normal >/dev/null &&
118 grep No normal >/dev/null
122 test_expect_success 'with indent-non-tab only (attribute)' '
124 git config --unset core.whitespace
125 echo "F whitespace=indent,-trailing,-space" >.gitattributes
126 prepare_output
128 grep Eight error >/dev/null &&
129 grep HT normal >/dev/null &&
130 grep With normal >/dev/null &&
131 grep Return normal >/dev/null &&
132 grep No normal >/dev/null
136 test_expect_success 'with cr-at-eol' '
138 rm -f .gitattributes
139 git config core.whitespace cr-at-eol
140 prepare_output
142 grep Eight normal >/dev/null &&
143 grep HT error >/dev/null &&
144 grep With error >/dev/null &&
145 grep Return normal >/dev/null &&
146 grep No normal >/dev/null
150 test_expect_success 'with cr-at-eol (attribute)' '
152 git config --unset core.whitespace
153 echo "F whitespace=trailing,cr-at-eol" >.gitattributes
154 prepare_output
156 grep Eight normal >/dev/null &&
157 grep HT error >/dev/null &&
158 grep With error >/dev/null &&
159 grep Return normal >/dev/null &&
160 grep No normal >/dev/null
164 test_expect_success 'trailing empty lines (1)' '
166 rm -f .gitattributes &&
167 test_must_fail git diff --check >output &&
168 grep "new blank line at" output &&
169 grep "trailing whitespace" output
173 test_expect_success 'trailing empty lines (2)' '
175 echo "F -whitespace" >.gitattributes &&
176 git diff --check >output &&
177 ! test -s output
181 test_expect_success 'do not color trailing cr in context' '
182 git config --unset core.whitespace
183 rm -f .gitattributes &&
184 echo AAAQ | tr Q "\015" >G &&
185 git add G &&
186 echo BBBQ | tr Q "\015" >>G
187 git diff --color G | tr "\015" Q >output &&
188 grep "BBB.*${blue_grep}Q" output &&
189 grep "AAA.*\[mQ" output
193 test_expect_success 'color new trailing blank lines' '
194 { echo a; echo b; echo; echo; } >x &&
195 git add x &&
196 { echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&
197 git diff --color x >output &&
198 cnt=$($grep_a "${blue_grep}" output | wc -l) &&
199 test $cnt = 2
202 test_done