mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-12-06 22:52:23 +03:00
[Tests] nvm_echo_with_colors: add unit tests
This commit is contained in:
1 file changed
+68
Executable
+68
@@ -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
|
||||
Reference in new issue
Block a user