]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: abstract the test case logic into a shared function
authorFrantisek Sumsal <frantisek@sumsal.cz>
Mon, 22 May 2023 10:39:25 +0000 (12:39 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Mon, 22 May 2023 14:02:49 +0000 (16:02 +0200)
test/units/test-control.sh
test/units/testsuite-13.nspawn.sh
test/units/testsuite-73.sh
test/units/testsuite-74.machine-id-setup.sh

index dd28939cbf55921cce2f4ec3d80eb13191b33c95..3e2549d0b30b8cc664b52fa63289b00885a0d33b 100644 (file)
@@ -82,6 +82,7 @@ run_subtests_with_signals() {
     show_summary
 }
 
+# Run all subtests (i.e. files named as testsuite-<testid>.<subtest_name>.sh)
 run_subtests() {
     local subtests=("${0%.sh}".*.sh)
     local subtest
@@ -100,6 +101,29 @@ run_subtests() {
     show_summary
 }
 
+# Run all test cases (i.e. functions prefixed with testcase_ in the current namespace)
+run_testcases() {
+    local testcase testcases
+
+    # Create a list of all functions prefixed with testcase_
+    mapfile -t testcases < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}')
+
+    if [[ "${#testcases[@]}" -eq 0 ]]; then
+        echo >&2 "No test cases found, this is most likely an error"
+        exit 1
+    fi
+
+    for testcase in "${testcases[@]}"; do
+        : "+++ $testcase BEGIN +++"
+        # Note: the subshell here is used purposefully, otherwise we might
+        #       unexpectedly inherit a RETURN trap handler from the called
+        #       function and call it for the second time once we return,
+        #       causing a "double-free"
+        ("$testcase")
+        : "+++ $testcase END +++"
+    done
+}
+
 show_summary() {(
     set +x
 
index 0cc96424e63e32f4322704210f66a529dc2e1629..682f67773569ea43b593209a5a7c9de5431de489 100755 (executable)
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
 # shellcheck source=test/units/util.sh
 . "$(dirname "$0")"/util.sh
 
+
 export SYSTEMD_LOG_LEVEL=debug
 export SYSTEMD_LOG_TARGET=journal
 
@@ -838,17 +841,7 @@ matrix_run_one() {
     return 0
 }
 
-# Create a list of all functions prefixed with testcase_
-mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}')
-
-if [[ "${#TESTCASES[@]}" -eq 0 ]]; then
-    echo >&2 "No test cases found, this is most likely an error"
-    exit 1
-fi
-
-for testcase in "${TESTCASES[@]}"; do
-    "$testcase"
-done
+run_testcases
 
 for api_vfs_writable in yes no network; do
     matrix_run_one no  no  $api_vfs_writable
index 523acd8d06f75c6aeeb3d86ab08f8cd8cfceab3f..022a70862f0c1b4bbc9b38bc372d9179080a7ae0 100755 (executable)
@@ -4,6 +4,8 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
 # shellcheck source=test/units/util.sh
 . "$(dirname "$0")"/util.sh
 
@@ -688,18 +690,7 @@ testcase_locale_gen_leading_space() {
 export SYSTEMD_KBD_MODEL_MAP=/usr/lib/systemd/tests/testdata/test-keymap-util/kbd-model-map
 
 enable_debug
-
-# Create a list of all functions prefixed with testcase_
-mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}')
-
-if [[ "${#TESTCASES[@]}" -eq 0 ]]; then
-    echo >&2 "No test cases found, this is most likely an error"
-    exit 1
-fi
-
-for testcase in "${TESTCASES[@]}"; do
-    "$testcase"
-done
+run_testcases
 
 touch /testok
 rm /failed
index a24f9d299e2e2b4aee2f2afd32081d1eae0c1f94..c2b9db5178230fa5fbc62f23c36c90185ff04c49 100755 (executable)
@@ -4,6 +4,11 @@
 set -eux
 set -o pipefail
 
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+# shellcheck source=test/units/util.sh
+. "$(dirname "$0")"/util.sh
+
 root_mock() {
     local root="${1:?}"
 
@@ -69,14 +74,4 @@ testcase_transient() {
 systemctl --state=failed --no-legend --no-pager >/failed
 test ! -s /failed
 
-# Create a list of all functions prefixed with testcase_
-mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}')
-
-if [[ "${#TESTCASES[@]}" -eq 0 ]]; then
-    echo >&2 "No test cases found, this is most likely an error"
-    exit 1
-fi
-
-for testcase in "${TESTCASES[@]}"; do
-    "$testcase"
-done
+run_testcases