Skip to content

Commit

Permalink
[WIP] tst_test.sh: Cleanup getopts usage
Browse files Browse the repository at this point in the history
tst_test.sh used getopts twice, but the real parsing was on tst_run().
For tst_net.sh (or other libs) will be useful if there is only single
parsing at the end of tst_test.sh, i.e. before entering tst_run().

This requires to move loading tst_test.sh to the end of the script
(before calling tst_run()).

Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
  • Loading branch information
pevik committed Mar 22, 2022
1 parent 5b9db1a commit 9d77e5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion testcases/commands/df/df01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ TST_USAGE=usage
TST_PARSE_ARGS=parse_args
TST_NEEDS_ROOT=1
TST_FORMAT_DEVICE=1
. tst_test.sh

usage()
{
Expand Down Expand Up @@ -192,4 +191,5 @@ test12()
fi
}

. tst_test.sh
tst_run
27 changes: 10 additions & 17 deletions testcases/lib/tst_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -642,17 +642,6 @@ tst_run()
done
fi

OPTIND=1

while getopts ":hi:$TST_OPTS" _tst_name $TST_ARGS; do
case $_tst_name in
'h') tst_usage; exit 0;;
'i') TST_ITERATIONS=$OPTARG;;
'?') tst_usage; exit 2;;
*) $TST_PARSE_ARGS "$_tst_name" "$OPTARG";;
esac
done

if ! tst_is_int "$TST_ITERATIONS"; then
tst_brk TBROK "Expected number (-i) not '$TST_ITERATIONS'"
fi
Expand Down Expand Up @@ -818,22 +807,26 @@ if [ -z "$TST_NO_DEFAULT_RUN" ]; then

TST_ARGS="$@"

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

while getopts ":hi:$TST_OPTS" _tst_name $TST_ARGS; do
case $_tst_name in
'h') tst_usage; exit 0;;
'i') TST_ITERATIONS=$OPTARG;;
'?') tst_usage; exit 2;;
*) $TST_PARSE_ARGS "$_tst_name" "$OPTARG";;
esac
done

shift $((OPTIND - 1))

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

0 comments on commit 9d77e5d

Please sign in to comment.