git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] chainlint.pl: fix line number reporting

From
Jeff King <peff@peff.net>
Date
Jul 6, 2024, 06:05 UTC
Message-ID
<20240706060515.GA700151@coredump.intra.peff.net>
In-Reply-To
<20240706060143.GD698153@coredump.intra.peff.net>

The previous commit taught chainlint.pl to handle test bodies in heredocs, but there are two small bugs related to line numbers:

  1. Prior to that commit, we'd leave the title and body untouched in
     @_. So we could later pull the line number out of the $_[1] array
     element. Now we shift off the front of the array, so we have to
     remember that element to grab the line number. This is a regression
     even for regular:
       test_expect_success 'title' '
               test body
       '
     invocations; the lines for ever test started fresh at 0.
  2. For an invocation like the one above, if the test_expect_success
     line is X, then "test body" would correctly start at X+1, since the
     hanging newline at the start of the single-quoted test body
     increments the count. But for a here-doc, there is an implicit
     newline at the end of the token stream before the here-doc starts.
     We have to increment "lineno" to account for this.
     Actually, this is not _quite_ correct, as there could be multiple
     here-docs, like:
       test_expect_success "$(cat <<END_OF_TITLE)" - <<END_OF_TEST
       this is the title
       END_OF_TITLE
       this is the test
       END_OF_TEST
     in which case we'd need to skip past END_OF_TITLE. Given how
     unlikely it is for anybody to do this, and since it would only
     affect line numbers, it's probably not worth caring about too much.
     The solution would probably be to record the starting line number
     of each here-doc section in the lexer/shellparser stage.
Signed-off-by: Jeff King <peff@peff.net>
---
Note to the maintainer: do not worry about applying these yet! The
parent message describes where they'd go in the series, but I'll send a
full series once Eric and I have worked out the details. Review comments
welcome, of course. :)

I actually suspect the "record the heredoc line number" thing would not be too hard. I.e., turn ShellParser's "heredoc" hash to point to hashrefs like: "{ content => ..., lineno => ... }". And that would give us a good spot to stick an "interpolate" boolean later if we want.

 t/chainlint.pl | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/t/chainlint.pl b/t/chainlint.pl
index eba509b8e1..c9ab79b6b0 100755
--- a/t/chainlint.pl
+++ b/t/chainlint.pl
@@ -620,15 +620,19 @@ sub unwrap {
 sub check_test {
 	my $self = shift @_;
 	my $title = unwrap(shift @_);
-	my $body = unwrap(shift @_);
-	$body = shift @_ if $body eq '-';
+	my $body = shift @_;
+	my $lineno = $body->[3];
+	$body = unwrap($body);
+	if ($body eq '-') {
+		$body = shift @_;
+		$lineno++;
+	}
 	$self->{ntests}++;
 	my $parser = TestParser->new(\$body);
 	my @tokens = $parser->parse();
 	my $problems = $parser->{problems};
 	return unless $emit_all || @$problems;
 	my $c = main::fd_colors(1);
-	my $lineno = $_[1]->[3];
 	my $start = 0;
 	my $checked = '';
 	for (sort {$a->[1]->[2] <=> $b->[1]->[2]} @$problems) {
-- 
2.45.2.1178.gaaad15bb7b
Previous: Jeff KingNext: Eric Sunshine
Message 25 of 65 in “here-doc test bodies”
  1. 0/2 here-doc test bodiesJeff King, Jul 1, 2024
  2. 1/2 test-lib: allow test snippets as here-docsJeff King, Jul 1, 2024
  3. Eric SunshineJul 1, 2024
  4. Junio C HamanoJul 1, 2024
  5. Jeff KingJul 2, 2024
  6. Jeff KingJul 2, 2024
  7. Eric SunshineJul 2, 2024
  8. Jeff KingJul 6, 2024
  9. Jeff KingJul 2, 2024
  10. Eric SunshineJul 2, 2024
  11. Jeff KingJul 6, 2024
  12. Eric SunshineJul 2, 2024
  13. Eric SunshineJul 2, 2024
  14. Eric SunshineJul 2, 2024
  15. Jeff KingJul 6, 2024
  16. Jeff KingJul 6, 2024
  17. Eric SunshineJul 6, 2024
  18. Eric SunshineJul 6, 2024
  19. Jeff KingJul 6, 2024
  20. Eric SunshineJul 6, 2024
  21. Jeff KingJul 6, 2024
  22. 2/2 t: convert some here-doc test bodiesJeff King, Jul 1, 2024
  23. chainlint.pl: recognize test bodies defined via heredocEric Sunshine, Jul 2, 2024
  24. Jeff KingJul 6, 2024
  25. 1/3 chainlint.pl: fix line number reportingJeff King, Jul 6, 2024
  26. Eric SunshineJul 8, 2024
  27. Jeff KingJul 8, 2024
  28. 2/3 t/chainlint: add test_expect_success call to test snippetsJeff King, Jul 6, 2024
  29. Jeff KingJul 6, 2024
  30. Eric SunshineJul 8, 2024
  31. 3/3 t/chainlint: add tests for test body in heredocJeff King, Jul 6, 2024
  32. Eric SunshineJul 8, 2024
  33. Jeff KingJul 8, 2024
  34. Junio C HamanoJul 6, 2024
  35. Jeff KingJul 6, 2024
  36. Eric SunshineJul 8, 2024
  37. Jeff KingJul 8, 2024
  38. Eric SunshineJul 8, 2024
  39. Eric SunshineJul 8, 2024
  40. Jeff KingJul 10, 2024
  41. Jeff KingJul 10, 2024
  42. Eric SunshineJul 10, 2024
  43. Jeff KingJul 10, 2024
  44. Eric SunshineJul 10, 2024
  45. Eric SunshineJul 8, 2024
  46. Jeff KingJul 8, 2024
  47. Eric SunshineJul 8, 2024
  48. Jeff KingJul 10, 2024
  49. Eric SunshineJul 10, 2024
  50. 0/9 here-doc test bodies (now with 100% more chainlinting)Jeff King, Jul 10, 2024
  51. 1/9 chainlint.pl: add test_expect_success call to test snippetsJeff King, Jul 10, 2024
  52. 2/9 chainlint.pl: only start threads if jobs > 1Jeff King, Jul 10, 2024
  53. 3/9 chainlint.pl: do not spawn more threads than we have scriptsJeff King, Jul 10, 2024
  54. 4/9 chainlint.pl: force CRLF conversion when opening input filesJeff King, Jul 10, 2024
  55. 5/9 chainlint.pl: check line numbers in expected outputJeff King, Jul 10, 2024
  56. Eric SunshineAug 21, 2024
  57. Jeff KingAug 21, 2024
  58. Eric SunshineAug 21, 2024
  59. 6/9 chainlint.pl: recognize test bodies defined via heredocJeff King, Jul 10, 2024
  60. 7/9 chainlint.pl: add tests for test body in heredocJeff King, Jul 10, 2024
  61. 8/9 test-lib: allow test snippets as here-docsJeff King, Jul 10, 2024
  62. 9/9 t: convert some here-doc test bodiesJeff King, Jul 10, 2024
  63. 10/9 t/.gitattributes: ignore whitespace in chainlint expect filesJeff King, Jul 10, 2024
  64. Junio C HamanoJul 10, 2024
  65. Eric SunshineAug 21, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.