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

Re: [PATCH 3/6] t: local VAR="VAL" (quote positional parameters)

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 8, 2024, 17:23 UTC
Message-ID
<xmqqil0rdazm.fsf@gitster.g>
In-Reply-To
<ZhQNq4ITp68ikVVy@tanuki>
Patrick Steinhardt <ps@pks.im> writes:
Show 32 quoted lines
> On Fri, Apr 05, 2024 at 05:08:59PM -0700, Junio C Hamano wrote:
>> Future-proof test scripts that do
>> 
>> 	local VAR=VAL
>> 
>> without quoting VAL (which is OK in POSIX but broken in some shells)
>> that is a positional parameter, e.g. $4.
>> 
>> Signed-off-by: Junio C Hamano <gitster@pobox.com>
>> ---
>>  t/lib-parallel-checkout.sh | 2 +-
>>  t/t2400-worktree-add.sh    | 2 +-
>>  t/t4210-log-i18n.sh        | 4 ++--
>>  t/test-lib-functions.sh    | 2 +-
>>  4 files changed, 5 insertions(+), 5 deletions(-)
>> 
>> diff --git a/t/lib-parallel-checkout.sh b/t/lib-parallel-checkout.sh
>> index acaee9cbb6..8324d6c96d 100644
>> --- a/t/lib-parallel-checkout.sh
>> +++ b/t/lib-parallel-checkout.sh
>> @@ -20,7 +20,7 @@ test_checkout_workers () {
>>  		BUG "too few arguments to test_checkout_workers"
>>  	fi &&
>>  
>> -	local expected_workers=$1 &&
>> +	local expected_workers="$1" &&
>>  	shift &&
>
> I was wondering a bit why this is a problem in t0610, but not over here.
> As far as I understand it these statements are fine in practice because
> the expanded values cannot be split, right? So if "$1" expanded to
> something with spaces in between things would start to break.
Correct.
> In any case, changing all of these to be quoted feels like the right
> thing to do regardless of whether or not it happens to work with the
> current values of "$1". Otherwise it's simply a confusing failure
> waiting to happen.
Again, agreed.  That is where my "Future-proof" comes from.

The true objective of this change is so that the last patch does not have to learn too much exceptions ;-) As long as expected_workers is expected to be a number (an unbroken sequence of digits), even if we add more callers to this helper in the future, $1 we see here is expected to be $IFS safe. So in that sense my "future-proof" is a white lie.

Previous: Patrick SteinhardtNext: Junio C Hamano
Message 10 of 23 in “local VAR="VAL"”
  1. 0/6 local VAR="VAL"Junio C Hamano, Apr 6, 2024
  2. 1/6 CodingGuidelines: describe "export VAR=VAL" ruleJunio C Hamano, Apr 6, 2024
  3. Eric SunshineApr 6, 2024
  4. Junio C HamanoApr 6, 2024
  5. Andreas SchwabApr 6, 2024
  6. Junio C HamanoApr 6, 2024
  7. Eric SunshineApr 6, 2024
  8. 3/6 t: local VAR="VAL" (quote positional parameters)Junio C Hamano, Apr 6, 2024
  9. Patrick SteinhardtApr 8, 2024
  10. Junio C HamanoApr 8, 2024
  11. 2/6 CodingGuidelines: quote assigned value in 'local var=$val'Junio C Hamano, Apr 6, 2024
  12. rsbecker@nexbridge.comApr 6, 2024
  13. Junio C HamanoApr 6, 2024
  14. Eric SunshineApr 6, 2024
  15. Junio C HamanoApr 6, 2024
  16. 4/6 t: local VAR="VAL" (quote command substitution)Junio C Hamano, Apr 6, 2024
  17. 5/6 t: local VAR="VAL" (quote ${magic-reference})Junio C Hamano, Apr 6, 2024
  18. 6/6 t: teach lint that RHS of 'local VAR=VAL' needs to be quotedJunio C Hamano, Apr 6, 2024
  19. Jeff KingApr 7, 2024
  20. Junio C HamanoApr 8, 2024
  21. Jeff KingApr 8, 2024
  22. 7/6 t0610: local VAR="VAL" fixJunio C Hamano, Apr 6, 2024
  23. 8/6 t1016: local VAR="VAL" fixJunio C Hamano, Apr 6, 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.