Skip to content
Myr

[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

authored by CJstate · committed by Jordan Harband · parent 38f0f2c · Browse files · 2 files+113 added−5 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+113 added−5 removed

  • tests:+98 added−1 removed
  • other:+15 added−4 removed

2 files+113 added−5 removed

modified:test/fast/Unit tests/nvm_print_color_legend+98 added-1 removed
1#!/bin/sh23ONESHOT=''4cleanup () {5 if [ -n "${ONESHOT}" ]; then6 command rm -f "${ONESHOT}"7 fi8}3die () { echo "$@" ; exit 1; }9die () { echo "$@" ; cleanup ; exit 1; }1011\. ../../../nvm.sh1213set -e1415# assert about nvm, not about the caller's environment16NVM_HAS_COLORS_WAS="${NVM_HAS_COLORS-}"17unset NVM_HAS_COLORS1819ESC="$(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 lines
86HELP_PLAIN="$(nvm --help | strip_colors)"87contains_line ' Color codes:' "${HELP_PLAIN}" \88 || die 'nvm --help no longer includes the color legend'8990# --- the fix: colors have to survive command substitution ------------------9192# the bug condition: inside `$( )`, `[ -t 1 ]` sees the capture pipe, so93# `nvm_has_colors` always fails94nvm_has_colors () { return 1; }9596WRAPPED="$(nvm_wrap_with_color_code 'b' 'b')"97case "${WRAPPED}" in98 *"${ESC}"*) die "expected plain text when colors are unsupported, got >${WRAPPED}<" ;;99esac100101NVM_HAS_COLORS=1102export NVM_HAS_COLORS103WRAPPED="$(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}<"106107WRAPPED="$(nvm_wrap_with_color_code 'X' 'X')"108[ "${WRAPPED}" = 'X' ] \109 || die "expected plain text for an unknown color code, got >${WRAPPED}<"110unset NVM_HAS_COLORS111112# `0` is what the resolving callers start from, so it must not read as truthy113NVM_HAS_COLORS=0114export NVM_HAS_COLORS115WRAPPED="$(nvm_wrap_with_color_code 'b' 'b')"116case "${WRAPPED}" in117 *"${ESC}"*) die "NVM_HAS_COLORS=0 must not enable colors, got >${WRAPPED}<" ;;118esac119unset NVM_HAS_COLORS120121UNSUPPORTED="$(nvm_print_color_legend)"122case "${UNSUPPORTED}" in123 *"${ESC}"*) die "the legend emitted colors while unsupported: ${UNSUPPORTED}" ;;124esac125126# 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.129ONESHOT="$(command mktemp)"130oneshot_reset () { nvm_echo 0 > "${ONESHOT}"; }131nvm_has_colors () {132 if [ "$(command cat "${ONESHOT}")" = 0 ]; then133 nvm_echo 1 > "${ONESHOT}"134 return 0135 fi136 return 1137}138139oneshot_reset140COLORED_OUTPUT="$(nvm_print_color_legend)"141# `grep -c` drains its input, so it cannot SIGPIPE its producer142COLORED="$(nvm_echo "${COLORED_OUTPUT}" | nvm_grep -c "${ESC}" || true)"143[ "${COLORED}" = 9 ] \144 || die "expected all 9 legend lines colored, got ${COLORED}: ${COLORED_OUTPUT}"145146# the sample specifically, which is the case #3896 reports147case "${COLORED_OUTPUT}" in148 *"${ESC}[0;34mb${ESC}[0m${ESC}[0;33my${ESC}[0m"*) : ;;149 *) die "the initial-colors sample was not colored: ${COLORED_OUTPUT}" ;;150esac151152# in a substitution, so `nvm_set_colors`' `export NVM_COLORS` stays contained153oneshot_reset154SET_COLORS_OUTPUT="$(nvm_set_colors 'bygre')"155case "${SET_COLORS_OUTPUT}" in156 *"${ESC}"*) : ;;157 *) die "nvm_set_colors did not color its confirmation line: ${SET_COLORS_OUTPUT}" ;;158esac159160# coloring must change only the escapes, never the text or the alignment161COLORED_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'164165# called directly rather than in a substitution, so a leak is observable at all166oneshot_reset167nvm_print_color_legend >/dev/null168[ -z "${NVM_HAS_COLORS-}" ] \169 || die "nvm_print_color_legend leaked NVM_HAS_COLORS=${NVM_HAS_COLORS}"170171# 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 lines
187 *gone*) : ;;188 *) die "nvm unload did not remove nvm_print_color_legend (${UNLOAD_OUT})" ;;189esac190191if [ -n "${NVM_HAS_COLORS_WAS}" ]; then192 NVM_HAS_COLORS="${NVM_HAS_COLORS_WAS}"193 export NVM_HAS_COLORS194fi195196cleanup197echo "nvm_print_color_legend: passed"
modified:nvm.sh+15 added-4 removed
1127 unmodified lines
1128 local LTS_AND_SYSTEM_COLOR1129 local CURRENT_COLOR1130 local NOT_INSTALLED_COLOR1131 local DEFAULT_COLOR1132 local NVM_HAS_COLORS1133 NVM_HAS_COLORS=011341135 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 lines
1140 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 else
29 unmodified lines
1178 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}11911192nvm_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