1
0
mirror of https://github.com/nvm-sh/nvm.git synced 2026-10-23 23:03:46 +08:00
Files
nvm/test/fast/Unit tests/nvm_auto use with a prerelease default
Jordan Harband 09822033d2 [Fix] nvm exec/nvm run: do not treat filenames with digits as versions
`nvm_is_valid_version` accepted strings like `123.js`, `v1.js`, and `1.2.3.4.5.6.js`, because `nvm_version_greater_than_or_equal_to` exits as soon as the first number is greater than zero, without looking at the rest.
So `nvm run 123.js` failed with `N/A: version "v123.js" is not yet installed` instead of running the file.

It now checks the shape first: one to three dot-separated numbers, optionally with a trailing dot, and a prerelease suffix (rc, nightly, v8-canary, test builds) only after a full `x.y.z`.
A suffix with a dot followed by a non-digit, like a file extension, is rejected, so names like `1.2.3-foo.js` are treated as files too.
Every version listed in the nodejs.org and iojs.org `index.tab` files is still accepted, so shell startup keeps using a `default` alias that resolves to an installed prerelease.

Names that are also valid versions, like `123` or `v1`, are still treated as versions; `nvm run current 123` runs such a file.

Refs #1166. See #3931.
2026-09-28 15:05:28 +09:00

40 lines
1011 B
Bash
Executable File

#!/bin/sh
ORIG_NVM_DIR="${NVM_DIR}"
TMP_DIR=''
cleanup () {
case "${TMP_DIR}" in
/*) rm -rf "${TMP_DIR}" ;;
esac
NVM_DIR="${ORIG_NVM_DIR}"
export NVM_DIR
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
TMP_DIR="$(mktemp -d)"
NVM_DIR="${TMP_DIR}/nvm"
export NVM_DIR
mkdir -p "${NVM_DIR}/alias" || die 'could not create alias dir'
RELEASE='v998.0.0'
NIGHTLY='v999.0.0-nightly20241031996708042b'
make_fake_node "${RELEASE}" || die "fake node ${RELEASE} could not be made"
make_fake_node "${NIGHTLY}" || die "fake node ${NIGHTLY} could not be made"
for TARGET in "${NIGHTLY}" node; do
nvm_make_alias default "${TARGET}" || die "could not alias default to ${TARGET}"
nvm deactivate >/dev/null 2>&1
nvm_auto use || die "nvm_auto use failed with default -> ${TARGET}"
CURRENT="$(nvm current)"
[ "${CURRENT}" = "${NIGHTLY}" ] || die "with default -> ${TARGET}, nvm_auto use left 'nvm current' as >${CURRENT}<, expected >${NIGHTLY}<"
done
cleanup