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