diff --git a/install.sh b/install.sh index 0cd20ae1..221b2190 100755 --- a/install.sh +++ b/install.sh @@ -269,15 +269,13 @@ nvm_install_node() { fi nvm_echo "=> Installing Node.js version $NODE_VERSION_LOCAL" - nvm install "$NODE_VERSION_LOCAL" - local CURRENT_NVM_NODE - - CURRENT_NVM_NODE="$(nvm_version current)" - if [ "$(nvm_version "$NODE_VERSION_LOCAL")" = "$CURRENT_NVM_NODE" ]; then - nvm_echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed" - else - nvm_echo >&2 "Failed to install Node.js $NODE_VERSION_LOCAL" - fi + local NVM_INSTALL_STATUS + nvm install "$NODE_VERSION_LOCAL" || { + NVM_INSTALL_STATUS=$? + nvm_echo >&2 "nvm install $NODE_VERSION_LOCAL failed" + return "$NVM_INSTALL_STATUS" + } + nvm_echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed" } install_nvm_as_script() { @@ -600,7 +598,9 @@ nvm_do_install() { nvm_check_global_modules - nvm_install_node + local NVM_NODE_STATUS + NVM_NODE_STATUS=0 + nvm_install_node || NVM_NODE_STATUS=$? nvm_reset @@ -609,6 +609,7 @@ nvm_do_install() { if ${BASH_OR_ZSH} ; then command printf "${COMPLETION_STR}" fi + return "$NVM_NODE_STATUS" } # diff --git a/test/install_script/nvm_do_install_node_status b/test/install_script/nvm_do_install_node_status new file mode 100755 index 00000000..f2e7a6ef --- /dev/null +++ b/test/install_script/nvm_do_install_node_status @@ -0,0 +1,105 @@ +#!/bin/sh + +[ -n "${BASH_VERSION-}" ] || exec bash "$0" "$@" + +# Do not let shell tracing satisfy the assertions on installer output. +set +x +v + +die () { echo "$@" ; exit 1; } + +# Re-execute this test so that the installer is not called in a conditional context. +if [ "${1-}" = '--install' ]; then + NODE_VERSION="$2" + TEST_NODE_STATUS="$3" + TEST_NODE_CURRENT="$4" + + NVM_ENV=testing \. "$(dirname "$0")/../../install.sh" || die 'Unable to source install.sh' + + # Exercise the installer without requiring a downloader or installing real Node.js versions. + nvm_has_executable() { [ "$1" = curl ]; } + nvm_download() { + case "$2" in + */nvm.sh) + cat > "$4" <<'NVM_SH' +nvm() { + [ "$1" = install ] && [ "$2" = "$NODE_VERSION" ] || return 99 + : > "${NVM_DIR}/node-install-called" + return "$TEST_NODE_STATUS" +} +nvm_version() { + case "$1" in + current) printf '%s\n' "$TEST_NODE_CURRENT" ;; + stable) cat "${NVM_DIR}/alias/stable" ;; + --lts) printf '%s\n' 'N/A'; return 3 ;; + *) printf '%s\n' 'v22.1.0' ;; + esac +} +NVM_SH + ;; + *) : > "$4" ;; + esac + } + nvm_check_global_modules() { return 0; } + + # Observe cleanup even when errexit terminates the child at the final return. + trap ' + if ! type nvm_reset >/dev/null 2>&1 && ! type nvm_do_install >/dev/null 2>&1; then + : > "${NVM_DIR}/installer-reset" + fi + ' EXIT + nvm_do_install + exit $? +fi + +TEST_DIR="$(mktemp -d)" || die 'Unable to create temporary directory' +trap 'rm -rf "${TEST_DIR}"' 0 +export HOME="${TEST_DIR}" +export NVM_DIR="${TEST_DIR}/nvm" +export PROFILE=/dev/null +export METHOD=script +unset NVM_SOURCE NVM_INSTALL_VERSION NVM_INSTALL_GITHUB_REPO +mkdir -p "${NVM_DIR}/alias" || die 'Unable to create installation directory' +printf '%s\n' 'v20.1.0' > "${NVM_DIR}/alias/stable" || die 'Unable to create local stable alias' + +check_install() { + NODE_VERSION="$1" + TEST_NODE_STATUS="$2" + TEST_NODE_CURRENT="$3" + EXPECTED_STATUS="$4" + shift 4 + rm -f "${NVM_DIR}/node-install-called" "${NVM_DIR}/installer-reset" "${TEST_DIR}/stderr" || die 'Unable to clear test markers' + EXIT_CODE=0 + OUTPUT="$(bash "$@" "$0" --install "${NODE_VERSION}" "${TEST_NODE_STATUS}" "${TEST_NODE_CURRENT}" 2>"${TEST_DIR}/stderr")" || EXIT_CODE=$? + ERRORS="$(cat "${TEST_DIR}/stderr")" + [ "${EXIT_CODE}" = "${EXPECTED_STATUS}" ] || die "Expected exit ${EXPECTED_STATUS}, got ${EXIT_CODE} (bash $*): ${OUTPUT} ${ERRORS}" + echo "${OUTPUT}" | grep -Fq 'Close and reopen your terminal' || die "Missing nvm setup instructions: ${OUTPUT}" + # shellcheck disable=SC2016 + echo "${OUTPUT}" | grep -Fq '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' || die "Missing nvm source instructions: ${OUTPUT}" + [ -f "${NVM_DIR}/installer-reset" ] || die 'Installer did not finish cleanup' + if [ -z "${NODE_VERSION}" ]; then + [ ! -e "${NVM_DIR}/node-install-called" ] || die 'Unexpected Node.js installation without NODE_VERSION' + else + [ -f "${NVM_DIR}/node-install-called" ] || die 'Node.js installation was not attempted' + if [ "${EXPECTED_STATUS}" = 0 ]; then + echo "${OUTPUT}" | grep -Fq 'has been successfully installed' || die "Missing Node.js success message: ${OUTPUT}" + else + echo "${ERRORS}" | grep -Fxq "nvm install ${NODE_VERSION} failed" || die "Missing nvm install failure message on stderr: ${ERRORS}" + ! echo "${OUTPUT}${ERRORS}" | grep -Fq 'has been successfully installed' || die "Unexpected Node.js success message: ${OUTPUT}" + fi + fi +} + +check_install '22.1.0' 23 system 23 +# Even if the requested version is already active, a failed install is not a success. +check_install '22.1.0' 23 v22.1.0 23 +check_install '22.1.0' 0 v22.1.0 0 +# Remote stable can differ from a user-defined local stable alias. +check_install stable 0 v22.1.0 0 +# nvm install accepts --lts, but nvm_version does not accept this bare flag. +check_install --lts 0 v22.1.0 0 +check_install '' 23 system 0 + +# A nonzero install result must not abort before cleanup and guidance under errexit. +check_install '22.1.0' 23 system 23 -e +check_install '22.1.0' 23 v22.1.0 23 --posix -e +check_install '22.1.0' 0 v22.1.0 0 -e