Skip to content

Commit 7c04aa7

Browse files
sunshinecogitster
authored andcommitted
chainlint: colorize problem annotations and test delimiters
When `chainlint.pl` detects problems in a test definition, it emits the test definition with "?!FOO?!" annotations highlighting the problems it discovered. For instance, given this problematic test: test_expect_success 'discombobulate frobnitz' ' git frob babble && (echo balderdash; echo gnabgib) >expect && for i in three two one do git nitfol $i done >actual test_cmp expect actual ' chainlint.pl will output: # chainlint: t1234-confusing.sh # chainlint: discombobulate frobnitz git frob babble && (echo balderdash ; ?!AMP?! echo gnabgib) >expect && for i in three two one do git nitfol $i ?!LOOP?! done >actual ?!AMP?! test_cmp expect actual in which it may be difficult to spot the "?!FOO?!" annotations. The problem is compounded when multiple tests, possibly in multiple scripts, fail "linting", in which case it may be difficult to spot the "# chainlint:" lines which delimit one problematic test from another. To ameliorate this potential problem, colorize the "?!FOO?!" annotations in order to quickly draw the test author's attention to the problem spots, and colorize the "# chainlint:" lines to help the author identify the name of each script and each problematic test. Colorization is disabled automatically if output is not directed to a terminal or if NO_COLOR environment variable is set. The implementation is specific to Unix (it employs `tput` if available) but works equally well in the Git for Windows development environment which emulates Unix sufficiently. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb41727 commit 7c04aa7

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

t/chainlint.pl

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,14 @@ sub check_test {
585585
my $parser = TestParser->new(\$body);
586586
my @tokens = $parser->parse();
587587
return unless $emit_all || grep(/\?![^?]+\?!/, @tokens);
588+
my $c = main::fd_colors(1);
588589
my $checked = join(' ', @tokens);
589590
$checked =~ s/^\n//;
590591
$checked =~ s/^ //mg;
591592
$checked =~ s/ $//mg;
593+
$checked =~ s/(\?![^?]+\?!)/$c->{rev}$c->{red}$1$c->{reset}/mg;
592594
$checked .= "\n" unless $checked =~ /\n$/;
593-
push(@{$self->{output}}, "# chainlint: $title\n$checked");
595+
push(@{$self->{output}}, "$c->{blue}# chainlint: $title$c->{reset}\n$checked");
594596
}
595597

596598
sub parse_cmd {
@@ -615,6 +617,41 @@ package main;
615617
$interval = sub { return Time::HiRes::tv_interval(shift); };
616618
}
617619

620+
# Restore TERM if test framework set it to "dumb" so 'tput' will work; do this
621+
# outside of get_colors() since under 'ithreads' all threads use %ENV of main
622+
# thread and ignore %ENV changes in subthreads.
623+
$ENV{TERM} = $ENV{USER_TERM} if $ENV{USER_TERM};
624+
625+
my @NOCOLORS = (bold => '', rev => '', reset => '', blue => '', green => '', red => '');
626+
my %COLORS = ();
627+
sub get_colors {
628+
return \%COLORS if %COLORS;
629+
if (exists($ENV{NO_COLOR}) ||
630+
system("tput sgr0 >/dev/null 2>&1") != 0 ||
631+
system("tput bold >/dev/null 2>&1") != 0 ||
632+
system("tput rev >/dev/null 2>&1") != 0 ||
633+
system("tput setaf 1 >/dev/null 2>&1") != 0) {
634+
%COLORS = @NOCOLORS;
635+
return \%COLORS;
636+
}
637+
%COLORS = (bold => `tput bold`,
638+
rev => `tput rev`,
639+
reset => `tput sgr0`,
640+
blue => `tput setaf 4`,
641+
green => `tput setaf 2`,
642+
red => `tput setaf 1`);
643+
chomp(%COLORS);
644+
return \%COLORS;
645+
}
646+
647+
my %FD_COLORS = ();
648+
sub fd_colors {
649+
my $fd = shift;
650+
return $FD_COLORS{$fd} if exists($FD_COLORS{$fd});
651+
$FD_COLORS{$fd} = -t $fd ? get_colors() : {@NOCOLORS};
652+
return $FD_COLORS{$fd};
653+
}
654+
618655
sub ncores {
619656
# Windows
620657
return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS});
@@ -630,6 +667,8 @@ sub show_stats {
630667
my $walltime = $interval->($start_time);
631668
my ($usertime) = times();
632669
my ($total_workers, $total_scripts, $total_tests, $total_errs) = (0, 0, 0, 0);
670+
my $c = fd_colors(2);
671+
print(STDERR $c->{green});
633672
for (@$stats) {
634673
my ($worker, $nscripts, $ntests, $nerrs) = @$_;
635674
print(STDERR "worker $worker: $nscripts scripts, $ntests tests, $nerrs errors\n");
@@ -638,7 +677,7 @@ sub show_stats {
638677
$total_tests += $ntests;
639678
$total_errs += $nerrs;
640679
}
641-
printf(STDERR "total: %d workers, %d scripts, %d tests, %d errors, %.2fs/%.2fs (wall/user)\n", $total_workers, $total_scripts, $total_tests, $total_errs, $walltime, $usertime);
680+
printf(STDERR "total: %d workers, %d scripts, %d tests, %d errors, %.2fs/%.2fs (wall/user)$c->{reset}\n", $total_workers, $total_scripts, $total_tests, $total_errs, $walltime, $usertime);
642681
}
643682

644683
sub check_script {
@@ -656,8 +695,9 @@ sub check_script {
656695
my $parser = ScriptParser->new(\$s);
657696
1 while $parser->parse_cmd();
658697
if (@{$parser->{output}}) {
698+
my $c = fd_colors(1);
659699
my $s = join('', @{$parser->{output}});
660-
$emit->("# chainlint: $path\n" . $s);
700+
$emit->("$c->{bold}$c->{blue}# chainlint: $path$c->{reset}\n" . $s);
661701
$nerrs += () = $s =~ /\?![^?]+\?!/g;
662702
}
663703
$ntests += $parser->{ntests};

0 commit comments

Comments
 (0)