Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

TAP: todo, diag helpers and more diagnostic output in TAP format. #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions libexec/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
20 changes: 19 additions & 1 deletion libexec/bats-format-tap-stream
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions new-tests.bats
Original file line number Diff line number Diff line change
@@ -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"
}

13 changes: 13 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down Expand Up @@ -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 ]
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/bats/passing_and_todo.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@test "a passing test" {
true
}

@test "a todo test" {
TODO
}

8 changes: 8 additions & 0 deletions test/fixtures/bats/todos.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@test "a todo test" {
TODO
}

@test "a todo test with a reason" {
TODO "a reason"
}