[New] ls-remote, install, version-remote: add --min=

add CLI option --min=<version>, fallback to env var "NVM_MIN" if set.

The latter two pick the newest match,
which is below the minimum only when every match is,
so they check the resolved version.
`ls-remote` prints its notice on stderr, so the list stays parseable.

Other minor changes:
 - fix: disable color in nvm_print_versions tests
 - trim leading space in mock output due to eslint errors
 - ensure prefixed versions like v18 also work
 - remove function ref duplication
 - avoid inline initialization for ksh compatibility
 - apply `--min=` before checking for an empty list; require a version

Co-authored-by: ryenus <ryenus@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
ryenusandJordan Harband authored and Jordan Harband committed 2026-10-29 20:23:46 +08:00
1 parent 68986f7778
commit ceef142f8a
3 files changed
+338 -4

No files matched your search

+7
View File
@@ -404,6 +404,13 @@ You can list available versions using `ls-remote`:
nvm ls-remote nvm ls-remote
``` ```
To hide versions below a given version, use `--min=`, or set `NVM_MIN` to the same kind of version, for example in your shell profile; `--min=` overrides it. `nvm install` and `nvm version-remote` accept it too, and fail rather than select a version below it:
```sh
nvm ls-remote --min=20
nvm ls-remote --only=lts --min=v18.2
```
When no version or `--lts` filter is given, the list also marks the version the `node` alias would install, and every version that one of your own aliases would resolve to. Each kind of annotation always starts in the same column, so the output can be sliced with `cut -c` or similar tools: When no version or `--lts` filter is given, the list also marks the version the `node` alias would install, and every version that one of your own aliases would resolve to. Each kind of annotation always starts in the same column, so the output can be sliced with `cut -c` or similar tools:
``` ```
+148 -4
View File
@@ -3089,6 +3089,46 @@ nvm_get_remote_aliases() {
done done
} }
# fails, naming where it came from ($2), unless the minimum version ($1) is like `18`, `v18.1`, or `18.1.0`
nvm_validate_min_version() {
if ! nvm_echo "${1-}" | nvm_grep -qE '^v?[0-9]+(\.[0-9]+){0,2}$'; then
nvm_err "\`${2-}\` must be a version, like \`18\`, \`v18.1\`, or \`18.1.0\`; got \"${1-}\"."
return 55
fi
}
nvm_is_below_min_version() {
[ -z "$(nvm_filter_min_versions "${2-}" "${1-}")" ]
}
# prints the rows of `nvm_remote_versions` output ($2) whose version is at or above $1; other rows, like `N/A`, are kept
nvm_filter_min_versions() {
nvm_echo "${2-}" | command awk -v min="${1-}" '
# a[1..3] are major, minor, and patch; a[4] is 0 for a prerelease, which is below its release
function parse(v, a, release, parts) {
sub(/^(iojs-)?v/, "", v);
release = index(v, "-") ? 0 : 1;
sub(/-.*/, "", v);
parts = split(v, a, ".");
a[4] = release;
return parts;
}
function below(v, m, a, b, i) {
parse(v, a);
# a partial minimum, like `27`, includes the prereleases of `27.0.0`
if (parse(m, b) < 3) {
b[4] = 0;
}
for (i = 1; i <= 4; i++) {
if (a[i] + 0 != b[i] + 0) {
return a[i] + 0 < b[i] + 0;
}
}
return 0;
}
$1 !~ /^(iojs-)?v[0-9]/ || !below($1, min)'
}
nvm_print_versions() { nvm_print_versions() {
local NVM_CURRENT local NVM_CURRENT
NVM_CURRENT=$(nvm_ls_current) NVM_CURRENT=$(nvm_ls_current)
@@ -4852,6 +4892,7 @@ nvm() {
nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>' nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>'
nvm_echo ' --lts When installing, only select from LTS (long-term support) versions' nvm_echo ' --lts When installing, only select from LTS (long-term support) versions'
nvm_echo ' --lts=<LTS name> When installing, only select from versions for a specific LTS line' nvm_echo ' --lts=<LTS name> When installing, only select from versions for a specific LTS line'
nvm_echo ' --min=<version> When installing, only select from versions at or above <version>. Defaults to $NVM_MIN'
nvm_echo ' --only=<categories> Only select from these version categories (see above)' nvm_echo ' --only=<categories> Only select from these version categories (see above)'
nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists' nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists'
nvm_echo ' --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version' nvm_echo ' --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version'
@@ -4891,6 +4932,7 @@ nvm() {
nvm_echo ' nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided' nvm_echo ' nvm ls-remote [<version>] List remote versions available for install, matching a given <version> if provided'
nvm_echo ' --lts When listing, only show LTS (long-term support) versions' nvm_echo ' --lts When listing, only show LTS (long-term support) versions'
nvm_echo ' --lts=<LTS name> When listing, only show versions for a specific LTS line' nvm_echo ' --lts=<LTS name> When listing, only show versions for a specific LTS line'
nvm_echo ' --min=<version> When listing, only show versions at or above <version>. Defaults to $NVM_MIN'
nvm_echo ' --only=<categories> Only select from these version categories (see above)' nvm_echo ' --only=<categories> Only select from these version categories (see above)'
nvm_echo ' --no-colors Suppress colored output' nvm_echo ' --no-colors Suppress colored output'
nvm_echo ' nvm version <version> Resolve the given description to a single local version' nvm_echo ' nvm version <version> Resolve the given description to a single local version'
@@ -4898,6 +4940,7 @@ nvm() {
nvm_echo ' nvm version-remote <version> Resolve the given description to a single remote version' nvm_echo ' nvm version-remote <version> Resolve the given description to a single remote version'
nvm_echo ' --lts When listing, only select from LTS (long-term support) versions' nvm_echo ' --lts When listing, only select from LTS (long-term support) versions'
nvm_echo ' --lts=<LTS name> When listing, only select from versions for a specific LTS line' nvm_echo ' --lts=<LTS name> When listing, only select from versions for a specific LTS line'
nvm_echo ' --min=<version> Only select from versions at or above <version>. Defaults to $NVM_MIN'
nvm_echo ' --only=<categories> Only select from these version categories (see above)' nvm_echo ' --only=<categories> Only select from these version categories (see above)'
nvm_echo ' nvm deactivate Undo effects of `nvm` on current shell' nvm_echo ' nvm deactivate Undo effects of `nvm` on current shell'
nvm_echo ' --silent Silences stdout/stderr output' nvm_echo ' --silent Silences stdout/stderr output'
@@ -5109,6 +5152,10 @@ nvm() {
REINSTALL_PACKAGES_FROM='' REINSTALL_PACKAGES_FROM=''
local SKIP_DEFAULT_PACKAGES local SKIP_DEFAULT_PACKAGES
SKIP_DEFAULT_PACKAGES='' SKIP_DEFAULT_PACKAGES=''
local NVM_MIN_VERSION
NVM_MIN_VERSION="${NVM_MIN-}"
local NVM_MIN_SOURCE
NVM_MIN_SOURCE='$NVM_MIN'
while [ $# -ne 0 ]; do while [ $# -ne 0 ]; do
case "$1" in case "$1" in
@@ -5159,6 +5206,15 @@ nvm() {
NVM_ONLY_FLAG_COUNT=$((NVM_ONLY_FLAG_COUNT + 1)) NVM_ONLY_FLAG_COUNT=$((NVM_ONLY_FLAG_COUNT + 1))
shift shift
;; ;;
--min)
nvm_err '`--min` requires a version, as `--min=<version>`.'
return 55
;;
--min=*)
NVM_MIN_VERSION="${1#--min=}"
NVM_MIN_SOURCE='--min='
shift
;;
--latest-npm) --latest-npm)
NVM_UPGRADE_NPM=1 NVM_UPGRADE_NPM=1
shift shift
@@ -5225,7 +5281,7 @@ nvm() {
esac esac
done done
# like `--reinstall-packages-from`, `--only` may also follow the version, but is needed to resolve it # like `--reinstall-packages-from`, `--only` and `--min=` may also follow the version, but are needed to resolve it
local NVM_INSTALL_ARG local NVM_INSTALL_ARG
for NVM_INSTALL_ARG in "$@"; do for NVM_INSTALL_ARG in "$@"; do
case "${NVM_INSTALL_ARG}" in case "${NVM_INSTALL_ARG}" in
@@ -5234,6 +5290,14 @@ nvm() {
NVM_ONLY_FLAG="${NVM_ONLY_FLAG#=}" NVM_ONLY_FLAG="${NVM_ONLY_FLAG#=}"
NVM_ONLY_FLAG_COUNT=$((NVM_ONLY_FLAG_COUNT + 1)) NVM_ONLY_FLAG_COUNT=$((NVM_ONLY_FLAG_COUNT + 1))
;; ;;
--min)
nvm_err '`--min` requires a version, as `--min=<version>`.'
return 55
;;
--min=*)
NVM_MIN_VERSION="${NVM_INSTALL_ARG#--min=}"
NVM_MIN_SOURCE='--min='
;;
---*) ---*)
nvm_err 'arguments with `---` are not supported - this is likely a typo' nvm_err 'arguments with `---` are not supported - this is likely a typo'
return 55; return 55;
@@ -5246,6 +5310,10 @@ nvm() {
esac esac
done done
if [ -n "${NVM_MIN_VERSION}" ] || [ "${NVM_MIN_SOURCE}" = '--min=' ]; then
nvm_validate_min_version "${NVM_MIN_VERSION}" "${NVM_MIN_SOURCE}" || return $?
fi
# validate early; it's resolved again once `lts/*` versions have set LTS # validate early; it's resolved again once `lts/*` versions have set LTS
NVM_ONLY_RESOLVED="$(NVM_NO_LTS_STATUS=1 nvm_resolve_only "${NVM_ONLY_FLAG_COUNT}" "${NVM_ONLY_FLAG-}" "${LTS-}")" || return $? NVM_ONLY_RESOLVED="$(NVM_NO_LTS_STATUS=1 nvm_resolve_only "${NVM_ONLY_FLAG_COUNT}" "${NVM_ONLY_FLAG-}" "${LTS-}")" || return $?
@@ -5339,6 +5407,15 @@ nvm() {
EXIT_CODE="$?" EXIT_CODE="$?"
fi fi
# the newest match is below the minimum only when every match is
local NVM_BELOW_MIN_VERSION
NVM_BELOW_MIN_VERSION=''
if [ $EXIT_CODE -eq 0 ] && [ "${VERSION}" != 'N/A' ] && [ -n "${NVM_MIN_VERSION}" ] && nvm_is_below_min_version "${VERSION}" "${NVM_MIN_VERSION}"; then
NVM_BELOW_MIN_VERSION="${VERSION}"
VERSION='N/A'
EXIT_CODE=3
fi
if [ "${VERSION}" = 'N/A' ] || [ $EXIT_CODE -ne 0 ]; then if [ "${VERSION}" = 'N/A' ] || [ $EXIT_CODE -ne 0 ]; then
local LTS_MSG local LTS_MSG
LTS_MSG='' LTS_MSG=''
@@ -5365,6 +5442,13 @@ nvm() {
else else
nvm_err "Version '${provided_version}' ${LTS_MSG-}not found - try \`${REMOTE_CMD}\` to browse available versions." nvm_err "Version '${provided_version}' ${LTS_MSG-}not found - try \`${REMOTE_CMD}\` to browse available versions."
fi fi
if [ -n "${NVM_BELOW_MIN_VERSION}" ]; then
if [ "${NVM_MIN_SOURCE}" = '--min=' ]; then
NVM_MIN_SOURCE="--min=${NVM_MIN_VERSION}"
fi
nvm_err "It matches ${NVM_BELOW_MIN_VERSION}, which is below the minimum version ${NVM_MIN_VERSION} from \`${NVM_MIN_SOURCE}\`."
return 3
fi
# look again in only the categories that weren't selected # look again in only the categories that weren't selected
local NVM_EXCLUDED_CATEGORIES local NVM_EXCLUDED_CATEGORIES
NVM_EXCLUDED_CATEGORIES='' NVM_EXCLUDED_CATEGORIES=''
@@ -5461,7 +5545,7 @@ nvm() {
fi fi
NVM_WRITE_TO_NVMRC=1 NVM_WRITE_TO_NVMRC=1
;; ;;
--only | --only=*) ;; --only | --only=* | --min=*) ;;
*) *)
ADDITIONAL_PARAMETERS="${ADDITIONAL_PARAMETERS} $1" ADDITIONAL_PARAMETERS="${ADDITIONAL_PARAMETERS} $1"
;; ;;
@@ -6307,6 +6391,10 @@ nvm() {
PATTERN='' PATTERN=''
local NVM_NO_COLORS local NVM_NO_COLORS
NVM_NO_COLORS='' NVM_NO_COLORS=''
local NVM_MIN_VERSION
NVM_MIN_VERSION="${NVM_MIN-}"
local NVM_MIN_SOURCE
NVM_MIN_SOURCE='$NVM_MIN'
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "${1-}" in case "${1-}" in
@@ -6317,6 +6405,14 @@ nvm() {
--lts=*) --lts=*)
NVM_LTS="${1##--lts=}" NVM_LTS="${1##--lts=}"
;; ;;
--min)
nvm_err '`--min` requires a version, as `--min=<version>`.'
return 55
;;
--min=*)
NVM_MIN_VERSION="${1#--min=}"
NVM_MIN_SOURCE='--min='
;;
--only | --only=*) --only | --only=*)
NVM_ONLY_FLAG="${1#--only}" NVM_ONLY_FLAG="${1#--only}"
NVM_ONLY_FLAG="${NVM_ONLY_FLAG#=}" NVM_ONLY_FLAG="${NVM_ONLY_FLAG#=}"
@@ -6348,6 +6444,10 @@ nvm() {
shift shift
done done
if [ -n "${NVM_MIN_VERSION}" ] || [ "${NVM_MIN_SOURCE}" = '--min=' ]; then
nvm_validate_min_version "${NVM_MIN_VERSION}" "${NVM_MIN_SOURCE}" || return $?
fi
NVM_ONLY_RESOLVED="$(NVM_NO_LTS_STATUS=1 nvm_resolve_only "${NVM_ONLY_FLAG_COUNT}" "${NVM_ONLY_FLAG-}" "${NVM_LTS-}")" || return $? NVM_ONLY_RESOLVED="$(NVM_NO_LTS_STATUS=1 nvm_resolve_only "${NVM_ONLY_FLAG_COUNT}" "${NVM_ONLY_FLAG-}" "${NVM_LTS-}")" || return $?
local NVM_ONLY_SELECTED local NVM_ONLY_SELECTED
NVM_ONLY_SELECTED="${NVM_ONLY_RESOLVED}" NVM_ONLY_SELECTED="${NVM_ONLY_RESOLVED}"
@@ -6356,6 +6456,23 @@ nvm() {
local EXIT_CODE local EXIT_CODE
NVM_OUTPUT="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN-}" &&:)" NVM_OUTPUT="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN-}" &&:)"
EXIT_CODE=$? EXIT_CODE=$?
if [ -n "${NVM_MIN_VERSION}" ] && [ -n "${NVM_OUTPUT}" ]; then
local NVM_MIN_HIDDEN
NVM_MIN_HIDDEN="$(nvm_echo "${NVM_OUTPUT}" | command awk '/^(iojs-)?v[0-9]/ { n++ } END { print n + 0 }')"
NVM_OUTPUT="$(nvm_filter_min_versions "${NVM_MIN_VERSION}" "${NVM_OUTPUT}")"
NVM_MIN_HIDDEN=$((NVM_MIN_HIDDEN - $(nvm_echo "${NVM_OUTPUT}" | command awk '/^(iojs-)?v[0-9]/ { n++ } END { print n + 0 }')))
if [ "${NVM_MIN_HIDDEN}" -gt 0 ]; then
# on stderr, so the list stays parseable
if [ "${NVM_MIN_SOURCE}" = '--min=' ]; then
NVM_MIN_SOURCE="--min=${NVM_MIN_VERSION}"
fi
if [ "${NVM_MIN_HIDDEN}" -eq 1 ]; then
nvm_err "1 version below ${NVM_MIN_VERSION} is hidden by \`${NVM_MIN_SOURCE}\`."
else
nvm_err "${NVM_MIN_HIDDEN} versions below ${NVM_MIN_VERSION} are hidden by \`${NVM_MIN_SOURCE}\`."
fi
fi
fi
if [ -n "${NVM_OUTPUT}" ]; then if [ -n "${NVM_OUTPUT}" ]; then
local NVM_REMOTE_LATEST_ALIAS local NVM_REMOTE_LATEST_ALIAS
NVM_REMOTE_LATEST_ALIAS='' NVM_REMOTE_LATEST_ALIAS=''
@@ -6711,6 +6828,10 @@ nvm() {
NVM_LTS='' NVM_LTS=''
local PATTERN local PATTERN
PATTERN='' PATTERN=''
local NVM_MIN_VERSION
NVM_MIN_VERSION="${NVM_MIN-}"
local NVM_MIN_SOURCE
NVM_MIN_SOURCE='$NVM_MIN'
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "${1-}" in case "${1-}" in
--) ;; --) ;;
@@ -6720,6 +6841,14 @@ nvm() {
--lts=*) --lts=*)
NVM_LTS="${1##--lts=}" NVM_LTS="${1##--lts=}"
;; ;;
--min)
nvm_err '`--min` requires a version, as `--min=<version>`.'
return 55
;;
--min=*)
NVM_MIN_VERSION="${1#--min=}"
NVM_MIN_SOURCE='--min='
;;
--only | --only=*) --only | --only=*)
NVM_ONLY_FLAG="${1#--only}" NVM_ONLY_FLAG="${1#--only}"
NVM_ONLY_FLAG="${NVM_ONLY_FLAG#=}" NVM_ONLY_FLAG="${NVM_ONLY_FLAG#=}"
@@ -6745,10 +6874,25 @@ nvm() {
unset PATTERN unset PATTERN
;; ;;
esac esac
if [ -n "${NVM_MIN_VERSION}" ] || [ "${NVM_MIN_SOURCE}" = '--min=' ]; then
nvm_validate_min_version "${NVM_MIN_VERSION}" "${NVM_MIN_SOURCE}" || return $?
fi
NVM_ONLY_RESOLVED="$(NVM_NO_LTS_STATUS=1 nvm_resolve_only "${NVM_ONLY_FLAG_COUNT}" "${NVM_ONLY_FLAG-}" "${NVM_LTS-}")" || return $? NVM_ONLY_RESOLVED="$(NVM_NO_LTS_STATUS=1 nvm_resolve_only "${NVM_ONLY_FLAG_COUNT}" "${NVM_ONLY_FLAG-}" "${NVM_LTS-}")" || return $?
local NVM_ONLY_SELECTED local NVM_ONLY_SELECTED
NVM_ONLY_SELECTED="${NVM_ONLY_RESOLVED}" NVM_ONLY_SELECTED="${NVM_ONLY_RESOLVED}"
NVM_VERSION_ONLY=true NVM_LTS="${NVM_LTS-}" nvm_remote_version "${PATTERN:-node}" local NVM_REMOTE_VERSION
local NVM_REMOTE_VERSION_EXIT_CODE
NVM_REMOTE_VERSION="$(NVM_VERSION_ONLY=true NVM_LTS="${NVM_LTS-}" nvm_remote_version "${PATTERN:-node}")"
NVM_REMOTE_VERSION_EXIT_CODE=$?
# the newest match is below the minimum only when every match is
if [ "${NVM_REMOTE_VERSION_EXIT_CODE}" -eq 0 ] && [ -n "${NVM_MIN_VERSION}" ] && nvm_is_below_min_version "${NVM_REMOTE_VERSION}" "${NVM_MIN_VERSION}"; then
NVM_REMOTE_VERSION='N/A'
NVM_REMOTE_VERSION_EXIT_CODE=3
fi
if [ -n "${NVM_REMOTE_VERSION}" ]; then
nvm_echo "${NVM_REMOTE_VERSION}"
fi
return "${NVM_REMOTE_VERSION_EXIT_CODE}"
;; ;;
"--version" | "-v") "--version" | "-v")
nvm_echo '0.41.0' nvm_echo '0.41.0'
@@ -6767,7 +6911,7 @@ nvm() {
nvm_install_binary nvm_install_source nvm_clang_version \ nvm_install_binary nvm_install_source nvm_clang_version \
nvm_get_mirror nvm_get_download_slug nvm_download_artifact nvm_is_cached_artifact \ nvm_get_mirror nvm_get_download_slug nvm_download_artifact nvm_is_cached_artifact \
nvm_install_npm_if_needed nvm_use_if_needed nvm_check_file_permissions \ nvm_install_npm_if_needed nvm_use_if_needed nvm_check_file_permissions \
nvm_print_versions nvm_compute_checksum \ nvm_print_versions nvm_validate_min_version nvm_is_below_min_version nvm_filter_min_versions nvm_compute_checksum \
nvm_get_checksum_binary \ nvm_get_checksum_binary \
nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \ nvm_get_checksum_alg nvm_get_checksum nvm_compare_checksum \
nvm_version nvm_rc_version nvm_match_version \ nvm_version nvm_rc_version nvm_match_version \
+183
View File
@@ -0,0 +1,183 @@
#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
rm -rf "${TEST_NVM_DIR-}"
unset -f nvm_download nvm_record_lts_status nvm_get_remote_aliases nvm_has_colors versions install_below_min
unset TEST_NVM_DIR OUTPUT EXIT_CODE NVM_MIN NVM_MIN_ARG NVM_MIN_COMMAND
}
: nvm.sh
\. ../../../nvm.sh
unset NVM_MIN NVM_ONLY
TEST_NVM_DIR="$(mktemp -d)" || die 'could not create a temporary NVM_DIR'
NVM_DIR="${TEST_NVM_DIR}"
nvm_has_colors() { return 1; }
nvm_record_lts_status() { :; }
nvm_get_remote_aliases() { :; }
nvm_download() {
case "$*" in
*"$(nvm_get_mirror node std)/index.tab"*)
printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\tlts\tsecurity\n'
printf '%s\t2026-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' \
v27.0.0-alpha.1.0.0 - \
v26.1.0 - \
v24.2.0 Krypton \
v22.12.0 Jod \
v20.18.1 Iron \
v20.0.0 - \
v18.20.5 Hydrogen \
v0.12.18 - \
v0.10.48 -
;;
*"$(nvm_get_mirror iojs std)/index.tab"*)
printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\n'
printf '%s\t2015-01-01\tsrc\t-\t-\t-\t-\t-\t-\n' v3.3.1 v2.5.0 v1.8.4
;;
*) return 42 ;;
esac
}
# prints only the version column, on one line
versions() {
nvm ls-remote --no-colors "$@" 2>/dev/null | command awk '{ printf "%s ", $1 }'
}
[ "$(versions)" = 'v0.10.48 v0.12.18 iojs-v1.8.4 iojs-v2.5.0 iojs-v3.3.1 v18.20.5 v20.0.0 v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|| die "without a minimum, expected every non-alpha version; got >$(versions)<"
[ "$(versions --min=20)" = 'v20.0.0 v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|| die "--min=20: got >$(versions --min=20)<"
[ "$(versions --min=v20.1)" = 'v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|| die "--min=v20.1: got >$(versions --min=v20.1)<"
[ "$(versions --min=20.18.1)" = 'v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|| die "--min=20.18.1: got >$(versions --min=20.18.1)<"
# io.js majors are compared like node majors
[ "$(versions --min=2)" = 'iojs-v2.5.0 iojs-v3.3.1 v18.20.5 v20.0.0 v20.18.1 v22.12.0 v24.2.0 v26.1.0 ' ] \
|| die "--min=2: got >$(versions --min=2)<"
# combined with --only and --lts
[ "$(versions --only=lts --min=20)" = 'v20.18.1 v22.12.0 v24.2.0 ' ] \
|| die "--only=lts --min=20: got >$(versions --only=lts --min=20)<"
[ "$(versions --min=20 --only=non-lts)" = 'v20.0.0 v26.1.0 ' ] \
|| die "--min=20 --only=non-lts: got >$(versions --min=20 --only=non-lts)<"
[ "$(versions --only=iojs --min=3)" = 'iojs-v3.3.1 ' ] \
|| die "--only=iojs --min=3: got >$(versions --only=iojs --min=3)<"
[ "$(versions --only=alpha,lts --min=24)" = 'v24.2.0 v27.0.0-alpha.1.0.0 ' ] \
|| die "--only=alpha,lts --min=24: got >$(versions --only=alpha,lts --min=24)<"
[ "$(versions --lts --min=22)" = 'v22.12.0 v24.2.0 ' ] \
|| die "--lts --min=22: got >$(versions --lts --min=22)<"
# a partial minimum includes the prereleases of its first release; a full one is above them
[ "$(versions --only=alpha --min=27)" = 'v27.0.0-alpha.1.0.0 ' ] \
|| die "--only=alpha --min=27: got >$(versions --only=alpha --min=27)<"
OUTPUT="$(nvm ls-remote --no-colors --only=alpha --min=27.0.0)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|| die "--only=alpha --min=27.0.0 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
# nothing at or above the minimum, or nothing matching the pattern, is N/A
OUTPUT="$(nvm ls-remote --no-colors --min=99)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|| die "--min=99 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
OUTPUT="$(nvm ls-remote --no-colors --only=lts --min=25)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|| die "--only=lts --min=25 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
OUTPUT="$(nvm ls-remote --no-colors --min=18 99)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] && [ "$(nvm_echo "${OUTPUT}" | command tr -d ' ')" = 'N/A' ] \
|| die "a pattern with no match should print N/A and exit 3 with --min; got >${OUTPUT}<, exit ${EXIT_CODE}"
# NVM_MIN is the default, and --min= overrides it
NVM_MIN=22
[ "$(versions)" = 'v22.12.0 v24.2.0 v26.1.0 ' ] \
|| die "NVM_MIN=22: got >$(versions)<"
[ "$(versions --min=26)" = 'v26.1.0 ' ] \
|| die "NVM_MIN=22 --min=26: got >$(versions --min=26)<"
nvm ls-remote --no-colors >/dev/null 2>"${TEST_NVM_DIR}/stderr"
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
[ "_${OUTPUT}" = '_8 versions below 22 are hidden by `$NVM_MIN`.' ] \
|| die "NVM_MIN=22 notice: got >${OUTPUT}<"
OUTPUT="$(nvm version-remote 20)"
[ "_${OUTPUT}" = '_N/A' ] || die "NVM_MIN=22 version-remote 20: got >${OUTPUT}<"
[ "$(nvm version-remote --min=20 20)" = 'v20.18.1' ] \
|| die "NVM_MIN=22 version-remote --min=20 20: got >$(nvm version-remote --min=20 20)<"
# a notice on stderr says how many versions are hidden, and by what
nvm ls-remote --no-colors --min=20 >/dev/null 2>"${TEST_NVM_DIR}/stderr"
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
[ "_${OUTPUT}" = '_6 versions below 20 are hidden by `--min=20`.' ] \
|| die "--min=20 notice: got >${OUTPUT}<"
nvm ls-remote --no-colors --only=lts --min=20 >/dev/null 2>"${TEST_NVM_DIR}/stderr"
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
[ "_${OUTPUT}" = '_1 version below 20 is hidden by `--min=20`.' ] \
|| die "--only=lts --min=20 notice: got >${OUTPUT}<"
nvm ls-remote --no-colors --min=0 >/dev/null 2>"${TEST_NVM_DIR}/stderr"
OUTPUT="$(cat "${TEST_NVM_DIR}/stderr")"
[ -z "${OUTPUT}" ] || die "--min=0 hides nothing, so should print no notice; got >${OUTPUT}<"
# version-remote and install select only from versions at or above the minimum
[ "$(nvm version-remote --min=20 20)" = 'v20.18.1' ] \
|| die "version-remote --min=20 20: got >$(nvm version-remote --min=20 20)<"
OUTPUT="$(nvm version-remote --min=22 20)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] && [ "_${OUTPUT}" = '_N/A' ] \
|| die "version-remote --min=22 20 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
OUTPUT="$(nvm version-remote --lts=iron --min=22)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] && [ "_${OUTPUT}" = '_N/A' ] \
|| die "version-remote --lts=iron --min=22 should print N/A and exit 3; got >${OUTPUT}<, exit ${EXIT_CODE}"
install_below_min() {
OUTPUT="$(nvm install "$@" 2>&1)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 3 ] || die "install $* should exit 3; got ${EXIT_CODE}: >${OUTPUT}<"
case "${OUTPUT}" in
*'It matches v20.18.1, which is below the minimum version 22 from `--min=22`.'*) ;;
*) die "install $* should explain the minimum; got >${OUTPUT}<" ;;
esac
}
install_below_min --min=22 20
install_below_min 20 --min=22
# NVM_MIN only applies to commands that select remote versions
mkdir -p "${NVM_DIR}/versions/node/v18.20.5/bin" || die 'could not create an installed version'
OUTPUT="$(nvm ls --no-colors --no-alias | command awk '{ printf "%s ", $1 }')"
case "${OUTPUT}" in
*v18.20.5*) ;;
*) die "NVM_MIN should not hide installed versions from nvm ls; got >${OUTPUT}<" ;;
esac
[ "${NVM_MIN}" = 22 ] || die "nvm ls changed NVM_MIN to >${NVM_MIN}<"
# the minimum must be a version, given with `=`
for NVM_MIN_ARG in --min --min= --min=lts/iron --min=node --min=18.x --min=v18.1.0.0; do
OUTPUT="$(nvm ls-remote --no-colors "${NVM_MIN_ARG}" 2>&1)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 55 ] || die "ls-remote ${NVM_MIN_ARG} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
OUTPUT="$(nvm version-remote "${NVM_MIN_ARG}" 20 2>&1)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 55 ] || die "version-remote ${NVM_MIN_ARG} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
OUTPUT="$(nvm install "${NVM_MIN_ARG}" 20 2>&1)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 55 ] || die "install ${NVM_MIN_ARG} 20 should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
OUTPUT="$(nvm install 20 "${NVM_MIN_ARG}" 2>&1)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 55 ] || die "install 20 ${NVM_MIN_ARG} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
done
unset NVM_MIN_ARG
NVM_MIN=node
for NVM_MIN_COMMAND in ls-remote version-remote install; do
OUTPUT="$(nvm "${NVM_MIN_COMMAND}" 2>&1)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 55 ] || die "NVM_MIN=node ${NVM_MIN_COMMAND} should exit 55; got ${EXIT_CODE}: >${OUTPUT}<"
done
unset NVM_MIN_COMMAND
cleanup