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

Multiple asserts on standalone #340

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 27 additions & 5 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -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=()

Expand All @@ -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"
Expand Down Expand Up @@ -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
11 changes: 7 additions & 4 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -132,13 +133,15 @@ 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
inner_exit_code=1
fi
output=$(echo "$output" | sed '$d')
fi

echo "$output"
return "$inner_exit_code"
else
Expand Down
Loading