diff --git a/install.sh b/install.sh index cd60d6e7..0cd20ae1 100755 --- a/install.sh +++ b/install.sh @@ -197,7 +197,7 @@ install_nvm_from_git() { NVM_REMOTE_SUFFIX=$((NVM_REMOTE_SUFFIX + 1)) NVM_REMOTE_NAME="nvm-origin-${NVM_REMOTE_SUFFIX}" done - command git --git-dir="${INSTALL_DIR}/.git" remote add "${NVM_REMOTE_NAME}" "${NVM_GIT_SOURCE}" || return 2 + command git --git-dir="${INSTALL_DIR}/.git" remote add "${NVM_REMOTE_NAME}" "${NVM_GIT_SOURCE}" || exit 2 fi else fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!" diff --git a/test/install_script/nvm_do_install_git_failure b/test/install_script/nvm_do_install_git_failure new file mode 100755 index 00000000..c2c32e9e --- /dev/null +++ b/test/install_script/nvm_do_install_git_failure @@ -0,0 +1,30 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +INSTALLER="$(dirname "$0")/../../install.sh" + +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 NVM_SOURCE="${TEST_DIR}/new-source" +export PROFILE="${TEST_DIR}/.bashrc" +unset NVM_ENV NVM_INSTALL_VERSION NVM_INSTALL_GITHUB_REPO NODE_VERSION + +command git init -q "${NVM_DIR}" || die 'Unable to initialize repository' +command git --git-dir="${NVM_DIR}/.git" remote add origin "${TEST_DIR}/old-source" || die 'Unable to add origin' +# A concurrent git operation can prevent the installer from adding its remote. +: > "${NVM_DIR}/.git/config.lock" || die 'Unable to lock Git config' +: > "${PROFILE}" || die 'Unable to create profile' +# Record sourcing without invoking the system npm if the installer continues. +printf '%s\n' ': > "${NVM_DIR}/sourced"' 'nvm_check_global_modules() { return 0; }' > "${NVM_DIR}/nvm.sh" || die 'Unable to write nvm.sh' + +for METHOD in '' git; do + EXIT_CODE=0 + OUTPUT="$(METHOD="${METHOD}" LC_ALL=C bash +e "${INSTALLER}" 2>&1)" || EXIT_CODE=$? + [ "${EXIT_CODE}" = '2' ] || die "Expected exit 2 for METHOD=${METHOD}, got ${EXIT_CODE}: ${OUTPUT}" + printf '%s\n' "${OUTPUT}" | grep -q 'could not lock config file .*: File exists$' || die "Expected Git config lock error: ${OUTPUT}" + [ -f "${PROFILE}" ] && [ ! -s "${PROFILE}" ] || die 'The installer modified the profile after failing' + [ ! -e "${NVM_DIR}/sourced" ] || die 'The installer sourced nvm.sh after failing' +done