diff --git a/nvm.sh b/nvm.sh index 52c2b7f8..d7b124a0 100755 --- a/nvm.sh +++ b/nvm.sh @@ -622,7 +622,7 @@ nvm_rc_version() { NVMRC_PATH="$(nvm_find_nvmrc)" if [ ! -e "${NVMRC_PATH}" ]; then if [ "${NVM_SILENT:-0}" -ne 1 ]; then - nvm_err "No .nvmrc file found" + nvm_err "No version provided and no .nvmrc file found" fi return 1 fi diff --git a/test/fast/Unit tests/nvm_rc_version errors when no version and no .nvmrc b/test/fast/Unit tests/nvm_rc_version errors when no version and no .nvmrc new file mode 100755 index 00000000..40a24fa1 --- /dev/null +++ b/test/fast/Unit tests/nvm_rc_version errors when no version and no .nvmrc @@ -0,0 +1,39 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + cd "${ORIG_PWD}" 2>/dev/null || true + [ -n "${TMP_DIR-}" ] && rm -rf "${TMP_DIR}" +} + +: nvm.sh +\. ../../../nvm.sh + +\. ../../common.sh + +ORIG_PWD="$(pwd)" + +# Run from a fresh, empty directory so no ambient .nvmrc above the test dir +# is found by the upward lookup. +TMP_DIR="$(mktemp -d)" +cd "${TMP_DIR}" || die "could not cd to temp dir" + +# The message must name both halves of the problem: no argument AND no .nvmrc. +try_err nvm_rc_version +EXPECTED='No version provided and no .nvmrc file found' +[ "_${CAPTURED_STDERR}" = "_${EXPECTED}" ] \ + || die "nvm_rc_version did not print >${EXPECTED}<; got >${CAPTURED_STDERR}<" +[ "_${CAPTURED_EXIT_CODE}" = "_1" ] \ + || die "nvm_rc_version expected exit code 1; got ${CAPTURED_EXIT_CODE}" + +# NVM_SILENT suppresses the message but the call still fails. +export NVM_SILENT=1 +try_err nvm_rc_version +unset NVM_SILENT +[ -z "${CAPTURED_STDERR}" ] \ + || die "NVM_SILENT nvm_rc_version should be silent; got >${CAPTURED_STDERR}<" +[ "_${CAPTURED_EXIT_CODE}" = "_1" ] \ + || die "NVM_SILENT nvm_rc_version expected exit code 1; got ${CAPTURED_EXIT_CODE}" + +cleanup