diff --git a/test/hooks.bats b/test/hooks.bats new file mode 100644 index 00000000..738c55ed --- /dev/null +++ b/test/hooks.bats @@ -0,0 +1,38 @@ +#!/usr/bin/env bats + +load test_helper + +create_hook() { + mkdir -p "$1/$2" + touch "$1/$2/$3" +} + +@test "prints usage help given no argument" { + run rbenv-hooks + assert_failure "Usage: rbenv hooks " +} + +@test "prints list of hooks" { + path1="${RBENV_TEST_DIR}/rbenv.d" + path2="${RBENV_TEST_DIR}/etc/rbenv_hooks" + create_hook "$path1" exec "hello.bash" + create_hook "$path1" exec "ahoy.bash" + create_hook "$path1" exec "invalid.sh" + create_hook "$path1" which "boom.bash" + create_hook "$path2" exec "bueno.bash" + + RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec + assert_success + assert_line 0 "${RBENV_TEST_DIR}/rbenv.d/exec/ahoy.bash" + assert_line 1 "${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash" + assert_line 2 "${RBENV_TEST_DIR}/etc/rbenv_hooks/exec/bueno.bash" +} + +@test "resolves relative paths" { + path="${RBENV_TEST_DIR}/rbenv.d" + create_hook "$path" exec "hello.bash" + mkdir -p "$HOME" + + RBENV_HOOK_PATH="${HOME}/../rbenv.d" run rbenv-hooks exec + assert_success "${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash" +} diff --git a/test/test_helper.bash b/test/test_helper.bash index 7d52e442..85e8853b 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -15,7 +15,10 @@ teardown() { } flunk() { - echo "$@" | sed "s:${RBENV_ROOT}:\$RBENV_ROOT:" >&2 + { if [ "$#" -eq 0 ]; then cat - + else echo "$@" + fi + } | sed "s:${RBENV_TEST_DIR}:TEST_DIR:" >&2 return 1 } @@ -35,18 +38,27 @@ assert_failure() { fi } -assert_output() { - if [ "$output" != "$1" ]; then - flunk "expected: $1" || true - flunk "got: $output" +assert_equal() { + if [ "$1" != "$2" ]; then + { echo "expected: $1" + echo "actual: $2" + } | flunk fi } +assert_output() { + assert_equal "$1" "$output" +} + assert_line() { - for line in "${lines[@]}"; do - if [ "$line" = "$1" ]; then return 0; fi - done - flunk "expected line \`$1'" + if [ "$1" -ge 0 ] 2>/dev/null; then + assert_equal "$2" "${lines[$1]}" + else + for line in "${lines[@]}"; do + if [ "$line" = "$1" ]; then return 0; fi + done + flunk "expected line \`$1'" + fi } refute_line() {