From 65e7178f42d84f2f9d4d8a02a90c8942a368136c Mon Sep 17 00:00:00 2001 From: Neil Davies <36968336+ndavies-om1@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:03:42 -0400 Subject: [PATCH 1/8] Adding plan to TAP output as per tap specs --- bash_unit | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/bash_unit b/bash_unit index 4699fcc..763ed56 100755 --- a/bash_unit +++ b/bash_unit @@ -33,7 +33,8 @@ CAT="$(type -P cat)" SED="$(type -P sed)" GREP="$(type -P grep)" RM="$(type -P rm)" -SHUF="$(type -P sort) -R" +SHUF="$(type -P shuf)" +TEMPFILE=$$.tmp fail() { local message=${1:-} @@ -185,16 +186,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 @@ -263,6 +254,7 @@ run_tests() { local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)" fi + test_count=$(cat $TEMPFILE) for test in $tests_to_run do ( @@ -273,7 +265,10 @@ run_tests() { exit $status ) failure=$(( $? || failure)) + ((test_count++)) done + echo $test_count > $TEMPFILE + return $failure } @@ -455,10 +450,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 +474,16 @@ 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 +} + output_format=text verbosity=normal test_pattern="" @@ -540,6 +547,7 @@ fi #run tests received as parameters failure=0 +echo 0 > $TEMPFILE for test_file in "$@" do notify_suite_starting "$test_file" @@ -562,12 +570,13 @@ do ) failure=$(( $? || failure)) done - + if ((failure)) then - notify_suites_failed + notify_suites_failed $(cat $TEMPFILE) else - notify_suites_succeded + notify_suites_succeded $(cat $TEMPFILE) fi +unlink $TEMPFILE exit $failure From 71763c192af854b893697a30dff836a581a5f520 Mon Sep 17 00:00:00 2001 From: Neil Davies <36968336+ndavies-om1@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:05:35 -0400 Subject: [PATCH 2/8] SHUF --- bash_unit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_unit b/bash_unit index 763ed56..085fbc7 100755 --- a/bash_unit +++ b/bash_unit @@ -33,7 +33,7 @@ CAT="$(type -P cat)" SED="$(type -P sed)" GREP="$(type -P grep)" RM="$(type -P rm)" -SHUF="$(type -P shuf)" +SHUF="$(type -P sort) -R" TEMPFILE=$$.tmp fail() { From cab26fcd743b05bfd9770346b7d25af98e485cdf Mon Sep 17 00:00:00 2001 From: ndavies-om1 Date: Tue, 29 Oct 2024 17:04:57 -0400 Subject: [PATCH 3/8] fixing tests --- bash_unit | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bash_unit b/bash_unit index 085fbc7..8e746ae 100755 --- a/bash_unit +++ b/bash_unit @@ -34,7 +34,7 @@ SED="$(type -P sed)" GREP="$(type -P grep)" RM="$(type -P rm)" SHUF="$(type -P sort) -R" -TEMPFILE=$$.tmp +TEMPFILE="$(pwd)/$$.tmp" fail() { local message=${1:-} @@ -267,7 +267,7 @@ run_tests() { failure=$(( $? || failure)) ((test_count++)) done - echo $test_count > $TEMPFILE + echo "${test_count}" > $TEMPFILE return $failure } @@ -573,9 +573,9 @@ done if ((failure)) then - notify_suites_failed $(cat $TEMPFILE) + notify_suites_failed $(cat "${TEMPFILE}") else - notify_suites_succeded $(cat $TEMPFILE) + notify_suites_succeded $(cat "${TEMPFILE}") fi unlink $TEMPFILE From 1b5c0c788916d8691cbd7f11d704e0701d6aab42 Mon Sep 17 00:00:00 2001 From: ndavies-om1 Date: Wed, 6 Nov 2024 08:53:12 -0500 Subject: [PATCH 4/8] further cleanup --- bash_unit | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bash_unit b/bash_unit index 8e746ae..2454c7f 100755 --- a/bash_unit +++ b/bash_unit @@ -254,7 +254,7 @@ run_tests() { local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)" fi - test_count=$(cat $TEMPFILE) + test_count=$(cat "${TEMPFILE}") for test in $tests_to_run do ( @@ -267,7 +267,7 @@ run_tests() { failure=$(( $? || failure)) ((test_count++)) done - echo "${test_count}" > $TEMPFILE + echo "${test_count}" > "${TEMPFILE}" return $failure } @@ -547,7 +547,7 @@ fi #run tests received as parameters failure=0 -echo 0 > $TEMPFILE +echo 0 > "${TEMPFILE}" for test_file in "$@" do notify_suite_starting "$test_file" From d5df1ff281f85dc0f45c04642b532e97d9627806 Mon Sep 17 00:00:00 2001 From: ndavies-om1 Date: Wed, 6 Nov 2024 09:02:49 -0500 Subject: [PATCH 5/8] fixing tap tests --- tests/test_tap_format | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/test_tap_format b/tests/test_tap_format index 65b59b5..e7d641e 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 +74,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 < Date: Fri, 15 Nov 2024 14:41:31 +0100 Subject: [PATCH 6/8] FIX tap documentation --- README.adoc | 1 + 1 file changed, 1 insertion(+) 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 From aa69431a4a9fa346315652bcc2bd9bc8ba65af29 Mon Sep 17 00:00:00 2001 From: Pascal Grange Date: Fri, 15 Nov 2024 15:30:26 +0100 Subject: [PATCH 7/8] FIX tap plan should include skipped and pending tests According to this example extracted from tap documentation, the skipped and pending tests should be included in the plan: ``` TAP version 14 1..2 ok 1 - do it later # Skipped ok 2 - works on windows # Skipped: only run on windows ``` --- bash_unit | 50 +++++++++++++++++++++++++++---------------- tests/test_tap_format | 18 +++++++++++++++- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/bash_unit b/bash_unit index 2454c7f..42b6ea7 100755 --- a/bash_unit +++ b/bash_unit @@ -236,25 +236,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 - test_count=$(cat "${TEMPFILE}") + local test_count=$(cat "${TEMPFILE}") + test_count=$((test_count + $(count "$pending_tests") + $(count "$tests_to_run") + $(count "$skipped_tests"))) + echo "${test_count}" > "${TEMPFILE}" + + 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 ( @@ -265,9 +272,7 @@ run_tests() { exit $status ) failure=$(( $? || failure)) - ((test_count++)) done - echo "${test_count}" > "${TEMPFILE}" return $failure } @@ -484,6 +489,15 @@ skip_if() { fi } +count() { + local tests="$1" + if [[ -z "$tests" ]]; then + echo 0 + else + echo "$tests" | wc -l + fi +} + output_format=text verbosity=normal test_pattern="" @@ -570,13 +584,13 @@ do ) failure=$(( $? || failure)) done - + if ((failure)) then - notify_suites_failed $(cat "${TEMPFILE}") + notify_suites_failed "$(cat "${TEMPFILE}")" else - notify_suites_succeded $(cat "${TEMPFILE}") + notify_suites_succeded "$(cat "${TEMPFILE}")" fi -unlink $TEMPFILE +unlink "$TEMPFILE" exit $failure diff --git a/tests/test_tap_format b/tests/test_tap_format index e7d641e..36015f7 100644 --- a/tests/test_tap_format +++ b/tests/test_tap_format @@ -42,7 +42,7 @@ test_tap_format_for_one_pending_test() { "\ # Running tests in code ok - pending_not_yet_implemented # todo test to be written -1..0" \ +1..1" \ "$(bash_unit_out_for_code < Date: Fri, 15 Nov 2024 16:37:40 +0100 Subject: [PATCH 8/8] Refactor test count file Give the variable an explicit name. Use mktemp for better portability. Use trap to ensure we remove this file at the end, whatever happens. --- bash_unit | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/bash_unit b/bash_unit index 42b6ea7..37d84a3 100755 --- a/bash_unit +++ b/bash_unit @@ -34,7 +34,13 @@ SED="$(type -P sed)" GREP="$(type -P grep)" RM="$(type -P rm)" SHUF="$(type -P sort) -R" -TEMPFILE="$(pwd)/$$.tmp" + +# 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:-} @@ -246,9 +252,9 @@ run_tests() { local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)" fi - local test_count=$(cat "${TEMPFILE}") + local test_count=$(cat "${TEST_COUNT_FILE}") test_count=$((test_count + $(count "$pending_tests") + $(count "$tests_to_run") + $(count "$skipped_tests"))) - echo "${test_count}" > "${TEMPFILE}" + echo "${test_count}" > "${TEST_COUNT_FILE}" for pending_test in $pending_tests do @@ -561,7 +567,7 @@ fi #run tests received as parameters failure=0 -echo 0 > "${TEMPFILE}" +echo 0 > "${TEST_COUNT_FILE}" for test_file in "$@" do notify_suite_starting "$test_file" @@ -587,10 +593,9 @@ done if ((failure)) then - notify_suites_failed "$(cat "${TEMPFILE}")" + notify_suites_failed "$(cat "${TEST_COUNT_FILE}")" else - notify_suites_succeded "$(cat "${TEMPFILE}")" + notify_suites_succeded "$(cat "${TEST_COUNT_FILE}")" fi -unlink "$TEMPFILE" exit $failure