Compare commits

..

2 Commits

Author SHA1 Message Date
Jordan Harband
9f32e7bad4 [Fix] nvm ls, nvm alias, nvm install: error when an LTS name is invalid 2024-09-07 21:17:46 -07:00
Jordan Harband
9bd27d3897 [Fix] nvm_normalize_lts: error when an LTS name is not lowercase 2024-09-04 13:54:41 -07:00
5 changed files with 14 additions and 59 deletions

15
nvm.sh
View File

@@ -196,9 +196,6 @@ nvm_install_latest_npm() {
nvm_echo 'Attempting to upgrade to the latest working version of npm...'
local NODE_VERSION
NODE_VERSION="$(nvm_strip_iojs_prefix "$(nvm_ls_current)")"
local NPM_VERSION
NPM_VERSION="$(npm --version 2>/dev/null)"
if [ "${NODE_VERSION}" = 'system' ]; then
NODE_VERSION="$(node --version)"
elif [ "${NODE_VERSION}" = 'none' ]; then
@@ -209,6 +206,8 @@ nvm_install_latest_npm() {
nvm_err 'Unable to obtain node version.'
return 1
fi
local NPM_VERSION
NPM_VERSION="$(npm --version 2>/dev/null)"
if [ -z "${NPM_VERSION}" ]; then
nvm_err 'Unable to obtain npm version.'
return 2
@@ -701,12 +700,10 @@ nvm_ensure_version_installed() {
nvm_err "N/A: version \"${PREFIXED_VERSION:-$PROVIDED_VERSION}\" is not yet installed."
fi
nvm_err ""
if [ "${PROVIDED_VERSION}" = 'lts' ]; then
nvm_err '`lts` is not an alias - you may need to run `nvm install --lts` to install and `nvm use --lts` to use it.'
elif [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then
nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it."
else
nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.'
if [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then
nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it."
else
nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.'
fi
return 1
fi

View File

@@ -5,26 +5,14 @@ die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
unset -f nvm_download
rm -rf "${NVM_DIR}/alias/lts"
mv "${NVM_DIR}/alias/lts-backup" "${NVM_DIR}/alias/lts"
}
\. ../../../nvm.sh
MOCKS_DIR="${PWD}/mocks"
LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"
mv "${NVM_DIR}/alias/lts" "${NVM_DIR}/alias/lts-backup" ||:
mkdir -p "${NVM_DIR}/alias/lts"
for LTS in $LTS_LIST; do
cp "${NVM_DIR}/alias/lts-backup/${LTS}" "${NVM_DIR}/alias/lts/"
done
set -ex
MOCKS_DIR="${PWD}/mocks"
# sample output at the time the test was written
TAB_PATH="${MOCKS_DIR}/nodejs.org-dist-index.tab"
nvm_download() {
@@ -33,6 +21,8 @@ nvm_download() {
nvm_ls_remote >/dev/null || die "nvm_ls_remote_failed?!"
LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
N=0
while IFS= read -r LTS; do
if [ $N -gt 0 ]; then

View File

@@ -2,26 +2,12 @@
cleanup() {
unset nvm_get_os
rm -rf "${NVM_DIR}/alias/lts"
mv "${NVM_DIR}/alias/lts-backup" "${NVM_DIR}/alias/lts"
}
die () { cleanup; echo "$@" ; exit 1; }
\. ../../../nvm.sh
MOCKS_DIR="../Unit tests/mocks"
LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"
mv "${NVM_DIR}/alias/lts" "${NVM_DIR}/alias/lts-backup" ||:
mkdir -p "${NVM_DIR}/alias/lts"
for LTS in $LTS_LIST; do
cp "${NVM_DIR}/alias/lts-backup/${LTS}" "${NVM_DIR}/alias/lts/"
done
ACTUAL="$(nvm_normalize_lts "foo")"
EXPECTED='foo'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
@@ -34,14 +20,15 @@ if ACTUAL="$(nvm_normalize_lts lts/ARGON)"; then
die "expected failure, got >${ACTUAL}<"
fi
STAR="$(cat "${MOCKS_DIR}/lts-star.txt")"
MOCKS_DIR="../Unit tests/mocks"
STAR="$(cat "$MOCKS_DIR/lts-star.txt")"
ACTUAL="$(nvm_normalize_lts "${STAR}")"
EXPECTED="${STAR}"
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
# or, back up LTS aliases, copy backups that are in LTS names
#
LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"
INDEX=1
printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do

View File

@@ -1,19 +0,0 @@
#!/bin/sh
set -ex
die () { echo "$@" ; exit 1; }
\. ../../../nvm.sh
# Deactivate any active node version
nvm deactivate >/dev/null 2>&1 || die 'deactivate failed'
# Attempt to use 'lts' without '--' and capture the error message
ERROR_OUTPUT=$(nvm use lts 2>&1) || true
EXPECTED_ERROR='`lts` is not an alias - you may need to run `nvm install --lts` to install and `nvm use --lts` to use it.'
# Check if the error message matches the expected output
echo "$ERROR_OUTPUT" | grep -q "$EXPECTED_ERROR" \
|| die "Expected error message not found. Got: $ERROR_OUTPUT"