diff --git a/README.adoc b/README.adoc index 2335a7c..6424d4b 100644 --- a/README.adoc +++ b/README.adoc @@ -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 diff --git a/bash_unit b/bash_unit index 4699fcc..37d84a3 100755 --- a/bash_unit +++ b/bash_unit @@ -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:-} @@ -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 @@ -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 ( @@ -274,6 +279,7 @@ run_tests() { ) failure=$(( $? || failure)) done + return $failure } @@ -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" } } @@ -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="" @@ -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" @@ -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 diff --git a/tests/test_tap_format b/tests/test_tap_format index 65b59b5..36015f7 100644 --- a/tests/test_tap_format +++ b/tests/test_tap_format @@ -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 < message on stdout # err> message on stderr -# code:2:test_not_ok()\ -" \ +# code:2:test_not_ok() +1..1" \ "$(bash_unit_out_for_code <&2" @@ -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 <