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 master
56# /
57# D---E---F branch
58#
59# git log --all --graph
60# * commit
61# | A
62# | * commit
63# | | F
64# | * commit
65# |/
66# | E
67# * commit
68# D
69#
70dh_test_setup_history () {
71 echo "file1" >file1 &&
72 echo "file2" >file2 &&
73 echo "file3" >file3 &&
74
75 cat file1 >file &&
76 git add file &&
77 test_tick &&
78 git commit -m "D" &&
79
80 git checkout -b branch &&
81 cat file2 >file &&
82 test_tick &&
83 git commit -a -m "E" &&
84
85 cat file3 >file &&
86 test_tick &&
87 git commit -a -m "F" &&
88
89 git checkout master &&
90 cat file2 >file &&
91 test_tick &&
92 git commit -a -m "A"
93}
94
95left_trim () {
96 "$PERL_PATH" -pe 's/^\s+//'
97}
98
99trim_graph () {
100 # graphs start with * or |
101 # followed by a space or / or \
102 "$PERL_PATH" -pe 's@^((\*|\|)( |/|\\))+@@'
103}
104
105test_expect_success 'diff-highlight highlights the beginning of a line' '
106 cat >a <<-\EOF &&
107 aaa
108 bbb
109 ccc
110 EOF
111
112 cat >b <<-\EOF &&
113 aaa
114 0bb
115 ccc
116 EOF
117
118 dh_test a b <<-EOF
119 @@ -1,3 +1,3 @@
120 aaa
121 -${CW}b${CR}bb
122 +${CW}0${CR}bb
123 ccc
124 EOF
125'
126
127test_expect_success 'diff-highlight highlights the end of a line' '
128 cat >a <<-\EOF &&
129 aaa
130 bbb
131 ccc
132 EOF
133
134 cat >b <<-\EOF &&
135 aaa
136 bb0
137 ccc
138 EOF
139
140 dh_test a b <<-EOF
141 @@ -1,3 +1,3 @@
142 aaa
143 -bb${CW}b${CR}
144 +bb${CW}0${CR}
145 ccc
146 EOF
147'
148
149test_expect_success 'diff-highlight highlights the middle of a line' '
150 cat >a <<-\EOF &&
151 aaa
152 bbb
153 ccc
154 EOF
155
156 cat >b <<-\EOF &&
157 aaa
158 b0b
159 ccc
160 EOF
161
162 dh_test a b <<-EOF
163 @@ -1,3 +1,3 @@
164 aaa
165 -b${CW}b${CR}b
166 +b${CW}0${CR}b
167 ccc
168 EOF
169'
170
171test_expect_success 'diff-highlight does not highlight whole line' '
172 cat >a <<-\EOF &&
173 aaa
174 bbb
175 ccc
176 EOF
177
178 cat >b <<-\EOF &&
179 aaa
180 000
181 ccc
182 EOF
183
184 dh_test a b <<-EOF
185 @@ -1,3 +1,3 @@
186 aaa
187 -bbb
188 +000
189 ccc
190 EOF
191'
192
193test_expect_failure 'diff-highlight highlights mismatched hunk size' '
194 cat >a <<-\EOF &&
195 aaa
196 bbb
197 EOF
198
199 cat >b <<-\EOF &&
200 aaa
201 b0b
202 ccc
203 EOF
204
205 dh_test a b <<-EOF
206 @@ -1,3 +1,3 @@
207 aaa
208 -b${CW}b${CR}b
209 +b${CW}0${CR}b
210 +ccc
211 EOF
212'
213
214# These two code points share the same leading byte in UTF-8 representation;
215# a naive byte-wise diff would highlight only the second byte.
216#
217# - U+00f3 ("o" with acute)
218o_accent=$(printf '\303\263')
219# - U+00f8 ("o" with stroke)
220o_stroke=$(printf '\303\270')
221
222test_expect_success 'diff-highlight treats multibyte utf-8 as a unit' '
223 echo "unic${o_accent}de" >a &&
224 echo "unic${o_stroke}de" >b &&
225 dh_test a b <<-EOF
226 @@ -1 +1 @@
227 -unic${CW}${o_accent}${CR}de
228 +unic${CW}${o_stroke}${CR}de
229 EOF
230'
231
232# Unlike the UTF-8 above, these are combining code points which are meant
233# to modify the character preceding them:
234#
235# - U+0301 (combining acute accent)
236combine_accent=$(printf '\314\201')
237# - U+0302 (combining circumflex)
238combine_circum=$(printf '\314\202')
239
240test_expect_failure 'diff-highlight treats combining code points as a unit' '
241 echo "unico${combine_accent}de" >a &&
242 echo "unico${combine_circum}de" >b &&
243 dh_test a b <<-EOF
244 @@ -1 +1 @@
245 -unic${CW}o${combine_accent}${CR}de
246 +unic${CW}o${combine_circum}${CR}de
247 EOF
248'
249
250test_expect_success 'diff-highlight works with the --graph option' '
251 dh_test_setup_history &&
252
253 # topo-order so that the order of the commits is the same as with --graph
254 # trim graph elements so we can do a diff
255 # trim leading space because our trim_graph is not perfect
256 git log --branches -p --topo-order |
257 "$DIFF_HIGHLIGHT" | left_trim >graph.exp &&
258 git log --branches -p --graph |
259 "$DIFF_HIGHLIGHT" | trim_graph | left_trim >graph.act &&
260 test_cmp graph.exp graph.act
261'
262
263# Most combined diffs won't meet diff-highlight's line-number filter. So we
264# create one here where one side drops a line and the other modifies it. That
265# should result in a diff like:
266#
267# - modified content
268# ++resolved content
269#
270# which naively looks like one side added "+resolved".
271test_expect_success 'diff-highlight ignores combined diffs' '
272 echo "content" >file &&
273 git add file &&
274 git commit -m base &&
275
276 >file &&
277 git commit -am master &&
278
279 git checkout -b other HEAD^ &&
280 echo "modified content" >file &&
281 git commit -am other &&
282
283 test_must_fail git merge master &&
284 echo "resolved content" >file &&
285 git commit -am resolved &&
286
287 cat >expect <<-\EOF &&
288 --- a/file
289 +++ b/file
290 @@@ -1,1 -1,0 +1,1 @@@
291 - modified content
292 ++resolved content
293 EOF
294
295 git show -c | "$DIFF_HIGHLIGHT" >actual.raw &&
296 sed -n "/^---/,\$p" <actual.raw >actual &&
297 test_cmp expect actual
298'
299
300test_done