Skip to content

Commit

Permalink
Make basher-init and the include function portable
Browse files Browse the repository at this point in the history
- Use local in stead of declare
- Use . in stead of source
- Add test for sh
  • Loading branch information
hugot committed Jan 16, 2018
1 parent 3fe36c6 commit fbc5bb5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/include.dash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
. "$BASHER_ROOT/lib/include.sh"
6 changes: 3 additions & 3 deletions lib/include.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include() {
declare package="$1"
declare file="$2"
local package="$1"
local file="$2"

if [ -z "$package" ] || [ -z "$file" ]; then
echo "Usage: include <package> <file>" >&2
Expand All @@ -13,7 +13,7 @@ include() {
fi

if [ -e "$BASHER_PREFIX/packages/$package/$file" ]; then
source "$BASHER_PREFIX/packages/$package/$file" >&2
. "$BASHER_PREFIX/packages/$package/$file" >&2
else
echo "File not found: $BASHER_PREFIX/packages/$package/$file" >&2
return 1
Expand Down
4 changes: 2 additions & 2 deletions libexec/basher-init
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ case "$shell" in
esac

if [ -e "$BASHER_ROOT/lib/include.$shell" ]; then
echo "source \"\$BASHER_ROOT/lib/include.$shell\""
echo ". \"\$BASHER_ROOT/lib/include.$shell\""
fi

if [ -e "$BASHER_ROOT/completions/basher.$shell" ]; then
echo "source \"\$BASHER_ROOT/completions/basher.$shell\""
echo ". \"\$BASHER_ROOT/completions/basher.$shell\""
fi

case "$shell" in
Expand Down
15 changes: 13 additions & 2 deletions tests/basher-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ load test_helper

@test "setup include function if it exists" {
run basher-init - bash
assert_line -n 4 'source "$BASHER_ROOT/lib/include.bash"'
assert_line -n 4 '. "$BASHER_ROOT/lib/include.bash"'
}

@test "doesn't setup include function if it doesn't exist" {
Expand All @@ -39,7 +39,7 @@ load test_helper
@test "setup basher completions if available" {
run basher-init - bash
assert_success
assert_line -n 5 'source "$BASHER_ROOT/completions/basher.bash"'
assert_line -n 5 '. "$BASHER_ROOT/completions/basher.bash"'
}

@test "does not setup basher completions if not available" {
Expand All @@ -60,3 +60,14 @@ load test_helper
assert_success
assert_line -n 6 'fpath=("$BASHER_ROOT/cellar/completions/zsh" $fpath)'
}

hasShell() {
which "$1" &>>/dev/null
}

@test "is sh-compatible" {
hasShell sh || skip "sh was not found in path."
run sh -ec 'eval "$(basher init - sh)"'
assert_success
}

0 comments on commit fbc5bb5

Please sign in to comment.