1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-09-29 12:38:26 +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 filename
if [ "${name:0:1}" = "/" ]; then
filename="${name}"
else
filename="$BATS_TEST_DIRNAME/${name}.bash"
# Argument is absolute, verify that the path exists and return after
# sourcing
if [[ "${name:0:1}" == "/" ]]; then
filename=$name
if [[ ! -f "$filename" ]]; then
echo "bats: $filename does not exist" >&2
exit 1
fi
source "$filename"
return
fi
if [[ ! -f "$filename" ]]; then
echo "bats: $filename does not exist" >&2
exit 1
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"
source "${filename}"
# 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() {