From 8873aab79f3a22657554ec42e7ad046fb4aebe2b Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Mon, 21 Oct 2013 11:32:09 -0500 Subject: [PATCH] Extended syntax: "begin" line before each test is run --- libexec/bats-exec-suite | 20 +++++++++++++++----- libexec/bats-exec-test | 11 ++++++++++- test/bats.bats | 9 +++++++++ test/suite.bats | 12 ++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/libexec/bats-exec-suite b/libexec/bats-exec-suite index e5389ad..96de2a5 100755 --- a/libexec/bats-exec-suite +++ b/libexec/bats-exec-suite @@ -1,9 +1,15 @@ #!/usr/bin/env bash set -e -count_only="" +count_only_flag="" if [ "$1" = "-c" ]; then - count_only=1 + count_only_flag=1 + shift +fi + +extended_syntax_flag="" +if [ "$1" = "-x" ]; then + extended_syntax_flag="-x" shift fi @@ -14,7 +20,7 @@ for filename in "$@"; do count=$(($count + $(bats-exec-test -c "$filename"))) done -if [ -n "$count_only" ]; then +if [ -n "$count_only_flag" ]; then echo "$count" exit fi @@ -28,9 +34,13 @@ for filename in "$@"; do IFS= read -r # 1..n while IFS= read -r line; do case "$line" in - "ok "* | "not ok "* ) + "begin "* ) index=$(($index + 1)) echo "${line/ $index / $(($offset + $index)) }" + ;; + "ok "* | "not ok "* ) + [ -n "$extended_syntax_flag" ] || index=$(($index + 1)) + echo "${line/ $index / $(($offset + $index)) }" [ "${line:0:6}" != "not ok" ] || status=1 ;; * ) @@ -38,7 +48,7 @@ for filename in "$@"; do ;; esac done - } < <( bats-exec-test "$filename" ) + } < <( bats-exec-test $extended_syntax_flag "$filename" ) offset=$(($offset + $index)) done diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index f047892..13de84f 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -9,6 +9,12 @@ if [ "$1" = "-c" ]; then shift fi +BATS_EXTENDED_SYNTAX="" +if [ "$1" = "-x" ]; then + BATS_EXTENDED_SYNTAX="$1" + shift +fi + BATS_TEST_FILENAME="$1" if [ -z "$BATS_TEST_FILENAME" ]; then echo "usage: bats-exec " >&2 @@ -65,6 +71,9 @@ skip() { bats_test_begin() { BATS_TEST_DESCRIPTION="$1" + if [ -n "$BATS_EXTENDED_SYNTAX" ]; then + echo "begin $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 + fi setup } @@ -122,7 +131,7 @@ bats_perform_tests() { test_number=1 status=0 for test_name in "$@"; do - "$0" "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 + "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 test_number=$(($test_number + 1)) done exit "$status" diff --git a/test/bats.bats b/test/bats.bats index e636ca9..f794a74 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -129,3 +129,12 @@ fixtures bats [ "${lines[1]}" = "ok 1 # skip a skipped test" ] [ "${lines[2]}" = "ok 2 # skip (a reason) a skipped test with a reason" ] } + +@test "extended syntax" { + run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats" + [ $status -eq 1 ] + [ "${lines[1]}" = "begin 1 a failing test" ] + [ "${lines[2]}" = "not ok 1 a failing test" ] + [ "${lines[4]}" = "begin 2 a passing test" ] + [ "${lines[5]}" = "ok 2 a passing test" ] +} diff --git a/test/suite.bats b/test/suite.bats index 87f61d5..14f5008 100755 --- a/test/suite.bats +++ b/test/suite.bats @@ -50,3 +50,15 @@ fixtures suite echo "$output" | grep "^ok . more truth" echo "$output" | grep "^ok . quasi-truth" } + +@test "extended syntax in suite" { + FLUNK=1 run bats-exec-suite -x "$FIXTURE_ROOT/multiple/"*.bats + [ $status -eq 1 ] + [ "${lines[0]}" = "1..3" ] + [ "${lines[1]}" = "begin 1 truth" ] + [ "${lines[2]}" = "ok 1 truth" ] + [ "${lines[3]}" = "begin 2 more truth" ] + [ "${lines[4]}" = "ok 2 more truth" ] + [ "${lines[5]}" = "begin 3 quasi-truth" ] + [ "${lines[6]}" = "not ok 3 quasi-truth" ] +}