Skip to content

Commit

Permalink
Add --file-reference-format/BATS_FILE_REFERENCE_FORMAT
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schulze-vireso committed Oct 21, 2022
1 parent ccacef3 commit 2532714
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
21 changes: 21 additions & 0 deletions lib/bats-core/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,24 @@ bats_append_arrays_as_args() { # <array...> -- <command ...>
"$@"
fi
}

bats_format_file_line_reference() { # <output> <file> <line>
# shellcheck disable=SC2034 # will be used in subimplementation
local output="${1?}"
shift
echo "$BATS_FILE_REFERENCE_FORMAT" "$@" >&2
"bats_format_file_line_reference_$BATS_FILE_REFERENCE_FORMAT" "$@"
}

bats_format_file_line_reference_comma_line() {
printf -v "$output" "%s, line %d" "$@"
}

bats_format_file_line_reference_colon_separated() {
printf -v "$output" "%s:%d" "$@"
}

bats_format_file_line_reference_url_realpath() {
local filename=${1?} line=${2?}
printf -v "$output" "file://%s:%d" "$(realpath "$filename")" "$line"
}
10 changes: 7 additions & 3 deletions lib/bats-core/tracing.bash
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ bats_print_stack_trace() {
printf "from function %s " "$quoted_fn"
fi

local reference
bats_format_file_line_reference reference "$filename" "$lineno"
if [[ $index -eq $count ]]; then
printf 'in test file file://%s:%d)\n' "$(realpath "$filename")" "$lineno"
printf 'in test file %s)\n' "$reference"
else
printf 'in file file://%s:%d)\n' "$(realpath "$filename")" "$lineno"
printf 'in file %s)\n' "$reference"
fi

((++index))
Expand Down Expand Up @@ -187,7 +189,9 @@ bats_emit_trace() {
fi
local padding='$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'
if ((BATS_LAST_STACK_DEPTH != ${#BASH_LINENO[@]})); then
printf '%s [%s:%d]\n' "${padding::${#BASH_LINENO[@]}-4}" "${file##*/}" "$line" >&4
local reference
bats_format_file_line_reference reference "${file##*/}" "$line"
printf '%s [%s]\n' "${padding::${#BASH_LINENO[@]}-4}" "$reference" >&4
fi
printf '%s %s\n' "${padding::${#BASH_LINENO[@]}-4}" "$BASH_COMMAND" >&4
BATS_LAST_BASH_COMMAND="$BASH_COMMAND"
Expand Down
16 changes: 16 additions & 0 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ HELP_TEXT_HEADER
A two character string of code quote delimiters
or 'custom' which requires setting $BATS_BEGIN_CODE_QUOTE and
$BATS_END_CODE_QUOTE. Can also be set via $BATS_CODE_QUOTE_STYLE
--file-reference-format Controls how file/line references e.g. in stack traces are printed:
- comma_line (default): a.bats, line 1
- colon_separated: a.bats:1
- url_realpath: file:///tests/a.bats:1
- custom: provide your own via defining bats_format_file_line_reference_custom
with parameters <output-var-name> <filename> <line>
-f, --filter <regex> Only run tests that match the regular expression
--filter-status <status> Only run tests with the given status in the last completed (no CTRL+C/SIGINT) run.
Valid <status> values are:
Expand Down Expand Up @@ -106,6 +112,7 @@ export BATS_RUN_TMPDIR=
export BATS_GUARANTEED_MINIMUM_VERSION=0.0.0
export BATS_LIB_PATH=${BATS_LIB_PATH-/usr/lib/bats}
BATS_REPORT_OUTPUT_DIR=${BATS_REPORT_OUTPUT_DIR-.}
export BATS_FILE_REFERENCE_FORMAT=${BATS_FILE_REFERENCE_FORMAT-comma_line}

if [[ ! -d "${BATS_TMPDIR}" ]]; then
printf "Error: BATS_TMPDIR (%s) does not exist or is not a directory" "${BATS_TMPDIR}" >&2
Expand Down Expand Up @@ -262,6 +269,10 @@ while [[ "$#" -ne 0 ]]; do
shift
flags+=('--filter-tags' "$1")
;;
--file-reference-format)
shift
BATS_FILE_REFERENCE_FORMAT=$1
;;
-*)
abort "Bad command line option '$1'"
;;
Expand All @@ -272,6 +283,11 @@ while [[ "$#" -ne 0 ]]; do
shift
done

if [[ ! $BATS_FILE_REFERENCE_FORMAT =~ (custom|comma_line|colon_separated|url_realpath) ]]; then
abort "Invalid BATS_FILE_REFERENCE_FORMAT '$BATS_FILE_REFERENCE_FORMAT' (e.g. via --file-reference-format)"
fi


if [[ -n "${BATS_RUN_TMPDIR:-}" ]]; then
if [[ -d "$BATS_RUN_TMPDIR" ]]; then
printf "Error: BATS_RUN_TMPDIR (%s) already exists\n" "$BATS_RUN_TMPDIR" >&2
Expand Down
24 changes: 24 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1491,3 +1491,27 @@ enforce_own_process_group() {
[ "${lines[1]}" == 'ok 1 Fail once' ]
[ ${#lines[@]} == 2 ]
}

@test "Error on invalid --file-reference-format" {
bats_require_minimum_version 1.5.0

reentrant_run -1 bats --file-reference-format invalid "$FIXTURE_ROOT/passing.bats"
[ "${lines[0]}" == "Error: Invalid BATS_FILE_REFERENCE_FORMAT 'invalid' (e.g. via --file-reference-format)" ]
}

@test "--file-reference-format switches format" {
bats_require_minimum_version 1.5.0

reentrant_run -1 bats --file-reference-format colon_separated "$FIXTURE_ROOT/failing.bats"
[ "${lines[2]}" == "# (in test file test/fixtures/bats/failing.bats:4)" ]

reentrant_run -1 bats --file-reference-format url_realpath "$FIXTURE_ROOT/failing.bats"
[ "${lines[2]}" == "# (in test file file://$FIXTURE_ROOT/failing.bats:4)" ]

bats_format_file_line_reference_custom() {
printf -v "$output" "%s<-%d" "$1" "$2"
}
export -f bats_format_file_line_reference_custom
reentrant_run -1 bats --file-reference-format custom "$FIXTURE_ROOT/failing.bats"
[ "${lines[2]}" == "# (in test file test/fixtures/bats/failing.bats<-4)" ]
}
2 changes: 1 addition & 1 deletion test/common.bats
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@

run -0 bats_append_arrays_as_args arr empty arr2 -- count_and_print_args
[ "${lines[0]}" == '2 a b' ]
}
}

0 comments on commit 2532714

Please sign in to comment.