diff --git a/bashunit b/bashunit index 98d9a439..0f435c1c 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="" _ARGS=() @@ -41,8 +42,9 @@ while [[ $# -gt 0 ]]; do argument="$1" case $argument in -a|--assert) - _ASSERT_FN="$2" - shift + _ASSERT_FN_KEY+=("$2") + _ASSERT_FN_VALUES+=("$3") + shift 2 ;; -f|--filter) _FILTER="$2" @@ -109,8 +111,28 @@ done set +eu -if [[ -n "$_ASSERT_FN" ]]; then - main::exec_assert "$_ASSERT_FN" "${_ARGS[@]}" +if [[ ${#_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 else main::exec_tests "$_FILTER" "${_ARGS[@]}" fi diff --git a/src/main.sh b/src/main.sh index f610b726..ee038677 100644 --- a/src/main.sh +++ b/src/main.sh @@ -93,23 +93,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 @@ -132,6 +133,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 @@ -139,6 +141,7 @@ function main::handle_assert_exit_code() { fi output=$(echo "$output" | sed '$d') fi + echo "$output" return "$inner_exit_code" else