diff-highlight: add some tests
Signed-off-by: Brian Henderson <henderson.bj@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brian Henderson committed
Aug 29, 2016 at 10:33 UTC
23b250ab0f041bba7af4ba8eb1a686f6850677a6
3 files changed
+190
contrib/diff-highlight/Makefile
new
+5
@@ -0,0 +1,5 @@
1
+# nothing to build
2
+all:
3
+
4
+test:
5
+ $(MAKE) -C t
contrib/diff-highlight/t/Makefile
new
+22
@@ -0,0 +1,22 @@
1
+-include ../../../config.mak.autogen
2
+-include ../../../config.mak
3
+
4
+# copied from ../../t/Makefile
5
+SHELL_PATH ?= $(SHELL)
6
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
7
+T = $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)
8
+
9
+all: test
10
+test: $(T)
11
+
12
+.PHONY: help clean all test $(T)
13
+
14
+help:
15
+ @echo 'Run "$(MAKE) test" to launch test scripts'
16
+ @echo 'Run "$(MAKE) clean" to remove trash folders'
17
+
18
+$(T):
19
+ @echo "*** $@ ***"; '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
20
+
21
+clean:
22
+ $(RM) -r 'trash directory'.*
contrib/diff-highlight/t/t9400-diff-highlight.sh
new
+163
@@ -0,0 +1,163 @@
1
+#!/bin/sh
2
+
3
+test_description='Test diff-highlight'
4
+
5
+CURR_DIR=$(pwd)
6
+TEST_OUTPUT_DIRECTORY=$(pwd)
7
+TEST_DIRECTORY="$CURR_DIR"/../../../t
8
+DIFF_HIGHLIGHT="$CURR_DIR"/../diff-highlight
9
+
10
+CW="$(printf "\033[7m")" # white
11
+CR="$(printf "\033[27m")" # reset
12
+
13
+. "$TEST_DIRECTORY"/test-lib.sh
14
+
15
+if ! test_have_prereq PERL
16
+then
17
+ skip_all='skipping diff-highlight tests; perl not available'
18
+ test_done
19
+fi
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).
26
+dh_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
+
48
+test_strip_patch_header () {
49
+ sed -n '/^@@/,$p' $*
50
+}
51
+
52
+test_expect_success 'diff-highlight highlights the beginning of a line' '
53
+ cat >a <<-\EOF &&
54
+ aaa
55
+ bbb
56
+ ccc
57
+ EOF
58
+
59
+ cat >b <<-\EOF &&
60
+ aaa
61
+ 0bb
62
+ ccc
63
+ EOF
64
+
65
+ dh_test a b <<-EOF
66
+ @@ -1,3 +1,3 @@
67
+ aaa
68
+ -${CW}b${CR}bb
69
+ +${CW}0${CR}bb
70
+ ccc
71
+ EOF
72
+'
73
+
74
+test_expect_success 'diff-highlight highlights the end of a line' '
75
+ cat >a <<-\EOF &&
76
+ aaa
77
+ bbb
78
+ ccc
79
+ EOF
80
+
81
+ cat >b <<-\EOF &&
82
+ aaa
83
+ bb0
84
+ ccc
85
+ EOF
86
+
87
+ dh_test a b <<-EOF
88
+ @@ -1,3 +1,3 @@
89
+ aaa
90
+ -bb${CW}b${CR}
91
+ +bb${CW}0${CR}
92
+ ccc
93
+ EOF
94
+'
95
+
96
+test_expect_success 'diff-highlight highlights the middle of a line' '
97
+ cat >a <<-\EOF &&
98
+ aaa
99
+ bbb
100
+ ccc
101
+ EOF
102
+
103
+ cat >b <<-\EOF &&
104
+ aaa
105
+ b0b
106
+ ccc
107
+ EOF
108
+
109
+ dh_test a b <<-EOF
110
+ @@ -1,3 +1,3 @@
111
+ aaa
112
+ -b${CW}b${CR}b
113
+ +b${CW}0${CR}b
114
+ ccc
115
+ EOF
116
+'
117
+
118
+test_expect_success 'diff-highlight does not highlight whole line' '
119
+ cat >a <<-\EOF &&
120
+ aaa
121
+ bbb
122
+ ccc
123
+ EOF
124
+
125
+ cat >b <<-\EOF &&
126
+ aaa
127
+ 000
128
+ ccc
129
+ EOF
130
+
131
+ dh_test a b <<-EOF
132
+ @@ -1,3 +1,3 @@
133
+ aaa
134
+ -bbb
135
+ +000
136
+ ccc
137
+ EOF
138
+'
139
+
140
+test_expect_failure 'diff-highlight highlights mismatched hunk size' '
141
+ cat >a <<-\EOF &&
142
+ aaa
143
+ bbb
144
+ EOF
145
+
146
+ cat >b <<-\EOF &&
147
+ aaa
148
+ b0b
149
+ ccc
150
+ EOF
151
+
152
+ dh_test a b <<-EOF
153
+ @@ -1,3 +1,3 @@
154
+ aaa
155
+ -b${CW}b${CR}b
156
+ +b${CW}0${CR}b
157
+ +ccc
158
+ EOF
159
+'
160
+
161
+# TODO add multi-byte test
162
+
163
+test_done