[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.
This commit is contained in:
6 files changed
+43
-16
No files matched your search
@@ -6,6 +6,11 @@ set -ex
|
||||
\. ../../nvm.sh
|
||||
\. ../common.sh
|
||||
|
||||
if [ "$(command id -u)" = '0' ]; then
|
||||
echo 'running as root, which may uninstall any version; skipping'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
make_fake_node v0.0.1
|
||||
sudo touch ""$(nvm_version_path v0.0.1)"/sudo"
|
||||
|
||||
|
||||
@@ -8,15 +8,18 @@ die () { echo "$@" ; exit 1; }
|
||||
\. ../../common.sh
|
||||
|
||||
# Test that mkdir failure propagates from brace group (not subshell)
|
||||
# a path under a regular file can not be created, even by root
|
||||
NOT_A_DIR="$(mktemp)"
|
||||
nvm_download_artifact_mkdir_test() {
|
||||
local tmpdir
|
||||
tmpdir="/nonexistent/path/that/cannot/exist"
|
||||
tmpdir="${NOT_A_DIR}/path"
|
||||
command mkdir -p "${tmpdir}/files" || {
|
||||
return 3
|
||||
}
|
||||
}
|
||||
nvm_download_artifact_mkdir_test 2>/dev/null
|
||||
EXIT_CODE=$?
|
||||
command rm -f "${NOT_A_DIR}"
|
||||
[ "${EXIT_CODE}" = "3" ] || die "Expected mkdir failure to propagate with exit code 3, got ${EXIT_CODE}"
|
||||
|
||||
# Test that download failure propagates with exit code 4
|
||||
|
||||
@@ -54,7 +54,15 @@ setup_chroot() {
|
||||
cp /lib64/ld-linux-x86-64.so.2 "${chroot_dir}/lib64/"
|
||||
|
||||
# /dev/null
|
||||
sudo mknod "${chroot_dir}/dev/null" c 1 3
|
||||
as_root mknod "${chroot_dir}/dev/null" c 1 3
|
||||
}
|
||||
|
||||
as_root() {
|
||||
if [ "$(command id -u)" = '0' ]; then
|
||||
"$@"
|
||||
else
|
||||
sudo "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
# The chroot fixtures assume a glibc layout (fixed dynamic-linker path under
|
||||
@@ -65,15 +73,17 @@ if [ -f "/etc/alpine-release" ]; then
|
||||
echo "on Alpine; skipping chroot arch checks (covered by 'nvm_get_arch alpine')"
|
||||
elif [ "$(command uname -s)" != 'Linux' ] || [ "$(command uname -m)" != 'x86_64' ] || [ ! -e /lib64/ld-linux-x86-64.so.2 ]; then
|
||||
echo "not on x86_64 glibc Linux; skipping chroot arch checks"
|
||||
elif [ "$(command id -u)" != '0' ] && ! command -v sudo >/dev/null 2>&1; then
|
||||
echo "not root and no sudo, so chroot is unavailable; skipping chroot arch checks"
|
||||
else
|
||||
setup_chroot "${CHROOT_WITH_ALPINE}"
|
||||
setup_chroot "${CHROOT_WITHOUT_ALPINE}"
|
||||
|
||||
# Run tests in chroot environments
|
||||
ARCH_WITH_ALPINE=$(sudo chroot "${CHROOT_WITH_ALPINE}" /bin/sh -c "export NVM_DIR=/ && . ./nvm.sh && nvm_get_arch")
|
||||
ARCH_WITH_ALPINE=$(as_root chroot "${CHROOT_WITH_ALPINE}" /bin/sh -c "export NVM_DIR=/ && . ./nvm.sh && nvm_get_arch")
|
||||
[ "${ARCH_WITH_ALPINE}" = "x64-musl" ] || die "Expected x64-musl for alpine environment but got ${ARCH_WITH_ALPINE}"
|
||||
|
||||
ARCH_WITHOUT_ALPINE=$(sudo chroot "${CHROOT_WITHOUT_ALPINE}" /bin/sh -c "export NVM_DIR=/ && . ./nvm.sh && nvm_get_arch")
|
||||
ARCH_WITHOUT_ALPINE=$(as_root chroot "${CHROOT_WITHOUT_ALPINE}" /bin/sh -c "export NVM_DIR=/ && . ./nvm.sh && nvm_get_arch")
|
||||
[ "${ARCH_WITHOUT_ALPINE}" != "x64-musl" ] || die "Did not expect x64-musl for non-alpine environment"
|
||||
fi
|
||||
|
||||
|
||||
@@ -11,8 +11,10 @@ cleanup() {
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
[ -d ../../../test/fixtures/nvmrc/test/fixtures/valid ] || die 'the `test/fixtures/nvmrc` submodule is not checked out; run `git submodule update --init`'
|
||||
|
||||
for f in ../../../test/fixtures/nvmrc/test/fixtures/valid/*; do
|
||||
try nvm_process_nvmrc $f/.nvmrc
|
||||
try nvm_process_nvmrc "${f}/.nvmrc"
|
||||
|
||||
EXPECTED="$(nvm_json_extract node < "${f}/expected.json" | tr -d '"')"
|
||||
|
||||
@@ -22,8 +24,8 @@ for f in ../../../test/fixtures/nvmrc/test/fixtures/valid/*; do
|
||||
done
|
||||
|
||||
for f in ../../../test/fixtures/nvmrc/test/fixtures/invalid/*; do
|
||||
try nvm_process_nvmrc $f/.nvmrc
|
||||
try_err nvm_process_nvmrc $f/.nvmrc
|
||||
try nvm_process_nvmrc "${f}/.nvmrc"
|
||||
try_err nvm_process_nvmrc "${f}/.nvmrc"
|
||||
STDERR="$(echo "$CAPTURED_STDERR" | awk '{if(NR > 8) print $0}' | strip_colors)"
|
||||
|
||||
EXPECTED="$(nvm_json_extract < "${f}/expected.json" | tr -d '"')"
|
||||
|
||||
@@ -26,8 +26,13 @@ fi
|
||||
[ "$(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
|
||||
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'
|
||||
# (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
|
||||
@@ -66,12 +66,14 @@ del_alias
|
||||
nvm alias test "$TEST_VERSION"
|
||||
test_version test || die
|
||||
|
||||
# 3. fails with invalid permissions
|
||||
del_nvmrc
|
||||
touch .nvmrc
|
||||
chmod 0 .nvmrc
|
||||
nvm_write_nvmrc $TEST_VERSION 2>/dev/null && die "\`nvm_write_nvmrc $TEST_VERSION\` did not fail with invalid permissions"
|
||||
del_nvmrc
|
||||
# 3. fails with invalid permissions (root ignores file permissions, so this can only be checked as another user)
|
||||
if [ "$(command id -u)" != '0' ]; then
|
||||
del_nvmrc
|
||||
touch .nvmrc
|
||||
chmod 0 .nvmrc
|
||||
nvm_write_nvmrc $TEST_VERSION 2>/dev/null && die "\`nvm_write_nvmrc $TEST_VERSION\` did not fail with invalid permissions"
|
||||
del_nvmrc
|
||||
fi
|
||||
|
||||
# 4. respects NVM_SILENT=1
|
||||
export NVM_SILENT=1
|
||||
|
||||
Reference in new issue
Block a user