chainlint: prefix annotated test definition with line numbers

When chainlint detects problems in a test, it prints out the name of the test script, the name of the problematic test, and a copy of the test definition with "?!FOO?!" annotations inserted at the locations where problems were detected. Taken together this information is sufficient for the test author to identify the problematic code in the original test definition. However, in a lengthy script or a lengthy test definition, the author may still end up using the editor's search feature to home in on the exact problem location. To further assist the test author, display line numbers along with the annotated test definition, thus allowing the author to jump directly to each problematic line. Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Eric Sunshine committed Nov 11, 2022 at 07:34 UTC 48d69d8f2fe4af753bdb6f626bc444ec24ce02b4
2 files changed +9 -3
t/Makefile
+1 -1
@@ -94,7 +94,7 @@ check-chainlint:
94 done \
95 } >'$(CHAINLINTTMP_SQ)'/expect && \
96 $(CHAINLINT) --emit-all '$(CHAINLINTTMP_SQ)'/tests | \
97 - grep -v '^[ ]*$$' >'$(CHAINLINTTMP_SQ)'/actual && \
97 + sed -e 's/^[1-9][0-9]* //;/^[ ]*$$/d' >'$(CHAINLINTTMP_SQ)'/actual && \
98 if test -f ../GIT-BUILD-OPTIONS; then \
99 . ../GIT-BUILD-OPTIONS; \
100 fi && \
t/chainlint.pl
+8 -2
@@ -614,6 +614,7 @@ sub check_test {
614 my $problems = $parser->{problems};
615 return unless $emit_all || @$problems;
616 my $c = main::fd_colors(1);
617 + my $lineno = $_[1]->[3];
618 my $start = 0;
619 my $checked = '';
620 for (sort {$a->[1]->[2] <=> $b->[1]->[2]} @$problems) {
@@ -623,10 +624,12 @@ sub check_test {
624 $start = $pos;
625 }
626 $checked .= substr($body, $start);
626 - $checked =~ s/^\n//;
627 + $checked =~ s/^/$lineno++ . ' '/mge;
628 + $checked =~ s/^\d+ \n//;
629 $checked =~ s/(\s) \?!/$1?!/mg;
630 $checked =~ s/\?! (\s)/?!$1/mg;
631 $checked =~ s/(\?![^?]+\?!)/$c->{rev}$c->{red}$1$c->{reset}/mg;
632 + $checked =~ s/^\d+/$c->{dim}$&$c->{reset}/mg;
633 $checked .= "\n" unless $checked =~ /\n$/;
634 push(@{$self->{output}}, "$c->{blue}# chainlint: $title$c->{reset}\n$checked");
635 }
@@ -658,7 +661,7 @@ if (eval {require Time::HiRes; Time::HiRes->import(); 1;}) {
661 # thread and ignore %ENV changes in subthreads.
662 $ENV{TERM} = $ENV{USER_TERM} if $ENV{USER_TERM};
663
661 -my @NOCOLORS = (bold => '', rev => '', reset => '', blue => '', green => '', red => '');
664 +my @NOCOLORS = (bold => '', rev => '', dim => '', reset => '', blue => '', green => '', red => '');
665 my %COLORS = ();
666 sub get_colors {
667 return \%COLORS if %COLORS;
@@ -669,6 +672,7 @@ sub get_colors {
672 if ($ENV{TERM} =~ /xterm|xterm-\d+color|xterm-new|xterm-direct|nsterm|nsterm-\d+color|nsterm-direct/) {
673 %COLORS = (bold => "\e[1m",
674 rev => "\e[7m",
675 + dim => "\e[2m",
676 reset => "\e[0m",
677 blue => "\e[34m",
678 green => "\e[32m",
@@ -678,9 +682,11 @@ sub get_colors {
682 if (system("tput sgr0 >/dev/null 2>&1") == 0 &&
683 system("tput bold >/dev/null 2>&1") == 0 &&
684 system("tput rev >/dev/null 2>&1") == 0 &&
685 + system("tput dim >/dev/null 2>&1") == 0 &&
686 system("tput setaf 1 >/dev/null 2>&1") == 0) {
687 %COLORS = (bold => `tput bold`,
688 rev => `tput rev`,
689 + dim => `tput dim`,
690 reset => `tput sgr0`,
691 blue => `tput setaf 4`,
692 green => `tput setaf 2`,