Skip to content

Commit

Permalink
Allow for customizing code quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schulze-vireso committed Oct 17, 2021
1 parent 01636e4 commit c6842c5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/bats-core/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ bats_prefix_lines_for_tap_output() {
if [[ -n "$line" ]]; then
printf '# %s\n' "$line"
fi
}
}

bats_quote_code() { # <var> <code>
printf -v "$1" -- "%s%s%s" "$BATS_BEGIN_CODE_QUOTE" "$2" "$BATS_END_CODE_QUOTE"
}
8 changes: 6 additions & 2 deletions lib/bats-core/tracing.bash
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ bats_print_stack_trace() {
# don't print "from function `source'"",
# when failing in free code during `source $test_file` from bats-exec-file
! [[ "$fn" == 'source' && $index -eq $count ]]; then
printf "from function \`%s' " "$fn"
local quoted_fn
bats_quote_code quoted_fn "$fn"
printf "from function %s " "$quoted_fn"
fi

if [[ $index -eq $count ]]; then
Expand All @@ -82,7 +84,9 @@ bats_print_failed_command() {
bats_frame_lineno "$frame" 'lineno'
bats_extract_line "$filename" "$lineno" 'failed_line'
bats_strip_string "$failed_line" 'failed_command'
printf '%s' "# \`${failed_command}' "
local quoted_failed_command
bats_quote_code quoted_failed_command "$failed_command"
printf '# %s' "${quoted_failed_command}"

if [[ "$BATS_ERROR_STATUS" -eq 1 ]]; then
printf 'failed%s\n' "$BATS_ERROR_SUFFIX"
Expand Down
23 changes: 23 additions & 0 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,29 @@ if [[ -n "$report_formatter" ]]; then
esac
fi

case ${BATS_CODE_QUOTE_STYLE:-'`'} in
"\`'")
BATS_BEGIN_CODE_QUOTE='`'
BATS_END_CODE_QUOTE="'"
;;
'``')
BATS_BEGIN_CODE_QUOTE='`'
BATS_END_CODE_QUOTE='`'
;;
custom)
if [[ ${BATS_BEGIN_CODE_QUOTE-UNSET} == unset
|| ${BATS_END_CODE_QUOTE-UNSET} == unset ]]; then
printf "ERROR: BATS_CODE_QUOTE_STYLE=custom requires BATS_BEGIN_CODE_QUOTE and BATS_END_CODE_QUOTE to be set\n"
exit 1
fi
;;
*)
printf "ERROR: Unknown BATS_CODE_QUOTE_STYLE: %s\n" "$BATS_CODE_QUOTE_STYLE"
exit 1
;;
esac
export BATS_BEGIN_CODE_QUOTE BATS_END_CODE_QUOTE

if [[ -n "$output" ]]; then
if [[ ! -w "${output}" ]]; then
abort "Output path ${output} is not writeable"
Expand Down
4 changes: 3 additions & 1 deletion libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ get_mills_since_epoch() {

bats_perform_test() {
if ! declare -F "$BATS_TEST_NAME" &>/dev/null; then
printf "bats: unknown test name \`%s'\n" "$BATS_TEST_NAME" >&2
local quoted_test_name
bats_quote_code quoted_test_name "$BATS_TEST_NAME"
printf "bats: unknown test name %s\n" "$quoted_test_name" >&2
exit 1
fi

Expand Down

0 comments on commit c6842c5

Please sign in to comment.