Re: [PATCH 1/3] selftests: use local variables consistently in runner.sh

From: Jori Koolstra

Date: Fri Nov 20 2026 - 06:15:24 EST



> Op 18-11-2026 11:13 EST schreef Shuah Khan <skhan@xxxxxxxxxxxxxxxxxxx>:
>
>
> On 11/18/26 06:40, Jori Koolstra (CIQ) wrote:
> > run_one() and run_many() assign their working variables without
> > declaring them local, so they leak into the global namespace of
> > whichever shell sources runner.sh, i.e. run_kselftest.sh or the
> > RUN_TESTS recipe in lib.mk.
> >
>
> Hmm. so if a timeout value of say 60 seconds comes from a test
> settings file, does it persist? Can you see that if you run a
> few tests using TARGETS variable?
>

No, because it is doing

export kselftest_timeout="$kselftest_default_timeout"

at the start of run_once(). I was just trying to get it to be a bit more
consistent and less bug-prone. We could also add readonly, which is in
POSIX. But that seemed too much.

> > Declare these variables local so that local is used consistently
> > throughout the file and the sourcing shell's namespace is not polluted.
> > This does not add a new requirement on the shell: local is already used
> > in run_many(), in_netns() and run_in_netns().
>
> Makes sense.
>
> >
> > Also, obey the convention to use upper case names for variables that are
> > constant once assigned.
>
> Can you elaborate on this convention?
>

It seems to be what was used for runner.sh, local variables set once are in
uppercase.

> I don't want to see these changes combined. We don't combine
> cleanup and convention changes with real changes.
>

I'll separate it out.

> >
> > Signed-off-by: Jori Koolstra (CIQ) <jkoolstra@xxxxxxxxx>
> > ---
> > tools/testing/selftests/kselftest/runner.sh | 29 ++++++++++++++++++-----------
> > 1 file changed, 18 insertions(+), 11 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kselftest/runner.sh b/tools/testing/selftests/kselftest/runner.sh
> > index 45fb8ec38493..9b1dad5441ea 100644
> > --- a/tools/testing/selftests/kselftest/runner.sh
> > +++ b/tools/testing/selftests/kselftest/runner.sh
> > @@ -58,13 +58,18 @@ tap_timeout()
> >
> > run_one()
> > {
> > + local DIR TEST BASENAME_TEST BASENAME_SANITIZED SETTINGS TEST_HDR_MSG
> > + local line field value interpreter stdbuf cmd rc
> > +
> > DIR="$1"
> > TEST="$2"
> >
> > BASENAME_TEST=$(basename $TEST)
> >
> > # Reset any "settings"-file variables.
> > - export kselftest_timeout="$kselftest_default_timeout"
> > + local kselftest_timeout="$kselftest_default_timeout"
> > + # Make kselftest_timeout variables available to tests
> > + export kselftest_timeout
> >
> > # Safe default if tr not available
> > kselftest_cmd_args_ref="KSELFTEST_ARGS"
> > @@ -91,8 +96,8 @@ run_one()
> > fi
> >
> > # Load per-test-directory kselftest "settings" file.
> > - settings="$BASE_DIR/$DIR/settings"
> > - if [ -r "$settings" ] ; then
> > + SETTINGS="$BASE_DIR/$DIR/settings"
> > + if [ -r "$SETTINGS" ] ; then
> > while read line ; do
> > # Skip comments.
> > if echo "$line" | grep -q '^#'; then
> > @@ -100,8 +105,8 @@ run_one()
> > fi
> > field=$(echo "$line" | cut -d= -f1)
> > value=$(echo "$line" | cut -d= -f2-)
> > - eval "kselftest_$field"="$value"
> > - done < "$settings"
> > + eval "local kselftest_$field"="$value"
> > + done < "$SETTINGS"
> > fi
> >
> > # Command line timeout overrides the settings file
> > @@ -164,8 +169,8 @@ run_one()
> >
> > in_netns()
> > {
> > - local name=$1
> > - ip netns exec $name bash <<-EOF
> > + local NAME=$1
> > + ip netns exec $NAME bash <<-EOF
> > BASE_DIR=$BASE_DIR
> > source $BASE_DIR/kselftest/runner.sh
> > logfile=$logfile
> > @@ -175,9 +180,9 @@ in_netns()
> >
> > run_in_netns()
> > {
> > - local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
> > - local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
> > - local rc
> > + local tmplog netns rc
> > + tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
> > + netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
> >
> > ip netns add $netns
> > if [ $? -ne 0 ]; then
> > @@ -198,8 +203,10 @@ run_in_netns()
> >
> > run_many()
> > {
> > + local DIR TEST BASENAME_TEST
> > + local pids pid rc
> > +
> > DIR="${PWD#${BASE_DIR}/}"
> > - local rc
> > pids=
> >
> > for TEST in "$@"; do
> >
>
> thanks,
> -- Shuah