From 93d6a43dae463860da0c7740941e9f96ced1e79c Mon Sep 17 00:00:00 2001 From: Spike Grobstein Date: Sat, 31 Aug 2013 10:08:59 -0400 Subject: [PATCH] added decho function for debugging output if DEBUG is defined, then when decho is called, it will output the given string to the console, prefixed with '# DEBUG: ' This allows easier debugging of tests but still will conditionally output said output. --- libexec/bats-exec-test | 7 +++++++ test/bats.bats | 13 +++++++++++++ test/fixtures/bats/debug.bats | 6 ++++++ 3 files changed, 26 insertions(+) create mode 100644 test/fixtures/bats/debug.bats diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index d4c21b3..20d10f8 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -23,6 +23,13 @@ fi BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")" BATS_TEST_NAMES=() +# if DEBUG is set, write the given string to the console. +decho() { + if [[ -n "$DEBUG" ]]; then + echo "# DEBUG: " $@ >&3 + fi +} + load() { local filename="$BATS_TEST_DIRNAME/$1.bash" [ -f "$filename" ] || { diff --git a/test/bats.bats b/test/bats.bats index fe24cd7..f6ec532 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -110,3 +110,16 @@ fixtures bats run bats "$FIXTURE_ROOT/dos_line.bats" [ $status -eq 0 ] } + +@test "test should not output DEBUG line if DEBUG is not set" { + run bats "$FIXTURE_ROOT/debug.bats" + [ $status -eq 0 ] + [[ ! $(echo $output | grep DEBUG) ]] +} + +@test "test should output DEBUG line if DEBUG is set" { + export DEBUG=1 + run bats "$FIXTURE_ROOT/debug.bats" + [ $status -eq 0 ] + echo $output | grep DEBUG +} diff --git a/test/fixtures/bats/debug.bats b/test/fixtures/bats/debug.bats new file mode 100644 index 0000000..d32d3a4 --- /dev/null +++ b/test/fixtures/bats/debug.bats @@ -0,0 +1,6 @@ +#!/usr/bin/env bats + +@test "this test contains decho call" { + decho "testing decho" + [[ 1 -eq 1 ]] +}