1#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
6
7test_expect_success setup '
8
9 git config diff.color.old red
10 git config diff.color.new green
11
12'
13
14word_diff () {
15 test_must_fail git diff --no-index "$@" pre post > output &&
16 test_decode_color <output >output.decrypted &&
17 test_cmp expect output.decrypted
18}
19
20cat > pre <<\EOF
21h(4)
22
23a = b + c
24EOF
25
26cat > post <<\EOF
27h(4),hh[44]
28
29a = b + c
30
31aa = a
32
33aeff = aeff * ( aaa )
34EOF
35
36cat > expect <<\EOF
37<WHITE>diff --git a/pre b/post<RESET>
38<WHITE>index 330b04f..5ed8eff 100644<RESET>
39<WHITE>--- a/pre<RESET>
40<WHITE>+++ b/post<RESET>
41<CYAN>@@ -1,3 +1,7 @@<RESET>
42<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
43<RESET>
44a = b + c<RESET>
45
46<GREEN>aa = a<RESET>
47
48<GREEN>aeff = aeff * ( aaa )<RESET>
49EOF
50
51test_expect_success 'word diff with runs of whitespace' '
52
53 word_diff --color-words
54
55'
56
57cat > expect <<\EOF
58<WHITE>diff --git a/pre b/post<RESET>
59<WHITE>index 330b04f..5ed8eff 100644<RESET>
60<WHITE>--- a/pre<RESET>
61<WHITE>+++ b/post<RESET>
62<CYAN>@@ -1,3 +1,7 @@<RESET>
63h(4),<GREEN>hh<RESET>[44]
64<RESET>
65a = b + c<RESET>
66
67<GREEN>aa = a<RESET>
68
69<GREEN>aeff = aeff * ( aaa<RESET> )
70EOF
71cp expect expect.letter-runs-are-words
72
73test_expect_success 'word diff with a regular expression' '
74
75 word_diff --color-words="[a-z]+"
76
77'
78
79test_expect_success 'set a diff driver' '
80 git config diff.testdriver.wordRegex "[^[:space:]]" &&
81 cat <<EOF > .gitattributes
82pre diff=testdriver
83post diff=testdriver
84EOF
85'
86
87test_expect_success 'option overrides .gitattributes' '
88
89 word_diff --color-words="[a-z]+"
90
91'
92
93cat > expect <<\EOF
94<WHITE>diff --git a/pre b/post<RESET>
95<WHITE>index 330b04f..5ed8eff 100644<RESET>
96<WHITE>--- a/pre<RESET>
97<WHITE>+++ b/post<RESET>
98<CYAN>@@ -1,3 +1,7 @@<RESET>
99h(4)<GREEN>,hh[44]<RESET>
100<RESET>
101a = b + c<RESET>
102
103<GREEN>aa = a<RESET>
104
105<GREEN>aeff = aeff * ( aaa )<RESET>
106EOF
107cp expect expect.non-whitespace-is-word
108
109test_expect_success 'use regex supplied by driver' '
110
111 word_diff --color-words
112
113'
114
115test_expect_success 'set diff.wordRegex option' '
116 git config diff.wordRegex "[[:alnum:]]+"
117'
118
119cp expect.letter-runs-are-words expect
120
121test_expect_success 'command-line overrides config' '
122 word_diff --color-words="[a-z]+"
123'
124
125cp expect.non-whitespace-is-word expect
126
127test_expect_success '.gitattributes override config' '
128 word_diff --color-words
129'
130
131test_expect_success 'remove diff driver regex' '
132 git config --unset diff.testdriver.wordRegex
133'
134
135cat > expect <<\EOF
136<WHITE>diff --git a/pre b/post<RESET>
137<WHITE>index 330b04f..5ed8eff 100644<RESET>
138<WHITE>--- a/pre<RESET>
139<WHITE>+++ b/post<RESET>
140<CYAN>@@ -1,3 +1,7 @@<RESET>
141h(4),<GREEN>hh[44<RESET>]
142<RESET>
143a = b + c<RESET>
144
145<GREEN>aa = a<RESET>
146
147<GREEN>aeff = aeff * ( aaa<RESET> )
148EOF
149
150test_expect_success 'use configured regex' '
151 word_diff --color-words
152'
153
154echo 'aaa (aaa)' > pre
155echo 'aaa (aaa) aaa' > post
156
157cat > expect <<\EOF
158<WHITE>diff --git a/pre b/post<RESET>
159<WHITE>index c29453b..be22f37 100644<RESET>
160<WHITE>--- a/pre<RESET>
161<WHITE>+++ b/post<RESET>
162<CYAN>@@ -1 +1 @@<RESET>
163aaa (aaa) <GREEN>aaa<RESET>
164EOF
165
166test_expect_success 'test parsing words for newline' '
167
168 word_diff --color-words="a+"
169
170
171'
172
173echo '(:' > pre
174echo '(' > post
175
176cat > expect <<\EOF
177<WHITE>diff --git a/pre b/post<RESET>
178<WHITE>index 289cb9d..2d06f37 100644<RESET>
179<WHITE>--- a/pre<RESET>
180<WHITE>+++ b/post<RESET>
181<CYAN>@@ -1 +1 @@<RESET>
182(<RED>:<RESET>
183EOF
184
185test_expect_success 'test when words are only removed at the end' '
186
187 word_diff --color-words=.
188
189'
190
191test_done