All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, "SZEDER Gábor" <szeder.dev@gmail.com>
Subject: Re: [PATCH 3/3] t/README: document how to loop around test cases
Date: Fri, 22 Mar 2024 03:05:36 +0100	[thread overview]
Message-ID: <ZfzncGTNVwR8BrhR@tanuki> (raw)
In-Reply-To: <xmqqzfur8q5z.fsf@gitster.g>

[-- Attachment #1: Type: text/plain, Size: 2648 bytes --]

On Thu, Mar 21, 2024 at 10:12:40AM -0700, Junio C Hamano wrote:
> 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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2024-03-22  2:05 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-21 13:47 [PATCH 0/3] t7800: fix quoting of looped test bodies Patrick Steinhardt
2024-03-21 13:47 ` [PATCH 1/3] t7800: improve test descriptions with empty arguments Patrick Steinhardt
2024-03-21 13:47 ` [PATCH 2/3] t7800: use single quotes for test bodies Patrick Steinhardt
2024-03-21 13:47 ` [PATCH 3/3] t/README: document how to loop around test cases Patrick Steinhardt
2024-03-21 17:12   ` Junio C Hamano
2024-03-22  2:05     ` Patrick Steinhardt [this message]
2024-03-22  2:12       ` Junio C Hamano
2024-03-21 13:48 ` [PATCH 0/3] t7800: fix quoting of looped test bodies Patrick Steinhardt
2024-03-22  2:23 ` [PATCH v2 " Patrick Steinhardt
2024-03-22  2:23   ` [PATCH v2 1/3] t7800: improve test descriptions with empty arguments Patrick Steinhardt
2024-03-22  2:23   ` [PATCH v2 2/3] t7800: use single quotes for test bodies Patrick Steinhardt
2024-03-22  2:23   ` [PATCH v2 3/3] t/README: document how to loop around test cases Patrick Steinhardt
2024-03-22 14:37   ` [PATCH v2 0/3] t7800: fix quoting of looped test bodies Junio C Hamano

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=ZfzncGTNVwR8BrhR@tanuki \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=szeder.dev@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.