Skip to content

Commit

Permalink
chainlint: colorize problem annotations and test delimiters
Browse files Browse the repository at this point in the history
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 <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
sunshineco authored and gitster committed Sep 13, 2022
1 parent fb41727 commit 7c04aa7
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions t/chainlint.pl
Expand Up @@ -585,12 +585,14 @@ sub check_test {
my $parser = TestParser->new(\$body);
my @tokens = $parser->parse();
return unless $emit_all || grep(/\?![^?]+\?!/, @tokens);
my $c = main::fd_colors(1);
my $checked = join(' ', @tokens);
$checked =~ s/^\n//;
$checked =~ s/^ //mg;
$checked =~ s/ $//mg;
$checked =~ s/(\?![^?]+\?!)/$c->{rev}$c->{red}$1$c->{reset}/mg;
$checked .= "\n" unless $checked =~ /\n$/;
push(@{$self->{output}}, "# chainlint: $title\n$checked");
push(@{$self->{output}}, "$c->{blue}# chainlint: $title$c->{reset}\n$checked");
}

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

# Restore TERM if test framework set it to "dumb" so 'tput' will work; do this
# outside of get_colors() since under 'ithreads' all threads use %ENV of main
# thread and ignore %ENV changes in subthreads.
$ENV{TERM} = $ENV{USER_TERM} if $ENV{USER_TERM};

my @NOCOLORS = (bold => '', rev => '', reset => '', blue => '', green => '', red => '');
my %COLORS = ();
sub get_colors {
return \%COLORS if %COLORS;
if (exists($ENV{NO_COLOR}) ||
system("tput sgr0 >/dev/null 2>&1") != 0 ||
system("tput bold >/dev/null 2>&1") != 0 ||
system("tput rev >/dev/null 2>&1") != 0 ||
system("tput setaf 1 >/dev/null 2>&1") != 0) {
%COLORS = @NOCOLORS;
return \%COLORS;
}
%COLORS = (bold => `tput bold`,
rev => `tput rev`,
reset => `tput sgr0`,
blue => `tput setaf 4`,
green => `tput setaf 2`,
red => `tput setaf 1`);
chomp(%COLORS);
return \%COLORS;
}

my %FD_COLORS = ();
sub fd_colors {
my $fd = shift;
return $FD_COLORS{$fd} if exists($FD_COLORS{$fd});
$FD_COLORS{$fd} = -t $fd ? get_colors() : {@NOCOLORS};
return $FD_COLORS{$fd};
}

sub ncores {
# Windows
return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS});
Expand All @@ -630,6 +667,8 @@ sub show_stats {
my $walltime = $interval->($start_time);
my ($usertime) = times();
my ($total_workers, $total_scripts, $total_tests, $total_errs) = (0, 0, 0, 0);
my $c = fd_colors(2);
print(STDERR $c->{green});
for (@$stats) {
my ($worker, $nscripts, $ntests, $nerrs) = @$_;
print(STDERR "worker $worker: $nscripts scripts, $ntests tests, $nerrs errors\n");
Expand All @@ -638,7 +677,7 @@ sub show_stats {
$total_tests += $ntests;
$total_errs += $nerrs;
}
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);
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);
}

sub check_script {
Expand All @@ -656,8 +695,9 @@ sub check_script {
my $parser = ScriptParser->new(\$s);
1 while $parser->parse_cmd();
if (@{$parser->{output}}) {
my $c = fd_colors(1);
my $s = join('', @{$parser->{output}});
$emit->("# chainlint: $path\n" . $s);
$emit->("$c->{bold}$c->{blue}# chainlint: $path$c->{reset}\n" . $s);
$nerrs += () = $s =~ /\?![^?]+\?!/g;
}
$ntests += $parser->{ntests};
Expand Down

0 comments on commit 7c04aa7

Please sign in to comment.