diff --git a/nvm.sh b/nvm.sh index 01213a3..6bc0082 100644 --- a/nvm.sh +++ b/nvm.sh @@ -16,6 +16,10 @@ nvm_is_zsh() { [ -n "${ZSH_VERSION-}" ] } +nvm_stdout_is_terminal() { + [ -t 1 ] +} + nvm_echo() { 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_sanitize_path nvm_has_colors nvm_process_parameters \ 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 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \ NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \ diff --git a/test/fast/Unit tests/nvm_stdout_is_terminal b/test/fast/Unit tests/nvm_stdout_is_terminal new file mode 100755 index 0000000..31b4666 --- /dev/null +++ b/test/fast/Unit tests/nvm_stdout_is_terminal @@ -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 false when stderr is redirected' + ! 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