#!/bin/sh TEST_DIR='' cleanup() { case "${TEST_DIR}" in /*) rm -rf "${TEST_DIR}" ;; esac unset -f nvm_test_tool 2>/dev/null alias nvm_test_tool='nvm_test_tool' unalias nvm_test_tool } die () { echo "$@" ; cleanup ; exit 1; } : nvm.sh \. ../../../nvm.sh TEST_DIR="$(mktemp -d)" || die 'could not make a temp dir' PATH="${TEST_DIR}:${PATH}" OUTPUT="$(nvm_executable_path nvm_test_tool)" && die "with no nvm_test_tool, expected failure, got >${OUTPUT}<" [ -z "${OUTPUT}" ] || die "with no nvm_test_tool, expected no output, got >${OUTPUT}<" printf '#!/bin/sh\necho tool\n' > "${TEST_DIR}/nvm_test_tool" chmod a+x "${TEST_DIR}/nvm_test_tool" OUTPUT="$(nvm_executable_path nvm_test_tool)" || die "expected success, got >${OUTPUT}<" [ "_${OUTPUT}" = "_${TEST_DIR}/nvm_test_tool" ] || die "expected >${TEST_DIR}/nvm_test_tool<, got >${OUTPUT}<" # defined before the alias, so zsh does not expand the alias in the definition nvm_test_tool() { echo function; } OUTPUT="$(nvm_executable_path nvm_test_tool)" || die "with a function, expected success, got >${OUTPUT}<" [ "_${OUTPUT}" = "_${TEST_DIR}/nvm_test_tool" ] || die "with a function, expected >${TEST_DIR}/nvm_test_tool<, got >${OUTPUT}<" # called directly, not in a command substitution, so that a change to the caller would persist nvm_executable_path nvm_test_tool >/dev/null || die 'with a function, expected success' [ "_$(nvm_test_tool)" = '_function' ] || die 'the caller lost its nvm_test_tool function' unset -f nvm_test_tool alias nvm_test_tool='echo alias' OUTPUT="$(nvm_executable_path nvm_test_tool)" || die "with an alias, expected success, got >${OUTPUT}<" [ "_${OUTPUT}" = "_${TEST_DIR}/nvm_test_tool" ] || die "with an alias, expected >${TEST_DIR}/nvm_test_tool<, got >${OUTPUT}<" nvm_executable_path nvm_test_tool >/dev/null || die 'with an alias, expected success' alias nvm_test_tool >/dev/null 2>&1 || die 'the caller lost its nvm_test_tool alias' unalias nvm_test_tool # a removed file is gone, even while the shell still has its path hashed OUTPUT="$(hash nvm_test_tool; command rm "${TEST_DIR}/nvm_test_tool"; nvm_executable_path nvm_test_tool)" && die "with nvm_test_tool removed but hashed, expected failure, got >${OUTPUT}<" [ -z "${OUTPUT}" ] || die "with nvm_test_tool removed but hashed, expected no output, got >${OUTPUT}<" cleanup