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

Re: [PATCH v3 12/15] ref-filter: align: introduce long-form syntax

From
Karthik Nayak <karthik.188@gmail.com>
Date
Jan 26, 2016, 09:40 UTC
Message-ID
<CAOLa=ZSnDe=q8SpicYW6gNKmNts3K=FpCFq6q8t_pUv+8AZhnQ@mail.gmail.com>
In-Reply-To
<xmqqa8ntdxzs.fsf@gitster.mtv.corp.google.com>
On Tue, Jan 26, 2016 at 5:15 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 66 quoted lines
> Eric Sunshine <sunshine@sunshineco.com> writes:
>
>>> +               git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
>>
>> This is not wrong, per se, but referencing $option inside the
>> non-interpolating single-quote context of the test body makes it a bit
>> harder to understand than it need be. As it is, at the time that the
>> test body actually gets executed, $option still evaluates to the
>> desired permutation value so it works.
>
> I think I said this in one of my recent reviews to somebody else,
> but this needs repeating.
>
> My position on this has been and still is a complete opposite.  It
> is a designed behaviour that variable references made inside test
> body that is quoted with sq pair can see the values of the variables
> that are defined outside test_expect_success, and you can freely use
> the global $TRASH_DIRECTORY, $_x40, etc. inside the test body that
> is quoted with single quote.  The $option here is no different.
>
> In fact, it is more desirable to avoid double-quoting the test body.
> The problem with using double-quote around the test body and
> formulating the executable shell script using variable interpolation
> before test_expect_success even runs is that you would be tempted to
> do something silly like this:
>
>     HEAD_SHA1=$(git rev-parse --verify HEAD)
>
>     test_expect_success "check something" "
>         HEAD_SHA1=$(git rev-parse --verify HEAD) &&
>         ... do other things that should not move the HEAD ... &&
>         test '$(git rev-parse HEAD)' = '$HEAD_SHA1' &&
>         test '$SOME_VAR' = '$OTHER_VAR' &&
>     "
>
> It is not just error prone (if variable reference had a shell
> metacharacter in it, e.g. a single-quote in OTHER_VAR's value, you'd
> have a syntax error in the test body), but because two invocations
> of rev-parse in the above are both made before test_expect_success
> runs, and would yield the same value.  It is not even testing the
> right thing.  If you broke rev-parse, the invocation will not fail
> inside test_expect_success but outside when the arguments to that
> helper is being prepared.
>
> So please do not write something like this:
>
>>     test_align_permutations () {
>>         while ...
>>         do
>>             test_expect_success "align:$option" "
>>                 git for-each-ref --format='...$option...' >actual &&
>>                 ...
>>             "
>>         done
>>     }
>
> but make it more like this:
>
>         for option in ...
>         do
>                 test_expect_success "align:$option" '
>                         git for-each-ref --format="...$option..." &&
>                         ...
>                 '
>         done
>
Thanks for this descriptive explanation, I shall use the suggested format.
-- 
Regards,
Karthik Nayak
Previous: Junio C HamanoNext: Karthik Nayak
Message 30 of 52 in “ref-filter: use parsing functions”
  1. 00/15 ref-filter: use parsing functionsKarthik Nayak, Jan 5, 2016
  2. 01/15 strbuf: introduce strbuf_split_str_omit_term()Karthik Nayak, Jan 5, 2016
  3. Junio C HamanoJan 5, 2016
  4. Karthik NayakJan 6, 2016
  5. Eric SunshineJan 21, 2016
  6. Karthik NayakJan 25, 2016
  7. 02/15 ref-filter: use strbuf_split_str_omit_term()Karthik Nayak, Jan 5, 2016
  8. 03/15 ref-filter: bump 'used_atom' and related code to the topKarthik Nayak, Jan 5, 2016
  9. 04/15 ref-filter: introduce struct used_atomKarthik Nayak, Jan 5, 2016
  10. Eric SunshineJan 21, 2016
  11. Karthik NayakJan 25, 2016
  12. 05/15 ref-filter: introduce parsing functions for each valid atomKarthik Nayak, Jan 5, 2016
  13. 06/15 ref-fitler: bump match_atom() name to the topKarthik Nayak, Jan 5, 2016
  14. 07/15 ref-filter: skip deref specifier in match_atom_name()Karthik Nayak, Jan 5, 2016
  15. 08/15 ref-filter: introduce color_atom_parser()Karthik Nayak, Jan 5, 2016
  16. Junio C HamanoJan 5, 2016
  17. Karthik NayakJan 6, 2016
  18. Junio C HamanoJan 5, 2016
  19. Karthik NayakJan 6, 2016
  20. 09/15 ref-filter: introduce align_atom_parser()Karthik Nayak, Jan 5, 2016
  21. 10/15 ref-filter: introduce parse_align_position()Karthik Nayak, Jan 5, 2016
  22. Eric SunshineJan 25, 2016
  23. Karthik NayakJan 26, 2016
  24. 11/15 ref-filter: convert variable 'width' to an unsigned intKarthik Nayak, Jan 5, 2016
  25. Junio C HamanoJan 5, 2016
  26. Karthik NayakJan 6, 2016
  27. 12/15 ref-filter: align: introduce long-form syntaxKarthik Nayak, Jan 5, 2016
  28. Eric SunshineJan 25, 2016
  29. Junio C HamanoJan 25, 2016
  30. Karthik NayakJan 26, 2016
  31. Karthik NayakJan 26, 2016
  32. Christian CouderJan 26, 2016
  33. Karthik NayakJan 26, 2016
  34. 13/15 ref-filter: introduce remote_ref_atom_parser()Karthik Nayak, Jan 5, 2016
  35. Eric SunshineJan 26, 2016
  36. Karthik NayakJan 26, 2016
  37. 14/15 ref-filter: introduce contents_atom_parser()Karthik Nayak, Jan 5, 2016
  38. Junio C HamanoJan 5, 2016
  39. Karthik NayakJan 6, 2016
  40. Junio C HamanoJan 7, 2016
  41. Karthik NayakJan 7, 2016
  42. 15/15 ref-filter: introduce objectname_atom_parser()Karthik Nayak, Jan 5, 2016
  43. Junio C HamanoJan 5, 2016
  44. Karthik NayakJan 6, 2016
  45. Eric SunshineJan 6, 2016
  46. Karthik NayakJan 7, 2016
  47. Junio C HamanoJan 7, 2016
  48. Karthik NayakJan 7, 2016
  49. Eric SunshineJan 7, 2016
  50. Karthik NayakJan 7, 2016
  51. Junio C HamanoJan 7, 2016
  52. Karthik NayakJan 9, 2016

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.