Skip to content

Commit

Permalink
feat: add setup_file and teardown_file
Browse files Browse the repository at this point in the history
note: most of this was merged under bats-core/bats-core#246

- Avoid messing with internal variable BATS_ROOT
- Don't let teardown errors stop exit output
- add helpers in lib/bats-core and add to install
- Move parallelization into exec-file but parallelize across files
- allow for preserving environment when running in parallel
- Unify output into bats run specific tmp dir
- Validate the test count in bin/bats:
    This tests what is actually written and not what we wanted to run.
    The latter is even more problematic in the parallel test cases,
    as we can't easily retrieve the count from the subshells.
- add devcontainer for simplified development in VS Code containers
- Cleanup for shellcheck
- Cleanup BATS_RUN_TMPDIR after run:
    this should avoid colissions between successive runs
    Error when the run dir is reused
  • Loading branch information
martin-schulze-vireso authored and sublimino committed Apr 27, 2020
1 parent 805650e commit 10b0daf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM bash:${bashver}

# Install parallel and accept the citation notice (we aren't using this in a
# context where it make sense to cite GNU Parallel).
RUN apk add --no-cache parallel && \
RUN apk add --no-cache parallel ncurses && \
mkdir -p ~/.parallel && touch ~/.parallel/will-cite

RUN ln -s /opt/bats/bin/bats /usr/sbin/bats
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ There was an initial [call for maintainers][call-maintain] for the original Bats

## Copyright

© 2017-2018 bats-core organization
© 2017-2020 bats-core organization

© 2011-2016 Sam Stephenson

Expand Down
16 changes: 8 additions & 8 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ if [[ "${#arguments[@]}" -eq 0 ]]; then
abort 'Must specify at least one <test>'
fi


if [[ -n "$output" ]]; then
if [[ ! -w "${output}" ]]; then
abort "Output path ${output} is not writeable"
fi
export BATS_REPORT_OUTPUT_PATH="$output"
fi

filenames=()
for filename in "${arguments[@]}"; do
expand_path "$filename" 'filename'
Expand All @@ -248,13 +256,5 @@ done
# shellcheck source=lib/bats-core/validator.bash
source "$BATS_ROOT/lib/bats-core/validator.bash"

if [[ -n "$output" ]]; then
if [[ ! -w "${output}" ]]; then
printf "Error: output path %s is not writeable\n" "${output}" >&2
exit 1
fi
export BATS_REPORT_OUTPUT_PATH="$output"
fi

set -o pipefail execfail
exec bats-exec-suite "${flags[@]}" "${filenames[@]}" | bats_test_count_validator | "bats-format-${formatter}" "${formatter_flags[@]}"
2 changes: 2 additions & 0 deletions shellcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ done < <(
)

LC_ALL=C.UTF-8 shellcheck "${targets[@]}"

exit $?
9 changes: 5 additions & 4 deletions test/parallell.bats
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ setup() {
[[ "${lines[$i]}" == "ok $i slow test $t" ]]
done
done
# In theory it should take 3s, but let's give it bit of extra time instead.
# also check that parallelization happens accross all files instead of
# In theory it should take 3s, but let's give it bit of extra time for load tolerance.
# (Since there is no limit to load, we cannot totally avoid erronous failures by limited tolerance.)
# Also check that parallelization happens accross all files instead of
# linearizing between files, which requires at least 12s
[[ "$duration" -lt 12 ]]
[[ "$duration" -lt 12 ]] || (echo "If this fails on Travis, make sure the failure is repeatable and not due to heavy load."; false)
}

@test "setup_file is not over parallelized" {
Expand All @@ -68,4 +69,4 @@ setup() {
[[ $duration -ge 6 ]]
# parallelization should at least get rid of 1/4th the total runtime
[[ $duration -lt 9 ]]
}
}

0 comments on commit 10b0daf

Please sign in to comment.