Skip to content

Commit

Permalink
Add support for global helper script
Browse files Browse the repository at this point in the history
Addresses sstephenson/bats#255

This feature takes the same type of arguments 'load' would.  It
can be used at the command line with --global-helper=helper_script,
or via environment variable by exporting BATS_GLOBAL_HELPER_SCRIPT.
  • Loading branch information
cdevinesr committed Jul 3, 2018
1 parent 924f967 commit 24dac79
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 14 additions & 2 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version() {

usage() {
version
printf "Usage: bats [-c] [-r] [-p | -t] <test> [<test> ...]\n"
printf "Usage: bats [-c] [-r] [-p | -t] [--global-helper=script_name] <test> [<test> ...]\n"
}

abort() {
Expand All @@ -28,6 +28,8 @@ help() {
echo " -r, --recursive Include tests in subdirectories"
echo " -t, --tap Show results in TAP format"
echo " -v, --version Display the version number"
echo " --global-helper Load a global test helper script before each test file"
echo " is processed"
echo
echo " For more information, see https://github.com/bats-core/bats-core"
echo
Expand Down Expand Up @@ -57,7 +59,17 @@ arguments=()
for arg in "$@"; do
if [[ "${arg:0:1}" = "-" ]]; then
if [[ "${arg:1:1}" = "-" ]]; then
options[${#options[*]}]="${arg:2}"
if [ "$(expr "${arg:2}" : "global-helper=.*$")" -ne 0 ]; then
BATS_GLOBAL_HELPER_SCRIPT="${arg##*=}"

if [ -z "${BATS_GLOBAL_HELPER_SCRIPT}" ]; then
abort "You must supply a script with --global-helper"
else
export BATS_GLOBAL_HELPER_SCRIPT
fi
else
options[${#options[*]}]="${arg:2}"
fi
else
index=1
while option="${arg:$index:1}"; do
Expand Down
7 changes: 7 additions & 0 deletions libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ bats_evaluate_preprocessed_source() {
source "$BATS_TEST_SOURCE"
}

bats_load_global_helper() {
if [[ -n "${BATS_GLOBAL_HELPER_SCRIPT:-}" ]]; then
load "${BATS_GLOBAL_HELPER_SCRIPT}"
fi
}

exec 3<&1

if [[ "$#" -eq 0 ]]; then
Expand All @@ -413,5 +419,6 @@ if [[ "$#" -eq 0 ]]; then
fi
else
bats_evaluate_preprocessed_source
bats_load_global_helper
bats_perform_test "$@"
fi

0 comments on commit 24dac79

Please sign in to comment.