[Fix] nvm alias: fix colors not showing by default

Colors were lost because `nvm_has_colors` checks `[ -t 1 ]`, which is false inside the `(...) | sort` pipeline in `nvm_list_aliases`.
Evaluate `nvm_has_colors` before the pipe and propagate via `NVM_HAS_COLORS`, matching the approach used by `nvm_print_versions`.

Bug introduced in 35212c1346.
This commit is contained in:
Jordan Harband
2026-03-13 16:13:41 -04:00
parent 59bd32be6b
commit a27a8b7da8
3 changed files with 69 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
#!/bin/sh
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
die () { echo "$@" ; exit 1; }
set -e
# Force nvm_has_colors to return true so nvm_list_aliases sets NVM_HAS_COLORS=1
nvm_has_colors() { return 0; }
nvm_alias_path() {
nvm_echo "../../../alias"
}
# nvm_list_aliases pipes through `sort`, which makes [ -t 1 ] false.
# Before the fix in 2eb8bbd0, colors were lost inside the pipeline.
# With the fix, NVM_HAS_COLORS is evaluated before the pipe and propagated.
OUTPUT=$(nvm_list_aliases test-stable-1)
COLORED_ARROW="$(command printf %b '\033[0;90m->\033[0m')"
case "${OUTPUT}" in
*"${COLORED_ARROW}"*) : ;; # pass - colored arrow survived the pipeline
*) die "Expected colored arrow in nvm_list_aliases output through pipeline, got >${OUTPUT}<" ;;
esac

View File

@@ -0,0 +1,32 @@
#!/bin/sh
: nvm.sh
\. ../../../nvm.sh
die () {
echo "$@"
exit 1
}
set -e
# Override nvm_has_colors to always return false, simulating a pipeline context
# where [ -t 1 ] is false (the bug condition from 2eb8bbd0)
nvm_has_colors() { return 1; }
# Without NVM_HAS_COLORS, output should have no color codes (plain arrow)
OUTPUT="$(nvm_print_formatted_alias fakealias fakedest)"
case "${OUTPUT}" in
*'\033['*) die "Expected no color codes without NVM_HAS_COLORS, got >${OUTPUT}<" ;;
esac
# With NVM_HAS_COLORS=1, output should contain color codes even though
# nvm_has_colors returns false (regression test for the pipeline fix)
NVM_HAS_COLORS=1
export NVM_HAS_COLORS
OUTPUT="$(nvm_print_formatted_alias fakealias fakedest)"
ARROW="$(command printf %b '\033[0;90m->\033[0m')"
case "${OUTPUT}" in
*"${ARROW}"*) : ;; # pass - colored arrow present
*) die "Expected colored arrow with NVM_HAS_COLORS=1, got >${OUTPUT}<" ;;
esac