From e1927f50968c074338fdb23463d7282a0d448409 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 4 Nov 2026 14:12:38 -0600 Subject: [PATCH] [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. --- ... with incorrect file permissions fails nicely | 5 +++++ .../nvm_download_artifact error propagation | 5 ++++- test/fast/Unit tests/nvm_get_arch_unofficial | 16 +++++++++++++--- test/fast/Unit tests/nvm_process_nvmrc | 8 +++++--- test/fast/Unit tests/nvm_stdout_is_terminal | 11 ++++++++--- test/fast/Unit tests/nvm_write_nvmrc | 14 ++++++++------ 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/test/fast/Running 'nvm uninstall' with incorrect file permissions fails nicely b/test/fast/Running 'nvm uninstall' with incorrect file permissions fails nicely index 95d89f98..6a6029dc 100755 --- a/test/fast/Running 'nvm uninstall' with incorrect file permissions fails nicely +++ b/test/fast/Running 'nvm uninstall' with incorrect file permissions fails nicely @@ -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" diff --git a/test/fast/Unit tests/nvm_download_artifact error propagation b/test/fast/Unit tests/nvm_download_artifact error propagation index e220ccd9..cc12f7ea 100755 --- a/test/fast/Unit tests/nvm_download_artifact error propagation +++ b/test/fast/Unit tests/nvm_download_artifact error propagation @@ -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 diff --git a/test/fast/Unit tests/nvm_get_arch_unofficial b/test/fast/Unit tests/nvm_get_arch_unofficial index af1557b5..f9d4d9f1 100755 --- a/test/fast/Unit tests/nvm_get_arch_unofficial +++ b/test/fast/Unit tests/nvm_get_arch_unofficial @@ -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 diff --git a/test/fast/Unit tests/nvm_process_nvmrc b/test/fast/Unit tests/nvm_process_nvmrc index 701477ef..d3e352ed 100755 --- a/test/fast/Unit tests/nvm_process_nvmrc +++ b/test/fast/Unit tests/nvm_process_nvmrc @@ -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 '"')" diff --git a/test/fast/Unit tests/nvm_stdout_is_terminal b/test/fast/Unit tests/nvm_stdout_is_terminal index 69d56490..2e0fcfe4 100755 --- a/test/fast/Unit tests/nvm_stdout_is_terminal +++ b/test/fast/Unit tests/nvm_stdout_is_terminal @@ -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/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_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