tools: tests: fix a few issues in bash scripts
v3 cover tools: tests: fix a few issues in bash scripts
Fix a few issues with tools tests reported by Andy.
---
Changes in v3:
- Use $* in the message string in assert_fail()
- Make shellcheck happy when using find exclusively instead of find + grep
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Use double quotes around $@ to prevent globbing but allow variable expansion
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Bartosz Golaszewski (4):
tools: tests: use tabs for indentation consistently
tools: tests: use "$@" instead of $*
tools: tests: remove unneeded ';' in while loops
tools: tests: remove dependency on grep
tools/gpio-tools-test.bash | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-)
---
base-commit: 27fe10150f6d5fa78d1a1ef1e922dc8395d1154d
change-id: 20240524-fix-bash-tests-545bf26447b3
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
PATCH v3 1/4 tools: tests: use tabs for indentation consistently
From: Bartosz Golaszewski <[email protected]>
Replace all spaces used for indentation with tabs.
--- tools/gpio-tools-test.bash | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tools/gpio-tools-test.bash +7 -7
@@ -203,9 +203,9 @@ dut_run_redirect() { dut_read_redirect() { output=$(<$SHUNIT_TMPDIR/$DUT_OUTPUT) - local ORIG_IFS="$IFS" - IFS=$'\n' lines=($output) - IFS="$ORIG_IFS" + local ORIG_IFS="$IFS" + IFS=$'\n' lines=($output) + IFS="$ORIG_IFS" } dut_read() {
@@ -269,12 +269,12 @@ dut_wait() { } dut_cleanup() { - if [ -n "$DUT_PID" ] - then + if [ -n "$DUT_PID" ] + then kill -SIGTERM $DUT_PID 2> /dev/null wait $DUT_PID || false - fi - rm -f $SHUNIT_TMPDIR/$DUT_OUTPUT + fi + rm -f $SHUNIT_TMPDIR/$DUT_OUTPUT } tearDown() {
Signature · 1 line
2.43.0
PATCH v3 2/4 tools: tests: use "$@" instead of $*
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
$@ does not break up quoted arguments which is what we want in all cases in the bash test-suite. Use it instead of $*. While at it: prevent globbing with double quotes but allow variable expansion.
--- tools/gpio-tools-test.bash | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/gpio-tools-test.bash +5 -5
@@ -27,9 +27,9 @@ GPIOSIM_APP_NAME="gpio-tools-test" MIN_KERNEL_VERSION="5.17.4" MIN_SHUNIT_VERSION="2.1.8" -# Run the command in $* and fail the test if the command succeeds. +# Run the command in $@ and fail the test if the command succeeds. assert_fail() { - $* || return 0 + "$@" || return 0 fail " '$*': command did not fail as expected" }
@@ -71,7 +71,7 @@ gpiosim_chip() { mkdir -p $BANKPATH - for ARG in $* + for ARG in "$@" do local KEY=$(echo $ARG | cut -d"=" -f1) local VAL=$(echo $ARG | cut -d"=" -f2)
@@ -253,7 +253,7 @@ dut_regex_match() { } dut_write() { - echo $* >&${COPROC[1]} + echo "$@" >&${COPROC[1]} } dut_kill() {
@@ -283,7 +283,7 @@ tearDown() { } request_release_line() { - $SOURCE_DIR/gpioget -c $* >/dev/null + $SOURCE_DIR/gpioget -c "$@" >/dev/null } #
Signature · 1 line
2.43.0
PATCH v3 3/4 tools: tests: remove unneeded ';' in while loops
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We're already breaking the line between while and do so there's no need for the ';' character.
--- tools/gpio-tools-test.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/gpio-tools-test.bash +2 -2
@@ -211,7 +211,7 @@ dut_read_redirect() { dut_read() { local LINE lines=() - while read -t 0.2 -u ${COPROC[0]} LINE; + while read -t 0.2 -u ${COPROC[0]} LINE do if [ -n "$DUT_FIRST_CHAR" ] then
@@ -232,7 +232,7 @@ dut_flush() { lines=() output= unset DUT_FIRST_CHAR - while read -t 0 -u ${COPROC[0]} JUNK; + while read -t 0 -u ${COPROC[0]} JUNK do read -t 0.1 -u ${COPROC[0]} JUNK || true done
Signature · 1 line
2.43.0
PATCH v3 4/4 tools: tests: remove dependency on grep
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We only use grep in one place where we don't really need it as we can use find directly.
--- tools/gpio-tools-test.bash | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
tools/gpio-tools-test.bash +4 -14
@@ -158,19 +158,10 @@ gpiosim_cleanup() { local BANKPATH=$DEVPATH/bank0 echo 0 > $DEVPATH/live - - ls $BANKPATH/line* > /dev/null 2>&1 - if [ "$?" = "0" ] - then - for LINE in $(find $BANKPATH/ | grep -E "line[0-9]+$") - do - test -e $LINE/hog && rmdir $LINE/hog - rmdir $LINE - done - fi - - rmdir $BANKPATH - rmdir $DEVPATH + find "$DEVPATH" -type d -name hog -exec rmdir '{}' '+' + find "$DEVPATH" -type d -name "line*" -exec rmdir '{}' '+' + find "$DEVPATH" -type d -name "bank*" -exec rmdir '{}' '+' + rmdir "$DEVPATH" done gpiosim_chip_symlink_cleanup
@@ -3078,7 +3069,6 @@ check_prog() { check_prog shunit2 check_prog modprobe check_prog timeout -check_prog grep # Check if we're running a kernel at the required version or later check_kernel $MIN_KERNEL_VERSION
Signature · 1 line
2.43.0
Quoted 3 lines · Bartosz Golaszewski
Fix a few issues with tools tests reported by Andy.
Signed-off-by Bartosz Golaszewski <[email protected]>
Looks good to me.
Quoted 27 lines
---
Changes in v3:
- Use $* in the message string inassert_fail()
- Make shellcheck happy when using find exclusively instead of find + grep
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Use double quotes around $@ to prevent globbing but allow variable expansion
- Link to v1: https://lore.kernel.org/r/[email protected]---
Bartosz Golaszewski (4):
tools: tests: use tabs for indentation consistently
tools: tests: use "$@" instead of $*
tools: tests: remove unneeded ';' in while loops
tools: tests: remove dependency on greptools/gpio-tools-test.bash | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-)---
base-commit:27fe10150f6d5fa78d1a1ef1e922dc8395d1154d
change-id: 20240524-fix-bash-tests-545bf26447b3Best regards,
--
Bartosz Golaszewski <[email protected]>
Andy Shevchenko
Reviewed-byQuoted 8 lines · Kent Gibson
Quoted 3 lines · Bartosz Golaszewski
Fix a few issues with tools tests reported by Andy.
Signed-off-by Bartosz Golaszewski <[email protected]>Looks good to me.
Reviewed-by Kent Gibson <[email protected]>
And okay to me,
Signature · 2 lines
With Best Regards, Andy Shevchenko
Bartosz Golaszewski
AppliedFrom: Bartosz Golaszewski <[email protected]>
Quoted 3 lines · Bartosz Golaszewski
Fix a few issues with tools tests reported by Andy.
Applied, thanks!
[1/4] tools: tests: use tabs for indentation consistently
commit: 649b2baedd9042bbffa7f2e2847375b23c5adc2e
[2/4] tools: tests: use "$@" instead of $*
commit: 4fae395c7c169e09441589ee64797cb62343da7d
[3/4] tools: tests: remove unneeded ';' in while loops
commit: d75d591af73e2ebe52137221f4d87ce23f6c22d8
[4/4] tools: tests: remove dependency on grep
commit: b569b91ce1e1749a4e4832e636721d3c05712593Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>