Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schulze-vireso committed Jun 8, 2022
1 parent ed58442 commit deced7c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
1 change: 1 addition & 0 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export BATS_TMPDIR="${TMPDIR:-/tmp}"
BATS_TMPDIR=${BATS_TMPDIR%/} # chop off trailing / to avoid duplication
export BATS_RUN_TMPDIR=
export BATS_GUARANTEED_MINIMUM_VERSION=0.0.0
export BATS_USER_HOME="${BATS_USER_HOME:-$HOME/.bats}"

if [[ ! -d "${BATS_TMPDIR}" ]];then
printf "Error: BATS_TMPDIR (%s) does not exist or is not a directory" "${BATS_TMPDIR}" >&2
Expand Down
3 changes: 0 additions & 3 deletions libexec/bats-core/bats-exec-file
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ while [[ "$#" -ne 0 ]]; do
# use singular to allow for users to override in file
BATS_NO_PARALLELIZE_WITHIN_FILE=1
;;
--rerun-failed)
flags+=(--rerun-failed)
;;
--dummy-flag)
;;
--trace)
Expand Down
20 changes: 13 additions & 7 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ while [[ "$#" -ne 0 ]]; do
;;
--rerun-failed)
bats_rerun_failed=1
flags+=("--rerun-failed")
;;
--dummy-flag)
;;
Expand Down Expand Up @@ -124,10 +123,18 @@ bats_gather_tests() {
test_count="${#all_tests[@]}"
}

