diff --git a/test/fast/Unit tests/nvm_explain_excluded_version b/test/fast/Unit tests/nvm_explain_excluded_version new file mode 100755 index 00000000..a023bb42 --- /dev/null +++ b/test/fast/Unit tests/nvm_explain_excluded_version @@ -0,0 +1,133 @@ +#!/bin/sh + +cleanup() { + unset -f nvm_version nvm_is_version_installed + unset NVM_ONLY NVM_ONLY_SELECTED +} +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +unset NVM_ONLY NVM_ONLY_SELECTED + +nvm_version() { + echo 'nvm_version stderr' >&2 + if [ "_${NVM_ONLY_SELECTED-}" != '_alpha,lts,non-lts,iojs' ] || [ $# -ne 1 ]; then + echo 'N/A' + return 3 + fi + case "${1-}" in + '') echo 'v3.0.0' ;; + v1.2.3 | 1.2.3) echo 'v1.2.3' ;; + iojs-1.0.0) echo 'iojs-v1.0.0' ;; + 27) echo 'v27.0.0-alpha.1' ;; + 'a "b" $c') echo 'v4.0.0' ;; + notinstalled) echo 'v5.0.0' ;; + failing) + echo 'v1.2.3' + return 3 + ;; + empty) ;; + *) + echo 'N/A' + return 3 + ;; + esac +} + +# `N/A` and empty count as installed, so only the function's own guard rejects them +nvm_is_version_installed() { + case "${1-}" in + v1.2.3 | iojs-v1.0.0 | v27.0.0-alpha.1 | v3.0.0 | v4.0.0 | N/A | '') return 0 ;; + esac + return 1 +} + +DEFAULT_CATEGORIES='lts,non-lts,iojs' +HINT='Include its category with `--only=`, or in `$NVM_ONLY`.' + +expect_explained() { + EXPECTED_LABEL="${1}" + EXPECTED_CATEGORIES="${2}" + shift 2 + OUTPUT="$(nvm_explain_excluded_version "$@" 2>/dev/null)" + [ "_${OUTPUT}" = '_' ] || die "nvm_explain_excluded_version $* printed to stdout: >${OUTPUT}<" + ERR_OUTPUT="$(nvm_explain_excluded_version "$@" 2>&1 >/dev/null)" + EXIT_CODE="$?" + [ "_${EXIT_CODE}" = '_0' ] || die "nvm_explain_excluded_version $* exit code: got >${EXIT_CODE}<, expected >0<" + EXPECTED="N/A: version \"${EXPECTED_LABEL}\" is installed, but excluded by the selected version categories (${EXPECTED_CATEGORIES}). + +${HINT}" + [ "_${ERR_OUTPUT}" = "_${EXPECTED}" ] || die "nvm_explain_excluded_version $* stderr: got >${ERR_OUTPUT}<, expected >${EXPECTED}<" +} + +expect_not_explained() { + OUTPUT="$(nvm_explain_excluded_version "$@" 2>&1)" + EXIT_CODE="$?" + [ "_${EXIT_CODE}" = '_1' ] || die "nvm_explain_excluded_version $* exit code: got >${EXIT_CODE}<, expected >1<" + [ "_${OUTPUT}" = '_' ] || die "nvm_explain_excluded_version $* output: got >${OUTPUT}<, expected nothing" +} + +# the resolved version matches the requested one, with or without a `v` prefix +expect_explained 'v1.2.3' "${DEFAULT_CATEGORIES}" v1.2.3 +expect_explained 'v1.2.3' "${DEFAULT_CATEGORIES}" 1.2.3 +expect_explained 'iojs-v1.0.0' "${DEFAULT_CATEGORIES}" iojs-1.0.0 + +# a pattern resolving to a different version shows both +expect_explained '27 -> v27.0.0-alpha.1' "${DEFAULT_CATEGORIES}" 27 +expect_explained 'a "b" $c -> v4.0.0' "${DEFAULT_CATEGORIES}" 'a "b" $c' + +# only the first argument is looked up +expect_explained 'v1.2.3' "${DEFAULT_CATEGORIES}" v1.2.3 extra +expect_explained '27 -> v27.0.0-alpha.1' "${DEFAULT_CATEGORIES}" 27 extra + +# the listed categories come from NVM_ONLY_SELECTED, then NVM_ONLY, then the default +NVM_ONLY='lts' +expect_explained 'v1.2.3' 'lts' v1.2.3 +NVM_ONLY_SELECTED='non-lts,iojs' +expect_explained 'v1.2.3' 'non-lts,iojs' v1.2.3 +unset NVM_ONLY +expect_explained 'v1.2.3' 'non-lts,iojs' v1.2.3 +NVM_ONLY='lts' +NVM_ONLY_SELECTED='' +expect_explained 'v1.2.3' 'lts' v1.2.3 +NVM_ONLY='' +expect_explained 'v1.2.3' "${DEFAULT_CATEGORIES}" v1.2.3 +unset NVM_ONLY NVM_ONLY_SELECTED + +# the exit code of the version lookup is ignored +expect_explained 'failing -> v1.2.3' "${DEFAULT_CATEGORIES}" failing + +# no match across every category +expect_not_explained missing +# a match that is not installed +expect_not_explained notinstalled +# an empty resolution +expect_not_explained empty + +# nounset, with no arguments, behaves as without it +PLAIN="$(nvm_explain_excluded_version 2>&1; echo "rc=$?")" +STRICT="$(set -u; nvm_explain_excluded_version 2>&1; echo "rc=$?")" +[ "_${STRICT}" = "_${PLAIN}" ] || die "nvm_explain_excluded_version with no arguments under set -u: got >${STRICT}<, expected >${PLAIN}<" + +# errexit survives a failing version lookup +OUTPUT="$(set -e; nvm_explain_excluded_version failing 2>/dev/null; echo reached)" +[ "_${OUTPUT}" = '_reached' ] || die "nvm_explain_excluded_version failing under set -e: got >${OUTPUT}<, expected >reached<" + +# direct calls leave the caller's variables alone +unset NVM_EXCLUDED_VERSION +NVM_ONLY_SELECTED='lts' +nvm_explain_excluded_version 27 2>/dev/null +EXIT_CODE="$?" +[ "_${EXIT_CODE}" = '_0' ] || die "direct nvm_explain_excluded_version 27 exit code: got >${EXIT_CODE}<, expected >0<" +[ "_${NVM_EXCLUDED_VERSION-unset}" = '_unset' ] || die "direct nvm_explain_excluded_version 27 set NVM_EXCLUDED_VERSION: >${NVM_EXCLUDED_VERSION-}<" +[ "_${NVM_ONLY_SELECTED-unset}" = '_lts' ] || die "direct nvm_explain_excluded_version 27 changed NVM_ONLY_SELECTED: got >${NVM_ONLY_SELECTED-unset}<, expected >lts<" +unset NVM_ONLY_SELECTED +nvm_explain_excluded_version missing +EXIT_CODE="$?" +[ "_${EXIT_CODE}" = '_1' ] || die "direct nvm_explain_excluded_version missing exit code: got >${EXIT_CODE}<, expected >1<" +[ "_${NVM_EXCLUDED_VERSION-unset}" = '_unset' ] || die "direct nvm_explain_excluded_version missing set NVM_EXCLUDED_VERSION: >${NVM_EXCLUDED_VERSION-}<" +[ "_${NVM_ONLY_SELECTED-unset}" = '_unset' ] || die "direct nvm_explain_excluded_version missing set NVM_ONLY_SELECTED: >${NVM_ONLY_SELECTED-}<" + +cleanup