From aa20d996b673a642ac77d214d01181548c5c79fd Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 30 Nov 2026 18:07:24 -0800 Subject: [PATCH] [Tests] `nvm_is_zsh`: add unit tests --- test/fast/Unit tests/nvm_is_zsh | 102 ++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 test/fast/Unit tests/nvm_is_zsh diff --git a/test/fast/Unit tests/nvm_is_zsh b/test/fast/Unit tests/nvm_is_zsh new file mode 100755 index 00000000..2c5b6473 --- /dev/null +++ b/test/fast/Unit tests/nvm_is_zsh @@ -0,0 +1,102 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +TEST_PWD="$(pwd)" +TEST_FLAGS="$-" +TEST_IFS="${IFS}" + +: nvm.sh +\. ../../../nvm.sh + +check_state () { + [ "_$(pwd)" = "_${TEST_PWD}" ] || die "$1: pwd changed to >$(pwd)<, expected >${TEST_PWD}<" + [ "_$-" = "_${TEST_FLAGS}" ] || die "$1: shell flags changed to >$-<, expected >${TEST_FLAGS}<" + [ "_${IFS}" = "_${TEST_IFS}" ] || die "$1: IFS changed to >${IFS}<, expected >${TEST_IFS}<" +} + +check_state 'after sourcing nvm.sh' + +check () { + OUTPUT="$(nvm_is_zsh 2>&1)" + EXIT_CODE=$? + [ "_${EXIT_CODE}" = "_$2" ] || die "$1: nvm_is_zsh exited >${EXIT_CODE}<, expected >$2<" + [ "_${OUTPUT}" = "_" ] || die "$1: nvm_is_zsh printed >${OUTPUT}<, expected nothing" +} + +if [ -n "${ZSH_VERSION-}" ]; then + check 'current shell has ZSH_VERSION' 0 + nvm_is_zsh || die 'current shell has ZSH_VERSION: direct nvm_is_zsh call failed' +else + check 'current shell lacks ZSH_VERSION' 1 + nvm_is_zsh && die 'current shell lacks ZSH_VERSION: direct nvm_is_zsh call succeeded' +fi +check_state 'after direct nvm_is_zsh call' + +( + ZSH_VERSION='5.9' + check 'ZSH_VERSION set' 0 + nvm_is_zsh || die 'ZSH_VERSION set: direct nvm_is_zsh call failed' + [ "_${ZSH_VERSION}" = "_5.9" ] || die "ZSH_VERSION set: ZSH_VERSION changed to >${ZSH_VERSION}<, expected >5.9<" +) || exit 1 + +( + ZSH_VERSION='0' + check 'ZSH_VERSION is 0' 0 +) || exit 1 + +( + ZSH_VERSION=' ' + check 'ZSH_VERSION is a space' 0 +) || exit 1 + +( + ZSH_VERSION='-n' + check 'ZSH_VERSION is -n' 0 +) || exit 1 + +( + ZSH_VERSION='' + check 'ZSH_VERSION empty' 1 + nvm_is_zsh && die 'ZSH_VERSION empty: direct nvm_is_zsh call succeeded' + [ "_${ZSH_VERSION+x}" = "_x" ] || die 'ZSH_VERSION empty: ZSH_VERSION was unset' + [ "_${ZSH_VERSION}" = "_" ] || die "ZSH_VERSION empty: ZSH_VERSION changed to >${ZSH_VERSION}<, expected empty" +) || exit 1 + +( + unset ZSH_VERSION + check 'ZSH_VERSION unset' 1 + nvm_is_zsh && die 'ZSH_VERSION unset: direct nvm_is_zsh call succeeded' + [ -z "${ZSH_VERSION+x}" ] || die "ZSH_VERSION unset: ZSH_VERSION became set to >${ZSH_VERSION}<" +) || exit 1 + +( + unset ZSH_VERSION + set -u + U_FLAGS="$-" + check 'ZSH_VERSION unset with set -u' 1 + nvm_is_zsh && die 'ZSH_VERSION unset with set -u: direct nvm_is_zsh call succeeded' + [ "_$-" = "_${U_FLAGS}" ] || die "ZSH_VERSION unset with set -u: shell flags changed to >$-<, expected >${U_FLAGS}<" + [ -z "${ZSH_VERSION+x}" ] || die 'ZSH_VERSION unset with set -u: ZSH_VERSION became set' +) || exit 1 + +( + ZSH_VERSION='5.9' + BASH_VERSION='' + ZSH_NAME='' + check 'only ZSH_VERSION set' 0 +) || exit 1 + +( + ZSH_VERSION='5.9' + BASH_VERSION='5.2.37(1)-release' + ZSH_NAME='' + check 'ZSH_VERSION and BASH_VERSION set' 0 +) || exit 1 + +( + unset ZSH_VERSION + BASH_VERSION='3.2.57(1)-release' + ZSH_NAME='zsh' + check 'only other shell vars set' 1 +) || exit 1