mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-10-11 00:00:13 +08:00
[Perf] nvm_echo: use the printf shell builtin under zsh
In zsh, `command printf` skips the builtin and forks `/usr/bin/printf`.
This commit is contained in:
@@ -22,11 +22,22 @@ nvm_stdout_is_terminal() {
|
||||
}
|
||||
|
||||
nvm_echo() {
|
||||
command printf %s\\n "$*" 2>/dev/null
|
||||
# in zsh, `command printf` skips the builtin and runs `/usr/bin/printf`
|
||||
if [ -n "${ZSH_VERSION-}" ]; then
|
||||
# shellcheck disable=SC3044
|
||||
builtin printf %s\\n "$*" 2>/dev/null
|
||||
else
|
||||
command printf %s\\n "$*" 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
nvm_echo_with_colors() {
|
||||
command printf %b\\n "$*" 2>/dev/null
|
||||
if [ -n "${ZSH_VERSION-}" ]; then
|
||||
# shellcheck disable=SC3044
|
||||
builtin printf %b\\n "$*" 2>/dev/null
|
||||
else
|
||||
command printf %b\\n "$*" 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
nvm_cd() {
|
||||
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
rm -rf "${TEST_DIR}"
|
||||
}
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TEST_DIR="$(pwd)/nvm_echo_tmp"
|
||||
[ ! -e "${TEST_DIR}" ] && mkdir "${TEST_DIR}"
|
||||
|
||||
# a `printf` that reports that it ran: `nvm_echo` must use a shell builtin
|
||||
# rather than resolving `printf` through `PATH` (zsh's `command` skips the
|
||||
# builtin and runs `/usr/bin/printf` on every call)
|
||||
MOCK_BIN="${TEST_DIR}/bin"
|
||||
mkdir -p "${MOCK_BIN}"
|
||||
{
|
||||
echo '#!/bin/sh'
|
||||
echo "echo 'MOCKED-PRINTF'"
|
||||
} > "${MOCK_BIN}/printf"
|
||||
chmod +x "${MOCK_BIN}/printf"
|
||||
|
||||
OUTPUT="$(nvm_echo 'hello world')"
|
||||
[ "${OUTPUT}" = 'hello world' ] || die "expected 'hello world', got '${OUTPUT}'"
|
||||
|
||||
OUTPUT="$(nvm_echo)"
|
||||
[ -z "${OUTPUT}" ] || die "expected no output, got '${OUTPUT}'"
|
||||
|
||||
OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_echo 'no fork, please' 2>&1)"
|
||||
[ "${OUTPUT}" = 'no fork, please' ] || die "for nvm_echo, expected 'no fork, please', got '${OUTPUT}'"
|
||||
|
||||
OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_echo_with_colors 'no fork, please' 2>&1)"
|
||||
[ "${OUTPUT}" = 'no fork, please' ] || die "for nvm_echo_with_colors, expected 'no fork, please', got '${OUTPUT}'"
|
||||
|
||||
cleanup
|
||||
Reference in New Issue
Block a user