[Fix] nvm_has_colors: also check if stdout is a terminal

This commit is contained in:
Jordan Harband
2026-01-26 23:31:00 -08:00
parent d2f93c1c8e
commit 35212c1346
17 changed files with 94 additions and 39 deletions

View File

@@ -33,6 +33,8 @@ printf '%s\n' "$(cat "${LTS_NAMES_PATH}" | tail -n +1)" | while IFS= read -r LTS
cp "${NVM_DIR}/alias/lts-backup/${LTS}" "${NVM_DIR}/alias/lts/"
done
nvm_has_colors() { return 0; }
nvm deactivate 2>/dev/null || die 'unable to deactivate'
\. ../../common.sh
@@ -67,12 +69,14 @@ printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do
INDEX=$(($INDEX + 1))
done
nvm_has_colors() { return 1; }
OUTPUT="$(nvm ls-remote lts/ARGON 2>&1)"
EXIT_CODE=$?
nvm_has_colors() { return 0; }
[ $EXIT_CODE -eq 3 ] || die "nvm ls-remote lts/ARGON did not exit 3, got '${EXIT_CODE}'"
EXPECTED_OUTPUT="LTS names must be lowercase
N/A"
N/A *"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote lts/ARGON did not output expected error message; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"
REMOTE="${PWD}/mocks/nvm_ls_remote.txt"

View File

@@ -0,0 +1,30 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
# nvm_has_colors should be false in command substitution (stdout is not a terminal)
[ "$(nvm_has_colors; echo $?)" = "1" ] || die 'nvm_has_colors should be false in command substitution'
# nvm_has_colors should be false when stdout goes to /dev/null
! nvm_has_colors >/dev/null || die 'nvm_has_colors should be false when stdout goes to /dev/null'
# nvm_has_colors should be false when stdout goes to a pipe
(! nvm_has_colors || echo "boo!") | read && die 'nvm_has_colors should be false when stdout goes to a pipe'
# nvm_has_colors should be false when NVM_NO_COLORS is set, even if stdout is a terminal
if [ -t 1 ]; then
! NVM_NO_COLORS="--no-colors" nvm_has_colors || die 'nvm_has_colors should be false when NVM_NO_COLORS is set'
fi
# nvm_has_colors should be true when stdout is a terminal and colors are supported
if [ -t 1 ] && nvm_has tput && [ "$(command tput -T "${TERM:-vt100}" colors)" -ge 8 ]; then
nvm_has_colors || die 'nvm_has_colors should be true when stdout is a terminal with color support'
fi
# nvm_has_colors should be true when redirected to /dev/tty (if available)
if (exec >/dev/tty) 2>/dev/null && nvm_has tput && [ "$(command tput -T "${TERM:-vt100}" colors)" -ge 8 ]; then
nvm_has_colors >/dev/tty || die 'nvm_has_colors should be true when stdout goes to /dev/tty'
fi

View File

@@ -39,7 +39,7 @@ nvm_version() {
}
OUTPUT="$(nvm_print_alias_path "$NVM_ALIAS_DIR" "$NVM_ALIAS_DIR/blah" | strip_colors)"
EXPECTED_OUTPUT='blah -> "blah" (-> v"blah")'
EXPECTED_OUTPUT='blah -> "blah" (-> v"blah" *)'
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_print_alias_path \"\$NVM_ALIAS_DIR\" \"\$NVM_ALIAS_DIR/blah\"' should strip alias dir and print nvm_alias output; got '$OUTPUT', expected '$EXPECTED_OUTPUT'"
cleanup

View File

@@ -33,7 +33,7 @@ nvm_version() {
}
OUTPUT="$(nvm_print_default_alias blah | strip_colors)"
EXPECTED_OUTPUT='blah -> "local-blah" (-> v"local-blah") (default)'
EXPECTED_OUTPUT='blah -> "local-blah" (-> v"local-blah" *) (default)'
[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "'nvm_print_default_alias blah' should strip alias dir and print nvm_print_implicit_alias output; got '$OUTPUT', expected '$EXPECTED_OUTPUT'"
cleanup