From 43d1972b0e02ca4ac8d211350200628d08929997 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 28 Dec 2011 21:20:43 -0600 Subject: [PATCH] Test (and fix) that teardown runs once after each test --- libexec/bats-exec | 8 +++++++- test/bats.bats | 9 +++++++++ test/fixtures/setup.bats | 10 ++++++---- test/fixtures/teardown.bats | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/teardown.bats diff --git a/libexec/bats-exec b/libexec/bats-exec index 1721fb9..d5eae1c 100755 --- a/libexec/bats-exec +++ b/libexec/bats-exec @@ -52,6 +52,12 @@ bats_test_function() { BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name" } +bats_teardown_trap() { + trap bats_exit_trap err exit + teardown + bats_exit_trap +} + bats_exit_trap() { trap - err exit if [ -z "$BATS_TEST_COMPLETED" ]; then @@ -84,7 +90,7 @@ bats_perform_test() { fi BATS_TEST_COMPLETED="" - trap bats_exit_trap err exit + trap bats_teardown_trap err exit setup "$BATS_TEST_NAME" BATS_TEST_COMPLETED=1 diff --git a/test/bats.bats b/test/bats.bats index 98426d8..99888ad 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -55,6 +55,15 @@ teardown() { @test "setup is run once before each test" { rm -f "$TMP/setup.log" run bats "$FIXTURE_ROOT/setup.bats" + [ $status -eq 0 ] run cat "$TMP/setup.log" [ ${#lines[@]} -eq 3 ] } + +@test "teardown is run once after each test, even if it fails" { + rm -f "$TMP/teardown.log" + run bats "$FIXTURE_ROOT/teardown.bats" + [ $status -eq 1 ] + run cat "$TMP/teardown.log" + [ ${#lines[@]} -eq 3 ] +} diff --git a/test/fixtures/setup.bats b/test/fixtures/setup.bats index 0b8ebe7..3cc52cc 100644 --- a/test/fixtures/setup.bats +++ b/test/fixtures/setup.bats @@ -1,15 +1,17 @@ +LOG="$TMP/setup.log" + setup() { - echo "$BATS_TEST_NAME" >> "$TMP/setup.log" + echo "$BATS_TEST_NAME" >> "$LOG" } @test "one" { - true + [ "$(tail -n 1 "$LOG")" = "test_one" ] } @test "two" { - true + [ "$(tail -n 1 "$LOG")" = "test_two" ] } @test "three" { - true + [ "$(tail -n 1 "$LOG")" = "test_three" ] } diff --git a/test/fixtures/teardown.bats b/test/fixtures/teardown.bats new file mode 100644 index 0000000..678e0f7 --- /dev/null +++ b/test/fixtures/teardown.bats @@ -0,0 +1,17 @@ +LOG="$TMP/teardown.log" + +teardown() { + echo "$BATS_TEST_NAME" >> "$LOG" +} + +@test "one" { + true +} + +@test "two" { + false +} + +@test "three" { + true +}