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

Re: [PATCH v3 0/3] Unify trailers formatting logic for pretty.c and ref-filter.c

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 7, 2021, 05:06 UTC
Message-ID
<xmqqy2g08o0r.fsf@gitster.c.googlers.com>
In-Reply-To
<xmqqim74a6x1.fsf@gitster.c.googlers.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 11 quoted lines
> "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.
Previous: Junio C HamanoNext: Hariom Verma via GitGitGadget
Message 35 of 43 in “Unify trailers formatting logic for pretty.c and ref-filter.c”
  1. 0/2 Unify trailers formatting logic for pretty.c and ref-filter.cHariom Verma via GitGitGadget, Sep 5, 2020
  2. 1/2 pretty.c: refactor trailer logic to `format_set_trailers_options()`Hariom Verma via GitGitGadget, Sep 5, 2020
  3. René ScharfeSep 5, 2020
  4. 2/2 ref-filter: using pretty.c logic for trailersHariom Verma via GitGitGadget, Sep 5, 2020
  5. 0/3 Unify trailers formatting logic for pretty.c and ref-filter.cHariom Verma via GitGitGadget, Jan 29, 2021
  6. 1/3 pretty.c: refactor trailer logic to `format_set_trailers_options()`Hariom Verma via GitGitGadget, Jan 29, 2021
  7. Junio C HamanoJan 29, 2021
  8. 2/3 pretty.c: capture invalid trailer argumentHariom Verma via GitGitGadget, Jan 29, 2021
  9. Christian CouderJan 29, 2021
  10. Hariom vermaJan 30, 2021
  11. Junio C HamanoJan 30, 2021
  12. Hariom vermaJan 30, 2021
  13. Junio C HamanoJan 30, 2021
  14. Hariom vermaJan 30, 2021
  15. 3/3 ref-filter: use pretty.c logic for trailersHariom Verma via GitGitGadget, Jan 29, 2021
  16. Ævar Arnfjörð BjarmasonJan 30, 2021
  17. Hariom vermaFeb 4, 2021
  18. Ævar Arnfjörð BjarmasonFeb 4, 2021
  19. Junio C HamanoJan 30, 2021
  20. Junio C HamanoJan 30, 2021
  21. Hariom vermaJan 30, 2021
  22. Junio C HamanoJan 30, 2021
  23. 0/3 Unify trailers formatting logic for pretty.c and ref-filter.cHariom Verma via GitGitGadget, Feb 6, 2021
  24. 1/3 pretty.c: refactor trailer logic to `format_set_trailers_options()`Hariom Verma via GitGitGadget, Feb 6, 2021
  25. 2/3 pretty.c: capture invalid trailer argumentHariom Verma via GitGitGadget, Feb 6, 2021
  26. 3/3 ref-filter: use pretty.c logic for trailersHariom Verma via GitGitGadget, Feb 6, 2021
  27. Junio C HamanoFeb 7, 2021
  28. Hariom vermaFeb 7, 2021
  29. Junio C HamanoFeb 7, 2021
  30. Hariom vermaFeb 7, 2021
  31. Junio C HamanoFeb 7, 2021
  32. Hariom vermaFeb 8, 2021
  33. Junio C HamanoFeb 8, 2021
  34. Junio C HamanoFeb 7, 2021
  35. Junio C HamanoFeb 7, 2021
  36. 0/4 Unify trailers formatting logic for pretty.c and ref-filter.cHariom Verma via GitGitGadget, Feb 13, 2021
  37. 1/4 t6300: use function to test trailer optionsHariom Verma via GitGitGadget, Feb 13, 2021
  38. 2/4 pretty.c: refactor trailer logic to `format_set_trailers_options()`Hariom Verma via GitGitGadget, Feb 13, 2021
  39. 3/4 pretty.c: capture invalid trailer argumentHariom Verma via GitGitGadget, Feb 13, 2021
  40. 4/4 ref-filter: use pretty.c logic for trailersHariom Verma via GitGitGadget, Feb 13, 2021
  41. brian m. carlsonFeb 8, 2021
  42. brian m. carlsonFeb 9, 2021
  43. Junio C HamanoFeb 9, 2021

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.