[Fix] `nvm --help`: render the set-colors legend in color
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Every legend line is built inside a command substitution, where `nvm_has_colors` can never succeed, because `[ -t 1 ]` sees the capture pipe rather than the terminal. The legend has therefore always printed plain, on every terminal and in every shell. Resolve color support once and pass it in, the way `nvm_print_alias_path` already accepts the same flag. A command-prefix assignment cannot carry it: POSIX expands a command's arguments before applying its assignments, so the substitutions would still see it unset, and in sh and ksh it would outlive the call and override `--no-colors` for everything after. The test needs no tty, and fails against an unfixed `nvm.sh` in sh, bash, dash, and zsh. Fixes #3896
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+113 added−5 removed
- tests:+98 added−1 removed
- other:+15 added−4 removed
Provenance
- Commit
3a72926- Tree
adb3d48- Author
CJstate <CJstate@users.noreply.github.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
38f0f2c
2 files changed+113 added−5 removed
modified:test/fast/Unit tests/nvm_print_color_legend+98 added-1 removed
1 #!/bin/sh2 3+ONESHOT=''4+cleanup () {5+ if [ -n "${ONESHOT}" ]; then6+ command rm -f "${ONESHOT}"7+ fi8+}3-die () { echo "$@" ; exit 1; }9+die () { echo "$@" ; cleanup ; exit 1; }10 11 \. ../../../nvm.sh12 13 set -e14 15+# assert about nvm, not about the caller's environment16+NVM_HAS_COLORS_WAS="${NVM_HAS_COLORS-}"17+unset NVM_HAS_COLORS18+19 ESC="$(printf '\033')"20 # The legend's letters are wrapped in color codes when the terminal supports21 # them, so every text assertion below compares against the output with any22 # escapes removed. That keeps these checks about structure and alignment only.63 unmodified lines86 HELP_PLAIN="$(nvm --help | strip_colors)"87 contains_line ' Color codes:' "${HELP_PLAIN}" \88 || die 'nvm --help no longer includes the color legend'89 90+# --- the fix: colors have to survive command substitution ------------------91+92+# the bug condition: inside `$( )`, `[ -t 1 ]` sees the capture pipe, so93+# `nvm_has_colors` always fails94+nvm_has_colors () { return 1; }95+96+WRAPPED="$(nvm_wrap_with_color_code 'b' 'b')"97+case "${WRAPPED}" in98+ *"${ESC}"*) die "expected plain text when colors are unsupported, got >${WRAPPED}<" ;;99+esac100+101+NVM_HAS_COLORS=1102+export NVM_HAS_COLORS103+WRAPPED="$(nvm_wrap_with_color_code 'b' 'b')"104+[ "${WRAPPED}" = "${ESC}[0;34mb${ESC}[0m" ] \105+ || die "expected a colored 'b' with NVM_HAS_COLORS=1, got >${WRAPPED}<"106+107+WRAPPED="$(nvm_wrap_with_color_code 'X' 'X')"108+[ "${WRAPPED}" = 'X' ] \109+ || die "expected plain text for an unknown color code, got >${WRAPPED}<"110+unset NVM_HAS_COLORS111+112+# `0` is what the resolving callers start from, so it must not read as truthy113+NVM_HAS_COLORS=0114+export NVM_HAS_COLORS115+WRAPPED="$(nvm_wrap_with_color_code 'b' 'b')"116+case "${WRAPPED}" in117+ *"${ESC}"*) die "NVM_HAS_COLORS=0 must not enable colors, got >${WRAPPED}<" ;;118+esac119+unset NVM_HAS_COLORS120+121+UNSUPPORTED="$(nvm_print_color_legend)"122+case "${UNSUPPORTED}" in123+ *"${ESC}"*) die "the legend emitted colors while unsupported: ${UNSUPPORTED}" ;;124+esac125+126+# Model the real asymmetry: yes to the first, direct call, no to every later127+# one. The state has to live in a file, because each `$( )` is a fresh subshell128+# that would take its own copy of an in-shell counter and answer yes every time.129+ONESHOT="$(command mktemp)"130+oneshot_reset () { nvm_echo 0 > "${ONESHOT}"; }131+nvm_has_colors () {132+ if [ "$(command cat "${ONESHOT}")" = 0 ]; then133+ nvm_echo 1 > "${ONESHOT}"134+ return 0135+ fi136+ return 1137+}138+139+oneshot_reset140+COLORED_OUTPUT="$(nvm_print_color_legend)"141+# `grep -c` drains its input, so it cannot SIGPIPE its producer142+COLORED="$(nvm_echo "${COLORED_OUTPUT}" | nvm_grep -c "${ESC}" || true)"143+[ "${COLORED}" = 9 ] \144+ || die "expected all 9 legend lines colored, got ${COLORED}: ${COLORED_OUTPUT}"145+146+# the sample specifically, which is the case #3896 reports147+case "${COLORED_OUTPUT}" in148+ *"${ESC}[0;34mb${ESC}[0m${ESC}[0;33my${ESC}[0m"*) : ;;149+ *) die "the initial-colors sample was not colored: ${COLORED_OUTPUT}" ;;150+esac151+152+# in a substitution, so `nvm_set_colors`' `export NVM_COLORS` stays contained153+oneshot_reset154+SET_COLORS_OUTPUT="$(nvm_set_colors 'bygre')"155+case "${SET_COLORS_OUTPUT}" in156+ *"${ESC}"*) : ;;157+ *) die "nvm_set_colors did not color its confirmation line: ${SET_COLORS_OUTPUT}" ;;158+esac159+160+# coloring must change only the escapes, never the text or the alignment161+COLORED_STRIPPED="$(nvm_echo "${COLORED_OUTPUT}" | strip_colors)"162+[ "${COLORED_STRIPPED}" = "${PLAIN}" ] \163+ || die 'the colored legend does not match the plain legend once escapes are stripped'164+165+# called directly rather than in a substitution, so a leak is observable at all166+oneshot_reset167+nvm_print_color_legend >/dev/null168+[ -z "${NVM_HAS_COLORS-}" ] \169+ || die "nvm_print_color_legend leaked NVM_HAS_COLORS=${NVM_HAS_COLORS}"170+171 # And `nvm unload` has to clean it up, the way it does its sibling helpers.172 # `set +e` inside the subshell so a non-zero `nvm unload` cannot abort it before173 # the lookup runs; its status is reported separately, so "unload failed" can174 # never be misreported as "the function survived".12 unmodified lines187 *gone*) : ;;188 *) die "nvm unload did not remove nvm_print_color_legend (${UNLOAD_OUT})" ;;189 esac190 191+if [ -n "${NVM_HAS_COLORS_WAS}" ]; then192+ NVM_HAS_COLORS="${NVM_HAS_COLORS_WAS}"193+ export NVM_HAS_COLORS194+fi195+196+cleanup197 echo "nvm_print_color_legend: passed"modified:nvm.sh+15 added-4 removed
1127 unmodified lines1128 local LTS_AND_SYSTEM_COLOR1129 local CURRENT_COLOR1130 local NOT_INSTALLED_COLOR1131 local DEFAULT_COLOR1132+ local NVM_HAS_COLORS1133+ NVM_HAS_COLORS=01134 1135 INSTALLED_COLOR="$(echo "$1" | awk '{ print substr($0, 1, 1); }')"1136 LTS_AND_SYSTEM_COLOR="$(echo "$1" | awk '{ print substr($0, 2, 1); }')"1137 CURRENT_COLOR="$(echo "$1" | awk '{ print substr($0, 3, 1); }')"2 unmodified lines1140 if ! nvm_has_colors; then1141 nvm_echo "Setting colors to: ${INSTALLED_COLOR} ${LTS_AND_SYSTEM_COLOR} ${CURRENT_COLOR} ${NOT_INSTALLED_COLOR} ${DEFAULT_COLOR}"1142 nvm_echo "WARNING: Colors may not display because they are not supported in this shell."1143 else1144+ NVM_HAS_COLORS=11145 nvm_echo_with_colors "Setting colors to: $(nvm_wrap_with_color_code "${INSTALLED_COLOR}" "${INSTALLED_COLOR}")$(nvm_wrap_with_color_code "${LTS_AND_SYSTEM_COLOR}" "${LTS_AND_SYSTEM_COLOR}")$(nvm_wrap_with_color_code "${CURRENT_COLOR}" "${CURRENT_COLOR}")$(nvm_wrap_with_color_code "${NOT_INSTALLED_COLOR}" "${NOT_INSTALLED_COLOR}")$(nvm_wrap_with_color_code "${DEFAULT_COLOR}" "${DEFAULT_COLOR}")"1146 fi1147 export NVM_COLORS="$1"1148 else29 unmodified lines1178 local CODE1179 CODE="$(nvm_print_color_code "${1}" 2>/dev/null ||:)"1180 local TEXT1181 TEXT="${2-}"1179- if nvm_has_colors && [ -n "${CODE}" ]; then1182+ # `nvm_has_colors` cannot answer for itself inside a command substitution,1183+ # where `[ -t 1 ]` sees the capture pipe: callers that already checked pass1184+ # the answer in, as `nvm_print_alias_path` also accepts it.1185+ if { [ "${NVM_HAS_COLORS-}" = 1 ] || nvm_has_colors; } && [ -n "${CODE}" ]; then1186 nvm_echo_with_colors "\033[${CODE}${TEXT}\033[0m"1187 else1188 nvm_echo "${TEXT}"1189 fi1190 }1191 1192 nvm_print_color_legend() {1187- # Every line is built in a command substitution, so this cannot stay1188- # inline: resolving color support has to happen in one scope that all of1189- # them share. Pure code motion for now; see the following commit.1193+ # Every line below builds its text in a command substitution, which cannot1194+ # detect color support itself, so resolve it once here. `local` matters:1195+ # leaking the flag would override `--no-colors` for every later call.1196+ local NVM_HAS_COLORS1197+ NVM_HAS_COLORS=01198+ if nvm_has_colors; then1199+ NVM_HAS_COLORS=11200+ fi1201 nvm_echo ' Initial colors are:'1202 nvm_echo_with_colors " $(nvm_wrap_with_color_code 'b' 'b')$(nvm_wrap_with_color_code 'y' 'y')$(nvm_wrap_with_color_code 'g' 'g')$(nvm_wrap_with_color_code 'r' 'r')$(nvm_wrap_with_color_code 'e' 'e')"1203 nvm_echo ' Color codes:'1204 nvm_echo_with_colors " $(nvm_wrap_with_color_code 'r' 'r')/$(nvm_wrap_with_color_code 'R' 'R') = $(nvm_wrap_with_color_code 'r' 'red') / $(nvm_wrap_with_color_code 'R' 'bold red')"3945 unmodified lines