From efd492cb1e2934fc365563fcde8b1aced56c6bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Combes?= Date: Tue, 12 Sep 2017 14:24:38 +0200 Subject: [PATCH 1/2] test(run): Add test for separate output and error --- test/bats.bats | 5 +++++ test/fixtures/bats/stdout_stderr_separate.bats | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/fixtures/bats/stdout_stderr_separate.bats diff --git a/test/bats.bats b/test/bats.bats index f1aff29..32ae3d1 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -262,3 +262,8 @@ fixtures bats [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 loop_func" ] } + +@test "testing stdout and stderr are separated" { + run bats "$FIXTURE_ROOT/stdout_stderr_separate.bats" + [ $status -eq 0 ] +} \ No newline at end of file diff --git a/test/fixtures/bats/stdout_stderr_separate.bats b/test/fixtures/bats/stdout_stderr_separate.bats new file mode 100644 index 0000000..ae83b31 --- /dev/null +++ b/test/fixtures/bats/stdout_stderr_separate.bats @@ -0,0 +1,14 @@ +# see issue #89 +echo_std_err() { + echo "std output" + (>&2 echo "err output") + return 0 +} + +@test "std err" { + run echo_std_err + [ $status -eq 0 ] + [ "${stdout}" = "std output" ] + [ "${stderr}" = "err output" ] + [ "${output}" = "std outputerr output" ] +} From 5bbdb8216cdd68edb902434696755f0a10db6dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Combes?= Date: Tue, 12 Sep 2017 14:25:34 +0200 Subject: [PATCH 2/2] feat(run): Make possible to run to register stdout and stderr in separate variables --- libexec/bats-exec-test | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 8f3bd51..123f7ba 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -48,17 +48,25 @@ load() { } run() { - local e E T oldIFS + local e E T oldIFS [[ ! "$-" =~ e ]] || e=1 [[ ! "$-" =~ E ]] || E=1 [[ ! "$-" =~ T ]] || T=1 set +e set +E set +T - output="$("$@" 2>&1)" - status="$?" + + eval "$({ t_sdterr=$({ t_stdout=$("$@"); t_ret=$?; } 2>&1; declare -p t_stdout >&2; declare -pi t_ret >&2); declare -p t_sdterr; } 2>&1)" + + status=$t_ret + output="${t_stdout}${t_sdterr}" + stdout=$t_stdout + stderr=$t_sdterr + oldIFS=$IFS IFS=$'\n' lines=($output) + IFS=$'\n' stdlines=($t_stdout) + IFS=$'\n' errlines=($t_sdterr) [ -z "$e" ] || set -e [ -z "$E" ] || set -E [ -z "$T" ] || set -T