3 test_description='Test diff-highlight'
6 TEST_OUTPUT_DIRECTORY=$(pwd)
7 TEST_DIRECTORY="$CURR_DIR"/../../../t
8 DIFF_HIGHLIGHT="$CURR_DIR"/../diff-highlight
10 CW="$(printf "\033[7m")" # white
11 CR="$(printf "\033[27m")" # reset
13 . "$TEST_DIRECTORY"/test-lib.sh
15 if ! test_have_prereq PERL
17 skip_all='skipping diff-highlight tests; perl not available'
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).
34 git commit -m "Add a file" &&
37 git diff file >diff.raw &&
38 git commit -a -m "Update a file" &&
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
48 test_strip_patch_header () {
52 # dh_test_setup_history generates a contrived graph such that we have at least
53 # 1 nesting (E) and 2 nestings (F).
59 # git log --all --graph
72 dh_test_setup_history () {
78 git checkout -b branch &&
81 git commit -a -m "E" &&
83 git checkout master &&
86 git commit -a -m "A" &&
88 git checkout branch &&
91 git commit -a -m "F" &&
93 git checkout master &&
100 "$PERL_PATH" -pe 's/^\s+//'
104 # graphs start with * or |
105 # followed by a space or / or \
106 "$PERL_PATH" -pe 's@^((\*|\|)( |/|\\))+@@'
109 test_expect_success 'diff-highlight highlights the beginning of a line' '
131 test_expect_success 'diff-highlight highlights the end of a line' '
153 test_expect_success 'diff-highlight highlights the middle of a line' '
175 test_expect_success 'diff-highlight does not highlight whole line' '
197 test_expect_failure 'diff-highlight highlights mismatched hunk size' '
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.
221 # - U+00f3 ("o" with acute)
222 o_accent=$(printf '\303\263')
223 # - U+00f8 ("o" with stroke)
224 o_stroke=$(printf '\303\270')
226 test_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 &&
231 -unic${CW}${o_accent}${CR}de
232 +unic${CW}${o_stroke}${CR}de
236 # Unlike the UTF-8 above, these are combining code points which are meant
237 # to modify the character preceding them:
239 # - U+0301 (combining acute accent)
240 combine_accent=$(printf '\314\201')
241 # - U+0302 (combining circumflex)
242 combine_circum=$(printf '\314\202')
244 test_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 &&
249 -unic${CW}o${combine_accent}${CR}de
250 +unic${CW}o${combine_circum}${CR}de
254 test_expect_success 'diff-highlight works with the --graph option' '
255 dh_test_setup_history &&
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
267 # Just reuse the previous graph test, but with --color. Our trimming
268 # doesn't know about color, so just sanity check that something got
270 test_expect_success 'diff-highlight works with color graph' '
271 git log --branches -p --date-order --graph --color |
272 "$DIFF_HIGHLIGHT" | trim_graph | left_trim >graph &&
276 # Most combined diffs won't meet diff-highlight's line-number filter. So we
277 # create one here where one side drops a line and the other modifies it. That
278 # should result in a diff like:
283 # which naively looks like one side added "+resolved".
284 test_expect_success 'diff-highlight ignores combined diffs' '
285 echo "content" >file &&
287 git commit -m base &&
290 git commit -am master &&
292 git checkout -b other HEAD^ &&
293 echo "modified content" >file &&
294 git commit -am other &&
296 test_must_fail git merge master &&
297 echo "resolved content" >file &&
298 git commit -am resolved &&
300 cat >expect <<-\EOF &&
303 @@@ -1,1 -1,0 +1,1 @@@
308 git show -c | "$DIFF_HIGHLIGHT" >actual.raw &&
309 sed -n "/^---/,\$p" <actual.raw >actual &&
310 test_cmp expect actual
313 test_expect_success 'diff-highlight handles --graph with leading dash' '
320 git commit -m before &&
322 sed s/old/new/ <file >file.tmp &&
325 git commit -m after &&
327 cat >expect <<-EOF &&
332 -the ${CW}old${CR} line
333 +the ${CW}new${CR} line
336 git log --graph -p -1 | "$DIFF_HIGHLIGHT" >actual.raw &&
337 trim_graph <actual.raw | sed -n "/^---/,\$p" >actual &&
338 test_cmp expect actual