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:
parent
1ed87ecb7f
commit
617e086a26
|
@ -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
|
||||||
|
|
||||||
|
if [[ ! -f "$filename" ]]; then
|
||||||
|
echo "bats: $filename does not exist" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
source "$filename"
|
||||||
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -f "$filename" ]]; then
|
# Set libpath with directory of current test file
|
||||||
echo "bats: $filename does not exist" >&2
|
# Defaults to BATS_LIB_PATH e.g. for testing
|
||||||
exit 1
|
local libpath="${BATS_LIB_PATH:-$HOME/.bats/lib:/usr/lib/bats}"
|
||||||
fi
|
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() {
|
run() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user