[Refactor] add `nvm_stdout_is_terminal` helper

Christopher Sahnwaldt 2019-02-24 00:03:19 +01:00 committed by Jordan Harband
parent a1abfd1fe4
commit 2410215b6a
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
2 changed files with 37 additions and 1 deletions

6
nvm.sh
View File

@ -16,6 +16,10 @@ nvm_is_zsh() {
[ -n "${ZSH_VERSION-}" ] [ -n "${ZSH_VERSION-}" ]
} }
nvm_stdout_is_terminal() {
[ -t 1 ]
}
nvm_echo() { nvm_echo() {
command printf %s\\n "$*" 2>/dev/null command printf %s\\n "$*" 2>/dev/null
} }
@ -3499,7 +3503,7 @@ nvm() {
nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \ nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \
nvm_sanitize_path nvm_has_colors nvm_process_parameters \ nvm_sanitize_path nvm_has_colors nvm_process_parameters \
node_version_has_solaris_binary iojs_version_has_solaris_binary \ node_version_has_solaris_binary iojs_version_has_solaris_binary \
nvm_curl_libz_support nvm_command_info nvm_is_zsh \ nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \
>/dev/null 2>&1 >/dev/null 2>&1
unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \ unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \ NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \

View File

@ -0,0 +1,32 @@
#!/bin/sh
tempfile=$(mktemp)
die () { echo "$@" ; cleanup; exit 1; }
cleanup() { rm -f "$tempfile"; }
\. ../../../nvm.sh
if [ -t 1 ] ; then
# test is running in a terminal, e.g. locally
nvm_stdout_is_terminal || die 'nvm_stdout_is_terminal should be true'
nvm_stdout_is_terminal 2>/dev/null || die 'nvm_stdout_is_terminal should be true when stderr is redirected'
nvm_stdout_is_terminal </dev/null || die 'nvm_stdout_is_terminal should be true when stdin is redirected'
else
# test is not running in a terminal, e.g. on travis-ci
! nvm_stdout_is_terminal || die 'nvm_stdout_is_terminal should be false'
! nvm_stdout_is_terminal 2>/dev/null || die 'nvm_stdout_is_terminal should be false when stderr is redirected'
! nvm_stdout_is_terminal </dev/null || die 'nvm_stdout_is_terminal should be false when stdin is redirected'
fi
(! nvm_stdout_is_terminal || echo "boo!") | read && die 'nvm_stdout_is_terminal should be false when stdout goes to a pipe'
! nvm_stdout_is_terminal >/dev/null || die 'nvm_stdout_is_terminal should be false when stdout goes to /dev/null'
! nvm_stdout_is_terminal >"$tempfile" || die 'nvm_stdout_is_terminal should be false when stdout goes to a file'
[ "$(nvm_stdout_is_terminal; echo $?)" = "1" ] || die 'nvm_stdout_is_terminal should be false in command substitution'
# also test the 'true' case while running on travis-ci or similar environments
nvm_stdout_is_terminal >/dev/tty || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty'
nvm_stdout_is_terminal >/dev/tty 2>/dev/null || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty and stderr is redirected'
nvm_stdout_is_terminal >/dev/tty </dev/null || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty and stdin is redirected'
cleanup