From 7030f9fe4fef4bc2a1f1ea3e6cd66ac38b4fcf61 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 9 Nov 2023 22:56:05 -0500 Subject: [PATCH] tests/utils/utils.sh: use a func. to set `BT_TESTS_{SRC,BUILD}DIR` 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 Change-Id: I39277e4fe7810c787315a5a8aa9ca50a55be299b --- tests/utils/utils.sh | 49 +++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index a068ccb5..ada568b8 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -25,15 +25,6 @@ # 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 -- 2.34.1