diff --git a/nvm.sh b/nvm.sh index b839d631..6a268062 100755 --- a/nvm.sh +++ b/nvm.sh @@ -83,18 +83,24 @@ nvm_type() { fi } -# resolves like `command "${1}"` does: only executables on the PATH, -# ignoring shell functions and aliases -nvm_has_executable() { +# prints the path `command "${1}"` runs: only executables on the PATH, +# ignoring shell functions and aliases; unlike `which`, available everywhere +nvm_executable_path() { ( # `|| true` so that shells with errexit-style options (eg, zsh's ERR_RETURN) # do not abort the subshell when the name is not an alias or a function unalias "${1-}" 2>/dev/null || true unset -f "${1-}" 2>/dev/null || true - command -v "${1-}" >/dev/null 2>&1 + # bash's and dash's `command -v` trust the hash table, even for a removed file + hash -r 2>/dev/null || true + command -v "${1-}" 2>/dev/null ) } +nvm_has_executable() { + nvm_executable_path "${1-}" >/dev/null +} + nvm_has_non_aliased() { nvm_has "${1-}" && ! nvm_is_alias "${1-}" } @@ -2224,7 +2230,7 @@ nvm_alias() { nvm_ls_current() { local NVM_LS_CURRENT_NODE_PATH - if ! NVM_LS_CURRENT_NODE_PATH="$(command which node 2>/dev/null)"; then + if ! NVM_LS_CURRENT_NODE_PATH="$(nvm_executable_path node)"; then nvm_echo 'none' elif nvm_tree_contains_path "$(nvm_version_dir iojs)" "${NVM_LS_CURRENT_NODE_PATH}"; then nvm_add_iojs_prefix "$(iojs --version 2>/dev/null)" @@ -6975,7 +6981,7 @@ nvm() { if [ "_${NVM_VERSION}" = '_system' ]; then if nvm_has_system_iojs >/dev/null 2>&1 || nvm_has_system_node >/dev/null 2>&1; then local NVM_BIN - NVM_BIN="$(nvm use system >/dev/null 2>&1 && command which node)" + NVM_BIN="$(nvm use system >/dev/null 2>&1 && nvm_executable_path node)" if [ -n "${NVM_BIN}" ]; then nvm_echo "${NVM_BIN}" return @@ -7366,7 +7372,7 @@ nvm() { nvm_version_greater nvm_version_greater_than_or_equal_to \ nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \ nvm_has_system_node nvm_has_system_iojs \ - nvm_download nvm_get_latest nvm_has nvm_has_executable nvm_install_default_packages nvm_get_default_packages \ + nvm_download nvm_get_latest nvm_has nvm_has_executable nvm_executable_path nvm_install_default_packages nvm_get_default_packages \ nvm_curl_use_compression nvm_curl_version \ nvm_auto nvm_supports_xz \ nvm_echo nvm_err nvm_grep nvm_cd nvm_cd_root nvm_npm \ diff --git a/test/fast/Unit tests/nvm_executable_path b/test/fast/Unit tests/nvm_executable_path new file mode 100755 index 00000000..b0e6d154 --- /dev/null +++ b/test/fast/Unit tests/nvm_executable_path @@ -0,0 +1,50 @@ +#!/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 diff --git a/test/fast/Unit tests/nvm_has_executable b/test/fast/Unit tests/nvm_has_executable new file mode 100755 index 00000000..2f4776ff --- /dev/null +++ b/test/fast/Unit tests/nvm_has_executable @@ -0,0 +1,47 @@ +#!/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_has_executable nvm_test_tool 2>&1)" && die 'with no nvm_test_tool, expected failure' +[ -z "${OUTPUT}" ] || die "with no nvm_test_tool, expected no output, got >${OUTPUT}<" + +# defined before the alias, so zsh does not expand the alias in the definition +nvm_test_tool() { echo function; } +alias nvm_test_tool='echo alias' +OUTPUT="$(nvm_has_executable nvm_test_tool 2>&1)" && die 'with only a function and an alias, expected failure' +[ -z "${OUTPUT}" ] || die "with only a function and an alias, expected no output, got >${OUTPUT}<" +# called directly, not in a command substitution, so that a change to the caller would persist +nvm_has_executable nvm_test_tool && die 'with only a function and an alias, expected failure' +# the backslash skips the alias, but not the function +[ "_$(\nvm_test_tool)" = '_function' ] || die 'the caller lost its nvm_test_tool function' +alias nvm_test_tool >/dev/null 2>&1 || die 'the caller lost its nvm_test_tool alias' +unalias nvm_test_tool +unset -f nvm_test_tool + +printf '#!/bin/sh\necho tool\n' > "${TEST_DIR}/nvm_test_tool" +chmod a+x "${TEST_DIR}/nvm_test_tool" + +OUTPUT="$(nvm_has_executable nvm_test_tool 2>&1)" || die "expected success, got >${OUTPUT}<" +[ -z "${OUTPUT}" ] || die "expected no output, got >${OUTPUT}<" + +# 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_has_executable nvm_test_tool 2>&1)" && die 'with nvm_test_tool removed but hashed, expected failure' +[ -z "${OUTPUT}" ] || die "with nvm_test_tool removed but hashed, expected no output, got >${OUTPUT}<" + +cleanup diff --git a/test/fast/Unit tests/nvm_ls_current and nvm which system without which b/test/fast/Unit tests/nvm_ls_current and nvm which system without which new file mode 100755 index 00000000..eb3851a8 --- /dev/null +++ b/test/fast/Unit tests/nvm_ls_current and nvm which system without which @@ -0,0 +1,57 @@ +#!/bin/sh + +TEST_DIR='' + +cleanup() { + case "${TEST_DIR}" in + /*) rm -rf "${TEST_DIR}" ;; + esac + unset -f node 2>/dev/null + alias node='node' + unalias node +} +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +TEST_DIR="$(mktemp -d)" || die 'could not make a temp dir' + +# some systems, like Fedora container images, have no `which` +# `command -v` prints only the name of a builtin, so search the PATH for each tool +for TOOL in dirname printf sed awk tr mv; do + TOOL_PATH="$(echo "${PATH}" | tr ':' '\n' | while IFS= read -r DIR; do + if [ -f "${DIR}/${TOOL}" ] && [ -x "${DIR}/${TOOL}" ]; then + echo "${DIR}/${TOOL}" + break + fi + done)" + [ -n "${TOOL_PATH}" ] || die "could not find ${TOOL}" + ln -s "${TOOL_PATH}" "${TEST_DIR}/${TOOL}" || die "could not link ${TOOL}" +done +printf '#!/bin/sh\necho v20.0.0\n' > "${TEST_DIR}/node" +chmod a+x "${TEST_DIR}/node" + +OUTPUT="$(PATH="${TEST_DIR}" nvm_ls_current)" +[ "_${OUTPUT}" = '_system' ] || die "without \`which\`, nvm_ls_current printed >${OUTPUT}<, expected >system<" + +OUTPUT="$(PATH="${TEST_DIR}" nvm which system 2>&1)" +[ "_${OUTPUT}" = "_${TEST_DIR}/node" ] || die "without \`which\`, nvm which system printed >${OUTPUT}<, expected >${TEST_DIR}/node<" + +# defined before the alias, so zsh does not expand the alias in the definition +node() { echo function; } +OUTPUT="$(PATH="${TEST_DIR}" nvm which system 2>&1)" +[ "_${OUTPUT}" = "_${TEST_DIR}/node" ] || die "without \`which\`, with a node function, nvm which system printed >${OUTPUT}<, expected >${TEST_DIR}/node<" +unset -f node + +alias node='echo alias' +OUTPUT="$(PATH="${TEST_DIR}" nvm which system 2>&1)" +[ "_${OUTPUT}" = "_${TEST_DIR}/node" ] || die "without \`which\`, with node aliased, nvm which system printed >${OUTPUT}<, expected >${TEST_DIR}/node<" +unalias node + +# like `which`, a removed node is gone, even while the shell still has its path hashed +OUTPUT="$(PATH="${TEST_DIR}"; hash node; mv "${TEST_DIR}/node" "${TEST_DIR}/removed-node"; nvm_ls_current)" +mv "${TEST_DIR}/removed-node" "${TEST_DIR}/node" || die 'could not restore the node fixture' +[ "_${OUTPUT}" = '_none' ] || die "with node removed but still hashed, nvm_ls_current printed >${OUTPUT}<, expected >none<" + +cleanup