Skip to content

Commit

Permalink
Merge pull request #426 from martin-schulze-vireso/bugfix/messed_up_p…
Browse files Browse the repository at this point in the history
…retty_output_on_FD3

pretty mode: Print FD3 output below test name
  • Loading branch information
martin-schulze-vireso committed Apr 9, 2021
2 parents 330c754 + 9b07203 commit 9441d7f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
writing non compliant extended output (#412)
* avoid collisions on `$BATS_RUN_TMPDIR` with `--no-tempdir-cleanup` and docker
by using `mktemp` additionally to PID (#409)
* pretty printer now puts text that is printed to FD 3 below the test name (#426)

## [1.3.0] - 2021-03-08

Expand Down
21 changes: 9 additions & 12 deletions docs/source/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,18 @@ bats provides a special file descriptor, `&3`, that you should use to print
your custom text. Here are some detailed guidelines to refer to:

- Printing **from within a test function**:
- To have text printed from within a test function you need to redirect the
output to file descriptor 3, eg `echo 'text' >&3`. This output will become
part of the TAP stream. You are encouraged to prepend text printed this way
with a hash (eg `echo '# text' >&3`) in order to produce 100% TAP compliant
- First you should consider if you want the text to be always visible or only
when the test fails. Text that is output directly to stdout or stderr (file
descriptor 1 or 2), ie `echo 'text'` is considered part of the test function
output and is printed only on test failures for diagnostic purposes,
regardless of the formatter used (TAP or pretty).
- To have text printed unconditionally from within a test function you need to
redirect the output to file descriptor 3, eg `echo 'text' >&3`. This output
will become part of the TAP stream. You are encouraged to prepend text printed
this way with a hash (eg `echo '# text' >&3`) in order to produce 100% TAP compliant
output. Otherwise, depending on the 3rd-party tools you use to analyze the
TAP stream, you can encounter unexpected behavior or errors.

- The pretty formatter that Bats uses by default to process the TAP stream
will filter out and not print text output to file descriptor 3.

- Text that is output directly to stdout or stderr (file descriptor 1 or 2),
ie `echo 'text'` is considered part of the test function output and is
printed only on test failures for diagnostic purposes, regardless of the
formatter used (TAP or pretty).

- Printing **from within the `setup` or `teardown` functions**: The same hold
true as for printing with test functions.

Expand Down
41 changes: 40 additions & 1 deletion libexec/bats-core/bats-format-pretty
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ trap update_screen_width WINCH
update_screen_width

begin() {
line_backoff_count=0
go_to_column 0
update_count_column_width
buffer_with_truncation $((count_column_left - 1)) ' %s' "$name"
Expand All @@ -48,26 +49,31 @@ begin() {
}

pass() {
move_up $line_backoff_count
go_to_column 0
buffer ' ✓ %s' "$name"
if [[ -n "$BATS_ENABLE_TIMING" ]]; then
set_color 2
buffer ' [%s]' "$1"
fi
advance
move_down $line_backoff_count
}

skip() {
local reason="$1"
if [[ -n "$reason" ]]; then
reason=": $reason"
fi
move_up $line_backoff_count
go_to_column 0
buffer ' - %s (skipped%s)' "$name" "$reason"
advance
move_down $line_backoff_count
}

fail() {
move_up $line_backoff_count
go_to_column 0
set_color 1 bold
buffer ' ✗ %s' "$name"
Expand All @@ -76,6 +82,7 @@ fail() {
buffer ' [%s]' "$1"
fi
advance
move_down $line_backoff_count
}

log() {
Expand Down Expand Up @@ -126,6 +133,18 @@ buffer_with_truncation() {
fi
}

move_up() {
if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
buffer '\x1B[%dA' "$1"
fi
}

move_down() {
if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
buffer '\x1B[%dB' "$1"
fi
}

go_to_column() {
local column="$1"
buffer '\x1B[%dG' $((column + 1))
Expand Down Expand Up @@ -223,15 +242,35 @@ bats_tap_stream_not_ok() {
}

bats_tap_stream_comment() {
# count the lines we printed after the begin text,
if (( line_backoff_count == 0 )); then
# if this is the first line after begin, go down one line
buffer "\n"
(( ++line_backoff_count )) # prefix-increment to avoid "error" due to returning 0
fi

(( ++line_backoff_count ))
(( line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
log "$1"
}

bats_tap_stream_suite() {
: #test_file="$1"
}

line_backoff_count=0
bats_tap_stream_unknown() { # <full line>
printf "%s\n" "$1"
# count the lines we printed after the begin text,
if (( line_backoff_count == 0 )); then
# if this is the first line after begin, go down one line
buffer "\n"
(( ++line_backoff_count )) # prefix-increment to avoid "error" due to returning 0
fi

(( ++line_backoff_count ))
(( line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
buffer "%s\n" "$1"
flush
}

bats_parse_internal_extended_tap
Expand Down

0 comments on commit 9441d7f

Please sign in to comment.