]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4034-diff-words.sh
sequencer: lib'ify read_and_refresh_cache()
[thirdparty/git.git] / t / t4034-diff-words.sh
CommitLineData
2e5d2003
JS
1#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
62d39359 6. "$TEST_DIRECTORY"/diff-lib.sh
2e5d2003 7
5094d158
JN
8cat >pre.simple <<-\EOF
9 h(4)
2e5d2003 10
5094d158
JN
11 a = b + c
12EOF
13cat >post.simple <<-\EOF
14 h(4),hh[44]
2e5d2003 15
5094d158 16 a = b + c
2e5d2003 17
5094d158 18 aa = a
2e5d2003 19
5094d158 20 aeff = aeff * ( aaa )
2e5d2003 21EOF
5094d158
JN
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]
2e5d2003 29
5094d158 30 a = b + c<RESET>
2e5d2003 31
5094d158 32 <GREEN>aa = a<RESET>
2e5d2003 33
5094d158 34 <GREEN>aeff = aeff * ( aaa<RESET> )
2e5d2003 35EOF
5094d158
JN
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>
2e5d2003 43
5094d158 44 a = b + c<RESET>
2e5d2003 45
5094d158 46 <GREEN>aa = a<RESET>
2e5d2003 47
5094d158 48 <GREEN>aeff = aeff * ( aaa )<RESET>
2e5d2003
JS
49EOF
50
5094d158
JN
51word_diff () {
52 test_must_fail git diff --no-index "$@" pre post >output &&
53 test_decode_color <output >output.decrypted &&
54 test_cmp expect output.decrypted
55}
2e5d2003 56
5094d158
JN
57test_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}
2e5d2003 67
5094d158
JN
68test_expect_success setup '
69 git config diff.color.old red &&
70 git config diff.color.new green &&
71 git config diff.color.func magenta
2e5d2003
JS
72'
73
5094d158
JN
74test_expect_success 'set up pre and post with runs of whitespace' '
75 cp pre.simple pre &&
76 cp post.simple post
882749a0
TR
77'
78
5094d158
JN
79test_expect_success 'word diff with runs of whitespace' '
80 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>
87
88 a = b + c<RESET>
89
90 <GREEN>aa = a<RESET>
91
92 <GREEN>aeff = aeff * ( aaa )<RESET>
93 EOF
94 word_diff --color-words &&
95 word_diff --word-diff=color &&
882749a0 96 word_diff --color --word-diff=color
882749a0
TR
97'
98
882749a0 99test_expect_success '--word-diff=porcelain' '
5094d158
JN
100 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
882749a0 120 word_diff --word-diff=porcelain
882749a0
TR
121'
122
882749a0 123test_expect_success '--word-diff=plain' '
5094d158
JN
124 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]+}
882749a0 131
5094d158 132 a = b + c
882749a0 133
5094d158 134 {+aa = a+}
882749a0 135
5094d158
JN
136 {+aeff = aeff * ( aaa )+}
137 EOF
138 word_diff --word-diff=plain &&
882749a0 139 word_diff --word-diff=plain --no-color
882749a0
TR
140'
141
5094d158
JN
142test_expect_success '--word-diff=plain --color' '
143 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>
882749a0 150
5094d158 151 a = b + c<RESET>
882749a0 152
5094d158 153 <GREEN>{+aa = a+}<RESET>
882749a0 154
5094d158
JN
155 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
156 EOF
882749a0 157 word_diff --word-diff=plain --color
882749a0
TR
158'
159
a4ca1465 160test_expect_success 'word diff without context' '
5094d158
JN
161 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>
169
170 <GREEN>aa = a<RESET>
171
172 <GREEN>aeff = aeff * ( aaa )<RESET>
173 EOF
168eff3c 174 word_diff --color-words --unified=0
168eff3c
MH
175'
176
2b6a5417 177test_expect_success 'word diff with a regular expression' '
5094d158 178 cp expect.letter-runs-are-words expect &&
2b6a5417 179 word_diff --color-words="[a-z]+"
2b6a5417
JS
180'
181
5094d158 182test_expect_success 'set up a diff driver' '
ae3b970a 183 git config diff.testdriver.wordRegex "[^[:space:]]" &&
5094d158
JN
184 cat <<-\EOF >.gitattributes
185 pre diff=testdriver
186 post diff=testdriver
187 EOF
80c49c3d
TR
188'
189
98a4d87b 190test_expect_success 'option overrides .gitattributes' '
5094d158 191 cp expect.letter-runs-are-words expect &&
80c49c3d 192 word_diff --color-words="[a-z]+"
80c49c3d
TR
193'
194
98a4d87b 195test_expect_success 'use regex supplied by driver' '
5094d158 196 cp expect.non-whitespace-is-word expect &&
80c49c3d 197 word_diff --color-words
80c49c3d
TR
198'
199
5094d158 200test_expect_success 'set up diff.wordRegex option' '
ae3b970a 201 git config diff.wordRegex "[[:alnum:]]+"
98a4d87b
BSSJ
202'
203
98a4d87b 204test_expect_success 'command-line overrides config' '
5094d158 205 cp expect.letter-runs-are-words expect &&
98a4d87b
BSSJ
206 word_diff --color-words="[a-z]+"
207'
208
5094d158
JN
209test_expect_success 'command-line overrides config: --word-diff-regex' '
210 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]
882749a0 217
5094d158 218 a = b + c<RESET>
882749a0 219
5094d158 220 <GREEN>{+aa = a+}<RESET>
882749a0 221
5094d158
JN
222 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
223 EOF
882749a0
TR
224 word_diff --color --word-diff-regex="[a-z]+"
225'
226
98a4d87b 227test_expect_success '.gitattributes override config' '
5094d158 228 cp expect.non-whitespace-is-word expect &&
98a4d87b
BSSJ
229 word_diff --color-words
230'
231
5094d158 232test_expect_success 'setup: remove diff driver regex' '
ff73aa40 233 test_unconfig diff.testdriver.wordRegex
98a4d87b
BSSJ
234'
235
5094d158
JN
236test_expect_success 'use configured regex' '
237 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>]
98a4d87b 244
5094d158 245 a = b + c<RESET>
98a4d87b 246
5094d158 247 <GREEN>aa = a<RESET>
98a4d87b 248
5094d158
JN
249 <GREEN>aeff = aeff * ( aaa<RESET> )
250 EOF
98a4d87b
BSSJ
251 word_diff --color-words
252'
253
2b6a5417 254test_expect_success 'test parsing words for newline' '
5094d158
JN
255 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
2b6a5417 265 word_diff --color-words="a+"
2b6a5417
JS
266'
267
2b6a5417 268test_expect_success 'test when words are only removed at the end' '
5094d158
JN
269 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
2b6a5417 279 word_diff --color-words=.
2b6a5417
JS
280'
281
882749a0 282test_expect_success '--word-diff=none' '
5094d158
JN
283 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
882749a0 294 word_diff --word-diff=plain --word-diff=none
882749a0
TR
295'
296
62d39359
JS
297test_expect_success 'unset default driver' '
298 test_unconfig diff.wordregex
299'
300
e90d065e 301test_language_driver ada
5094d158
JN
302test_language_driver bibtex
303test_language_driver cpp
304test_language_driver csharp
0719f3ee 305test_language_driver css
5094d158
JN
306test_language_driver fortran
307test_language_driver html
308test_language_driver java
53b10a14 309test_language_driver matlab
5094d158
JN
310test_language_driver objc
311test_language_driver pascal
5269edf1 312test_language_driver perl
5094d158
JN
313test_language_driver php
314test_language_driver python
315test_language_driver ruby
316test_language_driver tex
8d96e728 317
42536dd9
JM
318test_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
ff73aa40 339 test_config diff.suppress-blank-empty true &&
42536dd9
JM
340 word_diff --word-diff=plain
341'
342
c7c2bc0a
TR
343test_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
62d39359
JS
357test_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
6440d341 369test_expect_success 'wordRegex for the first file does not apply to the second' '
62d39359 370 echo "*.tex diff=tex" >.gitattributes &&
ff73aa40 371 test_config diff.tex.wordRegex "[a-z]+|." &&
62d39359
JS
372 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
2e5d2003 388test_done