Skip to content
Myr

[Perf] `nvm_version`: resolve an exact, installed version without `nvm_ls`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

An alias of the same name still takes precedence, as it does in `nvm_ls`.

authored by Cassidy Marble · committed by Jordan Harband · parent d211d5f · Browse files · 2 files+38 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−1 removed

  • tests:+28 added−1 removed
  • other:+10 added−0 removed

2 files+38 added−1 removed

modified:test/fast/Unit tests/nvm_version+28 added-1 removed
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }4cleanup () {5 unset -f nvm_ls_current nvm_ls5 unset -f nvm_ls_current nvm_ls nvm_is_version_installed6}78: nvm.sh9\. ../../../nvm.sh
37 unmodified lines
47try nvm_version foo48EXPECTED_OUTPUT="N/A"49[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version" did not return N/A when nvm_ls returns nothing'50[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die '"nvm_version" returning N/A did not exit code with code 3'5152# an exact installed version resolves without forking "nvm_ls"53nvm_is_version_installed() { return 0; }54nvm_ls() { die '"nvm_version" must not call "nvm_ls" for an exact installed version'; }55try nvm_version v1.2.356EXPECTED_OUTPUT="v1.2.3"57[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version v1.2.3" did not return the version itself; got '"$CAPTURED_STDOUT"58[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die '"nvm_version v1.2.3" did not exit 0; got '"$CAPTURED_EXIT_CODE"5960# an exact version that is not installed still falls back to "nvm_ls"61nvm_is_version_installed() { return 1; }62nvm_ls() { echo "pattern: $1"; }63try nvm_version v1.2.364EXPECTED_OUTPUT="pattern: v1.2.3"65[ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die '"nvm_version v1.2.3" did not fall back to "nvm_ls"; got '"$CAPTURED_STDOUT"66[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die '"nvm_version v1.2.3" did not exit 0; got '"$CAPTURED_EXIT_CODE"6768# only exact, fully-qualified versions take the fast path69nvm_is_version_installed() { die '"nvm_version" must not call "nvm_is_version_installed" for non-exact versions'; }70nvm_ls() { echo "pattern: $1"; }71for PATTERN in v1.2 v1 1.2.3 v1.2.3-rc.1 v1.2.3.4 v1.2.3.; do72 try nvm_version "$PATTERN"73 EXPECTED_OUTPUT="pattern: $PATTERN"74 [ "_$CAPTURED_STDOUT" = "_$EXPECTED_OUTPUT" ] || die "nvm_version $PATTERN did not fall back to nvm_ls; got $CAPTURED_STDOUT"75 [ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_version $PATTERN did not exit 0; got $CAPTURED_EXIT_CODE"76done77unset -f nvm_is_version_installed
modified:nvm.sh+10 added
911 unmodified lines
912 "_${NVM_NODE_PREFIX}" | "_${NVM_NODE_PREFIX}-")913 PATTERN="stable"914 ;;915 esac916917 # Fast path: an exact, fully-qualified version that is installed resolves to918 # itself, unless an alias of the same name takes precedence. Short-circuiting919 # it here avoids the `nvm_ls` subprocess pipeline on shell startup; every920 # other pattern falls through to `nvm_ls` unchanged.921 if nvm_is_installed_exact_version "${PATTERN}"; then922 nvm_echo "${PATTERN}"923 return 0924 fi925926 VERSION="$(nvm_ls "${PATTERN}" | command tail -1)"927 case "${VERSION}" in928 system[[:blank:]]*)929 VERSION='system'
4556 unmodified lines