From 9f32e7bad46bbaf6206628f8314db313e226cd0a Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 4 Sep 2024 13:55:11 -0700 Subject: [PATCH] [Fix] `nvm ls`, `nvm alias`, `nvm install`: error when an LTS name is invalid --- nvm.sh | 17 ++++++--- .../nvm install with nonlowercase LTS name | 35 +++++++++++++++++++ test/fast/Unit tests/nvm ls-remote | 10 ++++-- .../Unit tests/nvm_install_no_progress_bar | 2 +- test/installation_node/install LTS | 20 +++++------ 5 files changed, 67 insertions(+), 17 deletions(-) create mode 100755 test/fast/Unit tests/nvm install with nonlowercase LTS name diff --git a/nvm.sh b/nvm.sh index ce8bba56a1..9eeede2ac6 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1253,7 +1253,9 @@ nvm_alias() { nvm_err 'An alias is required.' return 1 fi - ALIAS="$(nvm_normalize_lts "${ALIAS}")" + if ! ALIAS="$(nvm_normalize_lts "${ALIAS}")"; then + return $? + fi if [ -z "${ALIAS}" ]; then return 2 @@ -1656,7 +1658,9 @@ $VERSION_LIST EOF if [ -n "${LTS-}" ]; then - LTS="$(nvm_normalize_lts "lts/${LTS}")" + if ! LTS="$(nvm_normalize_lts "lts/${LTS}")"; then + return $? + fi LTS="${LTS#lts/}" fi @@ -3421,9 +3425,11 @@ nvm() { ;; esac + local EXIT_CODE VERSION="$(NVM_VERSION_ONLY=true NVM_LTS="${LTS-}" nvm_remote_version "${provided_version}")" + EXIT_CODE="$?" - if [ "${VERSION}" = 'N/A' ]; then + if [ "${VERSION}" = 'N/A' ] || [ $EXIT_CODE -ne 0 ]; then local LTS_MSG local REMOTE_CMD if [ "${LTS-}" = '*' ]; then @@ -3432,6 +3438,10 @@ nvm() { elif [ -n "${LTS-}" ]; then LTS_MSG="(with LTS filter '${LTS}') " REMOTE_CMD="nvm ls-remote --lts=${LTS}" + if [ -z "${provided_version}" ]; then + nvm_err "Version with LTS filter '${LTS}' not found - try \`${REMOTE_CMD}\` to browse available versions." + return 3 + fi else REMOTE_CMD='nvm ls-remote' fi @@ -3496,7 +3506,6 @@ nvm() { FLAVOR="$(nvm_node_prefix)" fi - local EXIT_CODE EXIT_CODE=0 if nvm_is_version_installed "${VERSION}"; then diff --git a/test/fast/Unit tests/nvm install with nonlowercase LTS name b/test/fast/Unit tests/nvm install with nonlowercase LTS name new file mode 100755 index 0000000000..7dd27f59cb --- /dev/null +++ b/test/fast/Unit tests/nvm install with nonlowercase LTS name @@ -0,0 +1,35 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +REMOTE="${PWD}/mocks/nvm_ls_remote.txt" +REMOTE_IOJS="${PWD}/mocks/nvm_ls_remote_iojs.txt" + +nvm_download() { + if [ "$*" = "-L -s $(nvm_get_mirror node std)/index.tab -o -" ]; then + cat "${REMOTE}" + elif [ "$*" = "-L -s $(nvm_get_mirror iojs)/index.tab -o -" ]; then + cat "${REMOTE_IOJS}" + else + nvm_err "unknown nvm_download call: $*" + return 42 + fi +} + +nvm_install_binary() { + return 42 +} +nvm_install_source() { + return 42 +} + +ACTUAL="$(nvm install lts/ARGON 2>&1)" +EXIT_CODE=$? +[ $EXIT_CODE -eq 3 ] || die "Expected exit code of 3, got ${EXIT_CODE}" + +EXPECTED="LTS names must be lowercase +Version with LTS filter 'ARGON' not found - try \`nvm ls-remote --lts=ARGON\` to browse available versions." + +[ "${ACTUAL}" = "${EXPECTED}" ] || die "Expected >${EXPECTED}<, got >${ACTUAL}<" diff --git a/test/fast/Unit tests/nvm ls-remote b/test/fast/Unit tests/nvm ls-remote index 0c65bfb57f..5ba5e18803 100755 --- a/test/fast/Unit tests/nvm ls-remote +++ b/test/fast/Unit tests/nvm ls-remote @@ -1,7 +1,5 @@ #!/bin/sh -set -e - die () { echo "$@" ; cleanup ; exit 1; } cleanup() { @@ -56,6 +54,14 @@ printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do INDEX=$(($INDEX + 1)) done +OUTPUT="$(nvm ls-remote lts/ARGON 2>&1)" +EXIT_CODE=$? +[ $EXIT_CODE -eq 3 ] || die "nvm ls-remote lts/ARGON did not exit 3, got '${EXIT_CODE}'" + +EXPECTED_OUTPUT="LTS names must be lowercase + N/A" +[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote lts/ARGON did not output expected error message; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<" + REMOTE="${PWD}/mocks/nvm_ls_remote.txt" nvm_ls_remote() { cat "${REMOTE}" diff --git a/test/fast/Unit tests/nvm_install_no_progress_bar b/test/fast/Unit tests/nvm_install_no_progress_bar index 94c41a796e..d5c9c2797c 100755 --- a/test/fast/Unit tests/nvm_install_no_progress_bar +++ b/test/fast/Unit tests/nvm_install_no_progress_bar @@ -6,7 +6,7 @@ cleanup () { nvm cache clear nvm deactivate rm -rf ${NVM_DIR}/v* - nvm unalias default + nvm unalias default || true } die () { >&2 echo "$@" ; cleanup ; exit 1; } diff --git a/test/installation_node/install LTS b/test/installation_node/install LTS index e5638e27f9..f5c859625e 100755 --- a/test/installation_node/install LTS +++ b/test/installation_node/install LTS @@ -9,20 +9,20 @@ die () { echo "$@" ; exit 1; } nvm unalias default >/dev/null 2>&1 || die 'unable to unalias default' set +ex # needed for stderr -OUTPUT="$(nvm install --lts 3 2>&1)" +OUTPUT="$(nvm install --lts 0.12 2>&1)" +EXIT_CODE="$?" set -ex -EXIT_CODE="$(nvm install --lts 3 >/dev/null 2>&1 && echo $? || echo $?)" -EXPECTED_OUTPUT="Version '3' (with LTS filter) not found - try \`nvm ls-remote --lts\` to browse available versions." -[ "${EXIT_CODE}" = 3 ] || die "\`nvm install --lts 3\` did not exit with 3, got >${EXIT_CODE}<" +EXPECTED_OUTPUT="Version '0.12' (with LTS filter) not found - try \`nvm ls-remote --lts\` to browse available versions." +[ "${EXIT_CODE}" = 3 ] || die "\`nvm install --lts 0.12\` did not exit with 3, got >${EXIT_CODE}<" [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "\`nvm install --lts 3\` output >${OUTPUT}<, expected >${EXPECTED_OUTPUT}<" set +ex # needed for stderr -OUTPUT="$(nvm install --lts=argon 3 2>&1)" -set -ex -EXIT_CODE="$(nvm install --lts=argon 3 >/dev/null 2>&1 && echo $? || echo $?)" -EXPECTED_OUTPUT="Version '3' (with LTS filter 'argon') not found - try \`nvm ls-remote --lts=argon\` to browse available versions." -[ "${EXIT_CODE}" = 3 ] || die "\`nvm install --lts=argon 3\` did not exit with 3, got >${EXIT_CODE}<" -[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "\`nvm install --lts=argon 3\` output >${OUTPUT}<, expected >${EXPECTED_OUTPUT}<" +OUTPUT="$(nvm install --lts=argon 0.12 2>&1)" +EXIT_CODE="$?" +set -x +EXPECTED_OUTPUT="Version '0.12' (with LTS filter 'argon') not found - try \`nvm ls-remote --lts=argon\` to browse available versions." +[ "${EXIT_CODE}" = 3 ] || die "\`nvm install --lts=argon 0.12\` did not exit with 3, got >${EXIT_CODE}<" +[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "\`nvm install --lts=argon 0.12\` output >${OUTPUT}<, expected >${EXPECTED_OUTPUT}<" nvm install --lts 4.2.2 || die 'nvm install --lts 4.2.2 failed'