From b2ad63095f1d134954f40da50f6df7a828ea5d14 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 30 Nov 2026 18:07:21 -0800 Subject: [PATCH] [Tests] `nvm_filter_lts_versions`: add unit tests --- test/fast/Unit tests/nvm_filter_lts_versions | 164 +++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100755 test/fast/Unit tests/nvm_filter_lts_versions diff --git a/test/fast/Unit tests/nvm_filter_lts_versions b/test/fast/Unit tests/nvm_filter_lts_versions new file mode 100755 index 00000000..73160f9a --- /dev/null +++ b/test/fast/Unit tests/nvm_filter_lts_versions @@ -0,0 +1,164 @@ +#!/bin/sh + +ORIG_NVM_DIR="${NVM_DIR}" +TEST_DIR='' + +cleanup () { + unset -f nvm_ls_remote_index_tab + unset NVM_OFFLINE NVM_INDEX_NO_FETCH NVM_LTS_STATUS_PATH + case "${TEST_DIR}" in + /*) rm -rf "${TEST_DIR}" ;; + esac + NVM_DIR="${ORIG_NVM_DIR}" + export NVM_DIR +} + +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh +\. ../../common.sh + +TEST_DIR="$(mktemp -d)" || die 'could not create a temp dir' +NVM_DIR="${TEST_DIR}/nvm" +export NVM_DIR +mkdir -p "${NVM_DIR}" || die 'could not create NVM_DIR' +CALLS="${TEST_DIR}/calls" +RECORD="${NVM_DIR}/.lts-status" + +MOCK_WRITES_RECORD=0 +nvm_ls_remote_index_tab () { + printf '%s|%s|%s|%s|%s\n' "$#" "${NVM_LTS-unset}" "${1-}" "${2-}" "${3-}" >> "${CALLS}" + echo 'v99.0.0 from the fetch' + echo 'fetch stderr' >&2 + case "${MOCK_WRITES_RECORD}" in + 1) printf 'v24.11.0 krypton\n' > "${RECORD}" ;; + dir) mkdir -p "${RECORD}" ;; + esac +} + +unset NVM_OFFLINE NVM_INDEX_NO_FETCH NVM_LTS_STATUS_PATH +NVM_LTS=argon +export NVM_LTS + +EXPECTED_ERR='The LTS status of installed versions is not recorded yet; run `nvm ls-remote` to record it.' + +check_unrecorded () { + rm -f "${CALLS}" + try_err nvm_filter_lts_versions '*' < /dev/null + [ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "$1: exit code >${CAPTURED_EXIT_CODE}<, expected >3<" + [ "_${CAPTURED_STDERR}" = "_${EXPECTED_ERR}" ] || die "$1: stderr >${CAPTURED_STDERR}<, expected >${EXPECTED_ERR}<" + try nvm_filter_lts_versions '*' < /dev/null + [ "_${CAPTURED_STDOUT}" = '_' ] || die "$1: stdout >${CAPTURED_STDOUT}<, expected nothing" +} + +NVM_OFFLINE=1 +check_unrecorded 'offline, unrecorded' +[ ! -e "${CALLS}" ] || die "offline, unrecorded: index.tab should not be fetched; got >$(cat "${CALLS}")<" + +mkdir "${RECORD}" || die 'could not create a directory at the record path' +check_unrecorded 'offline, a directory at the record path' +unset NVM_OFFLINE +[ ! -e "${CALLS}" ] || die "offline, a directory at the record path: index.tab should not be fetched; got >$(cat "${CALLS}")<" +rmdir "${RECORD}" || die 'could not remove the directory at the record path' + +NVM_INDEX_NO_FETCH=yes +check_unrecorded 'no-fetch, unrecorded' +unset NVM_INDEX_NO_FETCH +[ ! -e "${CALLS}" ] || die "no-fetch, unrecorded: index.tab should not be fetched; got >$(cat "${CALLS}")<" + +NVM_OFFLINE=0 +check_unrecorded 'NVM_OFFLINE=0, fetch that records nothing' +unset NVM_OFFLINE +CALLED="$(cat "${CALLS}")" +EXPECTED_CALLS='3||node|std| +3||node|std|' +[ "_${CALLED}" = "_${EXPECTED_CALLS}" ] || die "fetch should be \`NVM_LTS='' nvm_ls_remote_index_tab node std ''\` once per call; got >${CALLED}<, expected >${EXPECTED_CALLS}<" + +MOCK_WRITES_RECORD=dir +check_unrecorded 'fetch that creates a directory at the record path' +rmdir "${RECORD}" || die 'could not remove the directory at the record path' + +MOCK_WRITES_RECORD=1 +rm -f "${CALLS}" +try_err nvm_filter_lts_versions '*' <${CAPTURED_EXIT_CODE}<, expected >0<" +[ "_${CAPTURED_STDERR}" = '_' ] || die "a fetch that records LTS status: stderr >${CAPTURED_STDERR}<, expected nothing" +rm -f "${CALLS}" "${RECORD}" +try nvm_filter_lts_versions '*' <${CAPTURED_EXIT_CODE}<, expected >0<" +[ "_${CAPTURED_STDOUT}" = '_v24.11.0' ] || die "a fetch that records LTS status: stdout >${CAPTURED_STDOUT}<, expected >v24.11.0<" +[ "_$(cat "${CALLS}")" = '_3||node|std|' ] || die "a fetch that records LTS status should run once; got >$(cat "${CALLS}")<" +MOCK_WRITES_RECORD=0 + +printf '%s\n' 'v24.11.0 krypton' 'v24.0.0 -' 'v20.19.0 iron' 'v22.11.0 -' 'v22.11.0 jod' > "${RECORD}" +rm -f "${CALLS}" + +INPUT='v24.11.0 +v24.0.0 +v26.0.0 +v20.19.0 * +system +iojs-v3.3.1 + +v22.11.0' + +check () { + try nvm_filter_lts_versions "$@" <${CAPTURED_EXIT_CODE}<, expected >0<" + [ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "\`nvm_filter_lts_versions $*\`: stdout >${CAPTURED_STDOUT}<, expected >${EXPECTED}<" +} + +EXPECTED='v24.11.0 +v20.19.0 * +v22.11.0' +check '*' + +NVM_OFFLINE=1 +check '*' +unset NVM_OFFLINE + +NVM_INDEX_NO_FETCH=1 +check '*' +unset NVM_INDEX_NO_FETCH + +printf '%s\n' 'v26.0.0 zzz' > "${TEST_DIR}/other-status" +NVM_LTS_STATUS_PATH="${TEST_DIR}/other-status" +export NVM_LTS_STATUS_PATH +check '*' +unset NVM_LTS_STATUS_PATH + +EXPECTED='v24.11.0' +check krypton + +EXPECTED='v22.11.0' +check jod +check jod krypton + +EXPECTED='' +check '' +check +check argon +check - +check 'r.' +check '.*' +check '^k' + +[ ! -e "${CALLS}" ] || die "with LTS status recorded, index.tab should not be fetched; got >$(cat "${CALLS}")<" + +try_err nvm_filter_lts_versions '*' <${CAPTURED_STDERR}<, expected nothing" + +nvm_filter_lts_versions '*' < /dev/null +[ "_${NVM_LTS_STATUS_PATH-unset}" = '_unset' ] || die "NVM_LTS_STATUS_PATH should not leak; got >${NVM_LTS_STATUS_PATH-}<" + +cleanup