Skip to content

Commit

Permalink
[Fix]: nvm ls, nvm alias, nvm install: error when an LTS name i…
Browse files Browse the repository at this point in the history
…s invalid
  • Loading branch information
ljharb committed Sep 4, 2024
1 parent 3834cb7 commit 5d977d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 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
22 changes: 22 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,22 @@
#!/bin/sh

die () { echo "$@" ; exit 1; }

\. ../../../nvm.sh

REMOTE="${PWD}/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "${REMOTE}"
}
REMOTE_IOJS="${PWD}/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "${REMOTE_IOJS}"
}
ACTUAL="$(nvm install lts/ARGON 2>&1)"
if [ $? -ne 3 ]; then
die "Expected exit code of 3, got $?"
fi

EXPECTED='LTS names must be lowercase'

[ "${ACTUAL}" = "${EXPECTED}" ] || die "Expected >${EXPECTED}<, got >${ACTUAL}<"
6 changes: 6 additions & 0 deletions test/fast/Unit tests/nvm ls-remote
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5d977d8

Please sign in to comment.