[Fix] install.sh: update from nvm's remote; nvm_ls: list versions without globs; nvm_ensure_version_installed: do not leak VERSION

`install.sh`: an existing install now fetches from the remote whose URL is nvm's source,
adding one when none matches,
instead of always fetching `origin`;
fresh installs still name it `origin`.
The remote's URL is read with `git config`,
since `git remote get-url` needs git 2.7.

`nvm_ls`: `find` lists each version directory's non-hidden subdirectories itself,
so it works under `set -f`,
which disables the globs it was given;
the directories only need to exist.

`nvm_ensure_version_installed`:
the resolved alias was assigned to `VERSION` without declaring it local.

Co-authored-by: dajiaohuang <mikewushuwen@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
Jordan Harbandanddajiaohuang committed 2026-11-19 22:51:28 -08:00
1 parent a4fadf1eae
commit dbda4b9387
6 files changed
+109 -64

No files matched your search

-38
View File
@@ -1,38 +0,0 @@
#!/bin/sh
cleanup() {
rm -rf "${TEST_DIR}"
}
die() {
echo "$@"
cleanup
exit 1
}
: nvm.sh
\. ../../../nvm.sh
TEST_DIR="$(mktemp -d)"
# glob characters and a space in the path are matched literally
DIR="${TEST_DIR}/a [b] *"
! nvm_dir_has_entries "${DIR}" || die 'a missing directory has no entries'
mkdir "${DIR}"
! nvm_dir_has_entries "${DIR}" || die 'an empty directory has no entries'
touch "${DIR}/.hidden"
! nvm_dir_has_entries "${DIR}" || die 'a directory with only dotfiles has no entries, as with `ls`'
ln -s "${TEST_DIR}/nonexistent" "${DIR}/broken"
nvm_dir_has_entries "${DIR}" || die 'a broken symlink is an entry'
rm "${DIR}/broken"
mkdir "${DIR}/v1.0.0"
nvm_dir_has_entries "${DIR}" || die 'a directory with a subdirectory has entries'
! nvm_dir_has_entries "${TEST_DIR}/a [b] " || die 'the directory name is not a pattern'
cleanup
@@ -70,4 +70,12 @@ EXPECTED_OUTPUT=''
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to give $EXPECTED_OUTPUT, got $OUTPUT"
[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm_ensure_version_installed system' with iojs installed to exit with 0, got $EXIT_CODE"
# An unresolved installed-version check must not overwrite a caller's VERSION.
VERSION='caller-value'
nvm_resolve_alias() { [ "_${1-}" = '_nonexistent' ] || return 2; nvm_echo 'v123.4.5'; }
nvm_ensure_version_installed nonexistent >/dev/null 2>&1 && die 'expected missing version failure'
[ "${VERSION}" = 'caller-value' ] || die "VERSION leaked: ${VERSION}"
unset -f nvm_resolve_alias
unset VERSION
cleanup
@@ -0,0 +1,25 @@
#!/bin/sh
die() { echo "$*" >&2; cleanup; exit 1; }
cleanup() { rm -rf "${TEST_DIR}"; }
\. ../../../nvm.sh --no-use
\. ../../common.sh
TEST_DIR="${PWD}/nvm_ls_hidden_noglob_tmp"
NVM_DIR="${TEST_DIR}"
export NVM_DIR
mkdir -p "${NVM_DIR}/versions/node/.hidden" "${NVM_DIR}/versions/node/v24.13.0/bin" || die 'could not create fixture'
make_echo "${NVM_DIR}/versions/node/v24.13.0/bin/node" 'v24.13.0' || die 'could not create fake node'
set -f
try nvm_ls 24
[ "_${CAPTURED_STDOUT}" = '_v24.13.0' ] || die "unexpected output with noglob: ${CAPTURED_STDOUT}"
case "$-" in *f*) ;; *) die 'nvm_ls changed noglob';; esac
set +f
try nvm_ls
case "${CAPTURED_STDOUT}" in *.hidden*) die "expected hidden entries to be skipped; got ${CAPTURED_STDOUT}" ;; esac
rm -rf "${NVM_DIR}/versions/node/v24.13.0"
try nvm_ls 24
[ "_${CAPTURED_STDERR}" = '_' ] || die "unexpected error for hidden-only directory: ${CAPTURED_STDERR}"
cleanup
+33
View File
@@ -0,0 +1,33 @@
#!/bin/sh
NVM_ENV=testing \. ../../install.sh
die() { echo "$*" >&2; exit 1; }
TEST_ROOT="$(mktemp -d)" || die 'could not create test directory'
trap 'rm -rf "${TEST_ROOT}"' EXIT HUP INT TERM
SOURCE="${TEST_ROOT}/source"
git init -q "${SOURCE}" || die 'could not initialize source'
git -C "${SOURCE}" -c user.name=test -c user.email=test@example.invalid commit --allow-empty -qm fixture || die 'could not commit fixture'
git -C "${SOURCE}" tag v0.0.1 || die 'could not tag fixture'
nvm_source() { printf '%s\n' "${SOURCE}"; }
NVM_INSTALL_VERSION=v0.0.1
export NVM_INSTALL_VERSION
NVM_DIR="${TEST_ROOT}/fresh"
export NVM_DIR
install_nvm_from_git || die 'fresh install failed'
[ "$(git -C "${NVM_DIR}" remote get-url origin)" = "${SOURCE}" ] || die 'fresh install did not retain origin'
NVM_DIR="${TEST_ROOT}/existing"
export NVM_DIR
git init -q "${NVM_DIR}" || die 'could not initialize existing install'
git -C "${NVM_DIR}" remote add fork "${TEST_ROOT}/wrong" || die 'could not add fork'
git -C "${NVM_DIR}" remote add origin "${SOURCE}" || die 'could not add origin'
install_nvm_from_git || die 'existing install did not select matching origin'
[ "$(git -C "${NVM_DIR}" remote get-url fork)" = "${TEST_ROOT}/wrong" ] || die 'fork remote changed'
git -C "${NVM_DIR}" remote set-url origin "${TEST_ROOT}/wrong-origin" || die 'could not change origin'
git -C "${NVM_DIR}" remote add nvm-origin "${TEST_ROOT}/occupied" || die 'could not occupy alternate name'
install_nvm_from_git || die 'existing install did not add a matching remote'
[ "$(git -C "${NVM_DIR}" remote get-url nvm-origin-1)" = "${SOURCE}" ] || die 'new remote missing'
[ "$(git -C "${NVM_DIR}" remote get-url origin)" = "${TEST_ROOT}/wrong-origin" ] || die 'origin remote changed'