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