Skip to content

Commit fe7df4f

Browse files
Merge pull request #692 from martin-schulze-vireso/fix/issue-690-blank-junit-report-in-docker
Fix blank junit report in docker
2 parents 5a47c79 + fb0909f commit fe7df4f

16 files changed

Lines changed: 101 additions & 43 deletions

File tree

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
run: |
8080
npm pack ./
8181
sudo npm install -g ./bats-*.tgz
82-
bats test
82+
bats test --print-output-on-failure
8383
8484
windows:
8585
runs-on: windows-2019
@@ -257,7 +257,7 @@ jobs:
257257
<<EOF cat >test.sh
258258
apk add sudo python3 # install bats-file's dependencies
259259
ln -sf python3 /usr/bin/python # bats-file uses python without version
260-
bats --tap --print-output-on-failure bats-*/test/"
260+
bats --tap --print-output-on-failure bats-*/test/
261261
EOF
262262
docker run -itv "$PWD":/code --entrypoint bash bats:test test.sh
263263
shell: 'script -q -e -c "bash {0}"' # work around tty issues

docs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
2828
### Fixed
2929

3030
* explicitly check for GNU parallel (#691)
31+
* wait for report-formatter to finish before ending `bats`' execution,
32+
to fix empty files with `--report-fomatter junit` under Docker (#692)
3133

3234
#### Documentation
3335

lib/bats-core/common.bash

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ bats_binary_search() { # <search-value> <array-name>
9797
return 1
9898
}
9999

100-
# store the values in ascending order in result array
101-
# Intended for short lists!
100+
# store the values in ascending (string!) order in result array
101+
# Intended for short lists! (uses insertion sort)
102102
bats_sort() { # <result-array-name> <values to sort...>
103103
local -r result_name=$1
104104
shift
@@ -109,14 +109,18 @@ bats_sort() { # <result-array-name> <values to sort...>
109109
fi
110110

111111
local -a sorted_array=()
112-
local -i j i=0
113-
for ((j = 1; j <= $#; ++j)); do
114-
for ((i = ${#sorted_array[@]}; i >= 0; --i)); do
115-
if [[ $i -eq 0 || ${sorted_array[$((i - 1))]} < ${!j} ]]; then
116-
sorted_array[$i]=${!j}
112+
local -i i
113+
while (( $# > 0 )); do # loop over input values
114+
local current_value="$1"
115+
shift
116+
for ((i = ${#sorted_array[@]}; i >= 0; --i)); do # loop over output array from end
117+
if (( i == 0 )) || [[ ${sorted_array[i - 1]} < $current_value ]]; then
118+
# shift bigger elements one position to the end
119+
sorted_array[i]=$current_value
117120
break
118121
else
119-
sorted_array[$i]=${sorted_array[$((i - 1))]}
122+
# insert new element at (freed) desired location
123+
sorted_array[i]=${sorted_array[i - 1]}
120124
fi
121125
done
122126
done

lib/bats-core/semaphore.bash

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#!/usr/bin/env bash
22

3-
# setup the semaphore environment for the loading file
4-
bats_semaphore_setup() {
5-
export -f bats_semaphore_get_free_slot_count
6-
export -f bats_semaphore_acquire_while_locked
7-
export BATS_SEMAPHORE_DIR="$BATS_RUN_TMPDIR/semaphores"
3+
bats_run_under_flock() {
4+
flock "$BATS_SEMAPHORE_DIR" "$@"
5+
}
86

9-
if command -v flock >/dev/null; then
10-
bats_run_under_lock() {
11-
flock "$BATS_SEMAPHORE_DIR" "$@"
12-
}
13-
elif command -v shlock >/dev/null; then
14-
bats_run_under_lock() {
7+
bats_run_under_shlock() {
158
local lockfile="$BATS_SEMAPHORE_DIR/shlock.lock"
169
while ! shlock -p $$ -f "$lockfile"; do
1710
sleep 1
@@ -23,6 +16,17 @@ bats_semaphore_setup() {
2316
rm -f "$lockfile"
2417
return $status
2518
}
19+
20+
# setup the semaphore environment for the loading file
21+
bats_semaphore_setup() {
22+
export -f bats_semaphore_get_free_slot_count
23+
export -f bats_semaphore_acquire_while_locked
24+
export BATS_SEMAPHORE_DIR="$BATS_RUN_TMPDIR/semaphores"
25+
26+
if command -v flock >/dev/null; then
27+
BATS_LOCKING_IMPLEMENTATION=flock
28+
elif command -v shlock >/dev/null; then
29+
BATS_LOCKING_IMPLEMENTATION=shlock
2630
else
2731
printf "ERROR: flock/shlock is required for parallelization within files!\n" >&2
2832
exit 1
@@ -87,7 +91,9 @@ bats_semaphore_acquire_slot() {
8791
while true; do
8892
# don't lock for reading, we are fine with spuriously getting no free slot
8993
if [[ $(bats_semaphore_get_free_slot_count) -gt 0 ]]; then
90-
bats_run_under_lock bash -c bats_semaphore_acquire_while_locked && break
94+
bats_run_under_"$BATS_LOCKING_IMPLEMENTATION" \
95+
bash -c bats_semaphore_acquire_while_locked \
96+
&& break
9197
fi
9298
sleep 1
9399
done

libexec/bats-core/bats

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ while [[ "$#" -ne 0 ]]; do
194194
;;
195195
--report-formatter)
196196
shift
197-
if [[ $1 =~ ^(cat|pretty|junit|tap|tap13)$ ]]; then
197+
if [[ $1 =~ ^(cat|pretty|junit|tap|tap13|/.*)$ ]]; then
198198
report_formatter="$1"
199199
else
200200
printf "Unknown report formatter '%s', valid options are %s\n" "$1" "${VALID_FORMATTERS}"
@@ -296,7 +296,6 @@ if [[ ! $BATS_LINE_REFERENCE_FORMAT =~ (custom|comma_line|colon|uri) ]]; then
296296
abort "Invalid BATS_LINE_REFERENCE_FORMAT '$BATS_LINE_REFERENCE_FORMAT' (e.g. via --line-reference-format)"
297297
fi
298298

299-
300299
if [[ -n "${BATS_RUN_TMPDIR:-}" ]]; then
301300
if [[ -d "$BATS_RUN_TMPDIR" ]]; then
302301
printf "Error: BATS_RUN_TMPDIR (%s) already exists\n" "$BATS_RUN_TMPDIR" >&2
@@ -480,9 +479,22 @@ trap 'BATS_INTERRUPTED=true' INT # let the lower levels handle the interruption
480479

481480
set -o pipefail execfail
482481

482+
# pipe stdin into command and to stdout
483+
# pipe command stdout to file
484+
bats_tee() { # <output-file> <command...>
485+
local output_file=$1 status=0
486+
shift
487+
exec 3<&1 # use FD3 to get around pipe
488+
tee >(cat >&3) | "$@" >"$output_file" || status=$?
489+
if (( status != 0 )); then
490+
printf "ERROR: command \`%s\` failed with status %d\n" "$*" "$status" >&2
491+
fi
492+
return $status
493+
}
494+
483495
if [[ -n "$report_formatter" ]]; then
484496
exec bats-exec-suite "${flags[@]}" "${filenames[@]}" |
485-
tee >("$interpolated_report_formatter" "${report_formatter_flags[@]}" >"${BATS_REPORT_OUTPUT_DIR}/${BATS_REPORT_FILENAME}") |
497+
bats_tee "${BATS_REPORT_OUTPUT_DIR}/${BATS_REPORT_FILENAME}" "$interpolated_report_formatter" "${report_formatter_flags[@]}" |
486498
bats_test_count_validator |
487499
"$interpolated_formatter" "${formatter_flags[@]}"
488500
else

libexec/bats-core/bats-exec-file

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ bats_run_teardown_file() {
118118
fi
119119
}
120120

121+
# shellcheck disable=SC2317
121122
bats_file_teardown_trap() {
122123
bats_run_teardown_file
123124
bats_file_exit_trap in-teardown_trap
@@ -126,6 +127,7 @@ bats_file_teardown_trap() {
126127
# shellcheck source=lib/bats-core/common.bash
127128
source "$BATS_ROOT/lib/bats-core/common.bash"
128129

130+
# shellcheck disable=SC2317
129131
bats_file_exit_trap() {
130132
local -r last_return_code=$?
131133
if [[ ${1:-} != in-teardown_trap ]]; then
@@ -165,7 +167,7 @@ bats_file_exit_trap() {
165167
bats_generate_warning 3 --no-stacktrace "$BATS_TEST_FILENAME"
166168
fi
167169

168-
exit $bats_exec_file_status
170+
exit "$bats_exec_file_status"
169171
}
170172

171173
function setup_file() {
@@ -292,6 +294,7 @@ bats_run_tests() {
292294
bats_exec_file_status=0
293295

294296
if [[ "${BATS_RUN_TESTS_SKIPPED-}" ]]; then
297+
# shellcheck disable=SC2317
295298
bats_test_begin() {
296299
printf "ok %d %s # skip %s\n" "$test_number_in_suite" "$1" "$BATS_RUN_TESTS_SKIPPED_REASON" >&3
297300
return 1

libexec/bats-core/bats-exec-suite

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ done
9999
if [[ "$num_jobs" != 1 ]]; then
100100
if ! type -p parallel >/dev/null && parallel --version &>/dev/null && [[ -z "$bats_no_parallelize_across_files" ]]; then
101101
abort "Cannot execute \"${num_jobs}\" jobs without GNU parallel"
102-
exit 1
103102
fi
104103
# shellcheck source=lib/bats-core/semaphore.bash
105104
source "${BATS_ROOT}/lib/bats-core/semaphore.bash"
@@ -310,12 +309,10 @@ fi
310309

311310
if [[ -n "$bats_no_parallelize_across_files" ]] && [[ ! "$num_jobs" -gt 1 ]]; then
312311
abort "The flag --no-parallelize-across-files requires at least --jobs 2"
313-
exit 1
314312
fi
315313

316314
if [[ -n "$bats_no_parallelize_within_files" ]] && [[ ! "$num_jobs" -gt 1 ]]; then
317315
abort "The flag --no-parallelize-across-files requires at least --jobs 2"
318-
exit 1
319316
fi
320317

321318
# only abort on the lowest levels
@@ -353,6 +350,7 @@ trap bats_suite_exit_trap EXIT
353350

354351
exec 3<&1
355352

353+
# shellcheck disable=SC2317
356354
bats_suite_exit_trap() {
357355
local print_bats_out="${BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS}"
358356
if [[ -z "${BATS_SETUP_SUITE_COMPLETED}" || -z "${BATS_TEARDOWN_SUITE_COMPLETED}" ]]; then
@@ -400,6 +398,7 @@ bats_run_teardown_suite() {
400398
fi
401399
}
402400

401+
# shellcheck disable=SC2317
403402
bats_teardown_suite_trap() {
404403
bats_run_teardown_suite
405404
bats_suite_exit_trap

libexec/bats-core/bats-exec-test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ bats_exit_trap() {
203203
# Marks the test as failed due to timeout.
204204
# The actual termination of subprocesses is done via pkill in the background
205205
# process in bats_start_timeout_countdown
206+
# shellcheck disable=SC2317
206207
bats_timeout_trap() {
207208
BATS_TIMED_OUT=1
208209
BATS_DEBUG_LAST_STACK_TRACE_IS_VALID=
@@ -302,6 +303,7 @@ bats_perform_test() {
302303
# is this skipped from outside ?
303304
if [[ -n "${BATS_TEST_SKIPPED-}" ]]; then
304305
# forward skip (with message) by overriding setup
306+
# shellcheck disable=SC2317
305307
setup() {
306308
skip "$BATS_TEST_SKIPPED"
307309
}

test/bats.bats

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ setup() {
140140
[ "$status" -eq 1 ]
141141
[ "${#lines[@]}" -eq 4 ]
142142
[ "${lines[1]}" = 'not ok 1 a failing test' ]
143-
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_with_negated_command.bats, line 3)" ]
143+
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_with_negated_command.bats, line 4)" ]
144144
[ "${lines[3]}" = "# \`! true' failed" ]
145145
}
146146

@@ -532,12 +532,13 @@ END_OF_ERR_MSG
532532
[ "$status" -eq 1 ]
533533

534534
expectedNumberOfTests=12
535-
linesPerTest=5
535+
linesPerTest=6
536536

537537
outputOffset=1
538538
currentErrorLine=9
539539

540540
for t in $(seq $expectedNumberOfTests); do
541+
echo "t=$t outputOffset=$outputOffset currentErrorLine=$currentErrorLine"
541542
# shellcheck disable=SC2076
542543
[[ "${lines[$outputOffset]}" =~ "not ok $t " ]]
543544

@@ -1544,3 +1545,20 @@ enforce_own_process_group() {
15441545
[ "${lines[3]}" == "WARNING: This test run only contains tests tagged \`bats:focus\`!" ]
15451546
[ "${#lines[@]}" == 4 ]
15461547
}
1548+
1549+
@test "Bats waits for report formatter to finish" {
1550+
REPORT_FORMATTER=$FIXTURE_ROOT/gobble_up_stdin_sleep_and_print_finish.bash
1551+
bats_require_minimum_version 1.5.0
1552+
reentrant_run -0 bats "$FIXTURE_ROOT/passing.bats" --report-formatter "$REPORT_FORMATTER" --output "$BATS_TEST_TMPDIR"
1553+
1554+
echo "'$(< "$BATS_TEST_TMPDIR/report.log")'"
1555+
[ "$(< "$BATS_TEST_TMPDIR/report.log")" = Finished ]
1556+
}
1557+
1558+
@test "Failing report formatter fails test run" {
1559+
REPORT_FORMATTER=$FIXTURE_ROOT/exit_11.bash
1560+
bats_require_minimum_version 1.5.0
1561+
reentrant_run ! bats "$FIXTURE_ROOT/passing.bats" --report-formatter "$REPORT_FORMATTER" --output "$BATS_TEST_TMPDIR"
1562+
1563+
[[ "${output}" = *"ERROR: command \`$REPORT_FORMATTER\` failed with status 11"* ]] || false
1564+
}

test/fixtures/bats/comment_style.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ function should_be_found_with_function_parens_and_whitespace() { #@test
2323
}
2424

2525
should_not_be_found() {
26+
# shellcheck disable=SC2317
2627
false
2728
#@test
2829
}
2930

3031
should_not_be_found() {
32+
# shellcheck disable=SC2317
3133
false
3234
} #@test

0 commit comments

Comments
 (0)