From a5eef5568ef299e309aadc96ecdb597662b612c9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 30 Nov 2026 18:07:20 -0800 Subject: [PATCH] [Tests] `nvm_echo_with_colors`: add unit tests --- test/fast/Unit tests/nvm_echo_with_colors | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 test/fast/Unit tests/nvm_echo_with_colors diff --git a/test/fast/Unit tests/nvm_echo_with_colors b/test/fast/Unit tests/nvm_echo_with_colors new file mode 100755 index 00000000..6c5046e5 --- /dev/null +++ b/test/fast/Unit tests/nvm_echo_with_colors @@ -0,0 +1,68 @@ +#!/bin/sh + +cleanup () { + unset -f printf 2>/dev/null + case "${TEST_DIR-}" in + /*) rm -rf "${TEST_DIR}" ;; + esac +} + +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +TEST_DIR="$(mktemp -d)" || die 'unable to create temp dir' + +# the trailing `x` keeps command substitution from stripping the newline +OUTPUT="$(nvm_echo_with_colors 'plain text'; echo x)" +EXPECTED="$(printf 'plain text\nx')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "plain text: got >${OUTPUT}<, expected >${EXPECTED}<" + +OUTPUT="$(nvm_echo_with_colors; echo x)" +EXPECTED="$(printf '\nx')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "no args: got >${OUTPUT}<, expected >${EXPECTED}<" + +OUTPUT="$(nvm_echo_with_colors ''; echo x)" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "empty arg: got >${OUTPUT}<, expected >${EXPECTED}<" + +OUTPUT="$(nvm_echo_with_colors 'one' 'two spaced' 'three'; echo x)" +EXPECTED="$(printf 'one two spaced three\nx')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "multiple args: got >${OUTPUT}<, expected >${EXPECTED}<" + +OUTPUT="$(nvm_echo_with_colors '\033[0;32mgreen\033[0m'; echo x)" +EXPECTED="$(printf '\033[0;32mgreen\033[0m\nx')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "color codes: got >${OUTPUT}<, expected >${EXPECTED}<" + +OUTPUT="$(nvm_echo_with_colors 'a\tb' 'c\\d' 'e\nf'; echo x)" +EXPECTED="$(printf 'a\tb c\\d e\nf\nx')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "escapes: got >${OUTPUT}<, expected >${EXPECTED}<" + +OUTPUT="$(nvm_echo_with_colors '100%' '%s %d'; echo x)" +EXPECTED="$(printf '%s\nx' '100% %s %d')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "format chars: got >${OUTPUT}<, expected >${EXPECTED}<" + +nvm_echo_with_colors 'status' > /dev/null || die "expected exit status 0, got $?" + +# a failing write must not leak printf's error message +ERR="$( { (nvm_echo_with_colors 'closed' >&-) ; } 2>&1 >/dev/null)" +[ "_${ERR}" = "_" ] || die "closed stdout: got stderr >${ERR}<, expected ><" + +# a `printf` on PATH must not be used +MOCK_BIN="${TEST_DIR}/bin" +mkdir "${MOCK_BIN}" || die 'unable to create mock bin dir' +{ + echo '#!/bin/sh' + echo 'echo MOCKED-PRINTF' +} > "${MOCK_BIN}/printf" +chmod +x "${MOCK_BIN}/printf" +OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_echo_with_colors 'builtin' 2>&1)" +[ "_${OUTPUT}" = "_builtin" ] || die "PATH printf: got >${OUTPUT}<, expected >builtin<" + +# a user-defined `printf` function must not be used +printf () { echo MOCKED-FUNCTION; } +OUTPUT="$(nvm_echo_with_colors 'function')" +unset -f printf +[ "_${OUTPUT}" = "_function" ] || die "printf function: got >${OUTPUT}<, expected >function<" + +cleanup