From: Petr Vorel <pvorel@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [RFC PATCH 1/1] Add automated tests for shell lib
Date: Wed, 3 Oct 2018 13:32:15 +0200 [thread overview]
Message-ID: <20181003113215.GB21139@dell5510> (raw)
In-Reply-To: <c859d712cb33f6bb151313d6664d786920e49500.1535725022.git.clanig@suse.com>
Hi Christian,
some more comments (beside these mentioned by Cyril).
Mostly just shell code style comments.
> +test2()
> +{
> + tst_res TPASS "Test $1 passed with no data ('$2')"
> +}
> +
> +tst_run
> +# output:
> +# test 1 TPASS: Test 1 passed with no data ('')
> +# test 2 TPASS: Test 2 passed with no data ('')
> +#
Note, I'd prefer not have trailing whitespace after '#' (actually anywhere).
Not only it's unnecessary it's also problematic for 'git am' applying patches.
In my case all the tests failed because of it.
Also some people setup their editors / IDE to delete trailing whitespace.
(Not pointing all whitespace here.)
> +# Summary:
> +# passed 2
> +# failed 0
> +# skipped 0
> +# warnings 0
> diff --git a/lib/newlib_tests/test_sh_newlib.sh b/lib/newlib_tests/test_sh_newlib.sh
...
> +setup()
> +{
> + path_sav=$PATH
> + PATH=""$PATH":$(dirname $0)/../../testcases/lib/"
> + color_blue='\033[1;34m'
> + color_green='\033[1;32m'
> + color_red='\033[1;31m'
> + standard_color='\033[0m'
Maybe this variables can be outside of setup (no benefit to have them in setup.
> + tmp_dir="/tmp/sh_lib_tst-$$/"
Although /tmp is everywhere, maybe using $TMPDIR would be good. See tst_test.sh
code:
if [ -z "$TMPDIR" ]; then
export TMPDIR="/tmp"
fi
TST_TMPDIR=$(mktemp -d "$TMPDIR/LTP_$TST_ID.XXXXXXXXXX")
chmod 777 "$TST_TMPDIR"
> + mkdir $tmp_dir || cleanup 1
> + sh_lib_tst_dir="$(dirname $0)/shell/"
Maybe just tstdir ?
> + tst_files="$(ls $sh_lib_tst_dir)"
> +print_help()
> +{
> + printf "#############################################################\n"
> + printf "# This script iterates over test cases for the new shell #\n"
> + printf "# library and verifies the output. #\n"
> + printf "#############################################################\n"
> + printf "\n"
> + printf "Usage:\n"
> + printf "\t$(basename $0)\n"
> + printf "\t$(basename $0) [test file 1] [test file 2] ...\n\n"
> + exit 0;
Maybe just:
cat <<EOF
Unit tests for new shell API
Usage: $(basename $0) [ TEST1 ] [ TEST2 ]
EOF
> +}
> +
> +parse_params()
> +{
> + [ -n "$1" ] && tst_files=
> + while [ -n "$1" ]; do
> + case "$1" in
> + --help) print_help;;
> + -h) print_help;;
> + -*)
> + printf "Unknown positional parameter "$1".\n"
> + cleanup 1;;
> + *) tst_files="$tst_files $1";;
> + esac
> + shift
> + done
> + return 0
Why return, when you don't use it elsewhere?
...
> +verify_output()
> +{
> + [ -e "$sh_lib_tst_dir$tst" ] || { printf "$tst not found\n" && \
> + cleanup 1 ;}
This might be more readable (nitpicking):
if [ -n "$sh_lib_tst_dir$tst" ]; then
echo "$tst not found"
cleanup 1
fi
> + # read all lines after line: `# output:`, and strip `# ` from beginning
> + sed -n -e '/^# output:/,$ p;' "$sh_lib_tst_dir$tst" | sed '1d; s/^# //'\
> + > "$tmp_dir$tst.wanted" || cleanup 1
> +
> + actual_output=$($sh_lib_tst_dir$tst)
> + # remove control signs, add newline at EOF and store in temporary file
> + actual_output=$(printf "$actual_output\n" > $tmp_dir$tst".actual") ||
> + cleanup 1
Suppose this part will be rewritten. But why do you assign empty string?
echo "$actual_output" > $tmp_dir$tst".actual || cleanup 1
...
> +
> + cmp $tmp_dir$tst".actual" $tmp_dir$tst".wanted" > /dev/null && return 0
> + return 1
This is enough (cmp returns it for you):
cmp -s "$tmp_dir$tst.actual" "$tmp_dir$tst.wanted"
> +}
...
> +run_tests()
> +{
> + pass_cnt=0
> + fail_cnt=0
> + printf "\n"
> + for tst in $tst_files; do
> + if verify_output; then
> + pass_cnt=$(expr $pass_cnt + 1)
> + printf ""$color_green"TPASS$standard_color $tst"
^
"" is empty string, why?
> + printf "\n"
You can also use {} to separate variable name:
echo "${color_green}TPASS$standard_color $tst"
echo adds new line for you (for simple usage it's portable).
It's safer to quote whole string (with variables).
> + else
> + fail_cnt=$(expr $fail_cnt + 1)
> + printf ""$color_red"TFAIL$standard_color $tst\n"
> + printf ""$color_blue"Diff:$standard_color\n"
> + diff -u "$tmp_dir$tst.actual" "$tmp_dir$tst.wanted"
> + printf "\n"
> + fi
> + done
> + printf "\nSummary:\n"
> + printf ""$color_red"Failed:$standard_color $fail_cnt\n"
> + printf ""$color_green"Passed:$standard_color $pass_cnt\n\n"
And how about adding helper functions print_red, print_green, print_blue?
Maybe not worth for this little script.
print_red()
{
printf "$red$1$reset"
}
print_red "TFAIL
echo "$tst"
> + return $fail_cnt
> +}
> +
> +setup
> +parse_params "$@"
> +run_tests
> +res=$?
> +cleanup $res
No need to have $res:
run_tests
cleanup $?
Kind regards,
Petr
next prev parent reply other threads:[~2018-10-03 11:32 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-22 19:34 [LTP] [RFC PATCH v3 1/2] tst_test.sh: Add TST_TEST_DATA and TST_TEST_DATA_IFS Petr Vorel
2018-05-22 19:34 ` [LTP] [RFC PATCH v3 2/2] lib: Add tests Petr Vorel
2018-05-24 13:46 ` Cyril Hrubis
2018-05-24 14:00 ` Petr Vorel
2018-08-28 11:18 ` [LTP] [PATCH 1/2] Make shell lib tests standalone Christian Lanig
2018-08-28 11:18 ` [LTP] [PATCH 2/2] Add wanted output to shell lib test case Christian Lanig
2018-08-29 17:24 ` [LTP] [PATCH 1/2] Make shell lib tests standalone Petr Vorel
2018-08-29 17:30 ` Petr Vorel
2018-08-31 15:24 ` [LTP] [RFC PATCH 0/1] Add automated tests for shell lib Christian Lanig
2018-08-31 15:24 ` [LTP] [RFC PATCH 1/1] " Christian Lanig
2018-10-03 9:51 ` Cyril Hrubis
2018-10-03 10:46 ` Petr Vorel
2018-10-03 11:32 ` Petr Vorel [this message]
2019-08-22 19:12 ` [LTP] [RFC PATCH v2 0/1] " Christian Lanig
2019-08-22 19:12 ` [LTP] [RFC PATCH v2 1/1] " Christian Lanig
2019-09-19 16:41 ` Petr Vorel
2019-09-30 18:27 ` Christian Lanig
2019-09-20 14:21 ` Clemens Famulla-Conrad
2019-09-19 14:26 ` [LTP] [RFC PATCH v2 0/1] " Petr Vorel
2018-08-31 11:46 ` [LTP] [PATCH 1/2] Make shell lib tests standalone Cyril Hrubis
2018-05-24 13:41 ` [LTP] [RFC PATCH v3 1/2] tst_test.sh: Add TST_TEST_DATA and TST_TEST_DATA_IFS Cyril Hrubis
2018-05-24 13:53 ` Petr Vorel
2018-05-24 14:00 ` Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181003113215.GB21139@dell5510 \
--to=pvorel@suse.cz \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.