From 715ca90d1365f3bf34ff313dca95ad7b281c3434 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 29 Sep 2024 20:44:35 +0200 Subject: [PATCH] feat: WIP adding key-value multiple asserts in array --- bashunit | 32 +++++++++++++++++++++++++++----- src/main.sh | 11 +++++++---- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/bashunit b/bashunit index 8ac501e8..edd7b3a2 100755 --- a/bashunit +++ b/bashunit @@ -26,7 +26,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=() @@ -34,8 +35,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" @@ -96,8 +98,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 613d09b6..bcdee24e 100644 --- a/src/main.sh +++ b/src/main.sh @@ -54,23 +54,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 @@ -93,6 +94,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 @@ -100,6 +102,7 @@ function main::handle_assert_exit_code() { fi output=$(echo "$output" | sed '$d') fi + echo "$output" return "$inner_exit_code" else