]> asedeno.scripts.mit.edu Git - git.git/blob - t/t4034-diff-words.sh
t7508-status: test all modes with color
[git.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 word_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
20 cat > pre <<\EOF
21 h(4)
22
23 a = b + c
24 EOF
25
26 cat > post <<\EOF
27 h(4),hh[44]
28
29 a = b + c
30
31 aa = a
32
33 aeff = aeff * ( aaa )
34 EOF
35
36 cat > 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>
44 a = b + c<RESET>
45
46 <GREEN>aa = a<RESET>
47
48 <GREEN>aeff = aeff * ( aaa )<RESET>
49 EOF
50
51 test_expect_success 'word diff with runs of whitespace' '
52
53         word_diff --color-words
54
55 '
56
57 cat > 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>
63 h(4),<GREEN>hh<RESET>[44]
64 <RESET>
65 a = b + c<RESET>
66
67 <GREEN>aa = a<RESET>
68
69 <GREEN>aeff = aeff * ( aaa<RESET> )
70 EOF
71 cp expect expect.letter-runs-are-words
72
73 test_expect_success 'word diff with a regular expression' '
74
75         word_diff --color-words="[a-z]+"
76
77 '
78
79 test_expect_success 'set a diff driver' '
80         git config diff.testdriver.wordRegex "[^[:space:]]" &&
81         cat <<EOF > .gitattributes
82 pre diff=testdriver
83 post diff=testdriver
84 EOF
85 '
86
87 test_expect_success 'option overrides .gitattributes' '
88
89         word_diff --color-words="[a-z]+"
90
91 '
92
93 cat > 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>
99 h(4)<GREEN>,hh[44]<RESET>
100 <RESET>
101 a = b + c<RESET>
102
103 <GREEN>aa = a<RESET>
104
105 <GREEN>aeff = aeff * ( aaa )<RESET>
106 EOF
107 cp expect expect.non-whitespace-is-word
108
109 test_expect_success 'use regex supplied by driver' '
110
111         word_diff --color-words
112
113 '
114
115 test_expect_success 'set diff.wordRegex option' '
116         git config diff.wordRegex "[[:alnum:]]+"
117 '
118
119 cp expect.letter-runs-are-words expect
120
121 test_expect_success 'command-line overrides config' '
122         word_diff --color-words="[a-z]+"
123 '
124
125 cp expect.non-whitespace-is-word expect
126
127 test_expect_success '.gitattributes override config' '
128         word_diff --color-words
129 '
130
131 test_expect_success 'remove diff driver regex' '
132         git config --unset diff.testdriver.wordRegex
133 '
134
135 cat > 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>
141 h(4),<GREEN>hh[44<RESET>]
142 <RESET>
143 a = b + c<RESET>
144
145 <GREEN>aa = a<RESET>
146
147 <GREEN>aeff = aeff * ( aaa<RESET> )
148 EOF
149
150 test_expect_success 'use configured regex' '
151         word_diff --color-words
152 '
153
154 echo 'aaa (aaa)' > pre
155 echo 'aaa (aaa) aaa' > post
156
157 cat > 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>
163 aaa (aaa) <GREEN>aaa<RESET>
164 EOF
165
166 test_expect_success 'test parsing words for newline' '
167
168         word_diff --color-words="a+"
169
170
171 '
172
173 echo '(:' > pre
174 echo '(' > post
175
176 cat > 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>
183 EOF
184
185 test_expect_success 'test when words are only removed at the end' '
186
187         word_diff --color-words=.
188
189 '
190
191 test_done