Skip to content

Commit

Permalink
Merge pull request #692 from martin-schulze-vireso/fix/issue-690-blan…
Browse files Browse the repository at this point in the history
…k-junit-report-in-docker

Fix blank junit report in docker
  • Loading branch information
martin-schulze-vireso committed Feb 3, 2023
2 parents 5a47c79 + fb0909f commit fe7df4f
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
run: |
npm pack ./
sudo npm install -g ./bats-*.tgz
bats test
bats test --print-output-on-failure
windows:
runs-on: windows-2019
Expand Down Expand Up @@ -257,7 +257,7 @@ jobs:
<<EOF cat >test.sh
apk add sudo python3 # install bats-file's dependencies
ln -sf python3 /usr/bin/python # bats-file uses python without version
bats --tap --print-output-on-failure bats-*/test/"
bats --tap --print-output-on-failure bats-*/test/
EOF
docker run -itv "$PWD":/code --entrypoint bash bats:test test.sh
shell: 'script -q -e -c "bash {0}"' # work around tty issues
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
### Fixed

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

#### Documentation

Expand Down
20 changes: 12 additions & 8 deletions lib/bats-core/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ bats_binary_search() { # <search-value> <array-name>
return 1
}

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

local -a sorted_array=()
local -i j i=0
for ((j = 1; j <= $#; ++j)); do
for ((i = ${#sorted_array[@]}; i >= 0; --i)); do
if [[ $i -eq 0 || ${sorted_array[$((i - 1))]} < ${!j} ]]; then
sorted_array[$i]=${!j}
local -i i
while (( $# > 0 )); do # loop over input values
local current_value="$1"
shift
for ((i = ${#sorted_array[@]}; i >= 0; --i)); do # loop over output array from end
if (( i == 0 )) || [[ ${sorted_array[i - 1]} < $current_value ]]; then
# shift bigger elements one position to the end
sorted_array[i]=$current_value
break
else
sorted_array[$i]=${sorted_array[$((i - 1))]}
# insert new element at (freed) desired location
sorted_array[i]=${sorted_array[i - 1]}
fi
done
done
Expand Down
30 changes: 18 additions & 12 deletions lib/bats-core/semaphore.bash
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#!/usr/bin/env bash

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

if command -v flock >/dev/null; then
bats_run_under_lock() {
flock "$BATS_SEMAPHORE_DIR" "$@"
}
elif command -v shlock >/dev/null; then
bats_run_under_lock() {
bats_run_under_shlock() {
local lockfile="$BATS_SEMAPHORE_DIR/shlock.lock"
while ! shlock -p $$ -f "$lockfile"; do
sleep 1
Expand All @@ -23,6 +16,17 @@ bats_semaphore_setup() {
rm -f "$lockfile"
return $status
}

# setup the semaphore environment for the loading file
bats_semaphore_setup() {
export -f bats_semaphore_get_free_slot_count
export -f bats_semaphore_acquire_while_locked
export BATS_SEMAPHORE_DIR="$BATS_RUN_TMPDIR/semaphores"

if command -v flock >/dev/null; then
BATS_LOCKING_IMPLEMENTATION=flock
elif command -v shlock >/dev/null; then
BATS_LOCKING_IMPLEMENTATION=shlock
else
printf "ERROR: flock/shlock is required for parallelization within files!\n" >&2
exit 1
Expand Down Expand Up @@ -87,7 +91,9 @@ bats_semaphore_acquire_slot() {
while true; do
# don't lock for reading, we are fine with spuriously getting no free slot
if [[ $(bats_semaphore_get_free_slot_count) -gt 0 ]]; then
bats_run_under_lock bash -c bats_semaphore_acquire_while_locked && break
bats_run_under_"$BATS_LOCKING_IMPLEMENTATION" \
bash -c bats_semaphore_acquire_while_locked \
&& break
fi
sleep 1
done
Expand Down
18 changes: 15 additions & 3 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ while [[ "$#" -ne 0 ]]; do
;;
--report-formatter)
shift
if [[ $1 =~ ^(cat|pretty|junit|tap|tap13)$ ]]; then
if [[ $1 =~ ^(cat|pretty|junit|tap|tap13|/.*)$ ]]; then
report_formatter="$1"
else
printf "Unknown report formatter '%s', valid options are %s\n" "$1" "${VALID_FORMATTERS}"
Expand Down Expand Up @@ -296,7 +296,6 @@ if [[ ! $BATS_LINE_REFERENCE_FORMAT =~ (custom|comma_line|colon|uri) ]]; then
abort "Invalid BATS_LINE_REFERENCE_FORMAT '$BATS_LINE_REFERENCE_FORMAT' (e.g. via --line-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 Expand Up @@ -480,9 +479,22 @@ trap 'BATS_INTERRUPTED=true' INT # let the lower levels handle the interruption

set -o pipefail execfail

# pipe stdin into command and to stdout
# pipe command stdout to file
bats_tee() { # <output-file> <command...>
local output_file=$1 status=0
shift
exec 3<&1 # use FD3 to get around pipe
tee >(cat >&3) | "$@" >"$output_file" || status=$?
if (( status != 0 )); then
printf "ERROR: command \`%s\` failed with status %d\n" "$*" "$status" >&2
fi
return $status
}

if [[ -n "$report_formatter" ]]; then
exec bats-exec-suite "${flags[@]}" "${filenames[@]}" |
tee >("$interpolated_report_formatter" "${report_formatter_flags[@]}" >"${BATS_REPORT_OUTPUT_DIR}/${BATS_REPORT_FILENAME}") |
bats_tee "${BATS_REPORT_OUTPUT_DIR}/${BATS_REPORT_FILENAME}" "$interpolated_report_formatter" "${report_formatter_flags[@]}" |
bats_test_count_validator |
"$interpolated_formatter" "${formatter_flags[@]}"
else
Expand Down
5 changes: 4 additions & 1 deletion libexec/bats-core/bats-exec-file
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ bats_run_teardown_file() {
fi
}

# shellcheck disable=SC2317
bats_file_teardown_trap() {
bats_run_teardown_file
bats_file_exit_trap in-teardown_trap
Expand All @@ -126,6 +127,7 @@ bats_file_teardown_trap() {
# shellcheck source=lib/bats-core/common.bash
source "$BATS_ROOT/lib/bats-core/common.bash"

# shellcheck disable=SC2317
bats_file_exit_trap() {
local -r last_return_code=$?
if [[ ${1:-} != in-teardown_trap ]]; then
Expand Down Expand Up @@ -165,7 +167,7 @@ bats_file_exit_trap() {
bats_generate_warning 3 --no-stacktrace "$BATS_TEST_FILENAME"
fi

exit $bats_exec_file_status
exit "$bats_exec_file_status"
}

function setup_file() {
Expand Down Expand Up @@ -292,6 +294,7 @@ bats_run_tests() {
bats_exec_file_status=0

if [[ "${BATS_RUN_TESTS_SKIPPED-}" ]]; then
# shellcheck disable=SC2317
bats_test_begin() {
printf "ok %d %s # skip %s\n" "$test_number_in_suite" "$1" "$BATS_RUN_TESTS_SKIPPED_REASON" >&3
return 1
Expand Down
5 changes: 2 additions & 3 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ done
if [[ "$num_jobs" != 1 ]]; then
if ! type -p parallel >/dev/null && parallel --version &>/dev/null && [[ -z "$bats_no_parallelize_across_files" ]]; then
abort "Cannot execute \"${num_jobs}\" jobs without GNU parallel"
exit 1
fi
# shellcheck source=lib/bats-core/semaphore.bash
source "${BATS_ROOT}/lib/bats-core/semaphore.bash"
Expand Down Expand Up @@ -310,12 +309,10 @@ fi

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

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

# only abort on the lowest levels
Expand Down Expand Up @@ -353,6 +350,7 @@ trap bats_suite_exit_trap EXIT

exec 3<&1

# shellcheck disable=SC2317
bats_suite_exit_trap() {
local print_bats_out="${BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS}"
if [[ -z "${BATS_SETUP_SUITE_COMPLETED}" || -z "${BATS_TEARDOWN_SUITE_COMPLETED}" ]]; then
Expand Down Expand Up @@ -400,6 +398,7 @@ bats_run_teardown_suite() {
fi
}

# shellcheck disable=SC2317
bats_teardown_suite_trap() {
bats_run_teardown_suite
bats_suite_exit_trap
Expand Down
2 changes: 2 additions & 0 deletions libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ bats_exit_trap() {
# Marks the test as failed due to timeout.
# The actual termination of subprocesses is done via pkill in the background
# process in bats_start_timeout_countdown
# shellcheck disable=SC2317
bats_timeout_trap() {
BATS_TIMED_OUT=1
BATS_DEBUG_LAST_STACK_TRACE_IS_VALID=
Expand Down Expand Up @@ -302,6 +303,7 @@ bats_perform_test() {
# is this skipped from outside ?
if [[ -n "${BATS_TEST_SKIPPED-}" ]]; then
# forward skip (with message) by overriding setup
# shellcheck disable=SC2317
setup() {
skip "$BATS_TEST_SKIPPED"
}
Expand Down
22 changes: 20 additions & 2 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ setup() {
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[1]}" = 'not ok 1 a failing test' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_with_negated_command.bats, line 3)" ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_with_negated_command.bats, line 4)" ]
[ "${lines[3]}" = "# \`! true' failed" ]
}

Expand Down Expand Up @@ -532,12 +532,13 @@ END_OF_ERR_MSG
[ "$status" -eq 1 ]

expectedNumberOfTests=12
linesPerTest=5
linesPerTest=6

outputOffset=1
currentErrorLine=9

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

Expand Down Expand Up @@ -1544,3 +1545,20 @@ enforce_own_process_group() {
[ "${lines[3]}" == "WARNING: This test run only contains tests tagged \`bats:focus\`!" ]
[ "${#lines[@]}" == 4 ]
}

@test "Bats waits for report formatter to finish" {
REPORT_FORMATTER=$FIXTURE_ROOT/gobble_up_stdin_sleep_and_print_finish.bash
bats_require_minimum_version 1.5.0
reentrant_run -0 bats "$FIXTURE_ROOT/passing.bats" --report-formatter "$REPORT_FORMATTER" --output "$BATS_TEST_TMPDIR"

echo "'$(< "$BATS_TEST_TMPDIR/report.log")'"
[ "$(< "$BATS_TEST_TMPDIR/report.log")" = Finished ]
}

@test "Failing report formatter fails test run" {
REPORT_FORMATTER=$FIXTURE_ROOT/exit_11.bash
bats_require_minimum_version 1.5.0
reentrant_run ! bats "$FIXTURE_ROOT/passing.bats" --report-formatter "$REPORT_FORMATTER" --output "$BATS_TEST_TMPDIR"

[[ "${output}" = *"ERROR: command \`$REPORT_FORMATTER\` failed with status 11"* ]] || false
}
2 changes: 2 additions & 0 deletions test/fixtures/bats/comment_style.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ function should_be_found_with_function_parens_and_whitespace() { #@test
}

should_not_be_found() {
# shellcheck disable=SC2317
false
#@test
}

should_not_be_found() {
# shellcheck disable=SC2317
false
} #@test
2 changes: 2 additions & 0 deletions test/fixtures/bats/exit_11.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exit 11
11 changes: 11 additions & 0 deletions test/fixtures/bats/external_function_calls.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,68 @@ load test_helper
help_me && false
}


@test "Call true function && return 1 stackdepth=1" {
help_me
help_me && return 1
}

@test "Call true function and invert stackdepth=2" {
help_me
# shellcheck disable=SC2314
! help_me
}

@test "Call false function || false stackdepth=1" {
# shellcheck disable=SC2314
! failing_helper
failing_helper || false
}

@test "Call false function && return 1 stackdepth=1" {
# shellcheck disable=SC2314
! failing_helper
failing_helper || return 1
}

@test "Call false function stackdepth=2" {
# shellcheck disable=SC2314
! failing_helper
failing_helper
}


@test "Call return_0 function && false stackdepth=1" {
return_0
return_0 && false
}


@test "Call return_0 function && return 1 stackdepth=1" {
return_0
return_0 && return 1
}

@test "Call return_0 function and invert stackdepth=2" {
return_0
# shellcheck disable=SC2314
! return_0
}

@test "Call return_1 function || false stackdepth=1" {
# shellcheck disable=SC2314
! return_1
return_1 || false
}

@test "Call return_1 function && return 1 stackdepth=1" {
# shellcheck disable=SC2314
! return_1
return_1 || return 1
}

@test "Call return_1 function stackdepth=2" {
# shellcheck disable=SC2314
! return_1
return_1
}
1 change: 1 addition & 0 deletions test/fixtures/bats/failing_with_negated_command.bats
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@test "a failing test" {
true
# shellcheck disable=SC2314
! true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
cat >/dev/null # eat up all input
sleep 1
echo "Finished" # mark finish
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ setup_suite() {
teardown_suite() {
echo "teardown_suite before" >&2
return 1
# shellcheck disable=SC2317
echo "teardown_suite after" >&2
}

0 comments on commit fe7df4f

Please sign in to comment.