Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty formatter: Demarcate files and allow for test prefixes #561

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ The format is based on [Keep a Changelog][kac] and this project adheres to

### Added

* Pretty formatter print filename when entering file (#561)
* BATS_TEST_NAME_PREFIX allows prefixing test names on stdout and in reports (#561)

#### Documentation

* document `$BATS_VERSION` (#557)
Expand All @@ -22,6 +25,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
* don't require `flock` *and* `shlock` for parallel mode test (#554)
* print name of failing test when using TAP13 with timing information (#559, #555)
* removed broken symlink, added regression test (#560)
* don't show empty lines as `#` with pretty formatter (#561)

## [1.6.0] - 2022-02-24

Expand Down
2 changes: 2 additions & 0 deletions docs/source/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ There are several global variables you can use to introspect on Bats tests:
- `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is located.
- `$BATS_TEST_NAMES` is an array of function names for each test case.
- `$BATS_TEST_NAME` is the name of the function containing the current test case.
- `BATS_TEST_NAME_PREFIX` will be prepended to the description of each test on
stdout and in reports.
- `$BATS_TEST_DESCRIPTION` is the description of the current test case.
- `$BATS_TEST_NUMBER` is the (1-based) index of the current test case in the test file.
- `$BATS_SUITE_TEST_NUMBER` is the (1-based) index of the current test case in the test suite (over all files).
Expand Down
19 changes: 3 additions & 16 deletions lib/bats-core/formatter.bash
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function bats_parse_internal_extended_tap() {
'# '*)
bats_tap_stream_comment "${line:2}" "$scope"
;;
'#')
bats_tap_stream_comment "" "$scope"
;;
'suite '*)
scope=suite
# pass on the
Expand All @@ -95,22 +98,6 @@ function bats_parse_internal_extended_tap() {
done
}

# given a prefix and a path, remove the prefix if the path starts with it
# e.g.
# remove_prefix /usr/bin /usr/bin/bash -> bash
# remove_prefix /usr /usr/lib/bash -> lib/bash
# remove_prefix /usr/bin /usr/local/bin/bash -> /usr/local/bin/bash
remove_prefix() {
base_path="$1"
path="$2"
if [[ "$path" == "$base_path"* ]]; then
# cut off the common prefix
printf "%s" "${path:${#base_path}}"
else
printf "%s" "$path"
fi
}

normalize_base_path() { # <target variable> <base path>
# the relative path root to use for reporting filenames
# this is mainly intended for suite mode, where this will be the suite root folder
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 @@ -324,7 +324,7 @@ skip() {
bats_test_begin() {
BATS_TEST_DESCRIPTION="$1"
if [[ -n "$BATS_EXTENDED_SYNTAX" ]]; then
printf 'begin %d %s\n' "$BATS_SUITE_TEST_NUMBER" "$BATS_TEST_DESCRIPTION" >&3
printf 'begin %d %s\n' "$BATS_SUITE_TEST_NUMBER" "${BATS_TEST_NAME_PREFIX:-}$BATS_TEST_DESCRIPTION" >&3
fi
setup
}
Expand Down
5 changes: 3 additions & 2 deletions libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ BATS_PRINT_OUTPUT_ON_FAILURE="${BATS_PRINT_OUTPUT_ON_FAILURE:-}"
BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS="${BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS:-}"
BATS_VERBOSE_RUN="${BATS_VERBOSE_RUN:-}"
BATS_GATHER_TEST_OUTPUTS_IN="${BATS_GATHER_TEST_OUTPUTS_IN:-}"
BATS_TEST_NAME_PREFIX="${BATS_TEST_NAME_PREFIX:-}"

while [[ "$#" -ne 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -146,7 +147,7 @@ bats_exit_trap() {
# output, since there's no way to reach the `bats_exit_trap` call.
BATS_ERROR_STATUS=1
fi
printf 'not ok %d %s\n' "$BATS_SUITE_TEST_NUMBER" "${BATS_TEST_DESCRIPTION}${BATS_TEST_TIME}" >&3
printf 'not ok %d %s\n' "$BATS_SUITE_TEST_NUMBER" "${BATS_TEST_NAME_PREFIX:-}${BATS_TEST_DESCRIPTION}${BATS_TEST_TIME}" >&3
local stack_trace
bats_get_failure_stack_trace stack_trace
bats_print_stack_trace "${stack_trace[@]}" >&3
Expand All @@ -159,7 +160,7 @@ bats_exit_trap() {
print_bats_out=1
status=1
else
printf 'ok %d %s%s\n' "$BATS_SUITE_TEST_NUMBER" "${BATS_TEST_DESCRIPTION}${BATS_TEST_TIME}" \
printf 'ok %d %s%s\n' "$BATS_SUITE_TEST_NUMBER" "${BATS_TEST_NAME_PREFIX:-}${BATS_TEST_DESCRIPTION}${BATS_TEST_TIME}" \
"$skipped" >&3
status=0
fi
Expand Down
2 changes: 1 addition & 1 deletion libexec/bats-core/bats-format-junit
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ bats_tap_stream_suite() { # <file name>
flush_log
suite_buffer finish_file
init_file
class="$(remove_prefix "$BASE_PATH" "$1")"
class="${1/$BASE_PATH}"
}

bats_tap_stream_unknown() { # <full line>
Expand Down
33 changes: 22 additions & 11 deletions libexec/bats-core/bats-format-pretty
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,36 @@ bats_tap_stream_begin() {
}

bats_tap_stream_ok() {
index="$1"
((++passed))
local duration=
if [[ "$1" == "--duration" ]]; then
pass "$2"
else
pass
duration="$2"
shift 2
fi
index="$1"
name="$2"
((++passed))

pass "$duration"
}

bats_tap_stream_skipped() {
index="$1"
name="$2"
((++skipped))
skip "$3"
}

bats_tap_stream_not_ok() {
index="$1"
((++failures))
local duration=
if [[ "$1" == "--duration" ]]; then
fail "$2"
else
fail
duration="$2"
shift 2
fi
index="$1"
name="$2"
((++failures))

fail "$duration"
}

bats_tap_stream_comment() { # <comment> <scope>
Expand All @@ -293,7 +300,11 @@ bats_tap_stream_suite() {
line_backoff_count=0
index=
# indicate filename for failures
name="File $(remove_prefix "$BASE_PATH" "$1")"
local file_name="${1/$BASE_PATH}"
name="File $file_name"
set_color 4 bold
buffer "%s\n" "$file_name"
clear_color
}

line_backoff_count=0
Expand Down
2 changes: 2 additions & 0 deletions man/bats.7.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ located.
* `$BATS_TEST_NAMES` is an array of function names for each test case.
* `$BATS_TEST_NAME` is the name of the function containing the current
test case.
* `BATS_TEST_NAME_PREFIX` will be prepended to the description of each test
on stdout and in reports.
* `$BATS_TEST_DESCRIPTION` is the description of the current test
case.
* `$BATS_TEST_NUMBER` is the (1-based) index of the current test case
Expand Down
25 changes: 15 additions & 10 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ setup() {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing.bats"
echo "$output"
[ $status -eq 0 ]
[ "${lines[1]}" = "1 test, 0 failures" ]
[ "${lines[2]}" = "1 test, 0 failures" ]
}

@test "summary passing and skipping tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[3]}" = "3 tests, 0 failures, 2 skipped" ]
[ "${lines[4]}" = "3 tests, 0 failures, 2 skipped" ]
}

@test "tap passing and skipping tests" {
Expand All @@ -75,13 +75,13 @@ setup() {
@test "summary passing and failing tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 0 ]
[ "${lines[4]}" = "2 tests, 1 failure" ]
[ "${lines[5]}" = "2 tests, 1 failure" ]
}

@test "summary passing, failing and skipping tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_failing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[5]}" = "3 tests, 1 failure, 1 skipped" ]
[ "${lines[6]}" = "3 tests, 1 failure, 1 skipped" ]
}

@test "tap passing, failing and skipping tests" {
Expand Down Expand Up @@ -1064,17 +1064,17 @@ EOF
@test "pretty formatter summary is colorized red on failure" {
run -1 bats --pretty "$FIXTURE_ROOT/failing.bats"

[ "${lines[3]}" == $'\033[0m\033[31;1m' ] # TODO: avoid checking for the leading reset too
[ "${lines[4]}" == '1 test, 1 failure' ]
[ "${lines[5]}" == $'\033[0m' ]
[ "${lines[4]}" == $'\033[0m\033[31;1m' ] # TODO: avoid checking for the leading reset too
[ "${lines[5]}" == '1 test, 1 failure' ]
[ "${lines[6]}" == $'\033[0m' ]
}

@test "pretty formatter summary is colorized green on success" {
run -0 bats --pretty "$FIXTURE_ROOT/passing.bats"

[ "${lines[1]}" == $'\033[0m\033[32;1m' ] # TODO: avoid checking for the leading reset too
[ "${lines[2]}" == '1 test, 0 failures' ]
[ "${lines[3]}" == $'\033[0m' ]
[ "${lines[2]}" == $'\033[0m\033[32;1m' ] # TODO: avoid checking for the leading reset too
[ "${lines[3]}" == '1 test, 0 failures' ]
[ "${lines[4]}" == $'\033[0m' ]
}

@test "--print-output-on-failure works as expected" {
Expand Down Expand Up @@ -1263,3 +1263,8 @@ EOF
[[ $FDS_LOG == *'otherfunc fds after: (0 1 2)'* ]] || false
[[ $FDS_LOG == *'setup_file fds after: (0 1 2)'* ]] || false
}

@test "Allow for prefixing tests' names with BATS_TEST_NAME_PREFIX" {
BATS_TEST_NAME_PREFIX='PREFIX: ' run bats "${FIXTURE_ROOT}/passing.bats"
[ "${lines[1]}" == "ok 1 PREFIX: a passing test" ]
}
2 changes: 1 addition & 1 deletion test/pretty-formatter.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ HERE
}
run format_example_stream
echo "$output"
[[ "${lines[0]}" == *'[123]'* ]]
[[ "${lines[1]}" == *'[123]'* ]]
}