1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-09-29 20:48:27 +02:00

Allow sourcing of helper files from BATS_LIB_PATH

The existing functionality of load() is preserved:
    1. Load helper files in the same directory the current testfile
       resides in
    2. Load helper files by absolute path

Additionally an environment variable BATS_LIB_PATH can be defined to
be source helper files from.

If BATS_LIB_PATH is empty the following locations are used:
    1. $HOME/.bats/lib
       Allows users to manage helper libraries themselves, similar to
       python/ruby/...
    2. /usr/lib/bats
       Allows to install helper libraries via a package manager.
This commit is contained in:
Nelo Wallus 2017-03-22 15:27:15 +01:00 committed by Nelo-T. Wallus
parent 1ed87ecb7f
commit 617e086a26

View File

@ -33,18 +33,38 @@ load() {
local name="$1" local name="$1"
local filename local filename
if [ "${name:0:1}" = "/" ]; then # Argument is absolute, verify that the path exists and return after
filename="${name}" # sourcing
else if [[ "${name:0:1}" == "/" ]]; then
filename="$BATS_TEST_DIRNAME/${name}.bash" filename=$name
fi
if [[ ! -f "$filename" ]]; then if [[ ! -f "$filename" ]]; then
echo "bats: $filename does not exist" >&2 echo "bats: $filename does not exist" >&2
exit 1 exit 1
fi fi
source "${filename}" source "$filename"
return
fi
# Set libpath with directory of current test file
# Defaults to BATS_LIB_PATH e.g. for testing
local libpath="${BATS_LIB_PATH:-$HOME/.bats/lib:/usr/lib/bats}"
libpath="$BATS_TEST_DIRNAME:$libpath"
# Test for library file in each libpath, source and return if it
# exists
for part in ${libpath//:/ }; do
filename="$part/$name.bash"
if [[ -f "$filename" ]]; then
source "$filename"
return
fi
done
echo "bats: No file $name in BATS_LIB_PATH found" >&2
exit 1
} }
run() { run() {