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

Allow calling assertions outside tests #258

Merged
merged 19 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.12.0...main)

- ...
- Allow calling assertions outside tests

## [0.12.0](https://github.com/TypedDevs/bashunit/compare/0.11.0...0.12.0) - 2024-06-11

Expand Down
21 changes: 12 additions & 9 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ source "$BASHUNIT_ROOT_DIR/src/helpers.sh"
source "$BASHUNIT_ROOT_DIR/src/upgrade.sh"
source "$BASHUNIT_ROOT_DIR/src/assertions.sh"
source "$BASHUNIT_ROOT_DIR/src/runner.sh"
source "$BASHUNIT_ROOT_DIR/src/main.sh"

###############
#### MAIN #####
###############

_ASSERT_FN=$_DEFAULT_ASSERT_FN
Chemaclass marked this conversation as resolved.
Show resolved Hide resolved
_FILTER=""
_FILES=()

while [[ $# -gt 0 ]]; do
argument="$1"
case $argument in
-a|--assert)
_ASSERT_FN="$2"
shift
shift
;;
-f|--filter)
_FILTER="$2"
shift
Expand Down Expand Up @@ -72,8 +75,8 @@ while [[ $# -gt 0 ]]; do
esac
done

console_header::print_version_with_env
runner::load_test_files "$_FILTER" "${_FILES[@]}"
console_results::render_result

exit 0
if [[ -n $_ASSERT_FN ]]; then
main::exec_assert "$_ASSERT_FN" "${_FILES[@]}"
else
main::exec_tests "$_FILTER" "${_FILES[@]}"
fi
1 change: 1 addition & 0 deletions src/default_env_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ _DEFAULT_SIMPLE_OUTPUT=false
_DEFAULT_STOP_ON_FAILURE=false
_DEFAULT_SHOW_EXECUTION_TIME=true
_DEFAULT_DEFAULT_PATH=
_DEFAULT_ASSERT_FN=""
Chemaclass marked this conversation as resolved.
Show resolved Hide resolved
CAT="$(which cat)"
27 changes: 27 additions & 0 deletions src/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

function main::exec_tests() {
local filter=$1
local args=("${@:2}")

console_header::print_version_with_env
runner::load_test_files "$filter" "${args[@]}"
console_results::render_result
exit 0
}

function main::exec_assert() {
local assert_fn=$1
local args=("${@:2}")

if ! type "$assert_fn" > /dev/null 2>&1; then
echo "Function $assert_fn does not exist."
exit 1
fi

"$assert_fn" "${args[@]}"

if [[ "$(state::get_tests_failed)" -gt 0 ]] || [[ "$(state::get_assertions_failed)" -gt 0 ]]; then
exit 1
fi
}
26 changes: 26 additions & 0 deletions tests/acceptance/bashunit_direct_fn_call_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

function set_up_before_script() {
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"
}

function test_bashunit_direct_fn_call_passes() {
local expected="foo"
local actual="foo"

./bashunit -a assert_equals --env "$TEST_ENV_FILE" "$expected" $actual
assert_successful_code
}

function test_bashunit_direct_fn_call_failure() {
local expected="foo"
local actual="bar"

assert_match_snapshot "$(./bashunit -a assert_equals --env "$TEST_ENV_FILE" "$expected" $actual)"
assert_general_error "$(./bashunit -a assert_equals --env "$TEST_ENV_FILE" "$expected" $actual)"
Chemaclass marked this conversation as resolved.
Show resolved Hide resolved
}

function test_bashunit_direct_fn_call_non_existing_fn() {
assert_match_snapshot "$(./bashunit -a non_existing_fn --env "$TEST_ENV_FILE")"
assert_general_error "$(./bashunit -a non_existing_fn --env "$TEST_ENV_FILE")"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
βœ— Failed: Main::exec assert
Expected 'foo'
but got 'bar'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Function non_existing_fn does not exist.