Skip to content

Commit

Permalink
[Fix] nvm ls, nvm alias, nvm install: error when an LTS name is…
Browse files Browse the repository at this point in the history
… invalid
  • Loading branch information
ljharb committed Sep 4, 2024
1 parent ea126a6 commit 84d47c3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
16 changes: 11 additions & 5 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -3421,17 +3425,20 @@ 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
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
Expand Down Expand Up @@ -3496,7 +3503,6 @@ nvm() {
FLAVOR="$(nvm_node_prefix)"
fi

local EXIT_CODE
EXIT_CODE=0

if nvm_is_version_installed "${VERSION}"; then
Expand Down
35 changes: 35 additions & 0 deletions test/fast/Unit tests/nvm install with nonlowercase LTS name
Original file line number Diff line number Diff line change
@@ -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}<"
8 changes: 8 additions & 0 deletions test/fast/Unit tests/nvm ls-remote
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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}"
Expand Down

0 comments on commit 84d47c3

Please sign in to comment.