Skip to content

Commit

Permalink
tst_test.sh: Add support for positional paramters
Browse files Browse the repository at this point in the history
Add support for passing positional paramters to the test. Many of shell
testcases use these and converting them to named parameters will only
complicate things for no good reason.

So now we can specify number of additional positional paramters in the
TST_POS_ARGS. These parameters will be then available in the script as
$1, $2, ..., $n.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
  • Loading branch information
metan-ucw committed Oct 13, 2016
1 parent fe05f14 commit 3860c62
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 36 deletions.
38 changes: 38 additions & 0 deletions doc/test-writing-guidelines.txt
Expand Up @@ -1499,6 +1499,44 @@ the options implemented in the library is appended when usage is printed.
Lastly the fucntion '$PARSE_ARGS' is called with the option name in '$1' and,
if option has argument, its value in '$2'.

[source,sh]
-------------------------------------------------------------------------------
#!/bin/sh
#
# Optional test positional paramters
#

TST_ID="example04"
TST_POS_ARGS=3
TST_USAGE=usage
TST_TESTFUNC=do_test

. tst_test.sh

usage()
{
cat << EOF
usage: $0 [min] [max] [size]

EOF
}

min="$1"
max="$2"
size="$3"

do_test()
{
...
}

tst_run
-------------------------------------------------------------------------------

You can also request a number of positional parameters by setting the
'$TST_POS_ARGS' variable. If you do, these will be available as they were
passed directly to the script in '$1', '$2', ..., '$n'.

2.3.4 Usefull library functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
102 changes: 66 additions & 36 deletions testcases/lib/tst_test.sh
Expand Up @@ -238,39 +238,13 @@ tst_is_int()
return $?
}

if [ -z "$TST_ID" ]; then
tst_brk TBROK "TST_ID is not defined"
fi
export TST_ID="$TST_ID"

if [ -z "$TST_TESTFUNC" ]; then
tst_brk TBROK "TST_TESTFUNC is not defined"
fi

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

if [ "$TST_CNT" -le 0 ]; then
tst_brk TBROK "TST_CNT must be > 0"
fi
fi

if [ -z "$LTPROOT" ]; then
export LTPROOT="$PWD"
export LTP_DATAROOT="$LTPROOT/datafiles"
else
export LTP_DATAROOT="$LTPROOT/testcases/data/$TCID"
fi

TST_ARGS="$@"
shift $#

tst_usage()
{
if [ -n "$TST_USAGE" ]; then
$TST_USAGE
else
echo "usage: $0"
echo "OPTIONS"
fi

echo "-h Prints this help"
Expand Down Expand Up @@ -299,14 +273,18 @@ tst_run()
for tst_i in $(grep TST_ "$TST_TEST_PATH" | sed 's/.*TST_//; s/[="} \t].*//'); do
case "$tst_i" in
SETUP|CLEANUP|TESTFUNC|ID|CNT);;
OPTS|USAGE|PARSE_ARGS);;
OPTS|USAGE|PARSE_ARGS|POS_ARGS);;
NEEDS_ROOT|NEEDS_TMPDIR|NEEDS_DEVICE|DEVICE);;
NEEDS_CMDS|NEEDS_MODULE|MODPATH);;
*) tst_res TWARN "Reserved variable TST_$tst_i used!";;
esac
done
fi

local name

OPTIND=1

while getopts "hi:$TST_OPTS" name $TST_ARGS; do
case $name in
'h') tst_usage; exit 0;;
Expand All @@ -324,12 +302,6 @@ tst_run()
tst_brk TBROK "Number of iterations (-i) must be > 0"
fi

shift $(($OPTIND - 1))

if [ $# -gt 0 ]; then
tst_brk TBROK "Invalid arguments '$@'"
fi

if [ "$TST_NEEDS_ROOT" == 1 ]; then
if [ "$(id -ru)" != 0 ]; then
tst_brk TCONF "Must be super/root for this test!"
Expand Down Expand Up @@ -444,3 +416,61 @@ if TST_TEST_PATH=$(which $0) 2>/dev/null; then
tst_brk TBROK "Test $0 must call tst_run!"
fi
fi

if [ -z "$TST_ID" ]; then
tst_brk TBROK "TST_ID is not defined"
fi
export TST_ID="$TST_ID"

if [ -z "$TST_TESTFUNC" ]; then
tst_brk TBROK "TST_TESTFUNC is not defined"
fi

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

if [ "$TST_CNT" -le 0 ]; then
tst_brk TBROK "TST_CNT must be > 0"
fi
fi

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

if [ "$TST_POS_ARGS" -le 0 ]; then
tst_brk TBROK "TST_POS_ARGS must be > 0"
fi
fi

if [ -z "$LTPROOT" ]; then
export LTPROOT="$PWD"
export LTP_DATAROOT="$LTPROOT/datafiles"
else
export LTP_DATAROOT="$LTPROOT/testcases/data/$TCID"
fi

TST_ARGS="$@"

while getopts ":hi:$TST_OPTS" tst_name; do
case $tst_name in
'h') TST_PRINT_HELP=1;;
*);;
esac
done

shift $((OPTIND - 1))

if [ -n "$TST_POS_ARGS" ]; then
if [ -z "$TST_PRINT_HELP" -a $# -ne "$TST_POS_ARGS" ]; then
tst_brk TBROK "Invalid number of positional paramters:"\
"have ($@) $#, expected ${TST_POS_ARGS}"
fi
else
if [ -z "$TST_PRINT_HELP" -a $# -ne 0 ]; then
tst_brk TBROK "Unexpected positional arguments '$@'"
fi
fi

0 comments on commit 3860c62

Please sign in to comment.