git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, "Junio C Hamano" <gitster@pobox.com>,
	"René Scharfe" <l.s.r@web.de>
Subject: Re: [PATCH 1/2] test-lib: allow test snippets as here-docs
Date: Sat, 6 Jul 2024 02:11:13 -0400	[thread overview]
Message-ID: <CAPig+cQnZjMBPooBqMJjPY78EiCEXQOSSyHBm4GtLcbsSqZKrw@mail.gmail.com> (raw)
In-Reply-To: <20240706053105.GB698153@coredump.intra.peff.net>

On Sat, Jul 6, 2024 at 1:31 AM Jeff King <peff@peff.net> wrote:
> On Tue, Jul 02, 2024 at 05:25:48PM -0400, Eric Sunshine wrote:
> > My implementation, however, takes a more formal and paranoid stance.
> > Rather than squirreling away only the most-recently-seen heredoc body,
> > it stores each heredoc body along with the tag which introduced it.
> > This makes it robust against cases when multiple heredocs are
> > initiated on the same line (even within different parse contexts):
> >
> >     cat <<EOFA && x=$(cat <<EOFB &&
> >     A body
> >     EOFA
> >     B body
> >     EOFB
> >
> > Of course, that's not likely to come up in the context of
> > test_expect_* calls, but I prefer the added robustness over the more
> > lax approach.
>
> Yes, that's so much better than what I wrote. I didn't engage my brain
> very much when I read the in-code comments about multiple tags on the
> same line, and I thought you meant:
>
>   cat <<FOO <<BAR
>   this is foo
>   FOO
>   this is bar
>   BAR
>
> which is...weird. It does "work" in the sense that "FOO" is a here-doc
> that should be skipped past. But it is not doing anything useful; cat
> sees only "this is bar" on stdin. So even for this case, the appending
> behavior that my patch does would not make sense.
>
> And of course for the actual useful thing, which you wrote above,
> appending is just nonsense. Recording and accessing by tag is the right
> thing.

In retrospect, I think my claim is bogus in the context of
ScriptParser::parse_cmd(). Specifically, ScriptParser::parse_cmd()
calls its parent ShellParser::parse_cmd() to latch one command.
ShellParser::parse_cmd() stops parsing as soon as it encounters a
command terminator (i.e. `;`, `&&`, `||`, `|`, '&', '\n') and returns
the command. Moreover, by definition, given the language
specification, the lexer only consumes the heredocs upon encountering
`\n`. Thus, if someone writes:

    test_expect_success title - <<\EOT && whatever &&
    ...test body...
    EOT

then ScriptParser::parse_cmd() will receive the command
`test_expect_success title -` from ShellParser::parse_cmd() but the
heredoc will not yet have been consumed by the lexer since it hasn't
yet encountered the newline[1].

So, the above example simply can't work correctly given the way
ScriptParser::parse_cmd() calls ScriptParser::check_test() as soon as
it encounters a `test_expect_success/failure` invocation since it
doesn't know if the heredocs have been latched at that point. To make
it properly robust, rather than immediately calling check_test(), it
would have to continue consuming commands, and saving the ones which
match `test_expect_success/failure` invocation, until it finally hits
a `\n`, and only then call check_test() with each command it saved.
But that's probably overkill at this point considering that we never
write code like the above, so the submitted patch[2] is probably good
enough for now.

FOOTNOTES

[1] One might rightly ask that if ShellParser::parse_cmd() returns
immediately upon seeing a command terminator (i.e. `;`, `&&`, etc.),
then how is it that even a simple:

    test_expect_success title - <<\EOT &&
    ...test body...
    EOT

can work correctly since the `\n` comes after the `&&`. The answer is
that, as a special case, the very last thing ShellParser::parse_cmd()
does is peek ahead to see if a `\n` follows the command terminator
(assuming the terminator is not itself a `\n`). When the next token is
indeed a `\n`, that peek operation causes the lexer to consume the
heredocs.

[2]: https://lore.kernel.org/git/20240702235034.88219-1-ericsunshine@charter.net/

  parent reply	other threads:[~2024-07-06  6:11 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-01 22:08 [PATCH 0/2] here-doc test bodies Jeff King
2024-07-01 22:08 ` [PATCH 1/2] test-lib: allow test snippets as here-docs Jeff King
2024-07-01 22:45   ` Eric Sunshine
2024-07-01 23:43     ` Junio C Hamano
2024-07-02  0:51     ` Jeff King
2024-07-02  1:13       ` Jeff King
2024-07-02 21:37         ` Eric Sunshine
2024-07-06  5:44           ` Jeff King
2024-07-02 21:19       ` Jeff King
2024-07-02 21:59         ` Eric Sunshine
2024-07-06  5:23           ` Jeff King
2024-07-02 21:25       ` Eric Sunshine
2024-07-02 22:36         ` Eric Sunshine
2024-07-02 22:48         ` Eric Sunshine
2024-07-06  5:31         ` Jeff King
2024-07-06  5:33           ` Jeff King
2024-07-06  6:11           ` Eric Sunshine [this message]
2024-07-06  6:47             ` Eric Sunshine
2024-07-06  6:55               ` Jeff King
2024-07-06  7:06                 ` Eric Sunshine
2024-07-06  6:54             ` Jeff King
2024-07-01 22:08 ` [PATCH 2/2] t: convert some here-doc test bodies Jeff King
2024-07-02 23:50 ` [PATCH] chainlint.pl: recognize test bodies defined via heredoc Eric Sunshine
2024-07-06  6:01   ` Jeff King
2024-07-06  6:05     ` [PATCH 1/3] chainlint.pl: fix line number reporting Jeff King
2024-07-08  5:08       ` Eric Sunshine
2024-07-08  9:10         ` Jeff King
2024-07-06  6:06     ` [PATCH 2/3] t/chainlint: add test_expect_success call to test snippets Jeff King
2024-07-06  6:09       ` Jeff King
2024-07-08  3:59         ` Eric Sunshine
2024-07-06  6:07     ` [PATCH 3/3] t/chainlint: add tests for test body in heredoc Jeff King
2024-07-08  2:43       ` Eric Sunshine
2024-07-08  8:59         ` Jeff King
2024-07-06 22:15     ` [PATCH] chainlint.pl: recognize test bodies defined via heredoc Junio C Hamano
2024-07-06 23:11       ` Jeff King
2024-07-08  3:51         ` Eric Sunshine
2024-07-08  9:08           ` Jeff King
2024-07-08 19:46             ` Eric Sunshine
2024-07-08 20:17               ` Eric Sunshine
2024-07-10  0:37                 ` Jeff King
2024-07-10  1:09             ` Jeff King
2024-07-10  3:02               ` Eric Sunshine
2024-07-10  7:06                 ` Jeff King
2024-07-10  7:29                   ` Eric Sunshine
2024-07-08  3:40     ` Eric Sunshine
2024-07-08  9:05       ` Jeff King
2024-07-08 20:06         ` Eric Sunshine
2024-07-10  0:48           ` Jeff King
2024-07-10  2:38             ` Eric Sunshine
2024-07-10  8:34 ` [PATCH v2 0/9] here-doc test bodies (now with 100% more chainlinting) Jeff King
2024-07-10  8:34   ` [PATCH v2 1/9] chainlint.pl: add test_expect_success call to test snippets Jeff King
2024-07-10  8:35   ` [PATCH v2 2/9] chainlint.pl: only start threads if jobs > 1 Jeff King
2024-07-10  8:35   ` [PATCH v2 3/9] chainlint.pl: do not spawn more threads than we have scripts Jeff King
2024-07-10  8:37   ` [PATCH v2 4/9] chainlint.pl: force CRLF conversion when opening input files Jeff King
2024-07-10  8:37   ` [PATCH v2 5/9] chainlint.pl: check line numbers in expected output Jeff King
2024-08-21  7:00     ` Eric Sunshine
2024-08-21 12:14       ` Jeff King
2024-08-21 17:02         ` Eric Sunshine
2024-07-10  8:38   ` [PATCH v2 6/9] chainlint.pl: recognize test bodies defined via heredoc Jeff King
2024-07-10  8:39   ` [PATCH v2 7/9] chainlint.pl: add tests for test body in heredoc Jeff King
2024-07-10  8:39   ` [PATCH v2 8/9] test-lib: allow test snippets as here-docs Jeff King
2024-07-10  8:39   ` [PATCH v2 9/9] t: convert some here-doc test bodies Jeff King
2024-07-10  8:47   ` [PATCH v2 10/9] t/.gitattributes: ignore whitespace in chainlint expect files Jeff King
2024-07-10 17:15     ` Junio C Hamano
2024-08-21  7:31   ` [PATCH v2 0/9] here-doc test bodies (now with 100% more chainlinting) Eric Sunshine
  -- strict thread matches above, loose matches on Subject: below --
2021-04-09 22:26 [RFC/PATCH 0/2] here-doc test bodies Jeff King
2021-04-09 22:28 ` [PATCH 1/2] test-lib: allow test snippets as here-docs Jeff King
2021-04-09 22:30   ` Jeff King
2021-04-09 22:56   ` Junio C Hamano
2021-04-10  0:57     ` Junio C Hamano
2021-04-10  1:26       ` Jeff King
2021-04-10  8:30   ` Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPig+cQnZjMBPooBqMJjPY78EiCEXQOSSyHBm4GtLcbsSqZKrw@mail.gmail.com \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).