Skip to content

Commit

Permalink
run: Print output/stderr on exit code errors
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schulze-vireso committed Oct 23, 2022
1 parent 4cce69a commit 683bdb3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
18 changes: 15 additions & 3 deletions lib/bats-core/test_functions.bash
Expand Up @@ -271,24 +271,36 @@ run() { # [!|-N] [--keep-empty-lines] [--separate-stderr] [--] <command to run..
BATS_RUN_COMMAND="${*}"
set "-$origFlags"

if [[ ${BATS_VERBOSE_RUN:-} ]]; then
printf "%s\n" "$output"
fi
bats_run_print_output() {
if [[ -n "$output" ]]; then
printf "%s\n" "$output"
fi
if [[ "$output_case" == separate && -n "$stderr" ]]; then
printf "stderr:\n%s\n" "$stderr"
fi
}

if [[ -n "$expected_rc" ]]; then
if [[ "$expected_rc" = "-1" ]]; then
if [[ "$status" -eq 0 ]]; then
BATS_ERROR_SUFFIX=", expected nonzero exit code!"
bats_run_print_output
return 1
fi
elif [ "$status" -ne "$expected_rc" ]; then
# shellcheck disable=SC2034
BATS_ERROR_SUFFIX=", expected exit code $expected_rc, got $status"
bats_run_print_output
return 1
fi
elif [[ "$status" -eq 127 ]]; then # "command not found"
bats_generate_warning 1 "$BATS_RUN_COMMAND"
fi

if [[ ${BATS_VERBOSE_RUN:-} ]]; then
bats_run_print_output
fi

# don't leak our trap into surrounding code
trap bats_interrupt_trap INT
}
Expand Down
12 changes: 6 additions & 6 deletions test/bats.bats
Expand Up @@ -1032,12 +1032,12 @@ END_OF_ERR_MSG
[ "${lines[1]}" == 'ok 1 no failure prints no output' ]
# ^ no output despite --show-output-of-passing-tests, because there is no failure
[ "${lines[2]}" == 'not ok 2 failure prints output' ]
[ "${lines[3]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure.bats, line 6)" ]
[ "${lines[4]}" == "# \`run -1 echo \"fail hard\"' failed, expected exit code 1, got 0" ]
[ "${lines[3]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure.bats, line 7)" ]
[ "${lines[4]}" == "# \`false' failed" ]
[ "${lines[5]}" == '# Last output:' ]
[ "${lines[6]}" == '# fail hard' ]
[ "${lines[7]}" == 'not ok 3 empty output on failure' ]
[ "${lines[8]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure.bats, line 10)" ]
[ "${lines[8]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure.bats, line 11)" ]
[ "${lines[9]}" == "# \`false' failed" ]
[ ${#lines[@]} -eq 10 ]
}
Expand All @@ -1048,14 +1048,14 @@ END_OF_ERR_MSG
[ "${lines[1]}" == 'ok 1 no failure prints no output' ]
# ^ no output despite --show-output-of-passing-tests, because there is no failure
[ "${lines[2]}" == 'not ok 2 failure prints output' ]
[ "${lines[3]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure_with_stderr.bats, line 7)" ]
[ "${lines[4]}" == "# \`run -1 --separate-stderr bash -c 'echo \"fail hard\"; echo with stderr >&2'' failed, expected exit code 1, got 0" ]
[ "${lines[3]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure_with_stderr.bats, line 8)" ]
[ "${lines[4]}" == "# \`false' failed" ]
[ "${lines[5]}" == '# Last output:' ]
[ "${lines[6]}" == '# fail hard' ]
[ "${lines[7]}" == '# Last stderr:' ]
[ "${lines[8]}" == '# with stderr' ]
[ "${lines[9]}" == 'not ok 3 empty output on failure' ]
[ "${lines[10]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure_with_stderr.bats, line 11)" ]
[ "${lines[10]}" == "# (in test file $RELATIVE_FIXTURE_ROOT/print_output_on_failure_with_stderr.bats, line 12)" ]
[ "${lines[11]}" == "# \`false' failed" ]
[ ${#lines[@]} -eq 12 ]
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/bats/print_output_on_failure.bats
Expand Up @@ -3,7 +3,8 @@
}
bats_require_minimum_version 1.5.0 # don't be fooled by order, this will run before the test above!
@test "failure prints output" {
run -1 echo "fail hard"
run echo "fail hard"
false
}

@test "empty output on failure" {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/bats/print_output_on_failure_with_stderr.bats
Expand Up @@ -4,7 +4,8 @@

@test "failure prints output" {
bats_require_minimum_version 1.5.0
run -1 --separate-stderr bash -c 'echo "fail hard"; echo with stderr >&2'
run --separate-stderr bash -c 'echo "fail hard"; echo with stderr >&2'
false
}

@test "empty output on failure" {
Expand Down
19 changes: 10 additions & 9 deletions test/run.bats
Expand Up @@ -96,15 +96,16 @@ print-stderr-stdout() {
[ "${lines[4]}" == "not ok 2 run -1 echo hi" ]
[ "${lines[5]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 7)" ]
[ "${lines[6]}" == "# \`run -1 echo hi' failed, expected exit code 1, got 0" ]
[ "${lines[7]}" == "not ok 3 run -2 exit 3" ]
[ "${lines[8]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 11)" ]
[ "${lines[9]}" == "# \`run -2 exit 3' failed, expected exit code 2, got 3" ]
[ "${lines[10]}" == "not ok 4 run ! true" ]
[ "${lines[11]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 15)" ]
[ "${lines[12]}" == "# \`run ! true' failed, expected nonzero exit code!" ]
[ "${lines[13]}" == "not ok 5 run multiple pass/fails" ]
[ "${lines[14]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 22)" ]
[ "${lines[15]}" == "# \`run -1 /etc' failed, expected exit code 1, got 126" ]
[ "${lines[7]}" == "# hi" ]
[ "${lines[8]}" == "not ok 3 run -2 exit 3" ]
[ "${lines[9]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 11)" ]
[ "${lines[10]}" == "# \`run -2 exit 3' failed, expected exit code 2, got 3" ]
[ "${lines[11]}" == "not ok 4 run ! true" ]
[ "${lines[12]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 15)" ]
[ "${lines[13]}" == "# \`run ! true' failed, expected nonzero exit code!" ]
[ "${lines[14]}" == "not ok 5 run multiple pass/fails" ]
[ "${lines[15]}" == "# (in test file ${RELATIVE_FIXTURE_ROOT}/failing.bats, line 22)" ]
[ "${lines[16]}" == "# \`run -1 /etc' failed, expected exit code 1, got 126" ]
}

@test "run invalid exit code check error message" {
Expand Down

0 comments on commit 683bdb3

Please sign in to comment.