blob: 912df91226f2a018016129a6634b837faee19869 [file] [log] [blame]
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
Johannes Sixt62d39352012-03-14 20:50:21 +01006. "$TEST_DIRECTORY"/diff-lib.sh
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01007
Jonathan Nieder5094d152011-01-11 15:49:57 -06008cat >pre.simple <<-\EOF
9 h(4)
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010010
Jonathan Nieder5094d152011-01-11 15:49:57 -060011 a = b + c
12EOF
13cat >post.simple <<-\EOF
14 h(4),hh[44]
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010015
Jonathan Nieder5094d152011-01-11 15:49:57 -060016 a = b + c
17
18 aa = a
19
20 aeff = aeff * ( aaa )
21EOF
22cat >expect.letter-runs-are-words <<-\EOF
23 <BOLD>diff --git a/pre b/post<RESET>
24 <BOLD>index 330b04f..5ed8eff 100644<RESET>
25 <BOLD>--- a/pre<RESET>
26 <BOLD>+++ b/post<RESET>
27 <CYAN>@@ -1,3 +1,7 @@<RESET>
28 h(4),<GREEN>hh<RESET>[44]
29
30 a = b + c<RESET>
31
32 <GREEN>aa = a<RESET>
33
34 <GREEN>aeff = aeff * ( aaa<RESET> )
35EOF
36cat >expect.non-whitespace-is-word <<-\EOF
37 <BOLD>diff --git a/pre b/post<RESET>
38 <BOLD>index 330b04f..5ed8eff 100644<RESET>
39 <BOLD>--- a/pre<RESET>
40 <BOLD>+++ b/post<RESET>
41 <CYAN>@@ -1,3 +1,7 @@<RESET>
42 h(4)<GREEN>,hh[44]<RESET>
43
44 a = b + c<RESET>
45
46 <GREEN>aa = a<RESET>
47
48 <GREEN>aeff = aeff * ( aaa )<RESET>
49EOF
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010050
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010051word_diff () {
Jonathan Nieder5094d152011-01-11 15:49:57 -060052 test_must_fail git diff --no-index "$@" pre post >output &&
Michael J Gruber68cfc6f2009-12-08 11:12:02 +010053 test_decode_color <output >output.decrypted &&
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010054 test_cmp expect output.decrypted
55}
56
Jonathan Nieder5094d152011-01-11 15:49:57 -060057test_language_driver () {
58 lang=$1
59 test_expect_success "diff driver '$lang'" '
60 cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
61 "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
62 "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
63 echo "* diff='"$lang"'" >.gitattributes &&
64 word_diff --color-words
65 '
66}
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010067
Jonathan Nieder5094d152011-01-11 15:49:57 -060068test_expect_success setup '
69 git config diff.color.old red &&
70 git config diff.color.new green &&
71 git config diff.color.func magenta
72'
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010073
Jonathan Nieder5094d152011-01-11 15:49:57 -060074test_expect_success 'set up pre and post with runs of whitespace' '
75 cp pre.simple pre &&
76 cp post.simple post
77'
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010078
79test_expect_success 'word diff with runs of whitespace' '
Jonathan Nieder5094d152011-01-11 15:49:57 -060080 cat >expect <<-\EOF &&
81 <BOLD>diff --git a/pre b/post<RESET>
82 <BOLD>index 330b04f..5ed8eff 100644<RESET>
83 <BOLD>--- a/pre<RESET>
84 <BOLD>+++ b/post<RESET>
85 <CYAN>@@ -1,3 +1,7 @@<RESET>
86 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010087
Jonathan Nieder5094d152011-01-11 15:49:57 -060088 a = b + c<RESET>
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010089
Jonathan Nieder5094d152011-01-11 15:49:57 -060090 <GREEN>aa = a<RESET>
Johannes Schindelin2e5d2002009-01-17 17:29:44 +010091
Jonathan Nieder5094d152011-01-11 15:49:57 -060092 <GREEN>aeff = aeff * ( aaa )<RESET>
93 EOF
94 word_diff --color-words &&
95 word_diff --word-diff=color &&
Thomas Rast882749a2010-04-14 17:59:06 +020096 word_diff --color --word-diff=color
Thomas Rast882749a2010-04-14 17:59:06 +020097'
98
Thomas Rast882749a2010-04-14 17:59:06 +020099test_expect_success '--word-diff=porcelain' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600100 sed 's/#.*$//' >expect <<-\EOF &&
101 diff --git a/pre b/post
102 index 330b04f..5ed8eff 100644
103 --- a/pre
104 +++ b/post
105 @@ -1,3 +1,7 @@
106 -h(4)
107 +h(4),hh[44]
108 ~
109 # significant space
110 ~
111 a = b + c
112 ~
113 ~
114 +aa = a
115 ~
116 ~
117 +aeff = aeff * ( aaa )
118 ~
119 EOF
Thomas Rast882749a2010-04-14 17:59:06 +0200120 word_diff --word-diff=porcelain
Thomas Rast882749a2010-04-14 17:59:06 +0200121'
122
Thomas Rast882749a2010-04-14 17:59:06 +0200123test_expect_success '--word-diff=plain' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600124 cat >expect <<-\EOF &&
125 diff --git a/pre b/post
126 index 330b04f..5ed8eff 100644
127 --- a/pre
128 +++ b/post
129 @@ -1,3 +1,7 @@
130 [-h(4)-]{+h(4),hh[44]+}
Thomas Rast882749a2010-04-14 17:59:06 +0200131
Jonathan Nieder5094d152011-01-11 15:49:57 -0600132 a = b + c
Thomas Rast882749a2010-04-14 17:59:06 +0200133
Jonathan Nieder5094d152011-01-11 15:49:57 -0600134 {+aa = a+}
Thomas Rast882749a2010-04-14 17:59:06 +0200135
Jonathan Nieder5094d152011-01-11 15:49:57 -0600136 {+aeff = aeff * ( aaa )+}
137 EOF
138 word_diff --word-diff=plain &&
Thomas Rast882749a2010-04-14 17:59:06 +0200139 word_diff --word-diff=plain --no-color
Thomas Rast882749a2010-04-14 17:59:06 +0200140'
141
Thomas Rast882749a2010-04-14 17:59:06 +0200142test_expect_success '--word-diff=plain --color' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600143 cat >expect <<-\EOF &&
144 <BOLD>diff --git a/pre b/post<RESET>
145 <BOLD>index 330b04f..5ed8eff 100644<RESET>
146 <BOLD>--- a/pre<RESET>
147 <BOLD>+++ b/post<RESET>
148 <CYAN>@@ -1,3 +1,7 @@<RESET>
149 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
Thomas Rast882749a2010-04-14 17:59:06 +0200150
Jonathan Nieder5094d152011-01-11 15:49:57 -0600151 a = b + c<RESET>
152
153 <GREEN>{+aa = a+}<RESET>
154
155 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
156 EOF
Thomas Rast882749a2010-04-14 17:59:06 +0200157 word_diff --word-diff=plain --color
Thomas Rast882749a2010-04-14 17:59:06 +0200158'
159
Johannes Schindelina4ca1462009-10-29 11:45:03 +0100160test_expect_success 'word diff without context' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600161 cat >expect <<-\EOF &&
162 <BOLD>diff --git a/pre b/post<RESET>
163 <BOLD>index 330b04f..5ed8eff 100644<RESET>
164 <BOLD>--- a/pre<RESET>
165 <BOLD>+++ b/post<RESET>
166 <CYAN>@@ -1 +1 @@<RESET>
167 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
168 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
Markus Heidelberg168eff32009-10-28 13:24:30 +0100169
Jonathan Nieder5094d152011-01-11 15:49:57 -0600170 <GREEN>aa = a<RESET>
171
172 <GREEN>aeff = aeff * ( aaa )<RESET>
173 EOF
Markus Heidelberg168eff32009-10-28 13:24:30 +0100174 word_diff --color-words --unified=0
Markus Heidelberg168eff32009-10-28 13:24:30 +0100175'
176
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100177test_expect_success 'word diff with a regular expression' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600178 cp expect.letter-runs-are-words expect &&
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100179 word_diff --color-words="[a-z]+"
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100180'
181
Jonathan Nieder5094d152011-01-11 15:49:57 -0600182test_expect_success 'set up a diff driver' '
Boyd Stephen Smith Jrae3b9702009-01-20 22:59:54 -0600183 git config diff.testdriver.wordRegex "[^[:space:]]" &&
Jonathan Nieder5094d152011-01-11 15:49:57 -0600184 cat <<-\EOF >.gitattributes
185 pre diff=testdriver
186 post diff=testdriver
187 EOF
Thomas Rast80c49c32009-01-17 17:29:48 +0100188'
189
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600190test_expect_success 'option overrides .gitattributes' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600191 cp expect.letter-runs-are-words expect &&
Thomas Rast80c49c32009-01-17 17:29:48 +0100192 word_diff --color-words="[a-z]+"
Thomas Rast80c49c32009-01-17 17:29:48 +0100193'
194
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600195test_expect_success 'use regex supplied by driver' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600196 cp expect.non-whitespace-is-word expect &&
Thomas Rast80c49c32009-01-17 17:29:48 +0100197 word_diff --color-words
Thomas Rast80c49c32009-01-17 17:29:48 +0100198'
199
Jonathan Nieder5094d152011-01-11 15:49:57 -0600200test_expect_success 'set up diff.wordRegex option' '
Boyd Stephen Smith Jrae3b9702009-01-20 22:59:54 -0600201 git config diff.wordRegex "[[:alnum:]]+"
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600202'
203
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600204test_expect_success 'command-line overrides config' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600205 cp expect.letter-runs-are-words expect &&
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600206 word_diff --color-words="[a-z]+"
207'
208
Thomas Rast882749a2010-04-14 17:59:06 +0200209test_expect_success 'command-line overrides config: --word-diff-regex' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600210 cat >expect <<-\EOF &&
211 <BOLD>diff --git a/pre b/post<RESET>
212 <BOLD>index 330b04f..5ed8eff 100644<RESET>
213 <BOLD>--- a/pre<RESET>
214 <BOLD>+++ b/post<RESET>
215 <CYAN>@@ -1,3 +1,7 @@<RESET>
216 h(4),<GREEN>{+hh+}<RESET>[44]
217
218 a = b + c<RESET>
219
220 <GREEN>{+aa = a+}<RESET>
221
222 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
223 EOF
Thomas Rast882749a2010-04-14 17:59:06 +0200224 word_diff --color --word-diff-regex="[a-z]+"
225'
226
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600227test_expect_success '.gitattributes override config' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600228 cp expect.non-whitespace-is-word expect &&
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600229 word_diff --color-words
230'
231
Jonathan Nieder5094d152011-01-11 15:49:57 -0600232test_expect_success 'setup: remove diff driver regex' '
Yann Droneaudff73aa42013-03-24 22:06:05 +0100233 test_unconfig diff.testdriver.wordRegex
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600234'
235
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600236test_expect_success 'use configured regex' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600237 cat >expect <<-\EOF &&
238 <BOLD>diff --git a/pre b/post<RESET>
239 <BOLD>index 330b04f..5ed8eff 100644<RESET>
240 <BOLD>--- a/pre<RESET>
241 <BOLD>+++ b/post<RESET>
242 <CYAN>@@ -1,3 +1,7 @@<RESET>
243 h(4),<GREEN>hh[44<RESET>]
244
245 a = b + c<RESET>
246
247 <GREEN>aa = a<RESET>
248
249 <GREEN>aeff = aeff * ( aaa<RESET> )
250 EOF
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600251 word_diff --color-words
252'
253
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100254test_expect_success 'test parsing words for newline' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600255 echo "aaa (aaa)" >pre &&
256 echo "aaa (aaa) aaa" >post &&
257 cat >expect <<-\EOF &&
258 <BOLD>diff --git a/pre b/post<RESET>
259 <BOLD>index c29453b..be22f37 100644<RESET>
260 <BOLD>--- a/pre<RESET>
261 <BOLD>+++ b/post<RESET>
262 <CYAN>@@ -1 +1 @@<RESET>
263 aaa (aaa) <GREEN>aaa<RESET>
264 EOF
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100265 word_diff --color-words="a+"
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100266'
267
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100268test_expect_success 'test when words are only removed at the end' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600269 echo "(:" >pre &&
270 echo "(" >post &&
271 cat >expect <<-\EOF &&
272 <BOLD>diff --git a/pre b/post<RESET>
273 <BOLD>index 289cb9d..2d06f37 100644<RESET>
274 <BOLD>--- a/pre<RESET>
275 <BOLD>+++ b/post<RESET>
276 <CYAN>@@ -1 +1 @@<RESET>
277 (<RED>:<RESET>
278 EOF
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100279 word_diff --color-words=.
Johannes Schindelin2b6a5412009-01-17 17:29:45 +0100280'
281
Thomas Rast882749a2010-04-14 17:59:06 +0200282test_expect_success '--word-diff=none' '
Jonathan Nieder5094d152011-01-11 15:49:57 -0600283 echo "(:" >pre &&
284 echo "(" >post &&
285 cat >expect <<-\EOF &&
286 diff --git a/pre b/post
287 index 289cb9d..2d06f37 100644
288 --- a/pre
289 +++ b/post
290 @@ -1 +1 @@
291 -(:
292 +(
293 EOF
Thomas Rast882749a2010-04-14 17:59:06 +0200294 word_diff --word-diff=plain --word-diff=none
Thomas Rast882749a2010-04-14 17:59:06 +0200295'
296
Johannes Sixt62d39352012-03-14 20:50:21 +0100297test_expect_success 'unset default driver' '
298 test_unconfig diff.wordregex
299'
300
Adrian Johnsone90d0652012-09-16 13:24:15 +0930301test_language_driver ada
Jonathan Nieder5094d152011-01-11 15:49:57 -0600302test_language_driver bibtex
303test_language_driver cpp
304test_language_driver csharp
William Duclot0719f3e2016-06-03 14:32:26 +0200305test_language_driver css
Jonathan Nieder5094d152011-01-11 15:49:57 -0600306test_language_driver fortran
307test_language_driver html
308test_language_driver java
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100309test_language_driver matlab
Jonathan Nieder5094d152011-01-11 15:49:57 -0600310test_language_driver objc
311test_language_driver pascal
Junio C Hamano5269edf2011-01-18 09:43:43 -0800312test_language_driver perl
Jonathan Nieder5094d152011-01-11 15:49:57 -0600313test_language_driver php
314test_language_driver python
315test_language_driver ruby
316test_language_driver tex
Thomas Rast8d96e722010-12-18 17:17:54 +0100317
Jim Meyering42536dd2011-05-20 19:20:12 +0200318test_expect_success 'word-diff with diff.sbe' '
319 cat >expect <<-\EOF &&
320 diff --git a/pre b/post
321 index a1a53b5..bc8fe6d 100644
322 --- a/pre
323 +++ b/post
324 @@ -1,3 +1,3 @@
325 a
326
327 [-b-]{+c+}
328 EOF
329 cat >pre <<-\EOF &&
330 a
331
332 b
333 EOF
334 cat >post <<-\EOF &&
335 a
336
337 c
338 EOF
Yann Droneaudff73aa42013-03-24 22:06:05 +0100339 test_config diff.suppress-blank-empty true &&
Jim Meyering42536dd2011-05-20 19:20:12 +0200340 word_diff --word-diff=plain
341'
342
Thomas Rastc7c2bc02012-01-12 12:15:33 +0100343test_expect_success 'word-diff with no newline at EOF' '
344 cat >expect <<-\EOF &&
345 diff --git a/pre b/post
346 index 7bf316e..3dd0303 100644
347 --- a/pre
348 +++ b/post
349 @@ -1 +1 @@
350 a a [-a-]{+ab+} a a
351 EOF
352 printf "%s" "a a a a a" >pre &&
353 printf "%s" "a a ab a a" >post &&
354 word_diff --word-diff=plain
355'
356
Johannes Sixt62d39352012-03-14 20:50:21 +0100357test_expect_success 'setup history with two files' '
358 echo "a b; c" >a.tex &&
359 echo "a b; c" >z.txt &&
360 git add a.tex z.txt &&
361 git commit -minitial &&
362
363 # modify both
364 echo "a bx; c" >a.tex &&
365 echo "a bx; c" >z.txt &&
366 git commit -mmodified -a
367'
368
Thomas Rast6440d342012-03-14 19:24:09 +0100369test_expect_success 'wordRegex for the first file does not apply to the second' '
Johannes Sixt62d39352012-03-14 20:50:21 +0100370 echo "*.tex diff=tex" >.gitattributes &&
Yann Droneaudff73aa42013-03-24 22:06:05 +0100371 test_config diff.tex.wordRegex "[a-z]+|." &&
Johannes Sixt62d39352012-03-14 20:50:21 +0100372 cat >expect <<-\EOF &&
373 diff --git a/a.tex b/a.tex
374 --- a/a.tex
375 +++ b/a.tex
376 @@ -1 +1 @@
377 a [-b-]{+bx+}; c
378 diff --git a/z.txt b/z.txt
379 --- a/z.txt
380 +++ b/z.txt
381 @@ -1 +1 @@
382 a [-b;-]{+bx;+} c
383 EOF
384 git diff --word-diff HEAD~ >actual &&
385 compare_diff_patch expect actual
386'
387
Johannes Schindelin2e5d2002009-01-17 17:29:44 +0100388test_done