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

Re: [PATCH 1/2] test-lib: allow test snippets as here-docs

From
Jeff King <peff@peff.net>
Date
Apr 10, 2021, 01:26 UTC
Message-ID
<YHD+3mKoQmzIagjF@coredump.intra.peff.net>
In-Reply-To
<xmqqh7kfklcp.fsf@gitster.g>
On Fri, Apr 09, 2021 at 05:57:10PM -0700, Junio C Hamano wrote:
Show 29 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> >> +	# start with a newline, to match hanging newline from open-quote style
> >> +	eval "$1=\$LF"
> >> +	local test_line
> >> +	while IFS= read -r test_line
> >> +	do
> >> +		eval "$1=\${$1}\${test_line}\${LF}"
> >> +	done
> >
> > I wonder if we can do this without relying on "read -r" (which I
> > distrust, perhaps out of superstition)?  Perhaps by slurping the
> > whole thing with "$(cat)"?
> 
> Meaning, something along this line...
> 
> ----- >8 --------- >8 --------- >8 --------- >8 ----
> #!/bin/sh
> LF='
> '
> ttt () {
> 	eval "$1"='$LF$(cat)'
> }
> ttt script <<\EOT
> 	a
> 	b
> EOT
> echo "<<<$script>>>"
> ----- 8< --------- 8< --------- 8< --------- 8< ----

I wrote it using cat initially. If you want to preserve trailing whitespace, you have to do something like:

  val=$(printf '\n'
        cat
	printf '.')
  val=${val%.}

I was worried about adding the overhead of the extra subshell and process for the command substitution, but perhaps that is overblown. TBH, worrying about whitespace may be overblown, too. Some test snippets have extra blank lines at the end, but possibly we should just not care.

I imagine "read -r" does not work reliably for binary junk, but keep in mind that we are talking about text shell script snippets (that are already in shell strings anyway).

-Peff
Previous: Junio C HamanoNext: Ævar Arnfjörð Bjarmason
Message 6 of 10 in “here-doc test bodies”
  1. 0/2 here-doc test bodiesJeff King, Apr 9, 2021
  2. 1/2 test-lib: allow test snippets as here-docsJeff King, Apr 9, 2021
  3. Jeff KingApr 9, 2021
  4. Junio C HamanoApr 9, 2021
  5. Junio C HamanoApr 10, 2021
  6. Jeff KingApr 10, 2021
  7. Ævar Arnfjörð BjarmasonApr 10, 2021
  8. 2/2 t1404: convert to here-doc test bodiesJeff King, Apr 9, 2021
  9. Derrick StoleeApr 10, 2021
  10. Jeff KingApr 10, 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.