diff --git a/bashunit b/bashunit index d06795f8..79e05ca9 100755 --- a/bashunit +++ b/bashunit @@ -30,7 +30,8 @@ source "$BASHUNIT_ROOT_DIR/src/runner.sh" source "$BASHUNIT_ROOT_DIR/src/bashunit.sh" source "$BASHUNIT_ROOT_DIR/src/main.sh" -_ASSERT_FN="" +_ASSERT_FN_KEY=() +_ASSERT_FN_VALUES=() _FILTER="" _RAW_ARGS=() _ARGS=() @@ -43,8 +44,9 @@ clock::init while [[ $# -gt 0 ]]; do case "$1" in -a|--assert) - _ASSERT_FN="$2" - shift + _ASSERT_FN_KEY+=("$2") + _ASSERT_FN_VALUES+=("$3") + shift 2 ;; -f|--filter) _FILTER="$2" @@ -134,6 +136,28 @@ set +eu ################# if [[ -n "$_ASSERT_FN" ]]; then main::exec_assert "$_ASSERT_FN" "${_ARGS[@]}" +elif [[ ${#_ASSERT_FN_KEY[@]} -gt 0 ]]; then + output="" + last_exit_code=0 + + for i in "${!_ASSERT_FN_KEY[@]}"; do + assert_fn="${_ASSERT_FN_KEY[$i]}" + assert_value="${_ASSERT_FN_VALUES[$i]}" + + if [[ -n "$output" ]]; then + # Pass the output from the previous assertion as input to the next + output=$(main::exec_assert "$assert_fn" "$assert_value" "$output" ) + else + # First assertion, run with original args + output=$(main::exec_assert "$assert_fn" "$assert_value" "${_ARGS[@]}" ) + fi + + last_exit_code=$? + if [[ $last_exit_code -ne 0 ]]; then + break # Stop if any assertion fails + fi + done + exit $last_exit_code elif [[ "$_BENCH_MODE" == true ]]; then main::exec_benchmarks "$_FILTER" "${_ARGS[@]}" else diff --git a/src/main.sh b/src/main.sh index 2955b075..d3f94a7a 100644 --- a/src/main.sh +++ b/src/main.sh @@ -134,23 +134,24 @@ function main::exec_assert() { local inner_exit_code=0 local bashunit_exit_code=0 - # Handle different assert_* functions + # Handle the `assert_exit_code` function by evaluating the command and capturing output case "$assert_fn" in assert_exit_code) + # Evaluate the command and capture both output and exit code +# output=$(eval "$last_arg" 2>&1) output=$(main::handle_assert_exit_code "$last_arg") inner_exit_code=$? - # Remove the last argument and append the exit code + # Remove the last argument (command) and append the actual exit code args=("${args[@]:0:last_index}") args+=("$inner_exit_code") ;; *) - # Add more cases here for other assert_* handlers if needed + # Any other assertion functions (like assert_contains) are passed normally ;; esac if [[ -n "$output" ]]; then echo "$output" 1>&1 - assert_fn="assert_same" fi # Run the assertion function and write into stderr @@ -173,6 +174,7 @@ function main::handle_assert_exit_code() { output=$(eval "$cmd" 2>&1 || echo "inner_exit_code:$?") local last_line last_line=$(echo "$output" | tail -n 1) + if echo "$last_line" | grep -q 'inner_exit_code:[0-9]*'; then inner_exit_code=$(echo "$last_line" | grep -o 'inner_exit_code:[0-9]*' | cut -d':' -f2) if ! [[ $inner_exit_code =~ ^[0-9]+$ ]]; then @@ -180,6 +182,7 @@ function main::handle_assert_exit_code() { fi output=$(echo "$output" | sed '$d') fi + echo "$output" return "$inner_exit_code" else