tests/utils/env.sh.in: add comments and remove redundancy
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 9 Nov 2023 16:43:10 +0000 (11:43 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 7 Feb 2024 21:25:56 +0000 (16:25 -0500)
This patch removes redundancy in `env.sh.in` by using a function which
sets a variable by name if it's empty and exports it.

This makes the file less error-prone: the variable name is written just
once, making it safer to add more.

The

    export "${varname?}"

form is to make ShellCheck happy, even if a simple `export $varname`
would perfectly work in this context.

The function is unset at the end to avoid polluting the scope of
whatever sources `env.sh`.

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

tests/utils/env.sh.in

index 78964537a560e3f531181ee31f954d34a7221fbd..12069fef8cfafdbecf01304d7b80e885c68803d6 100644 (file)
@@ -3,59 +3,47 @@
 # SPDX-FileCopyrightText: 2021-2022 EfficiOS, Inc.
 # SPDX-License-Identifier: GPL-2.0-only
 
-### Base paths to test suite ###
-if [ -z "${BT_TESTS_SRCDIR:-}" ]; then
-       BT_TESTS_SRCDIR="@abs_top_srcdir@/tests"
-fi
-export BT_TESTS_SRCDIR
-
-if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then
-       BT_TESTS_BUILDDIR="@abs_top_builddir@/tests"
-fi
-export BT_TESTS_BUILDDIR
-
-
-### External Tools ###
-if [ -z "${BT_TESTS_AWK_BIN:-}" ]; then
-       BT_TESTS_AWK_BIN="@AWK@"
-fi
-export BT_TESTS_AWK_BIN
-
-if [ -z "${BT_TESTS_GREP_BIN:-}" ]; then
-       BT_TESTS_GREP_BIN="@GREP@"
-fi
-export BT_TESTS_GREP_BIN
-
-if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then
-       BT_TESTS_PYTHON_BIN="@PYTHON@"
-fi
-export BT_TESTS_PYTHON_BIN
-
-if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then
-       BT_TESTS_PYTHON_CONFIG_BIN="@PYTHON_CONFIG@"
-fi
-export BT_TESTS_PYTHON_CONFIG_BIN
-
-if [ -z "${BT_TESTS_SED_BIN:-}" ]; then
-       BT_TESTS_SED_BIN="@SED@"
-fi
-export BT_TESTS_SED_BIN
-
-if [ -z "${BT_TESTS_CC_BIN:-}" ]; then
-       BT_TESTS_CC_BIN="@CC@"
-fi
-export BT_TESTS_CC_BIN
-
-
-### Optional features ###
-
-if [ -z "${BT_TESTS_ENABLE_ASAN:-}" ]; then
-       BT_TESTS_ENABLE_ASAN="@ENABLE_ASAN@"
-fi
-export BT_TESTS_ENABLE_ASAN
-
-# Define to 1 to enable tests that depend on the Python plugins support
-if [ -z "${BT_TESTS_ENABLE_PYTHON_PLUGINS:-}" ]; then
-       BT_TESTS_ENABLE_PYTHON_PLUGINS="@ENABLE_PYTHON_PLUGINS@"
-fi
-export BT_TESTS_ENABLE_PYTHON_PLUGINS
+# Sets the variable named `$1` to `$2` if it's not set, and exports it.
+_set_var_def() {
+       local -r varname=$1
+       local -r val=$2
+
+       if [[ -z ${!varname:-} ]]; then
+               eval "$varname='$val'"
+       fi
+
+       export "${varname?}"
+}
+
+# Base source directory of tests, if not set
+_set_var_def BT_TESTS_SRCDIR '@abs_top_srcdir@/tests'
+
+# Base build directory of tests, if not set
+_set_var_def BT_TESTS_BUILDDIR '@abs_top_builddir@/tests'
+
+# Name of the `awk` command to use when testing, if not set
+_set_var_def BT_TESTS_AWK_BIN '@AWK@'
+
+# Name of the `grep` command to use when testing, if not set
+_set_var_def BT_TESTS_GREP_BIN '@GREP@'
+
+# Name of the `python3` command to use when testing, if not set
+_set_var_def BT_TESTS_PYTHON_BIN '@PYTHON@'
+
+# Name of the `python3-config` command to use when testing, if not set
+_set_var_def BT_TESTS_PYTHON_CONFIG_BIN '@PYTHON_CONFIG@'
+
+# Name of the `sed` command to use when testing, if not set
+_set_var_def BT_TESTS_SED_BIN '@SED@'
+
+# Name of the `cc` command to use when testing, if not set
+_set_var_def BT_TESTS_CC_BIN '@CC@'
+
+# `1` if AddressSanitizer is used, if not set
+_set_var_def BT_TESTS_ENABLE_ASAN '@ENABLE_ASAN@'
+
+# `1` to run tests which depend on Python plugin support, if not set
+_set_var_def BT_TESTS_ENABLE_PYTHON_PLUGINS '@ENABLE_PYTHON_PLUGINS@'
+
+# No more
+unset -f _set_var_def
This page took 0.025971 seconds and 4 git commands to generate.