Skip to content

Commit

Permalink
t: use test_decode_color rather than literal ANSI codes
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
peff authored and gitster committed Jul 13, 2017
1 parent 5d3d068 commit 097b681
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
22 changes: 9 additions & 13 deletions t/t4207-log-decoration-colors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ test_description='Test for "git log --decorate" colors'

. ./test-lib.sh

get_color ()
{
git config --get-color no.such.slot "$1"
}

test_expect_success setup '
git config diff.color.commit yellow &&
git config color.decorate.branch green &&
Expand All @@ -20,14 +15,14 @@ test_expect_success setup '
git config color.decorate.stash magenta &&
git config color.decorate.HEAD cyan &&
c_reset=$(get_color reset) &&
c_reset="<RESET>" &&
c_commit=$(get_color yellow) &&
c_branch=$(get_color green) &&
c_remoteBranch=$(get_color red) &&
c_tag=$(get_color "reverse bold yellow") &&
c_stash=$(get_color magenta) &&
c_HEAD=$(get_color cyan) &&
c_commit="<YELLOW>" &&
c_branch="<GREEN>" &&
c_remoteBranch="<RED>" &&
c_tag="<BOLD;REVERSE;YELLOW>" &&
c_stash="<MAGENTA>" &&
c_HEAD="<CYAN>" &&
test_commit A &&
git clone . other &&
Expand Down Expand Up @@ -59,7 +54,8 @@ EOF
# to this test since it does not contain any decoration, hence --first-parent
test_expect_success 'Commit Decorations Colored Correctly' '
git log --first-parent --abbrev=10 --all --decorate --oneline --color=always |
sed "s/[0-9a-f]\{10,10\}/COMMIT_ID/" >out &&
sed "s/[0-9a-f]\{10,10\}/COMMIT_ID/" |
test_decode_color >out &&
test_cmp expected out
'

Expand Down
42 changes: 26 additions & 16 deletions t/t6006-rev-list-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ test_format () {
# Feed to --format to provide predictable colored sequences.
AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
has_color () {
printf '\033[31mfoo\033[m\n' >expect &&
test_cmp expect "$1"
test_decode_color <"$1" >decoded &&
echo "<RED>foo<RESET>" >expect &&
test_cmp expect decoded
}

has_no_color () {
Expand Down Expand Up @@ -170,19 +171,27 @@ $added
EOF

test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<EOF
commit $head2
foobarbazxyzzy
commit $head1
foobarbazxyzzy
EOF
test_expect_success 'basic colors' '
cat >expect <<-EOF &&
commit $head2
<RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
EOF
format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
git rev-list --format="$format" -1 master >actual.raw &&
test_decode_color <actual.raw >actual &&
test_cmp expect actual
'

test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<EOF
commit $head2
foo
commit $head1
foo
EOF
test_expect_success 'advanced colors' '
cat >expect <<-EOF &&
commit $head2
<BOLD;RED;BYELLOW>foo<RESET>
EOF
format="%C(red yellow bold)foo%C(reset)" &&
git rev-list --format="$format" -1 master >actual.raw &&
test_decode_color <actual.raw >actual &&
test_cmp expect actual
'

test_expect_success '%C(auto,...) does not enable color by default' '
git log --format=$AUTO_COLOR -1 >actual &&
Expand Down Expand Up @@ -224,8 +233,9 @@ test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
'

test_expect_success '%C(auto) respects --color' '
git log --color --format="%C(auto)%H" -1 >actual &&
printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
git log --color --format="%C(auto)%H" -1 >actual.raw &&
test_decode_color <actual.raw >actual &&
echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
test_cmp expect actual
'

Expand Down
18 changes: 8 additions & 10 deletions t/t6300-for-each-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -412,20 +412,18 @@ test_expect_success 'Check for invalid refname format' '
test_must_fail git for-each-ref --format="%(refname:INVALID)"
'

get_color ()
{
git config --get-color no.such.slot "$1"
}

cat >expected <<EOF
$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
$(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
$(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
$(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
$(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
EOF

test_expect_success 'Check %(color:...) ' '
git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
git for-each-ref \
--format="%(objectname:short) %(color:green)%(refname:short)" \
>actual.raw &&
test_decode_color <actual.raw >actual &&
test_cmp expected actual
'

Expand Down
1 change: 1 addition & 0 deletions t/test-lib-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ test_decode_color () {
function name(n) {
if (n == 0) return "RESET";
if (n == 1) return "BOLD";
if (n == 7) return "REVERSE";
if (n == 30) return "BLACK";
if (n == 31) return "RED";
if (n == 32) return "GREEN";
Expand Down

0 comments on commit 097b681

Please sign in to comment.