Skip to content

Commit

Permalink
Merge pull request #631 from martin-schulze-vireso/feature/also_show_…
Browse files Browse the repository at this point in the history
…stderr_with_--print-output-on-failure

Also print $stderr (from `run --separate-stderr`) for `--print-output-on-failure`
  • Loading branch information
martin-schulze-vireso committed Aug 15, 2022
2 parents 44e2da6 + 9f401f8 commit 50d4d1a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
avoiding `bash: bats: No such file or directory` on `docker<20.10` (or
`runc<v1.0.0-rc93`) (#622)
* `BATS_TEST_TIMEOUT` variable to force a timeout on test (including `setup()`) (#491)
* also print (nonempty) `$stderr` (from `run --separate-stderr`) with
`--print-output-on-failure` (#631)

#### Documentation

Expand Down
2 changes: 1 addition & 1 deletion lib/bats-core/test_functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ run() { # [!|-N] [--keep-empty-lines] [--separate-stderr] [--] <command to run..

if [[ "$output_case" == separate ]]; then
# shellcheck disable=SC2034
read -d '' -r stderr < "$bats_run_separate_stderr_file"
read -d '' -r stderr < "$bats_run_separate_stderr_file" || true
bats_separate_lines stderr_lines stderr
fi

Expand Down
11 changes: 8 additions & 3 deletions libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ bats_exit_trap() {
bats_print_stack_trace "${stack_trace[@]}" >&3
bats_print_failed_command "${stack_trace[@]}" >&3

if [[ $BATS_PRINT_OUTPUT_ON_FAILURE && -n "${output:-}" ]]; then
printf "Last output:\n%s\n" "$output" >> "$BATS_OUT"
fi
if [[ $BATS_PRINT_OUTPUT_ON_FAILURE ]]; then
if [[ -n "${output:-}" ]]; then
printf "Last output:\n%s\n" "$output"
fi
if [[ -n "${stderr:-}" ]]; then
printf "Last stderr: \n%s\n" "$stderr"
fi
fi >> "$BATS_OUT"

print_bats_out=1
status=1
Expand Down
18 changes: 18 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,24 @@ END_OF_ERR_MSG
[ ${#lines[@]} -eq 10 ]
}

@test "--print-output-on-failure also shows stderr (for run --separate-stderr)" {
run bats --print-output-on-failure --show-output-of-passing-tests "$FIXTURE_ROOT/print_output_on_failure_with_stderr.bats"
[ "${lines[0]}" == '1..3' ]
[ "${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[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[11]}" == "# \`false' failed" ]
[ ${#lines[@]} -eq 12 ]
}

@test "--show-output-of-passing-tests works as expected" {
bats_require_minimum_version 1.5.0
run -0 bats --show-output-of-passing-tests "$FIXTURE_ROOT/show-output-of-passing-tests.bats"
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/bats/print_output_on_failure_with_stderr.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@test "no failure prints no output" {
run echo success
}

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

@test "empty output on failure" {
false
}

0 comments on commit 50d4d1a

Please sign in to comment.