From a8e017fedcae615576f61d28ac273449053f6f8b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 14 Oct 2026 18:58:19 +0500 Subject: pollute the global namespace less --- lib/log.sh | 40 ++++++++++++++++++++-------------------- lib/test.sh | 4 ++-- main.sh | 18 +++++++++--------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/log.sh b/lib/log.sh index 74d4cc7..db2c498 100644 --- a/lib/log.sh +++ b/lib/log.sh @@ -35,12 +35,12 @@ fail_details() { done } -log_numof_tests_ok=0 -log_numof_tests_failed=0 -declare -a log_tests_failed=() -declare -A log_tests_should_fail=() +_log_numof_tests_ok=0 +_log_numof_tests_failed=0 +declare -a _log_tests_failed=() +declare -A _log_tests_should_fail=() -log_test_start() { +_log_test_start() { if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} TEST_FILE" >&2 return 1 @@ -49,7 +49,7 @@ log_test_start() { local test_file="$1" if [ -n "${test_should_fail:-}" ]; then - log_tests_should_fail["$test_file"]=1 + _log_tests_should_fail["$test_file"]=1 fi cat <&2 @@ -62,13 +62,13 @@ EOF log "Should fail: ${test_should_fail:-No}" } -log_test_ok() { +_log_test_ok() { if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} TEST_FILE" >&2 return 1 fi - log_numof_tests_ok=$((log_numof_tests_ok + 1)) + _log_numof_tests_ok=$((_log_numof_tests_ok + 1)) cat <&2 ---------------------------------------------------------------------- @@ -77,14 +77,14 @@ OK: $1 EOF } -log_test_fail() { +_log_test_fail() { if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} TEST_FILE" >&2 return 1 fi - log_numof_tests_failed=$((log_numof_tests_failed + 1)) - log_tests_failed+=("$1") + _log_numof_tests_failed=$((_log_numof_tests_failed + 1)) + _log_tests_failed+=("$1") cat <&2 ---------------------------------------------------------------------- @@ -93,7 +93,7 @@ FAIL: $1 EOF } -log_test_finish() { +_log_test_finish() { if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} TEST_FILE EXIT_CODE" >&2 return 1 @@ -102,27 +102,27 @@ log_test_finish() { local test_file="$1" local ret="$2" local test_should_fail - test_should_fail="${log_tests_should_fail["$test_file"]:+x}" + test_should_fail="${_log_tests_should_fail["$test_file"]:+x}" if [ "$ret" -eq 0 ] || [ -n "$test_should_fail" ]; then - log_test_ok "$test_file" + _log_test_ok "$test_file" else - log_test_fail "$test_file" + _log_test_fail "$test_file" fi } -log_tests_summary() { +_log_tests_summary() { cat <&2 ====================================================================== SUMMARY ====================================================================== -OK: $log_numof_tests_ok -FAIL: $log_numof_tests_failed +OK: $_log_numof_tests_ok +FAIL: $_log_numof_tests_failed EOF local test_file - for test_file in ${log_tests_failed[@]+"${log_tests_failed[@]}"}; do + for test_file in ${_log_tests_failed[@]+"${_log_tests_failed[@]}"}; do cat <&2 FAIL: $test_file EOF @@ -133,7 +133,7 @@ EOF EOF local ret=0 - [ "$log_numof_tests_failed" -gt 0 ] && ret=1 + [ "$_log_numof_tests_failed" -gt 0 ] && ret=1 return "$ret" } diff --git a/lib/test.sh b/lib/test.sh index 59e7ce6..7c5844b 100644 --- a/lib/test.sh +++ b/lib/test.sh @@ -6,13 +6,13 @@ test_should_fail= test_root_dir= -test_setup_global() { +_test_setup_global() { test_root_dir="$( mktemp -d )" # mktemp returns /var/..., which is actually in /private/var/... on macOS. test_root_dir="$( readlink -e -- "$test_root_dir" )" } -test_cleanup_global() { +_test_cleanup_global() { if [ "$( type -t test_cleanup )" == function ]; then test_cleanup || true fi diff --git a/main.sh b/main.sh index 80b3488..7fb4bf6 100755 --- a/main.sh +++ b/main.sh @@ -19,7 +19,7 @@ readonly tests_dir="$_script_dir/.." source "$_script_dir/lib/log.sh" -run_test_file() { +_test_run_file() { local test_file for test_file; do source "$_script_dir/lib/test.sh" @@ -39,7 +39,7 @@ run_test_file() { source "$tests_dir/$test_file" - log_test_start "$test_file" + _log_test_start "$test_file" set +e ( @@ -47,18 +47,18 @@ run_test_file() { shopt -s inherit_errexit 2> /dev/null || true shopt -s lastpipe - trap test_cleanup_global EXIT - test_setup_global + trap _test_cleanup_global EXIT + _test_setup_global test_run ) local ret="$?" set -e - log_test_finish "$test_file" "$ret" + _log_test_finish "$test_file" "$ret" done } -main() { +_test_main() { if [ "$#" -gt 1 ]; then echo "usage: $_script_name [PATTERN]" >&2 return 1 @@ -84,8 +84,8 @@ main() { test_files+=("$test_file") done - run_test_file ${test_files[@]+"${test_files[@]}"} - log_tests_summary + _test_run_file ${test_files[@]+"${test_files[@]}"} + _log_tests_summary } -main "$@" +_test_main "$@" -- cgit v1.3