Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ nvm_cd() {
\cd "$@"
}

# a caller that closed stderr, rather than redirecting it to /dev/null, makes
# the `>&2` fail; zsh cannot report that, so it aborts the script, which only a
# subshell contains, and `|| return 0` keeps the status from the caller
nvm_err() {
>&2 nvm_echo "$@"
(>&2 nvm_echo "$@") || return 0
}

nvm_err_with_colors() {
>&2 nvm_echo_with_colors "$@"
(>&2 nvm_echo_with_colors "$@") || return 0
}

nvm_grep() {
Expand Down
2 changes: 2 additions & 0 deletions test/fast/Unit tests/nvm_compare_checksum
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ EXPECTED_OUTPUT='Checksums matched!'
[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<"
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}"

(set +x; nvm_compare_checksum ../../../nvm.sh checksum 2>&-) || die "with stderr closed, expected to exit with code 0, got $?"

cleanup
21 changes: 21 additions & 0 deletions test/fast/Unit tests/nvm_err
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

die () { echo "$@" ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

try_err nvm_err 'an error'
EXPECTED_OUTPUT='an error'
[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<"
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}"

try_err nvm_err_with_colors 'an error'
EXPECTED_OUTPUT='an error'
[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<"
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}"

nvm_err 'an error' 2>&- || die "nvm_err with stderr closed exited with code $?"
nvm_err_with_colors 'an error' 2>&- || die "nvm_err_with_colors with stderr closed exited with code $?"
Loading