[LTP] [RFC PATCH 1/2] lib/tst_test.sh: TST_TESTFUNC_DATA and TST_TESTFUNC_DATA_IFS

Cyril Hrubis chrubis@suse.cz
Wed May 16 14:50:09 CEST 2018


Hi!
> This is specific only for shell.
> 
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
> If we accept it, it's a question if pass iteration number as well
> (and if yes, maybe it should be in $1 and data in $2).
> 
> Kind regards,
> Petr
> ---
>  doc/test-writing-guidelines.txt | 23 +++++++++++++++++++++++
>  testcases/lib/tst_test.sh       | 20 ++++++++++++++++++--
>  2 files changed, 41 insertions(+), 2 deletions(-)
> 
> diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
> index 8e405a034..2184f6365 100644
> --- a/doc/test-writing-guidelines.txt
> +++ b/doc/test-writing-guidelines.txt
> @@ -1485,6 +1485,29 @@ Otherwise, if '$TST_CNT' is set but there is no '$\{TST_TESTFUNC\}1', etc.,
>  the '$TST_TESTFUNC' is executed '$TST_CNT' times and the test number is passed
>  to it in the '$1'.
>  
> +[source,sh]
> +-------------------------------------------------------------------------------
> +#!/bin/sh
> +#
> +# Example test with tests using TST_TESTFUNC_DATA
> +#
> +
> +TST_TESTFUNC=do_test
> +TST_TESTFUNC_DATA="foo bar"

Why not just TST_TEST_DATA and TST_TEST_DATA_IFS?

> +. tst_test.sh
> +
> +do_test()
> +{
> +	1) tst_res TPASS "Test passed with data '$1'";;
> +}
> +
> +tst_run
> +-------------------------------------------------------------------------------
> +
> +It's possible to pass data for function with '$TST_TESTFUNC_DATA'. Optional
> +'$TST_TESTFUNC_DATA_IFS' variable is supported to set '$IFS' for splitting.
> +'$TST_TESTFUNC_DATA' cannot be mixed with '$TST_CNT'.
> +
>  2.3.2 Library variables
>  ^^^^^^^^^^^^^^^^^^^^^^^
>  
> diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
> index 8d49d34b6..31bf51e10 100644
> --- a/testcases/lib/tst_test.sh
> +++ b/testcases/lib/tst_test.sh
> @@ -246,7 +246,7 @@ tst_rescmp()
>  
>  tst_run()
>  {
> -	local tst_i
> +	local tst_i ifs

The variables here has to be still prefixed with tst_, even though they
are local these are still defined in the $TST_TESTFUNC.

>  	if [ -n "$TST_TEST_PATH" ]; then
>  		for tst_i in $(grep TST_ "$TST_TEST_PATH" | sed 's/.*TST_//; s/[="} \t\/:`].*//'); do
> @@ -255,7 +255,7 @@ tst_run()
>  			OPTS|USAGE|PARSE_ARGS|POS_ARGS);;
>  			NEEDS_ROOT|NEEDS_TMPDIR|NEEDS_DEVICE|DEVICE);;
>  			NEEDS_CMDS|NEEDS_MODULE|MODPATH|DATAROOT);;
> -			IPV6);;
> +			IPV6|TESTFUNC_DATA|TESTFUNC_DATA_IFS);;
>  			*) tst_res TWARN "Reserved variable TST_$tst_i used!";;
>  			esac
>  		done
> @@ -364,6 +364,18 @@ tst_run()
>  					TST_COUNT=$((TST_COUNT+1))
>  				done
>  			fi
> +		elif [ -n "$TST_TESTFUNC_DATA" ]; then
> +			if [ -n "$TST_TESTFUNC_DATA_IFS" ]; then
> +				ifs="$IFS"
> +				IFS="$TST_TESTFUNC_DATA_IFS"
> +			fi
> +			for tst_i in $TST_TESTFUNC_DATA; do
> +				[ -n "$ifs" ] && IFS="$ifs"
> +				local res=$(tst_resstr)
> +				$TST_TESTFUNC $tst_i
> +				tst_rescmp "$res"
> +				TST_COUNT=$((TST_COUNT+1))
> +			done

I do not like redefing IFS, it's really ugly.

What about something as:

DATA="foo:bar:d dd"
DELIM=":"
i=1
while true; do
        param="$(echo "$DATA" | cut -d$DELIM -f$i)"
        [ -z "$param" ] && break;
        echo "$param"
        i=$((i+1))
done

>  		else
>  			local res=$(tst_resstr)
>  			$TST_TESTFUNC
> @@ -400,6 +412,10 @@ if [ -z "$TST_NO_DEFAULT_RUN" ]; then
>  		tst_brk TBROK "TST_TESTFUNC is not defined"
>  	fi
>  
> +	if [ -n "$TST_CNT" -a -n "$TST_TESTFUNC_DATA" ]; then
> +		tst_brk TBROK "TST_CNT cannot be mixed with TST_TESTFUNC_DATA"
> +	fi

I'm not sure about this, why cannot each of the functions take
TST_TESTFUNC_DATA parameters?

I.e. we may have two test functions that take exaclty the same array of
parameters, setting TST_CNT=2 and TST_TESTFUNC_DATA may make sense...

>  	if [ -n "$TST_CNT" ]; then
>  		if ! tst_is_int "$TST_CNT"; then
>  			tst_brk TBROK "TST_CNT must be integer"

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list