Re: [PATCH v3 1/5] test: use bash specific test for feature tests

Subject: Re: [PATCH v3 1/5] test: use bash specific test for feature tests

Date: Thu, 13 Apr 2023 22:24:56 +0300

To: David Bremner, notmuch@notmuchmail.org

Cc:

From: Tomi Ollila


On Sun, Apr 09 2023, David Bremner wrote:

> It is desirable to have the tests consider these variables being
> undefined as equivalent to the feature not being present, and in
> particular for the tests not to generate errors.
>
> We know the test suite is tied to bash anyway, so this is a simple
> fix.
> ---
>  test/T060-count.sh               | 2 +-
>  test/T081-sexpr-search.sh        | 2 +-
>  test/T150-tagging.sh             | 2 +-
>  test/T160-json.sh                | 2 +-
>  test/T220-reply.sh               | 2 +-
>  test/T240-dump-restore.sh        | 2 +-
>  test/T391-python-cffi.sh         | 3 +--
>  test/T392-python-cffi-notmuch.sh | 2 +-
>  test/T520-show.sh                | 2 +-
>  test/T570-revision-tracking.sh   | 2 +-
>  test/T700-reindex.sh             | 2 +-
>  test/T800-asan.sh                | 2 +-
>  test/T810-tsan.sh                | 2 +-
>  test/T850-git.sh                 | 2 +-
>  14 files changed, 14 insertions(+), 15 deletions(-)
>
> diff --git a/test/T060-count.sh b/test/T060-count.sh
> index 48146706..4499b4cb 100755
> --- a/test/T060-count.sh
> +++ b/test/T060-count.sh
> @@ -157,7 +157,7 @@ print("4: {} messages".format(query.count_messages()))
>  EOF
>  test_expect_equal_file EXPECTED OUTPUT
>  
> -if [ $NOTMUCH_HAVE_SFSEXP -eq 1 ]; then
> +if [[ $NOTMUCH_HAVE_SFSEXP -eq 1 ]]; then

Hmm, I send reply to Felipe's email, before seeing this, so this is
followup ... which, at the end, reaches the same "conclusion" :D

anyway, while this is good progress the [[ $v -eq 1 ]] is still somewhat
fragile (using -eu shows some interesting behaviors):

the easy try: 

  $ bash -eu -c ' [[ $AAA -ne 1 ]] && echo 1 || echo 0'
  bash: line 1: AAA: unbound variable

weirder...:

  $ bash -eu -c 'a="x"; [[ $a -eq 2 ]]' 
  bash: line 1: x: unbound variable

wat, as $a is not numeric, it tries to re-eval $x ???

  $ bash -eu -c 'x=3; a="x"; [[ $a -eq 2 ]] && echo 1 || echo 0'
  0
  $ bash -eu -c 'x=2; a="x"; [[ $a -eq 2 ]] && echo 1 || echo 0' 
  1

for reference

  $ bash -eu -c 'a="2"; [[ $a -eq 2 ]] && echo 1 || echo 0'
  1


and again, w/ = instead of -eq

  $ bash -eu -c 'a="x"; [[ $a = 2 ]] && echo 1 || echo 0'
  0

and -- modified the first AAA case above:

  $ bash -eu -c ' AAA=b; [[ ${AAA-} = 1 ]] && echo 1 || echo 0'
  0

hmm, when using [[ ]] I recall == and != were to used... uh, see 
bash manual about the pattern matching and such... tried also
with (( ... )) (documenter before [[ ]] in bash manual page 
but now it fails even heavier..:

  $ bash -eu -c 'XXX="a"; (( ${XXX-} == 2 )) && echo 1 || echo 0'
  bash: line 1: a: unbound variable

  $ bash -eu -c 'a=2; XXX="a"; (( ${XXX-} == 2 )) && echo 1 || echo 0' 
  1

I would just use [ "${VAR-}" = 1 ] in shell script code (or barely
[[ ${VAR-0} == 1 ]] if desired not to have ""s around variables...but...)

... using [ "${VAR-}" = 1 ] is also good in a sense that someone may
use it as an example to write shell scripts with #!/bin/sh as a
hashbang -- and it works fine on Fedora since there /bin/sh is bash,
but would fail on Debian since there /bin/sh is dash.

HTH ;D

Tomi
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: