1#!/bin/sh
2
3test_description='Test diff-highlight'
4
5CURR_DIR=$(pwd)
6TEST_OUTPUT_DIRECTORY=$(pwd)
7TEST_DIRECTORY="$CURR_DIR"/../../../t
8DIFF_HIGHLIGHT="$CURR_DIR"/../diff-highlight
9
10CW="$(printf "\033[7m")" # white
11CR="$(printf "\033[27m")" # reset
12
13. "$TEST_DIRECTORY"/test-lib.sh
14
15if ! test_have_prereq PERL
16then
17 skip_all='skipping diff-highlight tests; perl not available'
18 test_done
19fi
20
21# dh_test is a test helper function which takes 3 file names as parameters. The
22# first 2 files are used to generate diff and commit output, which is then
23# piped through diff-highlight. The 3rd file should contain the expected output
24# of diff-highlight (minus the diff/commit header, ie. everything after and
25# including the first @@ line).
26dh_test () {
27 a="$1" b="$2" &&
28
29 cat >patch.exp &&
30
31 {
32 cat "$a" >file &&
33 git add file &&
34 git commit -m "Add a file" &&
35
36 cat "$b" >file &&
37 git diff file >diff.raw &&
38 git commit -a -m "Update a file" &&
39 git show >commit.raw
40 } >/dev/null &&
41
42 "$DIFF_HIGHLIGHT" <diff.raw | test_strip_patch_header >diff.act &&
43 "$DIFF_HIGHLIGHT" <commit.raw | test_strip_patch_header >commit.act &&
44 test_cmp patch.exp diff.act &&
45 test_cmp patch.exp commit.act
46}
47
48test_strip_patch_header () {
49 sed -n '/^@@/,$p' $*
50}
51
52# dh_test_setup_history generates a contrived graph such that we have at least
53# 1 nesting (E) and 2 nestings (F).
54#
55# A---B master
56# /
57# D---E---F branch
58#
59# git log --all --graph
60# * commit
61# | B
62# | * commit
63# | | F
64# * | commit
65# | | A
66# | * commit
67# |/
68# | E
69# * commit
70# D
71#
72dh_test_setup_history () {
73 echo file1 >file &&
74 git add file &&
75 test_tick &&
76 git commit -m "D" &&
77
78 git checkout -b branch &&
79 echo file2 >file &&
80 test_tick &&
81 git commit -a -m "E" &&
82
83 git checkout master &&
84 echo file2 >file &&
85 test_tick &&
86 git commit -a -m "A" &&
87
88 git checkout branch &&
89 echo file3 >file &&
90 test_tick &&
91 git commit -a -m "F" &&
92
93 git checkout master &&
94 echo file3 >file &&
95 test_tick &&
96 git commit -a -m "B"
97}
98
99left_trim () {
100 "$PERL_PATH" -pe 's/^\s+//'
101}
102
103trim_graph () {
104 # graphs start with * or |
105 # followed by a space or / or \
106 "$PERL_PATH" -pe 's@^((\*|\|)( |/|\\))+@@'
107}
108
109test_expect_success 'diff-highlight highlights the beginning of a line' '
110 cat >a <<-\EOF &&
111 aaa
112 bbb
113 ccc
114 EOF
115
116 cat >b <<-\EOF &&
117 aaa
118 0bb
119 ccc
120 EOF
121
122 dh_test a b <<-EOF
123 @@ -1,3 +1,3 @@
124 aaa
125 -${CW}b${CR}bb
126 +${CW}0${CR}bb
127 ccc
128 EOF
129'
130
131test_expect_success 'diff-highlight highlights the end of a line' '
132 cat >a <<-\EOF &&
133 aaa
134 bbb
135 ccc
136 EOF
137
138 cat >b <<-\EOF &&
139 aaa
140 bb0
141 ccc
142 EOF
143
144 dh_test a b <<-EOF
145 @@ -1,3 +1,3 @@
146 aaa
147 -bb${CW}b${CR}
148 +bb${CW}0${CR}
149 ccc
150 EOF
151'
152
153test_expect_success 'diff-highlight highlights the middle of a line' '
154 cat >a <<-\EOF &&
155 aaa
156 bbb
157 ccc
158 EOF
159
160 cat >b <<-\EOF &&
161 aaa
162 b0b
163 ccc
164 EOF
165
166 dh_test a b <<-EOF
167 @@ -1,3 +1,3 @@
168 aaa
169 -b${CW}b${CR}b
170 +b${CW}0${CR}b
171 ccc
172 EOF
173'
174
175test_expect_success 'diff-highlight does not highlight whole line' '
176 cat >a <<-\EOF &&
177 aaa
178 bbb
179 ccc
180 EOF
181
182 cat >b <<-\EOF &&
183 aaa
184 000
185 ccc
186 EOF
187
188 dh_test a b <<-EOF
189 @@ -1,3 +1,3 @@
190 aaa
191 -bbb
192 +000
193 ccc
194 EOF
195'
196
197test_expect_failure 'diff-highlight highlights mismatched hunk size' '
198 cat >a <<-\EOF &&
199 aaa
200 bbb
201 EOF
202
203 cat >b <<-\EOF &&
204 aaa
205 b0b
206 ccc
207 EOF
208
209 dh_test a b <<-EOF
210 @@ -1,3 +1,3 @@
211 aaa
212 -b${CW}b${CR}b
213 +b${CW}0${CR}b
214 +ccc
215 EOF
216'
217
218# These two code points share the same leading byte in UTF-8 representation;
219# a naive byte-wise diff would highlight only the second byte.
220#
221# - U+00f3 ("o" with acute)
222o_accent=$(printf '\303\263')
223# - U+00f8 ("o" with stroke)
224o_stroke=$(printf '\303\270')
225
226test_expect_success 'diff-highlight treats multibyte utf-8 as a unit' '
227 echo "unic${o_accent}de" >a &&
228 echo "unic${o_stroke}de" >b &&
229 dh_test a b <<-EOF
230 @@ -1 +1 @@
231 -unic${CW}${o_accent}${CR}de
232 +unic${CW}${o_stroke}${CR}de
233 EOF
234'
235
236# Unlike the UTF-8 above, these are combining code points which are meant
237# to modify the character preceding them:
238#
239# - U+0301 (combining acute accent)
240combine_accent=$(printf '\314\201')
241# - U+0302 (combining circumflex)
242combine_circum=$(printf '\314\202')
243
244test_expect_failure 'diff-highlight treats combining code points as a unit' '
245 echo "unico${combine_accent}de" >a &&
246 echo "unico${combine_circum}de" >b &&
247 dh_test a b <<-EOF
248 @@ -1 +1 @@
249 -unic${CW}o${combine_accent}${CR}de
250 +unic${CW}o${combine_circum}${CR}de
251 EOF
252'
253
254test_expect_success 'diff-highlight works with the --graph option' '
255 dh_test_setup_history &&
256
257 # date-order so that the commits are interleaved for both
258 # trim graph elements so we can do a diff
259 # trim leading space because our trim_graph is not perfect
260 git log --branches -p --date-order |
261 "$DIFF_HIGHLIGHT" | left_trim >graph.exp &&
262 git log --branches -p --date-order --graph |
263 "$DIFF_HIGHLIGHT" | trim_graph | left_trim >graph.act &&
264 test_cmp graph.exp graph.act
265'
266
267# Most combined diffs won't meet diff-highlight's line-number filter. So we
268# create one here where one side drops a line and the other modifies it. That
269# should result in a diff like:
270#
271# - modified content
272# ++resolved content
273#
274# which naively looks like one side added "+resolved".
275test_expect_success 'diff-highlight ignores combined diffs' '
276 echo "content" >file &&
277 git add file &&
278 git commit -m base &&
279
280 >file &&
281 git commit -am master &&
282
283 git checkout -b other HEAD^ &&
284 echo "modified content" >file &&
285 git commit -am other &&
286
287 test_must_fail git merge master &&
288 echo "resolved content" >file &&
289 git commit -am resolved &&
290
291 cat >expect <<-\EOF &&
292 --- a/file
293 +++ b/file
294 @@@ -1,1 -1,0 +1,1 @@@
295 - modified content
296 ++resolved content
297 EOF
298
299 git show -c | "$DIFF_HIGHLIGHT" >actual.raw &&
300 sed -n "/^---/,\$p" <actual.raw >actual &&
301 test_cmp expect actual
302'
303
304test_done