Files
nvm/test/fast/Unit tests/nvm_stdout_is_terminal
T
Jordan Harband e1927f5096 [Tests] make fast tests pass as root, without sudo, and without a terminal
Six fast tests failed outside of CI's setup,
which runs them as a non-root user with `sudo` and a terminal.
`nvm_download_artifact error propagation` expected `mkdir -p` under `/nonexistent` to fail,
which root can create; it now uses a path under a regular file, which nobody can create.
`nvm_get_arch_unofficial` now only uses `sudo` when it is not already root,
and skips the chroot checks when it can do neither.
`nvm_write_nvmrc`'s permissions check and the `nvm uninstall` permissions test are skipped as root,
since root ignores file permissions.
`nvm_stdout_is_terminal`'s `/dev/tty` checks are skipped when there is no controlling terminal,
as in `docker run` without `-t`.
`nvm_process_nvmrc` now fails with a clear message when the `test/fixtures/nvmrc` submodule is not checked out,
instead of on an unexpanded glob.
This is not a vulnerability fix.
2026-11-04 14:12:38 -06:00

39 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
tempfile=$(mktemp)
die () { echo "$@" ; cleanup; exit 1; }
cleanup() { rm -f "$tempfile"; }
: nvm.sh
\. ../../../nvm.sh
if [ -t 1 ] ; then
# test is running in a terminal, e.g. locally
nvm_stdout_is_terminal || die 'nvm_stdout_is_terminal should be true'
nvm_stdout_is_terminal 2>/dev/null || die 'nvm_stdout_is_terminal should be true when stderr is redirected'
nvm_stdout_is_terminal </dev/null || die 'nvm_stdout_is_terminal should be true when stdin is redirected'
else
# test is not running in a terminal, e.g. on travis-ci
! nvm_stdout_is_terminal || die 'nvm_stdout_is_terminal should be false'
! nvm_stdout_is_terminal 2>/dev/null || die 'nvm_stdout_is_terminal should be false when stderr is redirected'
! nvm_stdout_is_terminal </dev/null || die 'nvm_stdout_is_terminal should be false when stdin is redirected'
fi
(! nvm_stdout_is_terminal || echo "boo!") | read && die 'nvm_stdout_is_terminal should be false when stdout goes to a pipe'
! nvm_stdout_is_terminal >/dev/null || die 'nvm_stdout_is_terminal should be false when stdout goes to /dev/null'
! nvm_stdout_is_terminal >"$tempfile" || die 'nvm_stdout_is_terminal should be false when stdout goes to a file'
[ "$(nvm_stdout_is_terminal; echo $?)" = "1" ] || die 'nvm_stdout_is_terminal should be false in command substitution'
# also test the 'true' case while running on CI or similar environments
# (a process without a controlling terminal, eg in `docker run` without `-t`, can not open /dev/tty)
if (: >/dev/tty) 2>/dev/null; then
nvm_stdout_is_terminal >/dev/tty || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty'
nvm_stdout_is_terminal >/dev/tty 2>/dev/null || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty and stderr is redirected'
nvm_stdout_is_terminal >/dev/tty </dev/null || die 'nvm_stdout_is_terminal should be true when stdout goes to /dev/tty and stdin is redirected'
else
echo 'no controlling terminal; skipping /dev/tty checks'
fi
cleanup