From 6b965e18c4054b290febb4f28d7bef2850f2453e Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 8 Apr 2012 23:25:19 -0500 Subject: [PATCH] `bats -c filename` prints the number of tests in the file --- libexec/bats | 10 ++++++++-- libexec/bats-exec | 13 ++++++++++++- test/bats.bats | 10 ++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/libexec/bats b/libexec/bats index 67f9ab3..080bcb5 100755 --- a/libexec/bats +++ b/libexec/bats @@ -40,14 +40,20 @@ if [ "$1" = "-v" ] || [ "$1" = "--version" ]; then exit 0 fi +count_only="" +if [ "$1" = "-c" ]; then + count_only="-c" + shift +fi + filename="$1" if [ -z "$filename" ]; then { version - echo "usage: $0 " + echo "usage: $0 [-c] " } >&2 exit 1 else shift fi -exec bats-exec "$(expand_path "$filename")" "$@" +exec bats-exec $count_only "$(expand_path "$filename")" "$@" diff --git a/libexec/bats-exec b/libexec/bats-exec index b7bed27..278fedc 100755 --- a/libexec/bats-exec +++ b/libexec/bats-exec @@ -1,6 +1,12 @@ #!/usr/bin/env bash set -e +BATS_COUNT_ONLY="" +if [ "$1" = "-c" ]; then + BATS_COUNT_ONLY=1 + shift +fi + BATS_TEST_FILENAME="$1" if [ -z "$BATS_TEST_FILENAME" ]; then echo "usage: bats-exec " >&2 @@ -136,7 +142,12 @@ exec 3<&1 if [ "$#" -eq 0 ]; then bats_preprocess_source bats_evaluate_preprocessed_source - bats_perform_tests "${BATS_TEST_NAMES[@]}" + + if [ -n "$BATS_COUNT_ONLY" ]; then + echo "${#BATS_TEST_NAMES[@]}" + else + bats_perform_tests "${BATS_TEST_NAMES[@]}" + fi else bats_evaluate_preprocessed_source bats_perform_test "$@" diff --git a/test/bats.bats b/test/bats.bats index f230ab3..e95c2d5 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -86,3 +86,13 @@ teardown() { [ "${lines[5]}" = " failure stdout 2" ] [ "${lines[7]}" = " failure stderr" ] } + +@test "-c prints the number of tests" { + run bats -c "$FIXTURE_ROOT/empty.bats" + [ $status -eq 0 ] + [ "$output" = "0" ] + + run bats -c "$FIXTURE_ROOT/output.bats" + [ $status -eq 0 ] + [ "$output" = "4" ] +}