From 2e0b895c37eb0f89d2005df709feef5618596831 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 | 13 +++++-- .../nvm install with nonlowercase LTS name | 37 +++++++++++++++++++ test/fast/Unit tests/nvm ls-remote | 6 +++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100755 test/fast/Unit tests/nvm install with nonlowercase LTS name diff --git a/nvm.sh b/nvm.sh index ce8bba56a1..643db8d637 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 @@ -3422,6 +3426,8 @@ nvm() { esac VERSION="$(NVM_VERSION_ONLY=true NVM_LTS="${LTS-}" nvm_remote_version "${provided_version}")" + local EXIT_CODE + EXIT_CODE=$? if [ "${VERSION}" = 'N/A' ]; then local LTS_MSG @@ -3430,8 +3436,9 @@ nvm() { LTS_MSG='(with LTS filter) ' REMOTE_CMD='nvm ls-remote --lts' elif [ -n "${LTS-}" ]; then - LTS_MSG="(with LTS filter '${LTS}') " REMOTE_CMD="nvm ls-remote --lts=${LTS}" + nvm_err "Version with LTS filter '${LTS}' not found - try \`${REMOTE_CMD}\` to browse available versions." + return 3 else REMOTE_CMD='nvm ls-remote' fi 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..2aa510c740 --- /dev/null +++ b/test/fast/Unit tests/nvm install with nonlowercase LTS name @@ -0,0 +1,37 @@ +#!/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=$? +if [ $EXIT_CODE -ne 3 ]; then + die "Expected exit code of 3, got ${EXIT_CODE}" +fi + +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..5b1a4daa98 100755 --- a/test/fast/Unit tests/nvm ls-remote +++ b/test/fast/Unit tests/nvm ls-remote @@ -70,4 +70,10 @@ OUTPUT="$(nvm ls-remote | sed 's/[ \t]*$//')" EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )" [ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "bare nvm ls-remote did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<" +if OUTPUT="$(nvm ls-remote lts/ARGON 2>&1)"; then + die "nvm ls-remote lts/ARGON did not exit nonzero, got '$?'" +fi +EXPECTED_OUTPUT='LTS names must be lowercase' +[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote lts/ARGON did not output expected error message; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<" + cleanup