From 76fba228944a0138d9d88fa0beb5374de5982c06 Mon Sep 17 00:00:00 2001 From: "B. van Berkum" Date: Sun, 18 Sep 2016 06:43:42 +0200 Subject: [PATCH] TAP: TODO, fail, diag helpers. - New TODO state for tests, with helper TODO/todo. - fail and diag helper routines for easier testing. See Test Anything Protocol. --- libexec/bats-exec-test | 29 ++++++++++++++++++++++++ libexec/bats-format-tap-stream | 20 +++++++++++++++- new-tests.bats | 20 ++++++++++++++++ test/bats.bats | 13 +++++++++++ test/fixtures/bats/passing_and_todo.bats | 8 +++++++ test/fixtures/bats/todos.bats | 8 +++++++ 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 new-tests.bats create mode 100644 test/fixtures/bats/passing_and_todo.bats create mode 100644 test/fixtures/bats/todos.bats diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 8f3bd510..26b6526a 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -79,6 +79,25 @@ skip() { exit 0 } +TODO() { + BATS_TEST_TODO=${1:-1} + BATS_TEST_COMPLETED=1 + exit 0 +} + +diag() { + BATS_TEST_DIAGNOSTICS=1 + echo "$1" >>"$BATS_OUT" +} + +fail() { + if test -n "$1" + then + echo "Failed: $1" >>"$BATS_OUT" + fi + exit 0 +} + bats_test_begin() { BATS_TEST_DESCRIPTION="$1" if [ -n "$BATS_EXTENDED_SYNTAX" ]; then @@ -245,6 +264,12 @@ bats_exit_trap() { trap - err exit skipped="" + if [ -n "$BATS_TEST_TODO" ]; then + skipped=" # TODO" + if [ "1" != "$BATS_TEST_TODO" ]; then + skipped+=" ($BATS_TEST_TODO)" + fi + fi if [ -n "$BATS_TEST_SKIPPED" ]; then skipped=" # skip" if [ "1" != "$BATS_TEST_SKIPPED" ]; then @@ -260,6 +285,10 @@ bats_exit_trap() { status=1 else echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3 + if test -n "$BATS_TEST_DIAGNOSTICS" -a -s "$BATS_OUT" + then + sed -e "s/^/# /" < "$BATS_OUT" >&3 + fi status=0 fi diff --git a/libexec/bats-format-tap-stream b/libexec/bats-format-tap-stream index 614768f4..8c09f983 100755 --- a/libexec/bats-format-tap-stream +++ b/libexec/bats-format-tap-stream @@ -12,6 +12,7 @@ if [[ "$header" =~ $header_pattern ]]; then index=0 failures=0 skipped=0 + todo=0 name="" count_column_width=$(( ${#count} * 2 + 2 )) else @@ -51,6 +52,14 @@ skip() { advance } +todo() { + local reason="$1" + [ -z "$reason" ] || reason=": $reason" + go_to_column 0 + printf " - %s (todo%s)" "$name" "$reason" + advance +} + fail() { go_to_column 0 set_color 1 bold @@ -69,6 +78,10 @@ summary() { printf ", %d failure%s" "$failures" "$(plural "$failures")" + if [ "$todo" -gt 0 ]; then + printf ", %d todo" "$todo" + fi + if [ "$skipped" -gt 0 ]; then printf ", %d skipped" "$skipped" fi @@ -144,13 +157,18 @@ while IFS= read -r line; do flush ;; "ok "* ) + todo_expr="ok $index # TODO (\(([^)]*)\))?" + if [[ "$line" =~ $todo_expr ]]; then + let todo+=1 + buffer todo "${BASH_REMATCH[2]}" + else skip_expr="ok $index # skip (\(([^)]*)\))?" if [[ "$line" =~ $skip_expr ]]; then let skipped+=1 buffer skip "${BASH_REMATCH[2]}" else buffer pass - fi + fi; fi ;; "not ok "* ) let failures+=1 diff --git a/new-tests.bats b/new-tests.bats new file mode 100644 index 00000000..a8c2d292 --- /dev/null +++ b/new-tests.bats @@ -0,0 +1,20 @@ + +@test "test diag" { + diag "Foo" +} + +@test "test diag fail" { + diag "...." + fail "Reasons" +} + +@test "test diag todo" { + diag "...." + TODO "Foo" +} + +@test "test diag skip" { + diag "...." + skip "Foo" +} + diff --git a/test/bats.bats b/test/bats.bats index f1aff293..0dc2501f 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -52,6 +52,12 @@ fixtures bats [ "${lines[2]}" = "2 tests, 0 failures, 1 skipped" ] } +@test "summary passing and todo tests" { + run filter_control_sequences bats -p $FIXTURE_ROOT/passing_and_todo.bats + [ $status -eq 0 ] + [ "${lines[2]}" = "2 tests, 0 failures, 1 todo" ] +} + @test "summary passing and failing tests" { run filter_control_sequences bats -p $FIXTURE_ROOT/failing_and_passing.bats [ $status -eq 0 ] @@ -218,6 +224,13 @@ fixtures bats [ "${lines[2]}" = "ok 2 # skip (a reason) a skipped test with a reason" ] } +@test "todo tests" { + run bats "$FIXTURE_ROOT/todos.bats" + [ $status -eq 0 ] + [ "${lines[1]}" = "ok 1 # TODO a todo test" ] + [ "${lines[2]}" = "ok 2 # TODO (a reason) a todo test with a reason" ] +} + @test "extended syntax" { run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats" [ $status -eq 1 ] diff --git a/test/fixtures/bats/passing_and_todo.bats b/test/fixtures/bats/passing_and_todo.bats new file mode 100644 index 00000000..d9baab16 --- /dev/null +++ b/test/fixtures/bats/passing_and_todo.bats @@ -0,0 +1,8 @@ +@test "a passing test" { + true +} + +@test "a todo test" { + TODO +} + diff --git a/test/fixtures/bats/todos.bats b/test/fixtures/bats/todos.bats new file mode 100644 index 00000000..c3923dca --- /dev/null +++ b/test/fixtures/bats/todos.bats @@ -0,0 +1,8 @@ +@test "a todo test" { + TODO +} + +@test "a todo test with a reason" { + TODO "a reason" +} +