diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ff738e5..0054cf23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.17.0...main) - Added `-p|--parallel` to enable running tests in parallel + - Enabled only in macOS and Ubuntu - Added `assert_file_contains` and `assert_file_not_contains` - Added `assert_true` and `assert_false` - Added `BASHUNIT_DEV_LOG` diff --git a/Makefile b/Makefile index a4605d0b..d26f1879 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,10 @@ docker/alpine: @docker run --rm -it -v "$(shell pwd)":/project -w /project alpine:latest \ sh -c "apk add bash make shellcheck git && bash" +docker/ubuntu: + @docker run --rm -it -v "$(shell pwd)":/project -w /project ubuntu:latest \ + sh -c "apt update && apt install -y bash make shellcheck git && bash" + env/example: @echo "Copying variables without the values from .env into .env.example" @sed 's/=.*/=/' .env > .env.example diff --git a/src/globals.sh b/src/globals.sh index 4ff70e2c..748bf524 100644 --- a/src/globals.sh +++ b/src/globals.sh @@ -50,6 +50,10 @@ function cleanup_temp_files() { # shellcheck disable=SC2145 function log() { + if ! env::is_dev_mode_enabled; then + return + fi + local level="$1" shift diff --git a/src/main.sh b/src/main.sh index f610b726..aa867a39 100644 --- a/src/main.sh +++ b/src/main.sh @@ -19,23 +19,25 @@ function main::exec_tests() { trap 'main::cleanup' SIGINT trap '[[ $? -eq $EXIT_CODE_STOP_ON_FAILURE ]] && main::handle_stop_on_failure_sync' EXIT - if env::is_parallel_run_enabled && check_os::is_alpine; then - printf "%sWarning: Parallel test execution on Alpine Linux is currently" "${_COLOR_INCOMPLETE}" - printf "in a beta stage.\nThis means there may be unresolved issues, " - printf "particularly involving race conditions.%s\n" "${_COLOR_DEFAULT}" + if env::is_parallel_run_enabled && ! parallel::is_enabled; then + printf "%sWarning: Parallel tests are working only for macOS and Ubuntu.\n" "${_COLOR_INCOMPLETE}" + printf "For other OS (Linux/Alpine, Windows), --parallel is not enabled due to inconsistent results,\n" + printf "particularly involving race conditions.%s " "${_COLOR_DEFAULT}" + printf "%sFallback using --no-parallel%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}" fi - if env::is_parallel_run_enabled; then + if parallel::is_enabled; then parallel::reset fi console_header::print_version_with_env "$filter" "${test_files[@]}" runner::load_test_files "$filter" "${test_files[@]}" - if env::is_parallel_run_enabled; then + + if parallel::is_enabled; then wait fi - if env::is_parallel_run_enabled && parallel::must_stop_on_failure; then + if parallel::is_enabled && parallel::must_stop_on_failure; then printf "\r%sStop on failure enabled...%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}" fi diff --git a/src/parallel.sh b/src/parallel.sh index e4a0163c..4412e715 100755 --- a/src/parallel.sh +++ b/src/parallel.sh @@ -83,3 +83,10 @@ function parallel::reset() { rm -rf "$TEMP_DIR_PARALLEL_TEST_SUITE" [ -f "$TEMP_FILE_PARALLEL_STOP_ON_FAILURE" ] && rm "$TEMP_FILE_PARALLEL_STOP_ON_FAILURE" } + +function parallel::is_enabled() { + if env::is_parallel_run_enabled && (check_os::is_macos || check_os::is_ubuntu); then + return 0 + fi + return 1 +} diff --git a/src/runner.sh b/src/runner.sh index e46c3124..aa80890c 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -13,7 +13,7 @@ function runner::load_test_files() { # shellcheck source=/dev/null source "$test_file" runner::run_set_up_before_script - if env::is_parallel_run_enabled; then + if parallel::is_enabled; then runner::call_test_functions "$test_file" "$filter" 2>/dev/null & else runner::call_test_functions "$test_file" "$filter" @@ -22,7 +22,7 @@ function runner::load_test_files() { runner::clean_set_up_and_tear_down_after_script done - if env::is_parallel_run_enabled; then + if parallel::is_enabled; then wait runner::spinner & local spinner_pid=$! @@ -74,14 +74,14 @@ function runner::call_test_functions() { return fi - if ! env::is_simple_output_enabled && ! env::is_parallel_run_enabled; then + if ! env::is_simple_output_enabled && ! parallel::is_enabled; then echo "Running $script" fi helper::check_duplicate_functions "$script" || true for function_name in "${functions_to_run[@]}"; do - if env::is_parallel_run_enabled && parallel::must_stop_on_failure; then + if parallel::is_enabled && parallel::must_stop_on_failure; then break fi @@ -193,7 +193,7 @@ function runner::run_test() { runner::write_failure_result_output "$test_file" "$subshell_output" if env::is_stop_on_failure_enabled; then - if env::is_parallel_run_enabled; then + if parallel::is_enabled; then parallel::mark_stop_on_failure else exit "$EXIT_CODE_STOP_ON_FAILURE" @@ -249,7 +249,7 @@ function runner::parse_result() { shift local args=("$@") - if env::is_parallel_run_enabled; then + if parallel::is_enabled; then runner::parse_result_parallel "$function_name" "$execution_result" "${args[@]}" else runner::parse_result_sync "$function_name" "$execution_result" @@ -349,7 +349,12 @@ function runner::write_failure_result_output() { local test_file=$1 local error_msg=$2 - echo -e "$(state::get_tests_failed)) $test_file\n$error_msg" >> "$FAILURES_OUTPUT_PATH" + local test_nr="*" + if ! parallel::is_enabled; then + test_nr=$(state::get_tests_failed) + fi + + echo -e "$test_nr) $test_file\n$error_msg" >> "$FAILURES_OUTPUT_PATH" } function runner::run_set_up() { diff --git a/src/state.sh b/src/state.sh index 03078121..6eea2dbf 100644 --- a/src/state.sh +++ b/src/state.sh @@ -205,7 +205,7 @@ function state::print_line() { *) char="?" && log "warning" "unknown test type '$type'" ;; esac - if env::is_parallel_run_enabled; then + if parallel::is_enabled; then printf "%s" "$char" else if (( _TOTAL_TESTS_COUNT % 50 == 0 )); then diff --git a/tests/unit/globals_test.sh b/tests/unit/globals_test.sh index cf879169..e8d4aa28 100644 --- a/tests/unit/globals_test.sh +++ b/tests/unit/globals_test.sh @@ -10,6 +10,14 @@ function tear_down() { rm "$BASHUNIT_DEV_LOG" } +function set_up_before_script() { + export BASHUNIT_DEV_MODE=true +} + +function tear_down_after_script() { + export BASHUNIT_DEV_MODE=$_DEFAULT_DEV_MODE +} + function test_globals_current_dir() { assert_same "tests/unit" "$(current_dir)" }