Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel feature flag enabled for macOS and Ubuntu #371

Merged
merged 7 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/globals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions src/parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 12 additions & 7 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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=$!
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/globals_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
Expand Down
Loading