Skip to content

Commit 097b681

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d3d068 commit 097b681

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

t/t4207-log-decoration-colors.sh

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ test_description='Test for "git log --decorate" colors'
77

88
. ./test-lib.sh
99

10-
get_color ()
11-
{
12-
git config --get-color no.such.slot "$1"
13-
}
14-
1510
test_expect_success setup '
1611
git config diff.color.commit yellow &&
1712
git config color.decorate.branch green &&
@@ -20,14 +15,14 @@ test_expect_success setup '
2015
git config color.decorate.stash magenta &&
2116
git config color.decorate.HEAD cyan &&
2217
23-
c_reset=$(get_color reset) &&
18+
c_reset="<RESET>" &&
2419
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>" &&
3126
3227
test_commit A &&
3328
git clone . other &&
@@ -59,7 +54,8 @@ EOF
5954
# to this test since it does not contain any decoration, hence --first-parent
6055
test_expect_success 'Commit Decorations Colored Correctly' '
6156
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 &&
6359
test_cmp expected out
6460
'
6561

t/t6006-rev-list-format.sh

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ test_format () {
6161
# Feed to --format to provide predictable colored sequences.
6262
AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
6363
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
6667
}
6768

6869
has_no_color () {
@@ -170,19 +171,27 @@ $added
170171
171172
EOF
172173

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+
'
179184

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+
'
186195

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

226235
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 &&
229239
test_cmp expect actual
230240
'
231241

t/t6300-for-each-ref.sh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,20 +412,18 @@ test_expect_success 'Check for invalid refname format' '
412412
test_must_fail git for-each-ref --format="%(refname:INVALID)"
413413
'
414414

415-
get_color ()
416-
{
417-
git config --get-color no.such.slot "$1"
418-
}
419-
420415
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>
425420
EOF
426421

427422
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 &&
429427
test_cmp expected actual
430428
'
431429

t/test-lib-functions.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ test_decode_color () {
4242
function name(n) {
4343
if (n == 0) return "RESET";
4444
if (n == 1) return "BOLD";
45+
if (n == 7) return "REVERSE";
4546
if (n == 30) return "BLACK";
4647
if (n == 31) return "RED";
4748
if (n == 32) return "GREEN";

0 commit comments

Comments
 (0)