]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: prefix "internal" stuff with an underscore
authorFrantisek Sumsal <frantisek@sumsal.cz>
Mon, 22 May 2023 11:24:12 +0000 (13:24 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Mon, 22 May 2023 14:02:49 +0000 (16:02 +0200)
Since bash has no namespaces, let's do the second best thing and prefix
all "internal" stuff with an underscore, to minimize the chance of a name
conflict in the future.

test/units/test-control.sh

index 3e2549d0b30b8cc664b52fa63289b00885a0d33b..cd7048ae97c5b2896b9c375c5536421fb823a099 100644 (file)
@@ -6,12 +6,12 @@ if [[ "${BASH_SOURCE[0]}" -ef "$0" ]]; then
     exit 1
 fi
 
-declare -i CHILD_PID=0
-PASSED_TESTS=()
-FAILED_TESTS=()
+declare -i _CHILD_PID=0
+_PASSED_TESTS=()
+_FAILED_TESTS=()
 
 # Like trap, but passes the signal name as the first argument
-trap_with_sig() {
+_trap_with_sig() {
     local fun="${1:?}"
     local sig
     shift
@@ -23,16 +23,16 @@ trap_with_sig() {
 }
 
 # Propagate the caught signal to the current child process
-handle_signal() {
+_handle_signal() {
     local sig="${1:?}"
 
-    if [[ $CHILD_PID -gt 0 ]]; then
-        echo "Propagating signal $sig to child process $CHILD_PID"
-        kill -s "$sig" "$CHILD_PID"
+    if [[ $_CHILD_PID -gt 0 ]]; then
+        echo "Propagating signal $sig to child process $_CHILD_PID"
+        kill -s "$sig" "$_CHILD_PID"
     fi
 }
 
-# In order to make the handle_signal() stuff above work, we have to execute
+# In order to make the _handle_signal() stuff above work, we have to execute
 # each script asynchronously, since bash won't execute traps until the currently
 # executed command finishes. This, however, introduces another issue regarding
 # how bash's wait works. Quoting:
@@ -44,7 +44,7 @@ handle_signal() {
 #
 # In other words - every time we propagate a signal, wait returns with
 # 128+signal, so we have to wait again - repeat until the process dies.
-wait_harder() {
+_wait_harder() {
     local pid="${1:?}"
 
     while kill -0 "$pid" &>/dev/null; do
@@ -54,6 +54,31 @@ wait_harder() {
     wait "$pid"
 }
 
+_show_summary() {(
+    set +x
+
+    if [[ ${#_PASSED_TESTS[@]} -eq 0 && ${#_FAILED_TESTS[@]} -eq 0 ]]; then
+        echo >&2 "No tests were executed, this is most likely an error"
+        exit 1
+    fi
+
+    printf "PASSED TESTS: %3d:\n" "${#_PASSED_TESTS[@]}"
+    echo   "------------------"
+    for t in "${_PASSED_TESTS[@]}"; do
+        echo "$t"
+    done
+
+    if [[ "${#_FAILED_TESTS[@]}" -ne 0 ]]; then
+        printf "FAILED TESTS: %3d:\n" "${#_FAILED_TESTS[@]}"
+        echo   "------------------"
+        for t in "${_FAILED_TESTS[@]}"; do
+            echo "$t"
+        done
+    fi
+
+    [[ "${#_FAILED_TESTS[@]}" -eq 0 ]]
+)}
+
 # Like run_subtests, but propagate specified signals to the subtest script
 run_subtests_with_signals() {
     local subtests=("${0%.sh}".*.sh)
@@ -69,17 +94,17 @@ run_subtests_with_signals() {
         exit 1
     fi
 
-    trap_with_sig handle_signal "$@"
+    _trap_with_sig _handle_signal "$@"
 
     for subtest in "${subtests[@]}"; do
         : "--- $subtest BEGIN ---"
         "./$subtest" &
-        CHILD_PID=$!
-        wait_harder "$CHILD_PID" && PASSED_TESTS+=("$subtest") || FAILED_TESTS+=("$subtest")
+        _CHILD_PID=$!
+        _wait_harder "$_CHILD_PID" && _PASSED_TESTS+=("$subtest") || _FAILED_TESTS+=("$subtest")
         : "--- $subtest END ---"
     done
 
-    show_summary
+    _show_summary
 }
 
 # Run all subtests (i.e. files named as testsuite-<testid>.<subtest_name>.sh)
@@ -94,11 +119,11 @@ run_subtests() {
 
     for subtest in "${subtests[@]}"; do
         : "--- $subtest BEGIN ---"
-        "./$subtest" && PASSED_TESTS+=("$subtest") || FAILED_TESTS+=("$subtest")
+        "./$subtest" && _PASSED_TESTS+=("$subtest") || _FAILED_TESTS+=("$subtest")
         : "--- $subtest END ---"
     done
 
-    show_summary
+    _show_summary
 }
 
 # Run all test cases (i.e. functions prefixed with testcase_ in the current namespace)
@@ -123,28 +148,3 @@ run_testcases() {
         : "+++ $testcase END +++"
     done
 }
-
-show_summary() {(
-    set +x
-
-    if [[ ${#PASSED_TESTS[@]} -eq 0 && ${#FAILED_TESTS[@]} -eq 0 ]]; then
-        echo >&2 "No tests were executed, this is most likely an error"
-        exit 1
-    fi
-
-    printf "PASSED TESTS: %3d:\n" "${#PASSED_TESTS[@]}"
-    echo   "------------------"
-    for t in "${PASSED_TESTS[@]}"; do
-        echo "$t"
-    done
-
-    if [[ "${#FAILED_TESTS[@]}" -ne 0 ]]; then
-        printf "FAILED TESTS: %3d:\n" "${#FAILED_TESTS[@]}"
-        echo   "------------------"
-        for t in "${FAILED_TESTS[@]}"; do
-            echo "$t"
-        done
-    fi
-
-    [[ "${#FAILED_TESTS[@]}" -eq 0 ]]
-)}