diff --git a/libexec/bats b/libexec/bats index 71f392f7..0aea301c 100755 --- a/libexec/bats +++ b/libexec/bats @@ -20,6 +20,8 @@ help() { echo " -h, --help Display this help message" echo " -p, --pretty Show results in pretty format (default for terminals)" echo " -t, --tap Show results in TAP format" + echo " -x, --extended Show results in extended TAP format" + echo " -u, --junit Show results in JUnit format, hudson compatible" echo " -v, --version Display the version number" echo echo " For more information, see https://github.com/sstephenson/bats" @@ -52,9 +54,9 @@ expand_path() { } || echo "$1" } -BATS_LIBEXEC="$(abs_dirname "$0")" -export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")" -export BATS_CWD="$(abs_dirname .)" +BATS_LIBEXEC="${BATS_LIBEXEC-$(abs_dirname "$0")}" +export BATS_PREFIX="${BATS_PREFIX-$(abs_dirname "$BATS_LIBEXEC")}" +export BATS_CWD="${BATS_CWD-$(abs_dirname .)}" export PATH="$BATS_LIBEXEC:$PATH" options=() @@ -93,12 +95,19 @@ for option in "${options[@]}"; do "c" | "count" ) count_flag="-c" ;; + "x" | "extended" ) + pretty="" + extended_syntax_flag="-x" + ;; "t" | "tap" ) pretty="" ;; "p" | "pretty" ) pretty="1" ;; + "u" | "junit" ) + junit="1" + ;; * ) usage >&2 exit 1 @@ -130,11 +139,13 @@ else command="bats-exec-suite" fi -if [ -n "$pretty" ]; then +if [ -n "$junit" ]; then + extended_syntax_flag="-x" + formatter="bats-format-junit" +elif [ -n "$pretty" ]; then extended_syntax_flag="-x" formatter="bats-format-tap-stream" else - extended_syntax_flag="" formatter="cat" fi diff --git a/libexec/bats-exec-suite b/libexec/bats-exec-suite index 29ab255d..b75450fe 100755 --- a/libexec/bats-exec-suite +++ b/libexec/bats-exec-suite @@ -30,6 +30,9 @@ status=0 offset=0 for filename in "$@"; do index=0 + if test -n "$extended_syntax_flag"; then + echo suite bats.$(basename "$filename"| sed -e 's/\.bats//') + fi { IFS= read -r # 1..n while IFS= read -r line; do diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 8f3bd510..ca407c56 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -81,6 +81,7 @@ skip() { bats_test_begin() { BATS_TEST_DESCRIPTION="$1" + BATS_TEST_START_TIME=${SECONDS} if [ -n "$BATS_EXTENDED_SYNTAX" ]; then echo "begin $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 fi @@ -252,14 +253,20 @@ bats_exit_trap() { fi fi + if [ -n "${skipped}" ] || [ -z "$BATS_EXTENDED_SYNTAX" ]; then + BATS_TEST_TIME="" + else + BATS_TEST_TIME=" in "$((SECONDS - BATS_TEST_START_TIME))"sec" + fi + if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then - echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 + echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION${BATS_TEST_TIME}" >&3 bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3 bats_print_failed_command "${BATS_ERROR_STACK_TRACE[${#BATS_ERROR_STACK_TRACE[@]}-1]}" "$BATS_ERROR_STATUS" >&3 sed -e "s/^/# /" < "$BATS_OUT" >&3 status=1 else - echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3 + echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}${BATS_TEST_TIME}" >&3 status=0 fi diff --git a/libexec/bats-format-junit b/libexec/bats-format-junit new file mode 100755 index 00000000..c491b54a --- /dev/null +++ b/libexec/bats-format-junit @@ -0,0 +1,132 @@ +#!/usr/bin/env bash +set -e + +IFS= read -r header + +index=0 + +init_suite() { + count=0 + failures=0 + skipped=0 + suitetest_exec_time=0 + name="" + _buffer="" +} + +header() { + printf "\ + +\n" +} + +footer() { + printf "\n" +} + + +pass() { + echo " \n" +} + +fail() { + printf "\ + + + \n" +} + +skip() { + echo "\ + + $1 + \n" +} + +buffer() { + _buffer="${_buffer}$("$@")" +} + +flush() { + echo "${_buffer}" + _buffer="" +} + +_buffer_log="" +log() { + _buffer_log="${_buffer_log} +$1" +} + +flush_log() { + if [[ -n "${_buffer_log}" ]]; then + buffer fail "${_buffer_log}" + _buffer_log="" + fi +} + +finish_suite() { + [[ ${count} -gt 0 ]] && { + ( + flush_log + header + flush + footer + ) > "TestReport-${class-case}.xml" + } + init_suite +} + +trap finish_suite EXIT + +while IFS= read -r line; do + case "$line" in + "suite "*) + flush_log + finish_suite + suite_expr="suite (.*)" + if [[ "$line" =~ $suite_expr ]]; then + class="${BASH_REMATCH[1]}" + fi + ;; + "begin "* ) + flush_log + let index+=1 + name="${line#* $index }" + ;; + "ok "* ) + let count+=1 + expr_ok="ok $index .* in ([0-9]+)sec" + expr_skip="ok $index # skip (.*)" + if [[ "$line" =~ $expr_skip ]]; then + let skipped+=1 + test_exec_time=0 + buffer skip "${BASH_REMATCH[1]}" + elif [[ "$line" =~ $expr_ok ]]; then + test_exec_time="${BASH_REMATCH[1]}" + suitetest_exec_time=$((suitetest_exec_time + test_exec_time)) + buffer pass + else + log "Wrong output format: ${line}" + let failures+=1 + fi + ;; + "not ok "* ) + let count+=1 + let failures+=1 + expr_notok="not ok $index .* in ([0-9]+)sec" + if [[ "$line" =~ $expr_notok ]]; then + test_exec_time="${BASH_REMATCH[1]}" + suitetest_exec_time=$((suitetest_exec_time + test_exec_time)) + fi + ;; + "# "* ) + log "${line:2}" + ;; + esac +done + diff --git a/libexec/bats-format-tap-stream b/libexec/bats-format-tap-stream index 614768f4..c5f7a443 100755 --- a/libexec/bats-format-tap-stream +++ b/libexec/bats-format-tap-stream @@ -39,6 +39,7 @@ begin() { pass() { go_to_column 0 + set_color 2 printf " ✓ %s" "$name" advance } diff --git a/test/bats.bats b/test/bats.bats index f1aff293..187a834f 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -150,6 +150,7 @@ fixtures bats } @test "failing test file outside of BATS_CWD" { + unset BATS_LIBEXEC BATS_PREFIX BATS_CWD cd "$TMP" run bats "$FIXTURE_ROOT/failing.bats" [ $status -eq 1 ] @@ -222,9 +223,9 @@ fixtures bats 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[2]}" = 'not ok 1 a failing test in 0sec' ] [ "${lines[5]}" = 'begin 2 a passing test' ] - [ "${lines[6]}" = 'ok 2 a passing test' ] + [ "${lines[6]}" = 'ok 2 a passing test in 0sec' ] } @test "pretty and tap formats" { diff --git a/test/suite.bats b/test/suite.bats index 53716863..fb2b875d 100755 --- a/test/suite.bats +++ b/test/suite.bats @@ -55,10 +55,12 @@ fixtures 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" ] + [ "${lines[1]}" = "suite bats.a" ] + [ "${lines[2]}" = "begin 1 truth" ] + [ "${lines[3]}" = "ok 1 truth in 0sec" ] + [ "${lines[4]}" = "suite bats.b" ] + [ "${lines[5]}" = "begin 2 more truth" ] + [ "${lines[6]}" = "ok 2 more truth in 0sec" ] + [ "${lines[7]}" = "begin 3 quasi-truth" ] + [ "${lines[8]}" = "not ok 3 quasi-truth in 0sec" ] }