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

Re: [PATCH 3/3] t/README: document how to loop around test cases

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 22, 2024, 02:05 UTC
Message-ID
<ZfzncGTNVwR8BrhR@tanuki>
In-Reply-To
<xmqqzfur8q5z.fsf@gitster.g>
On Thu, Mar 21, 2024 at 10:12:40AM -0700, Junio C Hamano wrote:
Show 57 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > In some cases it makes sense to loop around test cases so that we can
> > execute the same test with slightly different arguments. There are some
> > gotchas around quoting here though that are easy to miss and that may
> > lead to easy-to-miss errors and portability issues.
> >
> > Document the proper way to do this in "t/README".
> >
> > Signed-off-by: Patrick Steinhardt <ps@pks.im>
> > ---
> >  t/README | 19 +++++++++++++++++++
> >  1 file changed, 19 insertions(+)
> >
> > diff --git a/t/README b/t/README
> > index 36463d0742..d56401a254 100644
> > --- a/t/README
> > +++ b/t/README
> > @@ -721,6 +721,25 @@ The "do's:"
> >     Note that we still &&-chain the loop to propagate failures from
> >     earlier commands.
> >  
> > + - Repeat tests with slightly different arguments in a loop.
> > +
> > +   In some cases it may make sense to re-run the same set of tests with
> > +   different options or commands to ensure that the command behaves
> > +   despite the different parameters. This can be achieved by looping
> > +   around a specific parameter:
> > +
> > +	for arg in '' "--foo"
> > +	do
> > +		test_expect_success "test command ${arg:-without arguments}" '
> > +			command $arg
> > +		'
> > +	done
> > +
> > +  Note that while the test title uses double quotes ("), the test body
> > +  should continue to use single quotes ('). The loop variable will be
> > +  accessible regardless of the single quotes as the test body is passed
> > +  to `eval`.
> 
> We also want to say that they are not equivalent, don't we?
> 
>         for var in '' a 'b"c'
>         do
>                 test_expect_success "with dq <$var>" "
>                         echo \"$var\"
>                 "
>         done
> 
> breaks, but if we use
> 
>                 test_expect_success "with sq <$var>" '
>                         echo "$var"
>                 '
> 
> in the loop, it works as expected.

Hum, good point. How about the below diff? Will reroll the patch series if that looks good to you.

--- a/t/README
+++ b/t/README
@@ -736,7 +736,8 @@ The "do's:"
        done

   Note that while the test title uses double quotes ("), the test body
-  should continue to use single quotes ('). The loop variable will be
+  should continue to use single quotes (') to avoid breakage in case the
+  values contain e.g. quoting characters. The loop variable will be
   accessible regardless of the single quotes as the test body is passed
   to `eval`.

Patrick
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 13 in “t7800: fix quoting of looped test bodies”
  1. 0/3 t7800: fix quoting of looped test bodiesPatrick Steinhardt, Mar 21, 2024
  2. 1/3 t7800: improve test descriptions with empty argumentsPatrick Steinhardt, Mar 21, 2024
  3. 2/3 t7800: use single quotes for test bodiesPatrick Steinhardt, Mar 21, 2024
  4. 3/3 t/README: document how to loop around test casesPatrick Steinhardt, Mar 21, 2024
  5. Junio C HamanoMar 21, 2024
  6. Patrick SteinhardtMar 22, 2024
  7. Junio C HamanoMar 22, 2024
  8. Patrick SteinhardtMar 21, 2024
  9. 0/3 t7800: fix quoting of looped test bodiesPatrick Steinhardt, Mar 22, 2024
  10. 1/3 t7800: improve test descriptions with empty argumentsPatrick Steinhardt, Mar 22, 2024
  11. 2/3 t7800: use single quotes for test bodiesPatrick Steinhardt, Mar 22, 2024
  12. 3/3 t/README: document how to loop around test casesPatrick Steinhardt, Mar 22, 2024
  13. Junio C HamanoMar 22, 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.