[PATCH 1/2] test: provide machinery to make and use test_shims

Tomi Ollila tomi.ollila at iki.fi
Tue Jun 25 14:05:09 PDT 2019


On Sun, May 26 2019, David Bremner wrote:

> These can be used e.g. to override return values for functions, in
> place of the existing scripting of gdb.
> ---
>  test/test-lib.sh | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/test/test-lib.sh b/test/test-lib.sh
> index ff18fae6..a423b7f4 100644
> --- a/test/test-lib.sh
> +++ b/test/test-lib.sh
> @@ -95,6 +95,8 @@ TEST_EMACSCLIENT=${TEST_EMACSCLIENT:-emacsclient}
>  TEST_GDB=${TEST_GDB:-gdb}
>  TEST_CC=${TEST_CC:-cc}
>  TEST_CFLAGS=${TEST_CFLAGS:-"-g -O0"}
> +TEST_SHIM_CFLAGS=${TEST_SHIM_CFLAGS:-"-fpic -shared"}
> +TEST_SHIM_LDFLAGS=${TEST_SHIM_LDFLAGS:-"-ldl"}
>  
>  # Protect ourselves from common misconfiguration to export
>  # CDPATH into the environment
> @@ -1032,6 +1034,20 @@ test_C () {
>      notmuch_dir_sanitize OUTPUT.stdout OUTPUT.stderr > OUTPUT
>  }
>  
> +make_shim () {
> +    base_name="$1"
> +    test_file="${base_name}.c"
> +    shim_file="${base_name}.so"
> +    cat > ${test_file}
> +    ${TEST_CC} ${TEST_CFLAGS} ${TEST_SHIM_CFLAGS} -I${NOTMUCH_SRCDIR}/test -I${NOTMUCH_SRCDIR}/lib -o ${shim_file} ${test_file} ${TEST_SHIM_LDFLAGS}
> +}
> +
> +notmuch_with_shim () {
> +    base_name="$1"
> +    shift
> +    shim_file="${base_name}.so"
> +    LD_PRELOAD=./${shim_file} notmuch-shared "$@"

LD_PRELOAD=./${shim_file}${LD_PRELOAD:+:$LD_PRELOAD} notmuch-shared "$@"

So that if LD_PRELOAD is already set and is non-empty then ':' and its
old value is appended to new value of LD_PRELOAD before this
notmuch-shared invocation.

Also I'm "insisting" that your patch 2/2 is updated based on
id:8736kipe9f.fsf at fifthhorseman.net -- dkg had good opening how to 
improve robustness in `eval` executions, but since there are quite
a few similar problems in code (i.e. and there are quite a few ways
to improve it) IMO maintaining status quo we can this feature improved
incrementally and choose how to fix *all* related problems...

> +}


--- above comment for this message -- below more on the test robustness
--- part -- I was not supposed to write so long (and should have gone
--- sleep an hour ago -- anyway if you read it all shout your opinion.


One option, which is relatively easy to start using (when needed) while
writing code is to use ${parameter at Q} transformation; 

from bash (4.4+) documentation:

    Q      The  expansion is a string that is the value of parameter
           quoted in a format that can be reused as input.

( that transforms e.g.  foo'bar to 'foo'\''bar' )

Like hinted above, this would require that minimum bash version supported
is 4.4. I did some linux distribution comparison and took gmime 3 along
with it.

Debian 9 (Stretch) has bash 4.4 -- and gmime 3.x

Debian 8 (Jessie) as bash 4.3 -- but (only) gmime 2.6 (and if I used
packages.debian.org correctly there is no backports either...)

Ubuntu 18.04 (bionic) has bash 4.4 -- and gmime 3.x

likewise, Ubuntu 16.04 (xenial) has neither (not even in backports)

SO, in above cases, to compile current notmuch, gmime 3 has to be compiled
separately. Then such user could also build newer bash... (and if there are
3rd party package repositories those can provide both...)

I'd guess the situation is pretty much the same with other long-term
supported distributions (centos 7 has bash 4.2, gmime 2.6 -- rhel/centos8
probably gmime30 3.2 and bash 4.4)

Latest short-term, like Fedora -- and rolling release distributions, if
those provide gmime 3.x those most probably have at least bash 4.4, if
not 5.0, provided).

I personally would like to use this feature; it is easy to add into scripts
and pretty lean learning curve. I tried to do the same with bash syntax
${param//\'/\'\\\'\'}, but that looks more complicated -- and even more 
complicated to get it working right on bash 4.1 where the feature is a bit
buggy when included inside double quotes ;(

The downside could be that we get less testing due to some users not wanting
to build newer bash (have to test whether it is hard so cannot say whether
the time is right...


BTW: tested ${var:q} on decade-old zsh (4.3.12 -- 2009-06-01), does the
same. According to zsh changes file perhaps even version from 2001-06-01
has this feature.... And from https://mywiki.wooledge.org/BashFAQ/061
(which seems pretty good page to dig for features):

${var at spec} / 4.4 (2016) / mksh (2012) for the syntax, zsh (1990s) for the feature

the feature could have been there even when I start using that shell ;D


Tomi


More information about the notmuch mailing list