Skip to content

Commit b71cbc1

Browse files
fix(junit-formatter): shield internals from env (fixes #1174)
1 parent 332afd0 commit b71cbc1

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
1111

1212
## [Unreleased]
1313

14+
### Fixed
15+
16+
* avoid interference from env to junit formatter internals (#1175)
17+
1418
## [1.13.0] - 2025-11-07
1519

1620
### Added

libexec/bats-core/bats-format-junit

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ set -euo pipefail
44
# shellcheck source=lib/bats-core/formatter.bash
55
source "$BATS_ROOT/$BATS_LIBDIR/bats-core/formatter.bash"
66

7-
BASE_PATH=.
8-
9-
while [[ "$#" -ne 0 ]]; do
10-
case "$1" in
11-
--base-path)
12-
shift
13-
normalize_base_path BASE_PATH "$1"
14-
;;
15-
esac
16-
shift
17-
done
18-
197
init_suite() {
208
suite_test_exec_time=0
219
# since we have to print the suite header before its contents but we don't know the contents before the header,
@@ -24,7 +12,6 @@ init_suite() {
2412
test_result_state="" # declare for the first flush, when no test has been encountered
2513
}
2614

27-
_buffer_log=
2815
init_file() {
2916
file_count=0
3017
file_failures=0
@@ -97,8 +84,8 @@ print_test_case() {
9784
fi
9885
}
9986

100-
xml_escape() {
101-
output=${1//&/\&}
87+
xml_escape() { # <string to escape>
88+
local output=${1//&/\&amp;}
10289
output=${output//</\&lt;}
10390
output=${output//>/\&gt;}
10491
output=${output//'"'/\&quot;}
@@ -168,7 +155,7 @@ $1"
168155
}
169156

170157
finish_file() {
171-
if [[ "${class-JUNIT_FORMATTER_NO_FILE_ENCOUNTERED}" != JUNIT_FORMATTER_NO_FILE_ENCOUNTERED ]]; then
158+
if [[ "${class}" != JUNIT_FORMATTER_NO_FILE_ENCOUNTERED ]]; then
172159
file_header
173160
printf "%s\n" "${_buffer}"
174161
file_footer
@@ -189,10 +176,6 @@ bats_tap_stream_plan() { # <number of tests>
189176
:
190177
}
191178

192-
init_suite
193-
trap finish_suite EXIT
194-
trap '' INT
195-
196179
bats_tap_stream_begin() { # <test index> <test name>
197180
flush_log
198181
# set after flushing to avoid overriding name of test
@@ -259,4 +242,44 @@ bats_tap_stream_unknown() { # <full line>
259242
:
260243
}
261244

262-
bats_parse_internal_extended_tap
245+
main() {
246+
local _buffer_log="" \
247+
name="" \
248+
class="JUNIT_FORMATTER_NO_FILE_ENCOUNTERED" \
249+
_buffer="" \
250+
_buffer_log="" \
251+
_suite_buffer="" \
252+
_system_out_log="" \
253+
test_result_state="" \
254+
test_skip_message=""
255+
256+
local -i file_count=0 \
257+
file_failures=0 \
258+
file_skipped=0 \
259+
file_exec_time=0 \
260+
test_exec_time=0 \
261+
suite_test_exec_time=0
262+
263+
local BASE_PATH=.
264+
265+
while [[ "$#" -ne 0 ]]; do
266+
case "$1" in
267+
--base-path)
268+
shift
269+
normalize_base_path BASE_PATH "$1"
270+
;;
271+
esac
272+
shift
273+
done
274+
275+
init_suite
276+
trap finish_suite EXIT
277+
trap '' INT
278+
279+
bats_parse_internal_extended_tap
280+
281+
trap - EXIT
282+
finish_suite # ensure we run this with the local variables still in scope
283+
}
284+
285+
main "$@"

test/junit-formatter.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ TESTSUITES_REGEX="<testsuites time=\"$FLOAT_REGEX\">"
175175
@test "don't choke on setup_suite errors (issue #1106)" {
176176
bats_require_minimum_version 1.5.0
177177
local stderr='' # silence shellcheck
178-
reentrant_run -1 --separate-stderr bats --formatter junit "$FIXTURE_ROOT/../suite_setup_teardown/error_in_setup_suite/test.bats"
178+
name=non-empty reentrant_run -1 --separate-stderr bats --formatter junit "$FIXTURE_ROOT/../suite_setup_teardown/error_in_setup_suite/test.bats"
179179
[ "${stderr}" == "" ]
180180
[[ "${lines[2]}" == '<testsuite name="setup_suite" '*'>' ]]
181181
[[ "${lines[3]}" == ' <testcase classname="setup_suite" '*'>' ]]

0 commit comments

Comments
 (0)