[Refactor] add `nvm_stdout_is_terminal` helper
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christopher Sahnwaldt · committed by Jordan Harband · parent a1abfd1 · Browse files · 2 files·+37 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+37 added−1 removed
- tests:+32 added−0 removed
- other:+5 added−1 removed
Provenance
- Commit
2410215- Tree
8597df4- Author
Christopher Sahnwaldt <jcsahnwaldt@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
a1abfd1
2 files changed+37 added−1 removed
added:test/fast/Unit tests/nvm_stdout_is_terminal+32 added
1+#!/bin/sh2+3+tempfile=$(mktemp)4+5+die () { echo "$@" ; cleanup; exit 1; }6+cleanup() { rm -f "$tempfile"; }7+8+\. ../../../nvm.sh9+10+if [ -t 1 ] ; then11+ # test is running in a terminal, e.g. locally12+ nvm_stdout_is_terminal || die 'nvm_stdout_is_terminal should be true'13+ nvm_stdout_is_terminal 2>/dev/null || die 'nvm_stdout_is_terminal should be true when stderr is redirected'14+ nvm_stdout_is_terminal </dev/null || die 'nvm_stdout_is_terminal should be true when stdin is redirected'15+else16+ # test is not running in a terminal, e.g. on travis-ci17+ ! nvm_stdout_is_terminal || die 'nvm_stdout_is_terminal should be false'18+ ! nvm_stdout_is_terminal 2>/dev/null || die 'nvm_stdout_is_terminal should be false when stderr is redirected'19+ ! nvm_stdout_is_terminal </dev/null || die 'nvm_stdout_is_terminal should be false when stdin is redirected'20+fi21+22+(! nvm_stdout_is_terminal || echo "boo!") | read && die 'nvm_stdout_is_terminal should be false when stdout goes to a pipe'23+! nvm_stdout_is_terminal >/dev/null || die 'nvm_stdout_is_terminal should be false when stdout goes to /dev/null'24+! nvm_stdout_is_terminal >"$tempfile" || die 'nvm_stdout_is_terminal should be false when stdout goes to a file'25+[ "$(nvm_stdout_is_terminal; echo $?)" = "1" ] || die 'nvm_stdout_is_terminal should be false in command substitution'26+27+# also test the 'true' case while running on travis-ci or similar environments28+nvm_stdout_is_terminal >/dev/tty || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty'29+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'30+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'31+32+cleanupmodified:nvm.sh+5 added-1 removed
14 unmodified lines15 nvm_is_zsh() {16 [ -n "${ZSH_VERSION-}" ]17 }18 19+nvm_stdout_is_terminal() {20+ [ -t 1 ]21+}22+23 nvm_echo() {24 command printf %s\\n "$*" 2>/dev/null25 }26 3475 unmodified lines3502 nvm_list_aliases nvm_make_alias nvm_print_alias_path \3503 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \3504 nvm_sanitize_path nvm_has_colors nvm_process_parameters \3505 node_version_has_solaris_binary iojs_version_has_solaris_binary \3502- nvm_curl_libz_support nvm_command_info nvm_is_zsh \3506+ nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \3507 >/dev/null 2>&13508 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \3509 NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \3510 >/dev/null 2>&1143 unmodified lines