]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4034-diff-words.sh
userdiff: simplify word-diff safeguard
[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
6
7test_expect_success setup '
8
a48fcd83
JN
9 git config diff.color.old red &&
10 git config diff.color.new green &&
89cb73a1 11 git config diff.color.func magenta
2e5d2003
JS
12
13'
14
2e5d2003
JS
15word_diff () {
16 test_must_fail git diff --no-index "$@" pre post > output &&
68cfc6f5 17 test_decode_color <output >output.decrypted &&
2e5d2003
JS
18 test_cmp expect output.decrypted
19}
20
21cat > pre <<\EOF
22h(4)
23
24a = b + c
25EOF
26
27cat > post <<\EOF
28h(4),hh[44]
29
30a = b + c
31
32aa = a
33
34aeff = aeff * ( aaa )
35EOF
36
37cat > expect <<\EOF
a471833d
KB
38<BOLD>diff --git a/pre b/post<RESET>
39<BOLD>index 330b04f..5ed8eff 100644<RESET>
40<BOLD>--- a/pre<RESET>
41<BOLD>+++ b/post<RESET>
68cfc6f5 42<CYAN>@@ -1,3 +1,7 @@<RESET>
2e5d2003 43<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
06a47552 44
2e5d2003
JS
45a = b + c<RESET>
46
47<GREEN>aa = a<RESET>
48
49<GREEN>aeff = aeff * ( aaa )<RESET>
50EOF
51
52test_expect_success 'word diff with runs of whitespace' '
53
54 word_diff --color-words
55
56'
57
882749a0
TR
58test_expect_success '--word-diff=color' '
59
60 word_diff --word-diff=color
61
62'
63
64test_expect_success '--color --word-diff=color' '
65
66 word_diff --color --word-diff=color
67
68'
69
70sed 's/#.*$//' > expect <<EOF
71diff --git a/pre b/post
72index 330b04f..5ed8eff 100644
73--- a/pre
74+++ b/post
75@@ -1,3 +1,7 @@
76-h(4)
77+h(4),hh[44]
78~
79 # significant space
80~
81 a = b + c
82~
83~
84+aa = a
85~
86~
87+aeff = aeff * ( aaa )
88~
89EOF
90
91test_expect_success '--word-diff=porcelain' '
92
93 word_diff --word-diff=porcelain
94
95'
96
97cat > expect <<EOF
98diff --git a/pre b/post
99index 330b04f..5ed8eff 100644
100--- a/pre
101+++ b/post
102@@ -1,3 +1,7 @@
103[-h(4)-]{+h(4),hh[44]+}
104
105a = b + c
106
107{+aa = a+}
108
109{+aeff = aeff * ( aaa )+}
110EOF
111
112test_expect_success '--word-diff=plain' '
113
114 word_diff --word-diff=plain
115
116'
117
118test_expect_success '--word-diff=plain --no-color' '
119
120 word_diff --word-diff=plain --no-color
121
122'
123
124cat > expect <<EOF
a471833d
KB
125<BOLD>diff --git a/pre b/post<RESET>
126<BOLD>index 330b04f..5ed8eff 100644<RESET>
127<BOLD>--- a/pre<RESET>
128<BOLD>+++ b/post<RESET>
882749a0
TR
129<CYAN>@@ -1,3 +1,7 @@<RESET>
130<RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
131
132a = b + c<RESET>
133
134<GREEN>{+aa = a+}<RESET>
135
136<GREEN>{+aeff = aeff * ( aaa )+}<RESET>
137EOF
138
139test_expect_success '--word-diff=plain --color' '
140
141 word_diff --word-diff=plain --color
142
143'
144
168eff3c 145cat > expect <<\EOF
a471833d
KB
146<BOLD>diff --git a/pre b/post<RESET>
147<BOLD>index 330b04f..5ed8eff 100644<RESET>
148<BOLD>--- a/pre<RESET>
149<BOLD>+++ b/post<RESET>
c2ff10c9 150<CYAN>@@ -1 +1 @@<RESET>
168eff3c 151<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
c2ff10c9 152<CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
168eff3c
MH
153
154<GREEN>aa = a<RESET>
155
156<GREEN>aeff = aeff * ( aaa )<RESET>
157EOF
158
a4ca1465 159test_expect_success 'word diff without context' '
168eff3c
MH
160
161 word_diff --color-words --unified=0
162
163'
164
2b6a5417 165cat > expect <<\EOF
a471833d
KB
166<BOLD>diff --git a/pre b/post<RESET>
167<BOLD>index 330b04f..5ed8eff 100644<RESET>
168<BOLD>--- a/pre<RESET>
169<BOLD>+++ b/post<RESET>
68cfc6f5 170<CYAN>@@ -1,3 +1,7 @@<RESET>
2b6a5417 171h(4),<GREEN>hh<RESET>[44]
06a47552 172
2b6a5417
JS
173a = b + c<RESET>
174
175<GREEN>aa = a<RESET>
176
177<GREEN>aeff = aeff * ( aaa<RESET> )
178EOF
98a4d87b 179cp expect expect.letter-runs-are-words
2b6a5417
JS
180
181test_expect_success 'word diff with a regular expression' '
182
183 word_diff --color-words="[a-z]+"
184
185'
186
80c49c3d 187test_expect_success 'set a diff driver' '
ae3b970a 188 git config diff.testdriver.wordRegex "[^[:space:]]" &&
80c49c3d
TR
189 cat <<EOF > .gitattributes
190pre diff=testdriver
191post diff=testdriver
192EOF
193'
194
98a4d87b 195test_expect_success 'option overrides .gitattributes' '
80c49c3d
TR
196
197 word_diff --color-words="[a-z]+"
198
199'
200
201cat > expect <<\EOF
a471833d
KB
202<BOLD>diff --git a/pre b/post<RESET>
203<BOLD>index 330b04f..5ed8eff 100644<RESET>
204<BOLD>--- a/pre<RESET>
205<BOLD>+++ b/post<RESET>
68cfc6f5 206<CYAN>@@ -1,3 +1,7 @@<RESET>
80c49c3d 207h(4)<GREEN>,hh[44]<RESET>
06a47552 208
80c49c3d
TR
209a = b + c<RESET>
210
211<GREEN>aa = a<RESET>
212
213<GREEN>aeff = aeff * ( aaa )<RESET>
214EOF
98a4d87b 215cp expect expect.non-whitespace-is-word
80c49c3d 216
98a4d87b 217test_expect_success 'use regex supplied by driver' '
80c49c3d
TR
218
219 word_diff --color-words
220
221'
222
ae3b970a
BSSJ
223test_expect_success 'set diff.wordRegex option' '
224 git config diff.wordRegex "[[:alnum:]]+"
98a4d87b
BSSJ
225'
226
227cp expect.letter-runs-are-words expect
228
229test_expect_success 'command-line overrides config' '
230 word_diff --color-words="[a-z]+"
231'
232
882749a0 233cat > expect <<\EOF
a471833d
KB
234<BOLD>diff --git a/pre b/post<RESET>
235<BOLD>index 330b04f..5ed8eff 100644<RESET>
236<BOLD>--- a/pre<RESET>
237<BOLD>+++ b/post<RESET>
882749a0
TR
238<CYAN>@@ -1,3 +1,7 @@<RESET>
239h(4),<GREEN>{+hh+}<RESET>[44]
240
241a = b + c<RESET>
242
243<GREEN>{+aa = a+}<RESET>
244
245<GREEN>{+aeff = aeff * ( aaa+}<RESET> )
246EOF
247
248test_expect_success 'command-line overrides config: --word-diff-regex' '
249 word_diff --color --word-diff-regex="[a-z]+"
250'
251
98a4d87b
BSSJ
252cp expect.non-whitespace-is-word expect
253
254test_expect_success '.gitattributes override config' '
255 word_diff --color-words
256'
257
258test_expect_success 'remove diff driver regex' '
ae3b970a 259 git config --unset diff.testdriver.wordRegex
98a4d87b
BSSJ
260'
261
262cat > expect <<\EOF
a471833d
KB
263<BOLD>diff --git a/pre b/post<RESET>
264<BOLD>index 330b04f..5ed8eff 100644<RESET>
265<BOLD>--- a/pre<RESET>
266<BOLD>+++ b/post<RESET>
68cfc6f5 267<CYAN>@@ -1,3 +1,7 @@<RESET>
98a4d87b 268h(4),<GREEN>hh[44<RESET>]
06a47552 269
98a4d87b
BSSJ
270a = b + c<RESET>
271
272<GREEN>aa = a<RESET>
273
274<GREEN>aeff = aeff * ( aaa<RESET> )
275EOF
276
277test_expect_success 'use configured regex' '
278 word_diff --color-words
279'
280
2b6a5417
JS
281echo 'aaa (aaa)' > pre
282echo 'aaa (aaa) aaa' > post
283
284cat > expect <<\EOF
a471833d
KB
285<BOLD>diff --git a/pre b/post<RESET>
286<BOLD>index c29453b..be22f37 100644<RESET>
287<BOLD>--- a/pre<RESET>
288<BOLD>+++ b/post<RESET>
68cfc6f5 289<CYAN>@@ -1 +1 @@<RESET>
2b6a5417
JS
290aaa (aaa) <GREEN>aaa<RESET>
291EOF
292
293test_expect_success 'test parsing words for newline' '
294
295 word_diff --color-words="a+"
296
80c49c3d 297
2b6a5417
JS
298'
299
300echo '(:' > pre
301echo '(' > post
302
303cat > expect <<\EOF
a471833d
KB
304<BOLD>diff --git a/pre b/post<RESET>
305<BOLD>index 289cb9d..2d06f37 100644<RESET>
306<BOLD>--- a/pre<RESET>
307<BOLD>+++ b/post<RESET>
68cfc6f5 308<CYAN>@@ -1 +1 @@<RESET>
2b6a5417
JS
309(<RED>:<RESET>
310EOF
311
312test_expect_success 'test when words are only removed at the end' '
313
314 word_diff --color-words=.
315
316'
317
882749a0
TR
318cat > expect <<\EOF
319diff --git a/pre b/post
320index 289cb9d..2d06f37 100644
321--- a/pre
322+++ b/post
323@@ -1 +1 @@
324-(:
325+(
326EOF
327
328test_expect_success '--word-diff=none' '
329
330 word_diff --word-diff=plain --word-diff=none
331
332'
333
8d96e728
TR
334word_diff_for_language () {
335 cp "$TEST_DIRECTORY/t4034/$1/pre" \
336 "$TEST_DIRECTORY/t4034/$1/post" \
337 "$TEST_DIRECTORY/t4034/$1/expect" . &&
338 echo "* diff=$1" >.gitattributes &&
339 word_diff --color-words && cp output output.$1
340}
341
342for lang_dir in $TEST_DIRECTORY/t4034/*; do
343 lang=${lang_dir#$TEST_DIRECTORY/t4034/}
344 test_expect_success "diff driver '$lang' has sane word regex" "
345 word_diff_for_language $lang
346 "
347done
348
2e5d2003 349test_done