Skip to content

Add plan to tap output #132

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

Merged
merged 8 commits into from
Jan 8, 2025
Merged
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
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ ok - test_fake_exports_faked_in_subshells
ok - test_fake_transmits_params_to_fake_code
ok - test_fake_transmits_params_to_fake_code_as_array
ok - test_should_pretty_format_even_when_LANG_is_unset
1..30
```

== How to write tests
Expand Down
78 changes: 53 additions & 25 deletions bash_unit
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ GREP="$(type -P grep)"
RM="$(type -P rm)"
SHUF="$(type -P sort) -R"

# Store the number of tests run in a file so that the value is available
# from the parent process. This will become an issue if we start considering
# parallel test execution in the future.
TEST_COUNT_FILE="$(mktemp)"
# shellcheck disable=2064 # Use single quotes, expands now, not when signaled.
trap "$RM -f \"$TEST_COUNT_FILE\"" EXIT

fail() {
local message=${1:-}
local stdout=${2:-}
Expand Down Expand Up @@ -185,16 +192,6 @@ assert_no_diff() {
"$message expected '${actual}' to be identical to '${expected}' but was different"
}

skip_if() {
local condition="$1"
local pattern="$2"
if eval "$condition" >/dev/null 2>&1
then
skip_pattern="${skip_pattern}${skip_pattern_separator}${pattern}"
skip_pattern_separator="|"
fi
}

fake() {
local command=$1
shift
Expand Down Expand Up @@ -245,24 +242,32 @@ maybe_shuffle() {
run_tests() {
local failure=0

for pending_test in $(set | "$GREP" -E '^(pending|todo).* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::')
do
notify_test_starting "$pending_test"
notify_test_pending "$pending_test"
done

local pending_tests=$(set | "$GREP" -E '^(pending|todo).* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::')
if [[ -n "$skip_pattern" ]]
then
for skipped_test in $(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -E "$skip_pattern" | "$SED" -e 's: .*::')
do
notify_test_starting "$skipped_test"
notify_test_skipped "$skipped_test"
done
local skipped_tests=$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -E "$skip_pattern" | "$SED" -e 's: .*::')
local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -v -E "$skip_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
else
local skipped_tests=""
local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
fi

local test_count=$(cat "${TEST_COUNT_FILE}")
test_count=$((test_count + $(count "$pending_tests") + $(count "$tests_to_run") + $(count "$skipped_tests")))
echo "${test_count}" > "${TEST_COUNT_FILE}"

for pending_test in $pending_tests
do
notify_test_starting "$pending_test"
notify_test_pending "$pending_test"
done

for skipped_test in $skipped_tests
do
notify_test_starting "$skipped_test"
notify_test_skipped "$skipped_test"
done

for test in $tests_to_run
do
(
Expand All @@ -274,6 +279,7 @@ run_tests() {
)
failure=$(( $? || failure))
done

return $failure
}

Expand Down Expand Up @@ -455,10 +461,12 @@ tap_format() {
"$SED" 's:^:# :' | color "$YELLOW"
}
notify_suites_succeded() {
:
local message="$1"
echo "1..$message"
}
notify_suites_failed() {
:
local message="$1"
echo "1..$message"
}
}

Expand All @@ -477,6 +485,25 @@ quiet_mode() {
}
}

skip_if() {
local condition="$1"
local pattern="$2"
if eval "$condition" >/dev/null 2>&1
then
skip_pattern="${skip_pattern}${skip_pattern_separator}${pattern}"
skip_pattern_separator="|"
fi
}

count() {
local tests="$1"
if [[ -z "$tests" ]]; then
echo 0
else
echo "$tests" | wc -l
fi
}

output_format=text
verbosity=normal
test_pattern=""
Expand Down Expand Up @@ -540,6 +567,7 @@ fi

#run tests received as parameters
failure=0
echo 0 > "${TEST_COUNT_FILE}"
for test_file in "$@"
do
notify_suite_starting "$test_file"
Expand All @@ -565,9 +593,9 @@ done

if ((failure))
then
notify_suites_failed
notify_suites_failed "$(cat "${TEST_COUNT_FILE}")"
else
notify_suites_succeded
notify_suites_succeded "$(cat "${TEST_COUNT_FILE}")"
fi

exit $failure
40 changes: 28 additions & 12 deletions tests/test_tap_format
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test_tap_format_for_one_succesfull_test() {
assert_equals \
"\
# Running tests in code
ok - test_ok\
" \
ok - test_ok
1..1" \
"$(bash_unit_out_for_code <<EOF
test_ok() {
assert true
Expand All @@ -27,8 +27,8 @@ test_tap_format_for_one_failing_test() {
"\
# Running tests in code
not ok - test_not_ok
# code:2:test_not_ok()\
" \
# code:2:test_not_ok()
1..1" \
"$(bash_unit_out_for_code <<EOF
test_not_ok() {
assert false
Expand All @@ -41,8 +41,8 @@ test_tap_format_for_one_pending_test() {
assert_equals \
"\
# Running tests in code
ok - pending_not_yet_implemented # todo test to be written\
" \
ok - pending_not_yet_implemented # todo test to be written
1..1" \
"$(bash_unit_out_for_code <<EOF
pending_not_yet_implemented() {
assert false
Expand All @@ -51,15 +51,31 @@ EOF
)"
}

test_tap_format_with_skipped_tests() {
bash_unit_output="$(bash_unit_out_for_code <<EOF
test_one() { echo -n ; }
test_two() { fail ; }
skip_if true two
EOF
)"

assert_equals "\
# Running tests in code
ok - test_two # skip test not run
ok - test_one
1..2" \
"$bash_unit_output"
}

test_tap_format_for_failing_test_with_stdout_stderr_outputs() {
assert_equals \
"\
# Running tests in code
not ok - test_not_ok
# out> message on stdout
# err> message on stderr
# code:2:test_not_ok()\
" \
# code:2:test_not_ok()
1..1" \
"$(bash_unit_out_for_code <<EOF
test_not_ok() {
assert_fails "echo message on stdout ; echo message on stderr >&2"
Expand All @@ -74,8 +90,8 @@ test_assertion_message_is_tap_formatted() {
# Running tests in code
not ok - test_not_ok
# obvious failure
# code:2:test_not_ok()\
" \
# code:2:test_not_ok()
1..1" \
"$(bash_unit_out_for_code <<EOF
test_not_ok() {
assert_fails true "obvious failure"
Expand All @@ -91,8 +107,8 @@ test_multi_lines_assertion_message_is_tap_formatted() {
not ok - test_not_ok
# obvious failure
# on multiple lines
# code:2:test_not_ok()\
" \
# code:2:test_not_ok()
1..1" \
"$(bash_unit_out_for_code <<EOF
test_not_ok() {
assert_fails true "obvious failure\non multiple lines"
Expand Down
Loading