Files
nvm/test/fast/Unit tests/nvm_offline_version
Jordan Harband 59bd32be6b [New] nvm install --offline: install from cache without network access
Add `--offline` flag to `nvm install` that resolves versions using only locally installed versions and cached downloads. No network calls are made.

New helper functions `nvm_ls_cached` and `nvm_offline_version` scan `$NVM_DIR/.cache/bin/` for previously downloaded tarballs.
In offline mode, `nvm_download_artifact` returns cached tarballs directly without checksum verification or download attempts.
The curl/wget requirement is skipped when `--offline` is set.
Supports `--lts` via locally stored LTS alias files.
2026-03-13 16:13:19 -04:00

40 lines
1.3 KiB
Bash

#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
\. ../../../nvm.sh
\. ../../common.sh
TEST_DIR="$(pwd)/nvm_offline_version_tmp"
cleanup() {
rm -rf "${TEST_DIR}"
}
[ ! -e "${TEST_DIR}" ] && mkdir -p "${TEST_DIR}"
# nvm_offline_version should find installed versions
INSTALLED_VERSION="$(nvm_version node)"
if [ "_${INSTALLED_VERSION}" != '_N/A' ] && [ "_${INSTALLED_VERSION}" != '_system' ]; then
try nvm_offline_version "${INSTALLED_VERSION}"
[ "_$CAPTURED_STDOUT" = "_${INSTALLED_VERSION}" ] \
|| die "nvm_offline_version '${INSTALLED_VERSION}' should return '${INSTALLED_VERSION}'; got '$CAPTURED_STDOUT'"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] \
|| die "nvm_offline_version '${INSTALLED_VERSION}' should exit 0; got '$CAPTURED_EXIT_CODE'"
fi
# nvm_offline_version with nonexistent version should return N/A
try nvm_offline_version "999.999.999"
[ "_$CAPTURED_STDOUT" = "_N/A" ] \
|| die "nvm_offline_version '999.999.999' should return 'N/A'; got '$CAPTURED_STDOUT'"
[ "_$CAPTURED_EXIT_CODE" = "_3" ] \
|| die "nvm_offline_version '999.999.999' should exit 3; got '$CAPTURED_EXIT_CODE'"
# nvm_ls_cached with nonexistent pattern should return nothing
try nvm_ls_cached "999.999"
[ -z "$CAPTURED_STDOUT" ] \
|| die "nvm_ls_cached '999.999' should return empty; got '$CAPTURED_STDOUT'"
cleanup