Skip to content

Commit

Permalink
fix: reimplement load to be more useable and consistent
Browse files Browse the repository at this point in the history
Existing support is preserved (absolute paths and relative paths with a `.bash` suffix), and now *any* relative path is supported, as well as scripts on the system `PATH`.

Fixes sstephenson#79
  • Loading branch information
dimo414 authored and sublimino committed Apr 26, 2020
1 parent 29e899d commit add6cd2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/bats-core/test_functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
BATS_TEST_DIRNAME="${BATS_TEST_FILENAME%/*}"
BATS_TEST_NAMES=()

# Shorthand for source-ing files relative to the BATS_TEST_DIRNAME,
# optionally with a .bash suffix appended. If the argument doesn't
# resolve relative to BATS_TEST_DIRNAME it is sourced as-is.
load() {
local name="$1"
local filename
local file="${1:?}"

if [[ "${name:0:1}" == '/' ]]; then
filename="${name}"
else
filename="$BATS_TEST_DIRNAME/${name}.bash"
# For backwards-compatibility first look for a .bash-suffixed file.
# TODO consider flipping the order here; it would be more consistent
# and less surprising to look for an exact-match first.
if [[ -f "${BATS_TEST_DIRNAME}/${file}.bash" ]]; then
file="${BATS_TEST_DIRNAME}/${file}.bash"
elif [[ -f "${BATS_TEST_DIRNAME}/${file}" ]]; then
file="${BATS_TEST_DIRNAME}/${file}"
fi

if [[ ! -f "$filename" ]]; then
printf 'bats: %s does not exist\n' "$filename" >&2
if [[ ! -f "$file" ]] && ! type -P "$file" >/dev/null; then
printf 'bats: %s does not exist\n' "$file" >&2
exit 1
fi

# Dynamically loaded user files provided outside of Bats.
# Dynamically loaded user file provided outside of Bats.
# Note: 'source "$file" || exit' doesn't work on bash3.2.
# shellcheck disable=SC1090
source "${filename}"
source "${file}"
}

run() {
Expand Down
18 changes: 18 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ fixtures bats
[ $status -eq 0 ]
}

@test "load sources relative scripts with filename extension" {
HELPER_NAME="test_helper.bash" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}

@test "load aborts if the specified script does not exist" {
HELPER_NAME="nonexistent" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 1 ]
Expand All @@ -211,6 +216,19 @@ fixtures bats
[ $status -eq 1 ]
}

@test "load relative script with ambiguous name" {
HELPER_NAME="ambiguous" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}

@test "load supports scripts on the PATH" {
path_dir="$BATS_TMPNAME/path"
mkdir -p "$path_dir"
cp "${FIXTURE_ROOT}/test_helper.bash" "${path_dir}/on_path"
PATH="${path_dir}:$PATH" HELPER_NAME="on_path" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}

@test "output is discarded for passing tests and printed for failing tests" {
run bats "$FIXTURE_ROOT/output.bats"
[ $status -eq 1 ]
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/bats/ambiguous
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Helper to detect regressions in load's lookup ordering.
# An exact name match should be prioritized over name.bash.

echo "Should not have loaded this file!" >&2
exit 1
6 changes: 6 additions & 0 deletions test/fixtures/bats/ambiguous.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Helper to detect regressions in load's lookup ordering.
# An exact name match should be prioritized over name.bash.

echo "This is the expected file to load."

help_me() { :; }

0 comments on commit add6cd2

Please sign in to comment.