t: use test_decode_color rather than literal ANSI codes

When we put literal ANSI terminal codes into our test scripts, it makes diffs on those scripts hard to read (the colors may be indistinguishable from diff coloring, or in the case of a reset, may not be visible at all). Some scripts get around this by including human-readable names and converting to literal codes with a git-config hack. This makes the actual code diffs look OK, but test_cmp output suffers from the same problem. Let's use test_decode_color instead, which turns the codes into obvious text tags. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 13, 2017 at 10:58 UTC 097b681baac3a5e9455c74e3072a458249691e40
4 files changed +44 -39
t/t4207-log-decoration-colors.sh
+9 -13
@@ -7,11 +7,6 @@ test_description='Test for "git log --decorate" colors'
7
8 . ./test-lib.sh
9
10 -get_color ()
11 -{
12 - git config --get-color no.such.slot "$1"
13 -}
14 -
10 test_expect_success setup '
11 git config diff.color.commit yellow &&
12 git config color.decorate.branch green &&
@@ -20,14 +15,14 @@ test_expect_success setup '
15 git config color.decorate.stash magenta &&
16 git config color.decorate.HEAD cyan &&
17
23 - c_reset=$(get_color reset) &&
18 + c_reset="<RESET>" &&
19
25 - c_commit=$(get_color yellow) &&
26 - c_branch=$(get_color green) &&
27 - c_remoteBranch=$(get_color red) &&
28 - c_tag=$(get_color "reverse bold yellow") &&
29 - c_stash=$(get_color magenta) &&
30 - c_HEAD=$(get_color cyan) &&
20 + c_commit="<YELLOW>" &&
21 + c_branch="<GREEN>" &&
22 + c_remoteBranch="<RED>" &&
23 + c_tag="<BOLD;REVERSE;YELLOW>" &&
24 + c_stash="<MAGENTA>" &&
25 + c_HEAD="<CYAN>" &&
26
27 test_commit A &&
28 git clone . other &&
@@ -59,7 +54,8 @@ EOF
54 # to this test since it does not contain any decoration, hence --first-parent
55 test_expect_success 'Commit Decorations Colored Correctly' '
56 git log --first-parent --abbrev=10 --all --decorate --oneline --color=always |
62 - sed "s/[0-9a-f]\{10,10\}/COMMIT_ID/" >out &&
57 + sed "s/[0-9a-f]\{10,10\}/COMMIT_ID/" |
58 + test_decode_color >out &&
59 test_cmp expected out
60 '
61
t/t6006-rev-list-format.sh
+26 -16
@@ -61,8 +61,9 @@ test_format () {
61 # Feed to --format to provide predictable colored sequences.
62 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
63 has_color () {
64 - printf '\033[31mfoo\033[m\n' >expect &&
65 - test_cmp expect "$1"
64 + test_decode_color <"$1" >decoded &&
65 + echo "<RED>foo<RESET>" >expect &&
66 + test_cmp expect decoded
67 }
68
69 has_no_color () {
@@ -170,19 +171,27 @@ $added
171
172 EOF
173
173 -test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
174 -commit $head2
175 -foobarbazxyzzy
176 -commit $head1
177 -foobarbazxyzzy
178 -EOF
174 +test_expect_success 'basic colors' '
175 + cat >expect <<-EOF &&
176 + commit $head2
177 + <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
178 + EOF
179 + format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
180 + git rev-list --format="$format" -1 master >actual.raw &&
181 + test_decode_color <actual.raw >actual &&
182 + test_cmp expect actual
183 +'
184
180 -test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
181 -commit $head2
182 -foo
183 -commit $head1
184 -foo
185 -EOF
185 +test_expect_success 'advanced colors' '
186 + cat >expect <<-EOF &&
187 + commit $head2
188 + <BOLD;RED;BYELLOW>foo<RESET>
189 + EOF
190 + format="%C(red yellow bold)foo%C(reset)" &&
191 + git rev-list --format="$format" -1 master >actual.raw &&
192 + test_decode_color <actual.raw >actual &&
193 + test_cmp expect actual
194 +'
195
196 test_expect_success '%C(auto,...) does not enable color by default' '
197 git log --format=$AUTO_COLOR -1 >actual &&
@@ -224,8 +233,9 @@ test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
233 '
234
235 test_expect_success '%C(auto) respects --color' '
227 - git log --color --format="%C(auto)%H" -1 >actual &&
228 - printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
236 + git log --color --format="%C(auto)%H" -1 >actual.raw &&
237 + test_decode_color <actual.raw >actual &&
238 + echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
239 test_cmp expect actual
240 '
241
t/t6300-for-each-ref.sh
+8 -10
@@ -412,20 +412,18 @@ test_expect_success 'Check for invalid refname format' '
412 test_must_fail git for-each-ref --format="%(refname:INVALID)"
413 '
414
415 -get_color ()
416 -{
417 - git config --get-color no.such.slot "$1"
418 -}
419 -
415 cat >expected <<EOF
421 -$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
422 -$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
423 -$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
424 -$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
416 +$(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
417 +$(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
418 +$(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
419 +$(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
420 EOF
421
422 test_expect_success 'Check %(color:...) ' '
428 - git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
423 + git for-each-ref \
424 + --format="%(objectname:short) %(color:green)%(refname:short)" \
425 + >actual.raw &&
426 + test_decode_color <actual.raw >actual &&
427 test_cmp expected actual
428 '
429
t/test-lib-functions.sh
+1
@@ -42,6 +42,7 @@ test_decode_color () {
42 function name(n) {
43 if (n == 0) return "RESET";
44 if (n == 1) return "BOLD";
45 + if (n == 7) return "REVERSE";
46 if (n == 30) return "BLACK";
47 if (n == 31) return "RED";
48 if (n == 32) return "GREEN";