tests/utils/utils.sh: use a func. to set `BT_TESTS_{SRC,BUILD}DIR`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 10 Nov 2023 03:56:05 +0000 (22:56 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 7 Feb 2024 21:25:56 +0000 (16:25 -0500)
By using a function to set those, the `scriptdir` and `testsdir` can be
local and not leak into whatever sources `utils.sh`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I39277e4fe7810c787315a5a8aa9ca50a55be299b

tests/utils/utils.sh

index a068ccb5c6ecc340a22c29d7af6588e3a10d0fb5..ada568b8dd5d0d31dfbdc5e2cacda548a45a1512 100644 (file)
 # An unbound variable is an error
 set -u
 
-# If `readlink -f` is available, then get a resolved absolute path to
-# the tests source directory. Otherwise, make do with a relative path.
-scriptdir="$(dirname "${BASH_SOURCE[0]}")"
-if readlink -f "." >/dev/null 2>&1; then
-       testsdir=$(readlink -f "$scriptdir/..")
-else
-       testsdir="$scriptdir/.."
-fi
-
 # Name of the OS on which we're running, if not set.
 #
 # One of:
@@ -71,18 +62,38 @@ if [ -z "${BT_TESTS_OS_TYPE:-}" ]; then
 fi
 export BT_TESTS_OS_TYPE
 
-# Base source directory of tests
-if [ -z "${BT_TESTS_SRCDIR:-}" ]; then
-       BT_TESTS_SRCDIR="$testsdir"
-fi
-export BT_TESTS_SRCDIR
+# Sets and exports, if not set:
+#
+# • `BT_TESTS_SRCDIR` to the base source directory of tests.
+# • `BT_TESTS_BUILDDIR` to the base build directory of tests.
+_set_vars_srcdir_builddir() {
+       # If `readlink -f` is available, then get a resolved absolute path
+       # to the tests source directory. Otherwise, make do with a relative
+       # path.
+       local -r scriptdir="$(dirname "${BASH_SOURCE[0]}")"
+       local testsdir
 
-# Base build directory of tests
-if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then
-       BT_TESTS_BUILDDIR="$testsdir"
-fi
-export BT_TESTS_BUILDDIR
+       if readlink -f "." >/dev/null 2>&1; then
+               testsdir=$(readlink -f "$scriptdir/..")
+       else
+               testsdir="$scriptdir/.."
+       fi
+
+       # Base source directory of tests
+       if [ -z "${BT_TESTS_SRCDIR:-}" ]; then
+               BT_TESTS_SRCDIR="$testsdir"
+       fi
+       export BT_TESTS_SRCDIR
+
+       # Base build directory of tests
+       if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then
+               BT_TESTS_BUILDDIR="$testsdir"
+       fi
+       export BT_TESTS_BUILDDIR
+}
 
+_set_vars_srcdir_builddir
+unset -f _set_vars_srcdir_builddir
 
 # Source the generated environment file if it's present
 if [ -f "${BT_TESTS_BUILDDIR}/utils/env.sh" ]; then
This page took 0.025422 seconds and 4 git commands to generate.