diff --git a/libexec/bats-core/bats b/libexec/bats-core/bats index 9827950d97..6728eae6ac 100755 --- a/libexec/bats-core/bats +++ b/libexec/bats-core/bats @@ -7,7 +7,7 @@ version() { usage() { version - printf "Usage: bats [-c] [-r] [-p | -t] [ ...]\n" + printf "Usage: bats [-c] [-r] [-p | -t] [--global-helper=script_name] [ ...]\n" } abort() { @@ -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 @@ -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 diff --git a/libexec/bats-core/bats-exec-test b/libexec/bats-core/bats-exec-test index ba5eef7df2..fdd1a64bd9 100755 --- a/libexec/bats-core/bats-exec-test +++ b/libexec/bats-core/bats-exec-test @@ -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 @@ -413,5 +419,6 @@ if [[ "$#" -eq 0 ]]; then fi else bats_evaluate_preprocessed_source + bats_load_global_helper bats_perform_test "$@" fi