Skip to content

Commit 1e4112b

Browse files
committed
Changes for the BashSupport Pro test runner.
- Add formatter "bashpro" to output BashSupport Pro's format - bats-core#183: Execute bats-core tests with the shell defined in the run configuration. Without this Bash is picked from $PATH, which is Bash v3 on macOS even if /usr/local/bin/bash was used to execute the first bats runner script. - Group tests by file in a hierarchy of suites (root > file > test)
1 parent 713504b commit 1e4112b

4 files changed

Lines changed: 113 additions & 11 deletions

File tree

bin/bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ BATS_BASE_LIBDIR=lib # this will be patched with the true value in install.sh
7272

7373
export BATS_ROOT=${BATS_PATH%/*/*}
7474
export -f bats_readlinkf
75-
exec env BATS_ROOT="$BATS_ROOT" BATS_LIBDIR="${BATS_BASE_LIBDIR:-lib}" "$BATS_ROOT/libexec/bats-core/bats" "$@"
75+
exec env BATS_ROOT="$BATS_ROOT" BATS_LIBDIR="${BATS_BASE_LIBDIR:-lib}" "$BASH" "$BATS_ROOT/libexec/bats-core/bats" "$@"

libexec/bats-core/bats

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set -e
33

44
export BATS_VERSION='1.12.0'
5-
VALID_FORMATTERS="pretty, junit, tap, tap13"
5+
VALID_FORMATTERS="pretty, junit, tap, tap13, bashpro"
66

77
version() {
88
printf 'Bats %s\n' "$BATS_VERSION"
@@ -57,7 +57,7 @@ HELP_TEXT_HEADER
5757
Specifying this flag multiple times allows for logical or (||):
5858
`--filter-tags A,B --filter-tags A,!C` matches tags (A && B) || (A && !C)
5959
-F, --formatter <type> Switch between formatters: pretty (default),
60-
tap (default w/o term), tap13, junit, /<absolute path to formatter>
60+
tap (default w/o term), tap13, junit, bashpro, /<absolute path to formatter>
6161
--gather-test-outputs-in <directory>
6262
Gather the output of failing *and* passing tests
6363
as files in directory (if existing, must be empty)
@@ -193,7 +193,7 @@ while [[ "$#" -ne 0 ]]; do
193193
-F | --formatter)
194194
shift
195195
# allow cat formatter to see extended output but don't advertise to users
196-
if [[ $1 =~ ^(pretty|junit|tap|tap13|cat|/.*)$ ]]; then
196+
if [[ $1 =~ ^(pretty|junit|tap|tap13|cat|/.*|bashpro)$ ]]; then
197197
formatter="$1"
198198
else
199199
printf "Unknown formatter '%s', valid options are %s\n" "$1" "${VALID_FORMATTERS}"
@@ -505,12 +505,12 @@ bats_tee() { # <output-file> <command...>
505505
}
506506

507507
if [[ -n "$report_formatter" ]]; then
508-
exec bats-exec-suite "${flags[@]}" "${filenames[@]}" |
508+
exec "$BASH" bats-exec-suite "${flags[@]}" "${filenames[@]}" |
509509
bats_tee "${BATS_REPORT_OUTPUT_DIR}/${BATS_REPORT_FILENAME}" "$interpolated_report_formatter" "${report_formatter_flags[@]}" |
510510
bats_test_count_validator |
511-
"$interpolated_formatter" "${formatter_flags[@]}"
511+
"$BASH" "$interpolated_formatter" "${formatter_flags[@]}"
512512
else
513-
exec bats-exec-suite "${flags[@]}" "${filenames[@]}" |
513+
exec "$BASH" bats-exec-suite "${flags[@]}" "${filenames[@]}" |
514514
bats_test_count_validator |
515-
"$interpolated_formatter" "${formatter_flags[@]}"
515+
"$BASH" "$interpolated_formatter" "${formatter_flags[@]}"
516516
fi

libexec/bats-core/bats-exec-suite

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ bats_run_teardown_suite() {
237237
# avoid being called twice, in case this is not called through bats_teardown_suite_trap
238238
# but from the end of file
239239
trap bats_suite_exit_trap EXIT
240-
240+
241241
bats_set_stacktrace_limit
242242

243243
BATS_TEARDOWN_SUITE_COMPLETED=
@@ -306,11 +306,15 @@ else
306306
bats_exec_suite_status=130 # bash's code for SIGINT exits
307307
break
308308
fi
309-
bats-exec-file "${flags[@]}" "$file_index" "$filename" "${TESTS_LIST_FILE}" || bats_exec_suite_status=1
309+
if [ -n "${BATS_SHELL:-}" ]; then
310+
"$BATS_SHELL" bats-exec-file "${flags[@]}" "$file_index" "$filename" "${TESTS_LIST_FILE}" || bats_exec_suite_status=1
311+
else
312+
bats-exec-file "${flags[@]}" "$file_index" "$filename" "${TESTS_LIST_FILE}" || bats_exec_suite_status=1
313+
fi
310314
done
311315
fi
312316

313317
set -eET
314318
bats_run_teardown_suite
315319

316-
bats_exit_suite
320+
bats_exit_suite
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
_bashpro_file=""
5+
declare -i begin_index=0 test_active=0 number_of_printed_log_lines_for_this_test_so_far=0
6+
7+
yaml_block_open=''
8+
add_yaml_entry() {
9+
if [[ -z "$yaml_block_open" ]]; then
10+
printf " ---\n"
11+
fi
12+
[[ -n "$1" ]] && printf " %s: %s\n" "$1" "$2"
13+
yaml_block_open=1
14+
}
15+
16+
close_previous_yaml_block() {
17+
if [[ -n "$yaml_block_open" ]]; then
18+
printf " ...\n"
19+
yaml_block_open=''
20+
fi
21+
}
22+
23+
trap '' INT
24+
25+
number_of_printed_log_lines_for_this_test_so_far=0
26+
27+
# shellcheck source=lib/bats-core/formatter.bash
28+
source "$BATS_ROOT/lib/bats-core/formatter.bash"
29+
30+
bats_tap_stream_plan() {
31+
# printf "TAP version 13\n"
32+
printf "1..%d\n" "$1"
33+
}
34+
35+
bats_tap_stream_begin() { #<test index> <test name>
36+
test_active=1
37+
number_of_printed_log_lines_for_this_test_so_far=0
38+
printf "# _id=%d, _file=%s, _test=%s\n" "$begin_index" "$_bashpro_file" "$2"
39+
}
40+
41+
bats_tap_stream_ok() { # <test index> <test name>
42+
close_previous_yaml_block
43+
test_active=0
44+
number_of_printed_log_lines_for_this_test_so_far=0
45+
printf "ok %d %s\n" "$1" "$2"
46+
if [[ "${BATS_FORMATTER_TEST_DURATION-x}" != x ]]; then
47+
add_yaml_entry "duration_ms" "${BATS_FORMATTER_TEST_DURATION}"
48+
fi
49+
}
50+
51+
pass_on_optional_data() {
52+
if [[ "${BATS_FORMATTER_TEST_DURATION-x}" != x ]]; then
53+
add_yaml_entry "duration_ms" "${BATS_FORMATTER_TEST_DURATION}"
54+
fi
55+
if [[ "${BATS_FORMATTER_TEST_TIMEOUT-x}" != x ]]; then
56+
add_yaml_entry "timeout_sec" "${BATS_FORMATTER_TEST_TIMEOUT}"
57+
fi
58+
}
59+
60+
bats_tap_stream_not_ok() { # <test index> <test name>
61+
close_previous_yaml_block
62+
test_active=1 # to print the error message for this test
63+
number_of_printed_log_lines_for_this_test_so_far=0
64+
65+
printf "not ok %d %s\n" "$1" "$2"
66+
pass_on_optional_data
67+
}
68+
69+
bats_tap_stream_skipped() { # <test index> <test name> <reason>
70+
close_previous_yaml_block
71+
number_of_printed_log_lines_for_this_test_so_far=0
72+
73+
printf "not ok %d %s # SKIP %s\n" "$1" "$2" "$3"
74+
pass_on_optional_data
75+
}
76+
77+
bats_tap_stream_comment() { # <comment text without leading '# '>
78+
if ((test_active == 1 && number_of_printed_log_lines_for_this_test_so_far == 0)); then
79+
add_yaml_entry "" ""
80+
fi
81+
((++number_of_printed_log_lines_for_this_test_so_far))
82+
printf "# %s\n" "$1"
83+
}
84+
85+
bats_tap_stream_suite() { # <file name>
86+
_bashpro_file="$1"
87+
test_active=0
88+
number_of_printed_log_lines_for_this_test_so_far=0
89+
}
90+
91+
bats_tap_stream_unknown() { # <full line>
92+
printf "%s\n" "$1"
93+
}
94+
95+
bats_parse_internal_extended_tap
96+
97+
# close the final block if there was one
98+
close_previous_yaml_block

0 commit comments

Comments
 (0)