From 19a05cc77df1a5de54074b7f2eaa4b56dc9f8e19 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Fri, 16 Nov 2012 14:25:45 -0600 Subject: [PATCH] Suite support for aggregating multiple tests under a single run --- libexec/bats | 7 ++- libexec/bats-exec-suite | 45 +++++++++++++++++++ test/bats.bats | 8 +--- test/fixtures/{ => bats}/empty.bats | 0 test/fixtures/{ => bats}/environment.bats | 0 test/fixtures/{ => bats}/failing.bats | 0 .../{ => bats}/failing_and_passing.bats | 0 test/fixtures/{ => bats}/load.bats | 0 test/fixtures/{ => bats}/output.bats | 0 test/fixtures/{ => bats}/passing.bats | 0 test/fixtures/{ => bats}/setup.bats | 0 test/fixtures/{ => bats}/teardown.bats | 0 test/fixtures/{ => bats}/test_helper.bash | 0 test/fixtures/suite/multiple/a.bats | 3 ++ test/fixtures/suite/multiple/b.bats | 7 +++ test/fixtures/suite/single/test.bats | 3 ++ test/suite.bats | 43 ++++++++++++++++++ test/test_helper.bash | 11 +++++ 18 files changed, 120 insertions(+), 7 deletions(-) create mode 100755 libexec/bats-exec-suite rename test/fixtures/{ => bats}/empty.bats (100%) rename test/fixtures/{ => bats}/environment.bats (100%) rename test/fixtures/{ => bats}/failing.bats (100%) rename test/fixtures/{ => bats}/failing_and_passing.bats (100%) rename test/fixtures/{ => bats}/load.bats (100%) rename test/fixtures/{ => bats}/output.bats (100%) rename test/fixtures/{ => bats}/passing.bats (100%) rename test/fixtures/{ => bats}/setup.bats (100%) rename test/fixtures/{ => bats}/teardown.bats (100%) rename test/fixtures/{ => bats}/test_helper.bash (100%) create mode 100644 test/fixtures/suite/multiple/a.bats create mode 100644 test/fixtures/suite/multiple/b.bats create mode 100644 test/fixtures/suite/single/test.bats create mode 100644 test/suite.bats create mode 100644 test/test_helper.bash diff --git a/libexec/bats b/libexec/bats index 160e44b..cb51fc9 100755 --- a/libexec/bats +++ b/libexec/bats @@ -56,4 +56,9 @@ else shift fi -exec bats-exec-test $count_only "$(expand_path "$filename")" "$@" +if [ -d "$filename" ]; then + shopt -s nullglob + exec bats-exec-suite $count_only "$(expand_path "$filename")"/*.bats +else + exec bats-exec-test $count_only "$(expand_path "$filename")" "$@" +fi diff --git a/libexec/bats-exec-suite b/libexec/bats-exec-suite new file mode 100755 index 0000000..e5389ad --- /dev/null +++ b/libexec/bats-exec-suite @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -e + +count_only="" +if [ "$1" = "-c" ]; then + count_only=1 + shift +fi + +trap "kill 0; exit 1" int + +count=0 +for filename in "$@"; do + count=$(($count + $(bats-exec-test -c "$filename"))) +done + +if [ -n "$count_only" ]; then + echo "$count" + exit +fi + +echo "1..$count" +status=0 +offset=0 +for filename in "$@"; do + index=0 + { + IFS= read -r # 1..n + while IFS= read -r line; do + case "$line" in + "ok "* | "not ok "* ) + index=$(($index + 1)) + echo "${line/ $index / $(($offset + $index)) }" + [ "${line:0:6}" != "not ok" ] || status=1 + ;; + * ) + echo "$line" + ;; + esac + done + } < <( bats-exec-test "$filename" ) + offset=$(($offset + $index)) +done + +exit "$status" diff --git a/test/bats.bats b/test/bats.bats index e95c2d5..84d3d7c 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -1,11 +1,7 @@ #!/usr/bin/env bats -FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures" -export TMP="$BATS_TEST_DIRNAME/tmp" - -teardown() { - rm -f "$TMP"/* -} +load test_helper +fixtures bats @test "no arguments prints usage instructions" { run bats diff --git a/test/fixtures/empty.bats b/test/fixtures/bats/empty.bats similarity index 100% rename from test/fixtures/empty.bats rename to test/fixtures/bats/empty.bats diff --git a/test/fixtures/environment.bats b/test/fixtures/bats/environment.bats similarity index 100% rename from test/fixtures/environment.bats rename to test/fixtures/bats/environment.bats diff --git a/test/fixtures/failing.bats b/test/fixtures/bats/failing.bats similarity index 100% rename from test/fixtures/failing.bats rename to test/fixtures/bats/failing.bats diff --git a/test/fixtures/failing_and_passing.bats b/test/fixtures/bats/failing_and_passing.bats similarity index 100% rename from test/fixtures/failing_and_passing.bats rename to test/fixtures/bats/failing_and_passing.bats diff --git a/test/fixtures/load.bats b/test/fixtures/bats/load.bats similarity index 100% rename from test/fixtures/load.bats rename to test/fixtures/bats/load.bats diff --git a/test/fixtures/output.bats b/test/fixtures/bats/output.bats similarity index 100% rename from test/fixtures/output.bats rename to test/fixtures/bats/output.bats diff --git a/test/fixtures/passing.bats b/test/fixtures/bats/passing.bats similarity index 100% rename from test/fixtures/passing.bats rename to test/fixtures/bats/passing.bats diff --git a/test/fixtures/setup.bats b/test/fixtures/bats/setup.bats similarity index 100% rename from test/fixtures/setup.bats rename to test/fixtures/bats/setup.bats diff --git a/test/fixtures/teardown.bats b/test/fixtures/bats/teardown.bats similarity index 100% rename from test/fixtures/teardown.bats rename to test/fixtures/bats/teardown.bats diff --git a/test/fixtures/test_helper.bash b/test/fixtures/bats/test_helper.bash similarity index 100% rename from test/fixtures/test_helper.bash rename to test/fixtures/bats/test_helper.bash diff --git a/test/fixtures/suite/multiple/a.bats b/test/fixtures/suite/multiple/a.bats new file mode 100644 index 0000000..fbc1f38 --- /dev/null +++ b/test/fixtures/suite/multiple/a.bats @@ -0,0 +1,3 @@ +@test "truth" { + true +} diff --git a/test/fixtures/suite/multiple/b.bats b/test/fixtures/suite/multiple/b.bats new file mode 100644 index 0000000..bb965a4 --- /dev/null +++ b/test/fixtures/suite/multiple/b.bats @@ -0,0 +1,7 @@ +@test "more truth" { + true +} + +@test "quasi-truth" { + [ -z "$FLUNK" ] +} diff --git a/test/fixtures/suite/single/test.bats b/test/fixtures/suite/single/test.bats new file mode 100644 index 0000000..e8182ce --- /dev/null +++ b/test/fixtures/suite/single/test.bats @@ -0,0 +1,3 @@ +@test "a passing test" { + true +} diff --git a/test/suite.bats b/test/suite.bats new file mode 100644 index 0000000..5d5132f --- /dev/null +++ b/test/suite.bats @@ -0,0 +1,43 @@ +#!/usr/bin/env bats + +load test_helper +fixtures suite + +@test "running a suite with no test files" { + run bats "$FIXTURE_ROOT/empty" + [ $status -eq 0 ] + [ $output = "1..0" ] +} + +@test "running a suite with one test file" { + run bats "$FIXTURE_ROOT/single" + [ $status -eq 0 ] + [ ${lines[0]} = "1..1" ] + [ ${lines[1]} = "ok 1 a passing test" ] +} + +@test "counting tests in a suite" { + run bats -c "$FIXTURE_ROOT/single" + [ $status -eq 0 ] + [ $output -eq 1 ] + + run bats -c "$FIXTURE_ROOT/multiple" + [ $status -eq 0 ] + [ $output -eq 3 ] +} + +@test "aggregated output of multiple tests in a suite" { + run bats "$FIXTURE_ROOT/multiple" + [ $status -eq 0 ] + [ ${lines[0]} = "1..3" ] + echo "$output" | grep "^ok . truth" + echo "$output" | grep "^ok . more truth" + echo "$output" | grep "^ok . quasi-truth" +} + +@test "a failing test in a suite results in an error exit code" { + FLUNK=1 run bats "$FIXTURE_ROOT/multiple" + [ $status -eq 1 ] + [ ${lines[0]} = "1..3" ] + echo "$output" | grep "^not ok . quasi-truth" +} diff --git a/test/test_helper.bash b/test/test_helper.bash new file mode 100644 index 0000000..9e10401 --- /dev/null +++ b/test/test_helper.bash @@ -0,0 +1,11 @@ +fixtures() { + FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/$1" +} + +setup() { + export TMP="$BATS_TEST_DIRNAME/tmp" +} + +teardown() { + rm -f "$TMP"/* +}