From 2a656b4ae7df23627559ac7e891ff3d419c71ef6 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 9 Nov 2023 11:43:10 -0500 Subject: [PATCH] tests/utils/env.sh.in: add comments and remove redundancy 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 Change-Id: If392a6513b04b9e5fc2fd7b010b4071369e05b07 --- tests/utils/env.sh.in | 100 +++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 56 deletions(-) diff --git a/tests/utils/env.sh.in b/tests/utils/env.sh.in index 78964537..12069fef 100644 --- a/tests/utils/env.sh.in +++ b/tests/utils/env.sh.in @@ -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 -- 2.34.1