[New] add prune command

`nvm prune` uninstalls every version except the newest install per major
(per minor with `--minor`, and always per minor for `v0.x`),
the current version, the `$NVM_DIR/current` symlink target,
and anything an alias resolves to.
It lists what it will remove and asks first;
`--dry-run` only lists, and `--force` skips the prompt.

Co-authored-by: Guillermo Esguerra <gesguerrab@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
authored and Jordan Harband committed 2026-02-23 13:21:38 -05:00
1 parent eb0c8b95c8
commit 585b6ec915
11 files changed
+668 -1

No files matched your search

+18
View File
@@ -38,6 +38,7 @@
- [io.js](#iojs)
- [System Version of Node](#system-version-of-node)
- [Listing Versions](#listing-versions)
- [Pruning old versions](#pruning-old-versions)
- [Setting Custom Colors](#setting-custom-colors)
- [Persisting custom colors](#persisting-custom-colors)
- [Suppressing colorized output](#suppressing-colorized-output)
@@ -611,6 +612,23 @@ If you want to see what versions are available to install:
nvm ls-remote
```
### Pruning old versions
To clean up old, unused Node.js versions, use `nvm prune`:
```sh
nvm prune
```
This will uninstall all installed versions except for the latest one within each major version group (each minor version group for `v0.x`; e.g., if you have `v16.1.0` and `v16.2.0`, it will keep `v16.2.0`), then show you what it's about to remove and ask for confirmation before deleting anything.
- **Safety**: The currently active version, and any version an alias (including `default`) points to, are **never** removed, even if they are not the latest in their group.
- **`.nvmrc` files**: `nvm prune` can't know which versions your projects' `.nvmrc` files need, so it may remove one of them; preview with `--dry-run` first.
- **Confirmation**: `nvm prune` lists the versions it will remove and asks for confirmation before deleting. Pass `--force` to skip the prompt, e.g. for use in scripts.
- **Global Modules**: `nvm prune` will warn you if it's about to uninstall a version that contains global npm modules (other than the bundled `npm` and `corepack`).
- **Preview**: Use `nvm prune --dry-run` to see which versions would be removed, without prompting or deleting anything.
- **Minor Version Retention**: Use `nvm prune --minor` to keep the latest version of each **minor** release group instead of major.
### Setting Custom Colors
You can set five colors that will be used to display version and alias information. These colors replace the default colors.
+2 -1
View File
@@ -144,6 +144,7 @@ __nvm_options() {
which) flags="--silent ${only}" ;;
version | reinstall-packages | copy-packages) flags="${only}" ;;
deactivate) flags='--silent' ;;
prune) flags='--minor --dry-run --force' ;;
*) return 0 ;;
esac
# Only split our fixed option lists, never user-supplied words.
@@ -163,7 +164,7 @@ __nvm_candidates() {
*)
__nvm_complete help install uninstall use run exec alias unalias \
reinstall-packages current list ls list-remote ls-remote install-latest-npm \
cache deactivate unload version version-remote which debug set-colors
cache deactivate unload version version-remote which prune debug set-colors
;;
esac
return 0
+164
View File
@@ -2853,6 +2853,161 @@ nvm_ls_remote() {
NVM_LTS="${NVM_LTS-}" nvm_ls_remote_index_tab node std "${PATTERN}"
}
nvm_prune() {
local NVM_PRUNE_ARG
local NVM_DRY_RUN
local NVM_FORCE
local NVM_KEEP_MINOR
local NVM_CANDIDATES
local NVM_PROTECTED
local NVM_ALIAS_DIR
local NVM_ALIAS_FILE
local NVM_PRUNE_VERSION
local NVM_VERSION_PATH
local NVM_GLOBALS_DIR
local NVM_GLOBAL
local NVM_GLOBALS_COUNT
local NVM_REMOVE
local NVM_REMOVE_LIST
local NVM_ANSWER
local NVM_REMOVED_COUNT
local NVM_FAILED_COUNT
NVM_DRY_RUN=0
NVM_FORCE=0
NVM_KEEP_MINOR=0
for NVM_PRUNE_ARG in "$@"; do
case "${NVM_PRUNE_ARG}" in
--dry-run) NVM_DRY_RUN=1 ;;
--force) NVM_FORCE=1 ;;
--minor) NVM_KEEP_MINOR=1 ;;
*)
nvm_err "Unknown argument: ${NVM_PRUNE_ARG}"
nvm_err 'Usage: nvm prune [--minor] [--dry-run] [--force]'
return 127
;;
esac
done
nvm_echo 'Pruning old versions...'
if [ "${NVM_DRY_RUN}" = 1 ]; then
nvm_echo 'Dry run: nothing will be removed.'
fi
# Every installed version except the newest in its group. For 0.x, each minor is its own release line.
NVM_CANDIDATES="$(nvm_ls | command awk '{
if ($1 == "system") next
for (i = 1; i <= NF; i++) {
if ($i ~ /^(iojs-)?v[0-9]+\.[0-9]+\.[0-9]+$/) {
v = $i
sub(/^(iojs-)?v/, "", v)
split(v, p, ".")
print p[1], p[2], p[3], $i
}
}
}' | command sort -k1,1n -k2,2n -k3,3n | command awk -v minor="${NVM_KEEP_MINOR}" '{
key = ($4 ~ /^iojs-/ ? "iojs" : "node") " " $1
if (minor == 1 || $1 == 0) key = key "." $2
if (key == prev) print last
prev = key
last = $4
}')"
NVM_PROTECTED=" $(nvm_ls_current) "
NVM_ALIAS_DIR="$(nvm_alias_path)"
while IFS= read -r NVM_ALIAS_FILE; do
if [ -n "${NVM_ALIAS_FILE}" ]; then
NVM_PROTECTED="${NVM_PROTECTED}$(nvm_resolve_local_alias "${NVM_ALIAS_FILE#"${NVM_ALIAS_DIR}/"}" 2>/dev/null) "
fi
done <<EOF
$(command find -L "${NVM_ALIAS_DIR}" -type f 2>/dev/null)
EOF
NVM_REMOVE=''
NVM_REMOVE_LIST=''
while IFS= read -r NVM_PRUNE_VERSION; do
case "${NVM_PRUNE_VERSION}" in '') continue ;; esac
case "${NVM_PROTECTED}" in *" ${NVM_PRUNE_VERSION} "*) continue ;; esac
nvm_is_version_installed "${NVM_PRUNE_VERSION}" || continue
NVM_VERSION_PATH="$(nvm_version_path "${NVM_PRUNE_VERSION}")"
# Another shell may have pointed the NVM_SYMLINK_CURRENT symlink here.
if [ "${NVM_DIR}/current" -ef "${NVM_VERSION_PATH}" ]; then
continue
fi
NVM_GLOBALS_DIR="${NVM_VERSION_PATH}/lib/node_modules"
NVM_GLOBALS_COUNT=0
while IFS= read -r NVM_GLOBAL; do
case "${NVM_GLOBAL#"${NVM_GLOBALS_DIR}/"}" in
'' | .* | npm | corepack) ;;
@*) NVM_GLOBALS_COUNT=$((NVM_GLOBALS_COUNT + $(command find "${NVM_GLOBAL}/" -mindepth 1 -maxdepth 1 ! -name '.*' 2>/dev/null | command wc -l))) ;;
*) NVM_GLOBALS_COUNT=$((NVM_GLOBALS_COUNT + 1)) ;;
esac
done <<GLOBALS
$(command find "${NVM_GLOBALS_DIR}" -mindepth 1 -maxdepth 1 2>/dev/null)
GLOBALS
NVM_REMOVE="${NVM_REMOVE}${NVM_PRUNE_VERSION}
"
if [ "${NVM_GLOBALS_COUNT}" -gt 0 ]; then
NVM_REMOVE_LIST="${NVM_REMOVE_LIST} ${NVM_PRUNE_VERSION} (warning: also removes ${NVM_GLOBALS_COUNT} global module(s))
"
else
NVM_REMOVE_LIST="${NVM_REMOVE_LIST} ${NVM_PRUNE_VERSION}
"
fi
done <<EOF
${NVM_CANDIDATES}
EOF
if [ -z "${NVM_REMOVE}" ]; then
nvm_echo 'Nothing to prune: every installed version is the newest in its group, the current version, or aliased.'
return 0
fi
if [ "${NVM_DRY_RUN}" = 1 ]; then
nvm_echo 'These versions would be removed:'
command printf '%s' "${NVM_REMOVE_LIST}"
return 0
fi
nvm_echo 'These versions will be removed:'
command printf '%s' "${NVM_REMOVE_LIST}"
if [ "${NVM_FORCE}" != 1 ]; then
nvm_echo 'Proceed? (y/N)'
NVM_ANSWER=''
IFS= read -r NVM_ANSWER || :
case "${NVM_ANSWER}" in
y | Y | yes | Yes | YES) ;;
*)
nvm_echo 'Aborted.'
return 0
;;
esac
fi
NVM_REMOVED_COUNT=0
NVM_FAILED_COUNT=0
while IFS= read -r NVM_PRUNE_VERSION; do
case "${NVM_PRUNE_VERSION}" in '') continue ;; esac
if nvm uninstall "${NVM_PRUNE_VERSION}" </dev/null >/dev/null; then
nvm_echo "Uninstalled ${NVM_PRUNE_VERSION}."
NVM_REMOVED_COUNT=$((NVM_REMOVED_COUNT + 1))
else
nvm_err "Failed to uninstall ${NVM_PRUNE_VERSION}."
NVM_FAILED_COUNT=$((NVM_FAILED_COUNT + 1))
fi
done <<EOF
${NVM_REMOVE}
EOF
if [ "${NVM_FAILED_COUNT}" -gt 0 ]; then
nvm_err "Pruned ${NVM_REMOVED_COUNT} version(s); ${NVM_FAILED_COUNT} failed."
return 1
fi
nvm_echo "Pruned ${NVM_REMOVED_COUNT} version(s)."
}
nvm_ls_remote_iojs() {
NVM_LTS="${NVM_LTS-}" nvm_ls_remote_index_tab iojs std "${1-}"
}
@@ -5146,6 +5301,11 @@ nvm() {
nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'
nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
nvm_echo ' --only=<categories> Only select from these version categories (see above)'
nvm_echo ' nvm prune Uninstall old versions, keeping the newest install per major version (per minor for 0.x), the current version, and any version an alias points to.'
nvm_echo ' Versions that only an .nvmrc file needs are not detected.'
nvm_echo ' --minor Keep the newest install per minor version instead of per major version'
nvm_echo ' --dry-run Preview what would be removed, without prompting or deleting anything'
nvm_echo ' --force Skip the confirmation prompt before deleting (for scripts)'
nvm_echo ' nvm use [current | <version>] Modify PATH to use <version>. Uses .nvmrc if version is omitted; otherwise errors.'
nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm use`:'
nvm_echo ' --silent Silences stdout/stderr output'
@@ -6127,6 +6287,9 @@ nvm() {
fi
done
;;
"prune")
nvm_prune "$@"
;;
"deactivate")
local NVM_SILENT
NVM_SILENT=''
@@ -7202,6 +7365,7 @@ nvm() {
nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \
nvm_process_nvmrc nvm_process_nvmrc_content nvm_nvmrc_invalid_msg nvm_is_version_or_alias \
nvm_write_nvmrc nvm_hash_reset \
nvm_prune \
>/dev/null 2>&1
unset NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS NVM_INSTALL_LOCK \
@@ -0,0 +1,45 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
nvm_ls() {
echo " v18.20.5"
echo " v18.20.7"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "none"
}
nvm_is_version_installed() {
return 0
}
UNINSTALLED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2"
return 0
fi
echo "nvm called with unexpected args: $*"
}
# Closed stdin: a prompt would read nothing and abort.
OUTPUT="$(nvm_prune --dry-run </dev/null; echo "exit: $?"; echo "uninstalled:${UNINSTALLED_VERSIONS}")"
EXPECTED="Pruning old versions...
Dry run: nothing will be removed.
These versions would be removed:
v18.20.5
exit: 0
uninstalled:"
[ "${OUTPUT}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${OUTPUT}<"
echo "All tests passed"
@@ -0,0 +1,45 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
# With --minor, grouping is per major.minor instead of per major: v18.20.5
# and v18.20.7 share a minor (keep only v18.20.7), while v18.21.0 is a
# separate minor and must be kept too.
nvm_ls() {
echo " v18.20.5"
echo " v18.20.7"
echo " v18.21.0"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "none"
}
nvm_is_version_installed() {
return 0
}
UNINSTALLED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2"
return 0
fi
echo "nvm called with unexpected args: $*"
}
nvm_prune --minor --force
EXPECTED=" v18.20.5"
if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then
die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'"
fi
echo "All tests passed"
@@ -0,0 +1,45 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
# For 0.x releases the minor version is effectively the major version
# (0.10.x and 0.12.x were distinct release lines), so even without --minor
# they must be grouped - and kept - separately.
nvm_ls() {
echo " v0.10.48"
echo " v0.10.49"
echo " v0.12.18"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "none"
}
nvm_is_version_installed() {
return 0
}
UNINSTALLED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2"
return 0
fi
echo "nvm called with unexpected args: $*"
}
nvm_prune --force
EXPECTED=" v0.10.48"
if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then
die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'"
fi
echo "All tests passed"
@@ -0,0 +1,95 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
TEMP_NVM_DIR="$(mktemp -d)"
[ -n "${TEMP_NVM_DIR}" ] && [ -d "${TEMP_NVM_DIR}" ] || die 'mktemp failed'
trap 'rm -rf "${TEMP_NVM_DIR}"' EXIT
NVM_DIR="${TEMP_NVM_DIR}"
export NVM_DIR
: nvm.sh
\. ../../nvm.sh
\. ../common.sh
[ "${NVM_DIR}" = "${TEMP_NVM_DIR}" ] || die "NVM_DIR changed to ${NVM_DIR}"
for v in v0.10.48 v0.12.18 v12.0.0 v12.1.0 v12.2.0 v16.0.0 v16.1.0 v18.20.5 v18.20.7 v20.0.0 v20.1.0 v22.0.0 v22.1.0 v22.2.0; do
make_fake_node "${v}" || die "make_fake_node ${v} failed"
done
# An incomplete install, with no node binary, is not offered for removal.
mkdir -p "$(nvm_version_path v14.0.0)"
make_fake_node v14.1.0 || die 'make_fake_node v14.1.0 failed'
make_fake_iojs v3.3.0 || die 'make_fake_iojs v3.3.0 failed'
make_fake_iojs v3.3.1 || die 'make_fake_iojs v3.3.1 failed'
# Symlinked alias files and directories count too.
mkdir -p "${TEMP_NVM_DIR}/linked/lts" "${NVM_DIR}/alias"
printf 'v18.20.5\n' > "${TEMP_NVM_DIR}/linked/default"
ln -s "${TEMP_NVM_DIR}/linked/default" "${NVM_DIR}/alias/default"
ln -s "${TEMP_NVM_DIR}/linked/lts" "${NVM_DIR}/alias/lts"
printf '12.1\n' > "${NVM_DIR}/alias/partial"
printf 'second\n' > "${NVM_DIR}/alias/first"
printf 'v16.0.0\n' > "${NVM_DIR}/alias/second"
printf 'v20.0.0\n' > "${NVM_DIR}/alias/lts/foo"
ln -s "$(nvm_version_path v22.0.0)" "${NVM_DIR}/current"
GLOBALS="$(nvm_version_path v22.1.0)/lib/node_modules"
mkdir -p "${GLOBALS}/npm" "${GLOBALS}/corepack" "${GLOBALS}/.bin" "${GLOBALS}/foo" "${GLOBALS}/@scope/a" "${GLOBALS}/@scope/b"
mkdir -p "${TEMP_NVM_DIR}/linked-scope/c"
ln -s "${TEMP_NVM_DIR}/linked-scope" "${GLOBALS}/@linked"
# A system node newer than every install of its major is not an install.
mkdir "${TEMP_NVM_DIR}/system-bin"
printf '#!/bin/sh\necho v22.3.0\n' > "${TEMP_NVM_DIR}/system-bin/node"
chmod +x "${TEMP_NVM_DIR}/system-bin/node"
PATH="${TEMP_NVM_DIR}/system-bin:${PATH}"
nvm_ls | grep -q '^ *system' || die "the system node stub is not listed: $(nvm_ls)"
OUTPUT="$(nvm prune --bogus 2>&1 </dev/null)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = 127 ] || die "an unknown argument should exit 127, got ${EXIT_CODE}"
case "${OUTPUT}" in
*'Unknown argument: --bogus'*) ;;
*) die "unexpected unknown-argument output: ${OUTPUT}" ;;
esac
EXPECTED_DRY_RUN="Pruning old versions...
Dry run: nothing will be removed.
These versions would be removed:
iojs-v3.3.0
v12.0.0
v22.1.0 (warning: also removes 4 global module(s))"
OUTPUT="$(set -u; nvm prune --dry-run 2>&1 </dev/null)" || die "dry run failed: ${OUTPUT}"
[ "${OUTPUT}" = "${EXPECTED_DRY_RUN}" ] || die "dry run: expected >${EXPECTED_DRY_RUN}<, got >${OUTPUT}<"
for v in v12.0.0 v22.1.0 iojs-v3.3.0; do
nvm_is_version_installed "${v}" || die "the dry run removed ${v}"
done
EXPECTED_OUTPUT="Pruning old versions...
These versions will be removed:
iojs-v3.3.0
v12.0.0
v22.1.0 (warning: also removes 4 global module(s))
Uninstalled iojs-v3.3.0.
Uninstalled v12.0.0.
Uninstalled v22.1.0.
Pruned 3 version(s)."
OUTPUT="$(set -u; nvm prune --force 2>&1 </dev/null)" || die "prune failed: ${OUTPUT}"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"
for v in v12.0.0 v22.1.0 iojs-v3.3.0; do
! nvm_is_version_installed "${v}" || die "${v} should have been removed"
done
for v in v0.10.48 v0.12.18 v12.1.0 v12.2.0 v16.0.0 v16.1.0 v18.20.5 v18.20.7 v20.0.0 v20.1.0 v22.0.0 v22.2.0 iojs-v3.3.1; do
nvm_is_version_installed "${v}" || die "${v} should have been kept"
done
for a in default partial first second lts/foo; do
[ -f "${NVM_DIR}/alias/${a}" ] || die "alias ${a} should have been kept"
done
OUTPUT="$(nvm prune --force 2>&1 </dev/null)" || die "second prune failed: ${OUTPUT}"
case "${OUTPUT}" in
*'Nothing to prune'*) ;;
*) die "expected nothing left to prune, got: ${OUTPUT}" ;;
esac
@@ -0,0 +1,56 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
nvm_ls() {
echo " iojs-v3.3.0"
echo " iojs-v3.3.1"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "none"
}
nvm_is_version_installed() {
case "$1" in
iojs-*) return 0 ;;
*) return 1 ;;
esac
}
UNINSTALLED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2"
return 0
fi
echo "nvm called with unexpected args: $*"
}
nvm_prune --force
EXPECTED=" iojs-v3.3.0"
if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then
die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'"
fi
# The current io.js version is kept even when it is not the newest.
nvm_ls_current() {
echo "iojs-v3.3.0"
}
UNINSTALLED_VERSIONS=""
nvm_prune --force
if [ -n "${UNINSTALLED_VERSIONS}" ]; then
die "Expected nothing uninstalled, got: '${UNINSTALLED_VERSIONS}'"
fi
echo "All tests passed"
@@ -0,0 +1,56 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
nvm_ls() {
echo " v18.20.5"
echo " v18.20.7"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "none"
}
nvm_is_version_installed() {
return 0
}
UNINSTALLED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2"
return 0
fi
echo "nvm called with unexpected args: $*"
}
for ANSWER in n N no '' yy 'y '; do
OUTPUT="$(printf '%s\n' "${ANSWER}" | { nvm_prune; echo "exit: $?"; echo "uninstalled:${UNINSTALLED_VERSIONS}"; })"
case "${OUTPUT}" in
*'Proceed? (y/N)'*'Aborted.'*'exit: 0'*'uninstalled:') ;;
*) die "answering >${ANSWER}< should abort, got: ${OUTPUT}" ;;
esac
done
OUTPUT="$({ nvm_prune; echo "exit: $?"; echo "uninstalled:${UNINSTALLED_VERSIONS}"; } </dev/null)"
case "${OUTPUT}" in
*'Aborted.'*'exit: 0'*'uninstalled:') ;;
*) die "no answer should abort, got: ${OUTPUT}" ;;
esac
for ANSWER in y Y yes Yes YES; do
OUTPUT="$(printf '%s\n' "${ANSWER}" | { nvm_prune; echo "exit: $?"; echo "uninstalled:${UNINSTALLED_VERSIONS}"; })"
case "${OUTPUT}" in
*'Proceed? (y/N)'*'Pruned 1 version(s).'*'exit: 0'*'uninstalled: v18.20.5') ;;
*) die "answering >${ANSWER}< should uninstall v18.20.5, got: ${OUTPUT}" ;;
esac
done
echo "All tests passed"
+88
View File
@@ -0,0 +1,88 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
nvm_ls() {
echo " v16.20.2"
echo " v18.20.5"
echo " v18.20.7"
echo " v20.17.0"
echo "-> v22.22.0"
echo " v24.13.0"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "v22.22.0"
}
nvm_is_version_installed() {
return 0
}
UNINSTALLED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
UNINSTALLED_VERSIONS="${UNINSTALLED_VERSIONS} $2"
return 0
fi
echo "nvm called with unexpected args: $*"
}
nvm_prune --force
EXPECTED=" v18.20.5"
if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then
die "Expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'"
fi
# The current version is kept even when it is not the newest.
nvm_ls() {
echo " v18.0.0"
echo "-> v18.1.0"
echo " v18.2.0"
}
nvm_ls_current() {
echo "v18.1.0"
}
UNINSTALLED_VERSIONS=""
nvm_prune --force
EXPECTED=" v18.0.0"
if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then
die "current version case: expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'"
fi
# Versions compare numerically, not lexically.
nvm_ls() {
echo " v18.10.0"
echo " v18.9.0"
echo " v18.9.10"
echo " v18.9.2"
}
nvm_ls_current() {
echo "none"
}
UNINSTALLED_VERSIONS=""
nvm_prune --force
EXPECTED=" v18.9.0 v18.9.2 v18.9.10"
if [ "${UNINSTALLED_VERSIONS}" != "${EXPECTED}" ]; then
die "numeric order case: expected uninstalled: '${EXPECTED}', got: '${UNINSTALLED_VERSIONS}'"
fi
for VAR in NVM_PRUNE_ARG NVM_DRY_RUN NVM_FORCE NVM_KEEP_MINOR NVM_CANDIDATES NVM_PROTECTED NVM_ALIAS_DIR NVM_ALIAS_FILE NVM_PRUNE_VERSION NVM_VERSION_PATH NVM_GLOBALS_DIR NVM_GLOBAL NVM_GLOBALS_COUNT NVM_REMOVE NVM_REMOVE_LIST NVM_ANSWER NVM_REMOVED_COUNT NVM_FAILED_COUNT; do
eval "[ -z \"\${${VAR}+set}\" ]" || die "nvm_prune leaked ${VAR}"
done
echo "All tests passed"
@@ -0,0 +1,54 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
. ../../nvm.sh
nvm_ls() {
echo " v18.0.0"
echo " v18.1.0"
echo " v18.2.0"
}
# Ignore any aliases in the real NVM_DIR.
nvm_alias_path() {
echo /nonexistent
}
nvm_ls_current() {
echo "none"
}
nvm_is_version_installed() {
return 0
}
ATTEMPTED_VERSIONS=""
nvm() {
if [ "$1" = "uninstall" ]; then
ATTEMPTED_VERSIONS="${ATTEMPTED_VERSIONS} $2"
[ "$2" != v18.0.0 ]
return
fi
echo "nvm called with unexpected args: $*"
}
OUTPUT="$(nvm_prune --force 2>&1; echo "exit: $?"; echo "attempted:${ATTEMPTED_VERSIONS}")"
EXPECTED="Pruning old versions...
These versions will be removed:
v18.0.0
v18.1.0
Failed to uninstall v18.0.0.
Uninstalled v18.1.0.
Pruned 1 version(s); 1 failed.
exit: 1
attempted: v18.0.0 v18.1.0"
[ "${OUTPUT}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${OUTPUT}<"
STDERR="$(nvm_prune --force 2>&1 >/dev/null)"
EXPECTED_STDERR="Failed to uninstall v18.0.0.
Pruned 1 version(s); 1 failed."
[ "${STDERR}" = "${EXPECTED_STDERR}" ] || die "expected stderr >${EXPECTED_STDERR}<, got >${STDERR}<"
echo "All tests passed"