TEST_ROOT=${1%/*}
BATS_RUN_LOGS_DIRECTORY="$TEST_ROOT/.bats/run-logs"
export BATS_RERUN_FAILED_FILE="$BATS_RUN_LOGS_DIRECTORY/failed-tests.list"
export BATS_RERUN_FAILED_TMPFILE="${BATS_RUN_TMPDIR}/failed-tests.list.tmp"

: > "$BATS_RERUN_FAILED_TMPFILE"

if [[ -n "$bats_rerun_failed" ]]; then
mkdir -p "$PWD/.bats"
export BATS_RERUN_FAILED_FILE="$PWD/.bats/rerun-failed-tests.list"
export BATS_RERUN_FAILED_TMPFILE="${BATS_RERUN_FAILED_FILE}.tmp"
if [[ ! -d "$BATS_RUN_LOGS_DIRECTORY" ]]; then
printf "Error: --rerun-failed needs '%s/' to save failed tests. Please create this folder, add it to .gitignore and try again.\n" "$BATS_RUN_LOGS_DIRECTORY"
exit 1
fi
if [[ -s "$BATS_RERUN_FAILED_FILE" ]]; then
test_count="$(wc -l < "$BATS_RERUN_FAILED_FILE")"
cp "$BATS_RERUN_FAILED_FILE" "$TESTS_LIST_FILE"
Expand All @@ -136,10 +143,9 @@ if [[ -n "$bats_rerun_failed" ]]; then
printf "Delete the file '%s' to run all tests again.\n" "${BATS_RERUN_FAILED_FILE#"$PWD/"}" >&2
test_count=0
else
printf "No recording of previos runs found. Running all tests!\n" >&2
printf "No recording of previous runs found. Running all tests!\n" >&2
bats_gather_tests "$@"
fi
: >"$BATS_RERUN_FAILED_TMPFILE" # truncate the file if it exists, else create
else
bats_gather_tests "$@"
fi
Expand Down Expand Up @@ -205,7 +211,7 @@ bats_suite_exit_trap() {
printf "\n# Received SIGINT, aborting ...\n\n"
fi

if [[ -n "$bats_rerun_failed" && "${BATS_INTERRUPTED-NOTSET}" == NOTSET ]]; then
if [[ -d "$BATS_RUN_LOGS_DIRECTORY" && "${BATS_INTERRUPTED-NOTSET}" == NOTSET ]]; then
# only overwrite file once we finished with all tests
# else we would discard tests when aborting a test run with CTRL+C
mv "$BATS_RERUN_FAILED_TMPFILE" "$BATS_RERUN_FAILED_FILE"
Expand Down
7 changes: 1 addition & 6 deletions libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ while [[ "$#" -ne 0 ]]; do
shift
BATS_GATHER_TEST_OUTPUTS_IN="$1"
;;
--rerun-failed)
BATS_RERUN_FAILED=1
;;
*)
break
;;
Expand Down Expand Up @@ -128,9 +125,7 @@ bats_exit_trap() {
local print_bats_out="${BATS_SHOW_OUTPUT_OF_SUCCEEDING_TESTS}"

if [[ -z "$BATS_TEST_COMPLETED" || -z "$BATS_TEARDOWN_COMPLETED" || "${BATS_INTERRUPTED-NOTSET}" != NOTSET ]]; then
if [[ -n "$BATS_RERUN_FAILED" ]]; then
printf "%s\t%s\n" "$BATS_TEST_FILENAME" "$BATS_TEST_NAME" >> "${BATS_RERUN_FAILED_TMPFILE}"
fi
printf "%s\t%s\n" "$BATS_TEST_FILENAME" "$BATS_TEST_NAME" >> "${BATS_RERUN_FAILED_TMPFILE}"
if [[ "$BATS_ERROR_STATUS" -eq 0 ]]; then
# For some versions of bash, `$?` may not be set properly for some error
# conditions before triggering the EXIT trap directly (see #72 and #81).
Expand Down
30 changes: 21 additions & 9 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1234,29 +1234,38 @@ END_OF_ERR_MSG
TMPDIR=/tmp/ bats "$FIXTURE_ROOT/BATS_variables_dont_contain_double_slashes.bats"
}

@test "Without .bats/run-logs --rerun-failed returns an error" {
run -1 bats --rerun-failed "$FIXTURE_ROOT/passing_and_failing.bats"
[[ "${lines[0]}" == "Error: --rerun-failed needs"*".bats/run-logs/' to save failed tests. Please create this folder, add it to .gitignore and try again." ]] || false
}

@test "Without previous recording --rerun-failed runs all tests and then reruns only failed tests" {
cd "$BATS_TEST_TMPDIR" # don't pollute the source folder
run -1 bats --rerun-failed "$FIXTURE_ROOT/passing_and_failing.bats"
cp "$FIXTURE_ROOT/passing_and_failing.bats" .
mkdir -p .bats/run-logs
run -1 bats --rerun-failed "passing_and_failing.bats"
# without previous recording, all tests should be run
[ "${lines[0]}" == 'No recording of previos runs found. Running all tests!' ]
[ "${lines[0]}" == 'No recording of previous runs found. Running all tests!' ]
[ "${lines[1]}" == '1..2' ]
[ "${lines[2]}" == 'ok 1 a passing test' ]
[ "${lines[3]}" == 'not ok 2 a failing test' ]

run -1 bats --tap --rerun-failed "$FIXTURE_ROOT/passing_and_failing.bats"
run -1 bats --tap --rerun-failed "passing_and_failing.bats"
# now we should only run the failing test
[ "${lines[0]}" == 1..1 ]
[ "${lines[1]}" == "not ok 1 a failing test" ]
}

@test "--rerun-failed gives warning on empty failed test list" {
cd "$BATS_TEST_TMPDIR" # don't pollute the source folder
cp "$FIXTURE_ROOT/passing.bats" .
mkdir -p .bats/run-logs
# have no failing tests
run -0 bats --rerun-failed "$FIXTURE_ROOT/passing.bats"
run -0 bats --rerun-failed "passing.bats"
# try to rerun the empty list of failing tests
run -0 bats --rerun-failed "$FIXTURE_ROOT/passing.bats"
run -0 bats --rerun-failed "passing.bats"
[ "${lines[0]}" == "There where no failed tests in the last recorded run." ]
[ "${lines[1]}" == "Delete the file '.bats/rerun-failed-tests.list' to run all tests again." ]
[ "${lines[1]}" == "Delete the file '.bats/run-logs/failed-tests.list' to run all tests again." ]
[ "${lines[2]}" == "1..0" ]
[ "${#lines[@]}" -eq 3 ]
}
Expand All @@ -1272,20 +1281,23 @@ enforce_own_process_group() {
fi

cd "$BATS_TEST_TMPDIR" # don't pollute the source folder
local RERUN_FILE=".bats/rerun-failed-tests.list"
cp "$FIXTURE_ROOT/sigint_in_failing_test.bats" .
mkdir -p .bats/run-logs
local RERUN_FILE=".bats/run-logs/failed-tests.list"

# don't hang yet, so we get a useful rerun file
run -1 env bats --rerun-failed "$FIXTURE_ROOT/sigint_in_failing_test.bats"
run -1 env DONT_ABORT=1 bats "sigint_in_failing_test.bats"

orig_date=$(date -r "$RERUN_FILE")

ls -alR .
# check that we have something to rerun
[ -s "$RERUN_FILE" ]

sleep 1 # ensure we would get different timestamps for each run

# now rerun but abort midrun
run -1 enforce_own_process_group bats --rerun-failed "$FIXTURE_ROOT/sigint_in_failing_test.bats"
run -1 enforce_own_process_group bats --rerun-failed "$sigint_in_failing_test.bats"

new_date=$(date -r "$RERUN_FILE")
echo "$orig_date"
Expand Down
4 changes: 0 additions & 4 deletions test/fixtures/bats/sigint_in_failing_test.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
setup() {
load '../../concurrent-coordination'
}

@test "failing" {
if [[ -z "${DONT_ABORT:-}" ]]; then
# emulate CTRL-C by sending SIGINT to the whole process group
Expand Down

0 comments on commit deced7c

Please sign in to comment.