Merge branch 'ab/ci-updates'
[git/debian.git] / t / t4034-diff-words.sh
blobd5abcf4b4c6fa9f828b635dccbea37eae3ed4eb7
1 #!/bin/sh
3 test_description='word diff colors'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-diff.sh
9 cat >pre.simple <<-\EOF
10 h(4)
12 a = b + c
13 EOF
14 cat >post.simple <<-\EOF
15 h(4),hh[44]
17 a = b + c
19 aa = a
21 aeff = aeff * ( aaa )
22 EOF
23 pre=$(git rev-parse --short $(git hash-object pre.simple))
24 post=$(git rev-parse --short $(git hash-object post.simple))
25 cat >expect.letter-runs-are-words <<-EOF
26 <BOLD>diff --git a/pre b/post<RESET>
27 <BOLD>index $pre..$post 100644<RESET>
28 <BOLD>--- a/pre<RESET>
29 <BOLD>+++ b/post<RESET>
30 <CYAN>@@ -1,3 +1,7 @@<RESET>
31 h(4),<GREEN>hh<RESET>[44]
33 a = b + c<RESET>
35 <GREEN>aa = a<RESET>
37 <GREEN>aeff = aeff * ( aaa<RESET> )
38 EOF
39 cat >expect.non-whitespace-is-word <<-EOF
40 <BOLD>diff --git a/pre b/post<RESET>
41 <BOLD>index $pre..$post 100644<RESET>
42 <BOLD>--- a/pre<RESET>
43 <BOLD>+++ b/post<RESET>
44 <CYAN>@@ -1,3 +1,7 @@<RESET>
45 h(4)<GREEN>,hh[44]<RESET>
47 a = b + c<RESET>
49 <GREEN>aa = a<RESET>
51 <GREEN>aeff = aeff * ( aaa )<RESET>
52 EOF
54 word_diff () {
55 pre=$(git rev-parse --short $(git hash-object pre)) &&
56 post=$(git rev-parse --short $(git hash-object post)) &&
57 test_must_fail git diff --no-index "$@" pre post >output &&
58 test_decode_color <output >output.decrypted &&
59 sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected
60 test_cmp expected output.decrypted
63 test_language_driver () {
64 lang=$1
65 test_expect_success "diff driver '$lang'" '
66 cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
67 "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
68 "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
69 echo "* diff='"$lang"'" >.gitattributes &&
70 word_diff --color-words
74 test_expect_success setup '
75 git config diff.color.old red &&
76 git config diff.color.new green &&
77 git config diff.color.func magenta
80 test_expect_success 'set up pre and post with runs of whitespace' '
81 cp pre.simple pre &&
82 cp post.simple post
85 test_expect_success 'word diff with runs of whitespace' '
86 cat >expect <<-EOF &&
87 <BOLD>diff --git a/pre b/post<RESET>
88 <BOLD>index $pre..$post 100644<RESET>
89 <BOLD>--- a/pre<RESET>
90 <BOLD>+++ b/post<RESET>
91 <CYAN>@@ -1,3 +1,7 @@<RESET>
92 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
94 a = b + c<RESET>
96 <GREEN>aa = a<RESET>
98 <GREEN>aeff = aeff * ( aaa )<RESET>
99 EOF
100 word_diff --color-words &&
101 word_diff --word-diff=color &&
102 word_diff --color --word-diff=color
105 test_expect_success '--word-diff=porcelain' '
106 sed "s/#.*$//" >expect <<-EOF &&
107 diff --git a/pre b/post
108 index $pre..$post 100644
109 --- a/pre
110 +++ b/post
111 @@ -1,3 +1,7 @@
112 -h(4)
113 +h(4),hh[44]
115 # significant space
117 a = b + c
120 +aa = a
123 +aeff = aeff * ( aaa )
126 word_diff --word-diff=porcelain
129 test_expect_success '--word-diff=plain' '
130 cat >expect <<-EOF &&
131 diff --git a/pre b/post
132 index $pre..$post 100644
133 --- a/pre
134 +++ b/post
135 @@ -1,3 +1,7 @@
136 [-h(4)-]{+h(4),hh[44]+}
138 a = b + c
140 {+aa = a+}
142 {+aeff = aeff * ( aaa )+}
144 word_diff --word-diff=plain &&
145 word_diff --word-diff=plain --no-color
148 test_expect_success '--word-diff=plain --color' '
149 cat >expect <<-EOF &&
150 <BOLD>diff --git a/pre b/post<RESET>
151 <BOLD>index $pre..$post 100644<RESET>
152 <BOLD>--- a/pre<RESET>
153 <BOLD>+++ b/post<RESET>
154 <CYAN>@@ -1,3 +1,7 @@<RESET>
155 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
157 a = b + c<RESET>
159 <GREEN>{+aa = a+}<RESET>
161 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
163 word_diff --word-diff=plain --color
166 test_expect_success 'word diff without context' '
167 cat >expect <<-EOF &&
168 <BOLD>diff --git a/pre b/post<RESET>
169 <BOLD>index $pre..$post 100644<RESET>
170 <BOLD>--- a/pre<RESET>
171 <BOLD>+++ b/post<RESET>
172 <CYAN>@@ -1 +1 @@<RESET>
173 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
174 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
176 <GREEN>aa = a<RESET>
178 <GREEN>aeff = aeff * ( aaa )<RESET>
180 word_diff --color-words --unified=0
183 test_expect_success 'word diff with a regular expression' '
184 cp expect.letter-runs-are-words expect &&
185 word_diff --color-words="[a-z]+"
188 test_expect_success 'word diff with zero length matches' '
189 cp expect.letter-runs-are-words expect &&
190 word_diff --color-words="[a-z${LF}]*"
193 test_expect_success 'set up a diff driver' '
194 git config diff.testdriver.wordRegex "[^[:space:]]" &&
195 cat <<-\EOF >.gitattributes
196 pre diff=testdriver
197 post diff=testdriver
201 test_expect_success 'option overrides .gitattributes' '
202 cp expect.letter-runs-are-words expect &&
203 word_diff --color-words="[a-z]+"
206 test_expect_success 'use regex supplied by driver' '
207 cp expect.non-whitespace-is-word expect &&
208 word_diff --color-words
211 test_expect_success 'set up diff.wordRegex option' '
212 git config diff.wordRegex "[[:alnum:]]+"
215 test_expect_success 'command-line overrides config' '
216 cp expect.letter-runs-are-words expect &&
217 word_diff --color-words="[a-z]+"
220 test_expect_success 'command-line overrides config: --word-diff-regex' '
221 cat >expect <<-EOF &&
222 <BOLD>diff --git a/pre b/post<RESET>
223 <BOLD>index $pre..$post 100644<RESET>
224 <BOLD>--- a/pre<RESET>
225 <BOLD>+++ b/post<RESET>
226 <CYAN>@@ -1,3 +1,7 @@<RESET>
227 h(4),<GREEN>{+hh+}<RESET>[44]
229 a = b + c<RESET>
231 <GREEN>{+aa = a+}<RESET>
233 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
235 word_diff --color --word-diff-regex="[a-z]+"
238 test_expect_success '.gitattributes override config' '
239 cp expect.non-whitespace-is-word expect &&
240 word_diff --color-words
243 test_expect_success 'setup: remove diff driver regex' '
244 test_unconfig diff.testdriver.wordRegex
247 test_expect_success 'use configured regex' '
248 cat >expect <<-EOF &&
249 <BOLD>diff --git a/pre b/post<RESET>
250 <BOLD>index $pre..$post 100644<RESET>
251 <BOLD>--- a/pre<RESET>
252 <BOLD>+++ b/post<RESET>
253 <CYAN>@@ -1,3 +1,7 @@<RESET>
254 h(4),<GREEN>hh[44<RESET>]
256 a = b + c<RESET>
258 <GREEN>aa = a<RESET>
260 <GREEN>aeff = aeff * ( aaa<RESET> )
262 word_diff --color-words
265 test_expect_success 'test parsing words for newline' '
266 echo "aaa (aaa)" >pre &&
267 echo "aaa (aaa) aaa" >post &&
268 pre=$(git rev-parse --short $(git hash-object pre)) &&
269 post=$(git rev-parse --short $(git hash-object post)) &&
270 cat >expect <<-EOF &&
271 <BOLD>diff --git a/pre b/post<RESET>
272 <BOLD>index $pre..$post 100644<RESET>
273 <BOLD>--- a/pre<RESET>
274 <BOLD>+++ b/post<RESET>
275 <CYAN>@@ -1 +1 @@<RESET>
276 aaa (aaa) <GREEN>aaa<RESET>
278 word_diff --color-words="a+"
281 test_expect_success 'test when words are only removed at the end' '
282 echo "(:" >pre &&
283 echo "(" >post &&
284 pre=$(git rev-parse --short $(git hash-object pre)) &&
285 post=$(git rev-parse --short $(git hash-object post)) &&
286 cat >expect <<-EOF &&
287 <BOLD>diff --git a/pre b/post<RESET>
288 <BOLD>index $pre..$post 100644<RESET>
289 <BOLD>--- a/pre<RESET>
290 <BOLD>+++ b/post<RESET>
291 <CYAN>@@ -1 +1 @@<RESET>
292 (<RED>:<RESET>
294 word_diff --color-words=.
297 test_expect_success '--word-diff=none' '
298 echo "(:" >pre &&
299 echo "(" >post &&
300 pre=$(git rev-parse --short $(git hash-object pre)) &&
301 post=$(git rev-parse --short $(git hash-object post)) &&
302 cat >expect <<-EOF &&
303 diff --git a/pre b/post
304 index $pre..$post 100644
305 --- a/pre
306 +++ b/post
307 @@ -1 +1 @@
311 word_diff --word-diff=plain --word-diff=none
314 test_expect_success 'unset default driver' '
315 test_unconfig diff.wordregex
318 test_language_driver ada
319 test_language_driver bibtex
320 test_language_driver cpp
321 test_language_driver csharp
322 test_language_driver css
323 test_language_driver dts
324 test_language_driver fortran
325 test_language_driver html
326 test_language_driver java
327 test_language_driver matlab
328 test_language_driver objc
329 test_language_driver pascal
330 test_language_driver perl
331 test_language_driver php
332 test_language_driver python
333 test_language_driver ruby
334 test_language_driver scheme
335 test_language_driver tex
337 test_expect_success 'word-diff with diff.sbe' '
338 cat >pre <<-\EOF &&
343 cat >post <<-\EOF &&
348 pre=$(git rev-parse --short $(git hash-object pre)) &&
349 post=$(git rev-parse --short $(git hash-object post)) &&
350 cat >expect <<-EOF &&
351 diff --git a/pre b/post
352 index $pre..$post 100644
353 --- a/pre
354 +++ b/post
355 @@ -1,3 +1,3 @@
358 [-b-]{+c+}
360 test_config diff.suppress-blank-empty true &&
361 word_diff --word-diff=plain
364 test_expect_success 'word-diff with no newline at EOF' '
365 printf "%s" "a a a a a" >pre &&
366 printf "%s" "a a ab a a" >post &&
367 pre=$(git rev-parse --short $(git hash-object pre)) &&
368 post=$(git rev-parse --short $(git hash-object post)) &&
369 cat >expect <<-EOF &&
370 diff --git a/pre b/post
371 index $pre..$post 100644
372 --- a/pre
373 +++ b/post
374 @@ -1 +1 @@
375 a a [-a-]{+ab+} a a
377 word_diff --word-diff=plain
380 test_expect_success 'setup history with two files' '
381 echo "a b; c" >a.tex &&
382 echo "a b; c" >z.txt &&
383 git add a.tex z.txt &&
384 git commit -minitial &&
386 # modify both
387 echo "a bx; c" >a.tex &&
388 echo "a bx; c" >z.txt &&
389 git commit -mmodified -a
392 test_expect_success 'wordRegex for the first file does not apply to the second' '
393 echo "*.tex diff=tex" >.gitattributes &&
394 test_config diff.tex.wordRegex "[a-z]+|." &&
395 cat >expect <<-\EOF &&
396 diff --git a/a.tex b/a.tex
397 --- a/a.tex
398 +++ b/a.tex
399 @@ -1 +1 @@
400 a [-b-]{+bx+}; c
401 diff --git a/z.txt b/z.txt
402 --- a/z.txt
403 +++ b/z.txt
404 @@ -1 +1 @@
405 a [-b;-]{+bx;+} c
407 git diff --word-diff HEAD~ >actual &&
408 compare_diff_patch expect actual
411 test_done