git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Hariom Verma via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,
	"Christian Couder" <christian.couder@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Hariom Verma" <hariom18599@gmail.com>
Subject: Re: [PATCH v3 0/3] Unify trailers formatting logic for pretty.c and ref-filter.c
Date: Sat, 06 Feb 2021 21:06:44 -0800	[thread overview]
Message-ID: <xmqqy2g08o0r.fsf@gitster.c.googlers.com> (raw)
In-Reply-To: <xmqqim74a6x1.fsf@gitster.c.googlers.com> (Junio C. Hamano's message of "Sat, 06 Feb 2021 19:33:14 -0800")

Junio C Hamano <gitster@pobox.com> writes:

> "Hariom Verma via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>>      @@ t/t6300-for-each-ref.sh: test_expect_success '%(trailers:only) and %(trailers:un
>>       +	option="$2"
>>       +	expect="$3"
>>       +	test_expect_success "$title" '
>>      -+		echo $expect >expect &&
>>      ++		printf "$expect\n" >expect &&
>
> Are we sure that "$expect" would not ever have any '%' in it, to
> confuse printf?

Just to make sure we won't waste your time in useless roundtrip(s),
let me say that possible unacceptable answers are:

 - no, right now nobody passes a % in it
 - no, I do not expect anybody needs to pass a % in it
 - when somebody really needs to pass a %, they can write it as %%

The last one is the worst one, by the way.

The point of adding a test_trailer_option HELPER function is to HELP
the developers who write tests, now and in the future.  There are
some things they MUST know to use the helper successfully, like it
takes three parameters, the first one being the test title, the
second one is the string you'd give as the "--format=<format>"
option to the for-each-ref command, and the third one is the
expected output.

Forcing them to know any more than that is *not* helping them.

The shell programming language is perfectly capable of passing an
argument that happens to be a multi-line string to functions and
external commands, and the developers who are writing test knows
that already (the last argument to test_expect_success used
everywhere in the test suite, that is a multi-line code snippet, is
a good example).  When they need to write an expected output that is
two lines, they expect to be able to write things like

	test_trailer_option title format-string \
	'expected line #1
	expected line #2'

without having to worry about the need for special formatting that
is applicable *only* when passing argument to this helper.  They do
not need to know that they cannot pass backslash-en literally, and
have to say '\\n' instead, or they have to double a per-cent sign,
only when using this helper but not other helper functions.

In the message I am replying to, I used

	printf "%s\n" "$expect"

for a reason.  We expect that trailer options output are complete
lines, so it is annoying to force the caller to write the final
newline, especially if many of the callers have only one line of
expected output.  So

    test_trailer_option title format-string 'expected output'

would end up doing

	printf "%s\n" "expected output" >expect

to write a complete line, i.e. a caller does not have to say any of

    test_trailer_option title format-string 'expected output\n'

    test_trailer_option title format-string 'expected output
    '

    lf='
    '
    test_trailer_option title format-string "expected output$lf"

If we do not need to extend test_trailer_option with further
"features" (like "check output case insensitively this time" or
"allow output lines in any order"), we can make it even nicer and
easier to use for callers, by the way.

For example, with this (by the way, make sure there are SP on both
sides around "()", that's our house style):

	test_trailer_option () {
		title=$1 option=$2
		shift 2
		if test $# != 0
		then
			printf "%s\n" "$@"
		fi >expect
		test_expect_success "$title" '
			... >actual &&
			test_cmp expect actual &&
			... >actual &&
			test_cmp expect actual
		'
	}

the caller can do

	test_trailer_option 'single line output' \
		'trailers:key=Signed-off-by' \
		'Signed-off-by: A U Thor <author@example.com>'

	test_trailer_option 'expect two lines' \
		'trailers:key=Reviewed-by' \
		'Reviewed-by: A U Thor <author@example.com>' \
		'Reviewed-by: R E Viewer <reviewer@example.com>'

	test_trailer_option 'no output expected' 'trailers:key=no-such:' ''

That is, instead of "the first arg is title, the second is format
and the third is the entire expected output", the helper's manual
can say "give title and format as the first and the second argument.
Each argument after that is an expected output, one line per arg."

Another possibility is to feed the expected output from the standard
input of the helper, e.g.

	test_trailer_option () {
		title=$1 option=$2
		cat >expect
		test_expect_success "$title" '
			... >actual &&
			test_cmp expect actual &&
			... >actual &&
			test_cmp expect actual
		'
	}

And the caller can now do:

	test_trailer_option 'expect two lines' 'trailers:key=Reviewed-by' <<-\EOF
        Reviewed-by: A U Thor <author@example.com>
        Reviewed-by: R E Viewer <reviewer@example.com>
	EOF

It is a bit cumbersome when the expected output is a single line:

	test_trailer_option 'single line output' 'trailers:key=Signed-off-by' <<-\EOF
	Signed-off-by: A U Thor <author@example.com>
	EOF

but the contrast between the "two-line expected" case and this one
would be easy to see when reading the tests.  The pattern to write
"expect no output" would be quite simple, too:

	test_trailer_option 'no output expected' 'trailers:key=no-such:' </dev/null

Among the ones designed while writing this response, I would think I
like the last one, i.e. "the first arg is title, the second arg is
format, and the expected output is given from the standasd output"
probably the best.

Thanks.

  reply	other threads:[~2021-02-07  5:08 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-05 19:48 [PATCH 0/2] Unify trailers formatting logic for pretty.c and ref-filter.c Hariom Verma via GitGitGadget
2020-09-05 19:48 ` [PATCH 1/2] pretty.c: refactor trailer logic to `format_set_trailers_options()` Hariom Verma via GitGitGadget
2020-09-05 21:59   ` René Scharfe
2020-09-05 19:48 ` [PATCH 2/2] ref-filter: using pretty.c logic for trailers Hariom Verma via GitGitGadget
2021-01-29 21:09 ` [PATCH v2 0/3] Unify trailers formatting logic for pretty.c and ref-filter.c Hariom Verma via GitGitGadget
2021-01-29 21:09   ` [PATCH v2 1/3] pretty.c: refactor trailer logic to `format_set_trailers_options()` Hariom Verma via GitGitGadget
2021-01-29 23:49     ` Junio C Hamano
2021-01-29 21:09   ` [PATCH v2 2/3] pretty.c: capture invalid trailer argument Hariom Verma via GitGitGadget
2021-01-29 22:28     ` Christian Couder
2021-01-30 19:16       ` Hariom verma
2021-01-30  0:01     ` Junio C Hamano
2021-01-30 19:00       ` Hariom verma
2021-01-30  0:07     ` Junio C Hamano
2021-01-30 19:06       ` Hariom verma
2021-01-29 21:09   ` [PATCH v2 3/3] ref-filter: use pretty.c logic for trailers Hariom Verma via GitGitGadget
2021-01-30 20:45     ` Ævar Arnfjörð Bjarmason
2021-02-04 18:46       ` Hariom verma
2021-02-04 20:53         ` Ævar Arnfjörð Bjarmason
2021-01-30  1:17   ` [PATCH v2 0/3] Unify trailers formatting logic for pretty.c and ref-filter.c Junio C Hamano
2021-01-30  1:28   ` Junio C Hamano
2021-01-30 19:15     ` Hariom verma
2021-01-30 20:20     ` Junio C Hamano
2021-02-06  9:15   ` [PATCH v3 " Hariom Verma via GitGitGadget
2021-02-06  9:15     ` [PATCH v3 1/3] pretty.c: refactor trailer logic to `format_set_trailers_options()` Hariom Verma via GitGitGadget
2021-02-06  9:15     ` [PATCH v3 2/3] pretty.c: capture invalid trailer argument Hariom Verma via GitGitGadget
2021-02-06  9:15     ` [PATCH v3 3/3] ref-filter: use pretty.c logic for trailers Hariom Verma via GitGitGadget
2021-02-07  5:45       ` Junio C Hamano
2021-02-07 12:06         ` Hariom verma
2021-02-07 18:19           ` Junio C Hamano
2021-02-07 19:38             ` Hariom verma
2021-02-07 20:09               ` Junio C Hamano
2021-02-08 17:07                 ` Hariom verma
2021-02-08 18:29                   ` Junio C Hamano
     [not found]                     ` <xmqqlfby5o9h.fsf@gitster.c.googlers.com>
2021-02-08 23:43                       ` brian m. carlson
2021-02-09  3:04                       ` brian m. carlson
2021-02-09 20:54                         ` Junio C Hamano
2021-02-07  3:33     ` [PATCH v3 0/3] Unify trailers formatting logic for pretty.c and ref-filter.c Junio C Hamano
2021-02-07  5:06       ` Junio C Hamano [this message]
2021-02-13  1:52     ` [PATCH v4 0/4] " Hariom Verma via GitGitGadget
2021-02-13  1:52       ` [PATCH v4 1/4] t6300: use function to test trailer options Hariom Verma via GitGitGadget
2021-02-13  1:52       ` [PATCH v4 2/4] pretty.c: refactor trailer logic to `format_set_trailers_options()` Hariom Verma via GitGitGadget
2021-02-13  1:52       ` [PATCH v4 3/4] pretty.c: capture invalid trailer argument Hariom Verma via GitGitGadget
2021-02-13  1:52       ` [PATCH v4 4/4] ref-filter: use pretty.c logic for trailers Hariom Verma via GitGitGadget

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=xmqqy2g08o0r.fsf@gitster.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=avarab@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=hariom18599@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 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).