[Perf] `nvm_get_os`: read `/etc/os-release` instead of forking `uname -a`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
The file is read, not sourced. An `ID` with no OS of its own is only taken as Linux when the kernel's `ostype` says so; otherwise `uname -a` decides.
authored by Cassidy Marble · committed by Jordan Harband · parent c28f2b7 · Browse files · 4 files·+224 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+224 added−11 removed
- tests:+157 added−0 removed
- other:+67 added−11 removed
Provenance
- Commit
1dd1d8c- Tree
b99076a- Author
Cassidy Marble <cassidy@marble.sh>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
c28f2b7
4 files changed+224 added−11 removed
modified:test/fast/Unit tests/nvm_get_arch+4 added
19 unmodified lines20 # Sets the PATH for these tests to include the symlinks to the mocked21 # binaries22 export PATH=".:${PATH}"23 24+# `nvm_get_os` reads `/etc/os-release` where it is available; this test drives25+# the OS through the mocked `uname`, so point it at a file that does not exist26+export NVM_OS_RELEASE="${MOCKS_DIR}/nonexistent-os-release"27+28 # Setups mock binaries for a given OS and arch that mimic29 # the output of the real binaries used by nvm_get_arch to guess30 # the architecture of a given system.31 setup_mock_arch() {76 unmodified linesmodified:test/fast/Unit tests/nvm_get_arch alpine+4 added
13 unmodified lines14 15 MOCKS_DIR="$(pwd)/../../mocks"16 export PATH=".:${PATH}"17 18+# `nvm_get_os` reads `/etc/os-release` where it is available; this test drives19+# the OS through the mocked `uname`, so point it at a file that does not exist20+export NVM_OS_RELEASE="${MOCKS_DIR}/nonexistent-os-release"21+22 # On Alpine (where /etc/alpine-release exists), both x64 and arm64 should get23 # the -musl suffix, since unofficial-builds publishes musl binaries for both.24 # On non-Alpine, neither should get -musl.25 27 unmodified linesadded:test/fast/Unit tests/nvm_get_os+149 added
1+#!/bin/sh2+3+TEST_PWD=$(pwd)4+TEST_DIR="${TEST_PWD}/nvm_get_os_tmp"5+6+: nvm.sh7+\. ../../../nvm.sh8+9+cleanup() {10+ rm -rf "${TEST_DIR}"11+}12+13+die() {14+ echo "$@"15+ cleanup16+ exit 117+}18+19+[ ! -e "${TEST_DIR}" ] && mkdir "${TEST_DIR}"20+21+OS_RELEASE="${TEST_DIR}/os-release"22+MISSING_OS_RELEASE="${TEST_DIR}/nonexistent-os-release"23+24+# a `uname` that reports that it ran (and fails), so that any test can prove25+# whether `nvm_get_os` forked it26+MOCK_BIN="${TEST_DIR}/bin"27+mkdir -p "${MOCK_BIN}"28+{29+ echo '#!/bin/sh'30+ echo 'echo "uname was called" >&2'31+ echo 'exit 1'32+} > "${MOCK_BIN}/uname"33+chmod +x "${MOCK_BIN}/uname"34+35+# asserts that `nvm_get_os` maps the os-release file to $EXPECTED, without36+# forking `uname`; the kernel is mocked as Linux, so Linux distro `ID`s pass on any host37+KERNEL_LINUX="${TEST_DIR}/ostype-linux"38+echo 'Linux' > "${KERNEL_LINUX}"39+assert_os_release() {40+ local DESCRIPTION41+ DESCRIPTION="${1}"42+ local EXPECTED43+ EXPECTED="${2}"44+ local OUTPUT45+ NVM_OS_RELEASE="${OS_RELEASE}"46+ NVM_OS_KERNEL_OSTYPE="${KERNEL_LINUX}"47+ export NVM_OS_RELEASE NVM_OS_KERNEL_OSTYPE48+ OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_get_os 2>&1)"49+ unset NVM_OS_RELEASE NVM_OS_KERNEL_OSTYPE50+ case "${OUTPUT}" in51+ *'uname was called'*) die "for ${DESCRIPTION}, \`nvm_get_os\` forked \`uname\` despite \`${OS_RELEASE}\` being readable" ;;52+ esac53+ [ "${OUTPUT}" = "${EXPECTED}" ] || die "for ${DESCRIPTION}, expected '${EXPECTED}', got '${OUTPUT}'"54+}55+56+printf 'NAME=Ubuntu\nID=ubuntu\n' > "${OS_RELEASE}"57+assert_os_release 'a distro-style os-release' 'linux'58+59+printf 'NAME="Arch Linux"\nID=arch\n' > "${OS_RELEASE}"60+assert_os_release 'an os-release with a quoted NAME' 'linux'61+62+printf 'NAME=FreeBSD\nVERSION=14.0-RELEASE\nID=freebsd\n' > "${OS_RELEASE}"63+assert_os_release 'the FreeBSD 13.2+ os-release' 'freebsd'64+65+printf 'ID=freebsd' > "${OS_RELEASE}"66+assert_os_release 'an os-release whose ID has no trailing newline' 'freebsd'67+68+printf 'NAME=MSYS2\nID=msys2\nPRETTY_NAME="MSYS2"\nID_LIKE="cygwin arch"\n' > "${OS_RELEASE}"69+assert_os_release 'the MSYS2 os-release' 'win'70+71+printf 'NAME="Oracle Solaris"\nPRETTY_NAME="Oracle Solaris 11.4"\nCPE_NAME="cpe:/o:oracle:solaris:11:4"\nID=solaris\nVERSION=11.4\nVERSION_ID=11.4\nBUILD_ID=11.4.0.0.1.15.0\nHOME_URL="https://www.oracle.com/solaris/"\nSUPPORT_URL="https://support.oracle.com/"\n' > "${OS_RELEASE}"72+assert_os_release 'the Oracle Solaris 11.4 os-release' 'sunos'73+74+printf 'NAME="OmniOS"\nPRETTY_NAME="OmniOS Community Edition v11 r151048"\nCPE_NAME="cpe:/o:omniosce:omnios:11:151048:0"\nID=omnios\nVERSION=r151048\nVERSION_ID=r151048\nBUILD_ID=151048.0.2023.11.04\nHOME_URL="https://omnios.org/"\nSUPPORT_URL="https://omnios.org/"\nBUG_REPORT_URL="https://github.com/omniosorg/omnios-build/issues/new"\n' > "${OS_RELEASE}"75+assert_os_release 'the OmniOS r151048 os-release' 'sunos'76+77+printf 'ID=openindiana\nID_LIKE="illumos solaris"\n' > "${OS_RELEASE}"78+assert_os_release 'an OpenIndiana os-release' 'sunos'79+80+printf 'ID=smartos\n' > "${OS_RELEASE}"81+assert_os_release 'a SmartOS os-release' 'sunos'82+83+printf 'ID=illumos\n' > "${OS_RELEASE}"84+assert_os_release 'an illumos os-release' 'sunos'85+86+printf 'ID=ghostbsd\nID_LIKE=freebsd\n' > "${OS_RELEASE}"87+assert_os_release 'a GhostBSD os-release' 'freebsd'88+89+printf 'ID=freebsd # a comment\n' > "${OS_RELEASE}"90+assert_os_release 'an ID followed by whitespace and a comment' 'freebsd'91+92+# the file is read, not sourced: nothing in it runs93+printf 'exit 3\nID=ubuntu\n' > "${OS_RELEASE}"94+assert_os_release 'an os-release containing shell code' 'linux'95+96+# a file without an `ID`, or no file at all, falls back to parsing `uname -a`97+{98+ echo '#!/bin/sh'99+ echo 'echo "${NVM_MOCK_UNAME}"'100+} > "${MOCK_BIN}/uname"101+chmod +x "${MOCK_BIN}/uname"102+103+assert_uname_os() {104+ local UNAME_OUTPUT105+ UNAME_OUTPUT="${1}"106+ local OS_RELEASE_FILE107+ OS_RELEASE_FILE="${2}"108+ local EXPECTED109+ EXPECTED="${3}"110+ local OUTPUT111+ NVM_OS_RELEASE="${OS_RELEASE_FILE}"112+ NVM_OS_KERNEL_OSTYPE="${MISSING_OS_RELEASE}"113+ export NVM_OS_RELEASE NVM_OS_KERNEL_OSTYPE114+ NVM_MOCK_UNAME="${UNAME_OUTPUT}"115+ export NVM_MOCK_UNAME116+ OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_get_os)"117+ unset NVM_OS_RELEASE NVM_OS_KERNEL_OSTYPE NVM_MOCK_UNAME118+ [ "${OUTPUT}" = "${EXPECTED}" ] || die "for uname -a '${UNAME_OUTPUT}', expected '${EXPECTED}', got '${OUTPUT}'"119+}120+121+printf 'NAME=OnlyAName\n' > "${OS_RELEASE}"122+assert_uname_os 'Linux host 6.1.0 #1 SMP x86_64 GNU/Linux' "${OS_RELEASE}" 'linux'123+assert_uname_os 'Darwin host 23.1.0 arm64' "${OS_RELEASE}" 'darwin'124+assert_uname_os 'SunOS host 5.11' "${OS_RELEASE}" 'sunos'125+assert_uname_os 'FreeBSD host 14.0-RELEASE' "${OS_RELEASE}" 'freebsd'126+assert_uname_os 'OpenBSD host 7.4' "${OS_RELEASE}" 'openbsd'127+assert_uname_os 'AIX host 3 7' "${OS_RELEASE}" 'aix'128+assert_uname_os 'CYGWIN_NT-10.0 host' "${OS_RELEASE}" 'win'129+assert_uname_os 'MINGW64_NT-10.0 host' "${OS_RELEASE}" 'win'130+assert_uname_os 'MSYS_NT-10.0 host' "${OS_RELEASE}" 'win'131+assert_uname_os 'Darwin host 23.1.0 arm64' "${MISSING_OS_RELEASE}" 'darwin'132+133+# a pre-set `ID` in the environment must not be mistaken for the file's `ID`134+ID=1000135+export ID136+assert_uname_os 'Darwin host 23.1.0 arm64' "${OS_RELEASE}" 'darwin'137+unset ID138+139+# DragonFly BSD has no `nvm` OS of its own: fall back to `uname -a` as before140+printf 'NAME="DragonFly"\nID=dragonfly\n' > "${OS_RELEASE}"141+assert_uname_os 'DragonFly host 6.4-RELEASE' "${OS_RELEASE}" ''142+143+# an unlisted `ID` on a non-Linux kernel is not assumed to be Linux144+printf 'NAME=HardenedBSD\nID=hardenedbsd\n' > "${OS_RELEASE}"145+assert_uname_os 'FreeBSD host 14.0-STABLE' "${OS_RELEASE}" 'freebsd'146+printf 'NAME=Helios\nID=helios\n' > "${OS_RELEASE}"147+assert_uname_os 'SunOS host 5.11' "${OS_RELEASE}" 'sunos'148+149+cleanupmodified:nvm.sh+67 added-11 removed
2561 unmodified lines2562 fi2563 }2564 2565 nvm_get_os() {2566- local NVM_UNAME2567- NVM_UNAME="$(command uname -a)"2566+ # dash's `local` keeps the caller's value, so these must start empty2567 local NVM_OS2568+ NVM_OS=''2569+ local NVM_UNAME2570+ local NVM_OS_RELEASE_PATH2571+ local NVM_OS_RELEASE_LINE2572+ local NVM_OS_RELEASE_ID2573+ NVM_OS_RELEASE_ID=''2574+ local NVM_OS_KERNEL_PATH2575+ local NVM_OS_KERNEL_TYPE2576+ NVM_OS_KERNEL_TYPE=''2577+2578+ # `/etc/os-release` is available on Linux, FreeBSD 13.2+, and the MSYS2 and2579+ # Cygwin runtimes; where it is readable, its `ID` names the OS without2580+ # spawning `uname`. The file is read, not sourced, so nothing in it runs.2581+ # systems without the file (macOS, OpenBSD, ...) and files without an `ID`2582+ # fall through to `uname -a` below.2583+ NVM_OS_RELEASE_PATH="${NVM_OS_RELEASE:-/etc/os-release}"2584+ if [ -r "${NVM_OS_RELEASE_PATH}" ]; then2585+ while IFS= read -r NVM_OS_RELEASE_LINE || [ -n "${NVM_OS_RELEASE_LINE}" ]; do2586+ case "${NVM_OS_RELEASE_LINE}" in2587+ ID=*)2588+ NVM_OS_RELEASE_ID="${NVM_OS_RELEASE_LINE#ID=}"2589+ break2590+ ;;2591+ esac2592+ done < "${NVM_OS_RELEASE_PATH}"2593+ # an `ID` is a single word, which may be quoted and followed by2594+ # whitespace (including a CR) or a comment2595+ NVM_OS_RELEASE_ID="${NVM_OS_RELEASE_ID%%[[:space:]#]*}"2596+ NVM_OS_RELEASE_ID="${NVM_OS_RELEASE_ID#[\"\']}"2597+ NVM_OS_RELEASE_ID="${NVM_OS_RELEASE_ID%[\"\']}"2598+ case "${NVM_OS_RELEASE_ID}" in2599+ freebsd) NVM_OS=freebsd ;;2600+ cygwin* | mingw* | msys*) NVM_OS=win ;;2601+ # illumos distributions and Oracle Solaris report `SunOS` from2602+ # `uname -a`, and GhostBSD reports `FreeBSD`; map these directly instead2603+ # of forking `uname`2604+ solaris | omnios | openindiana | smartos | illumos) NVM_OS=sunos ;;2605+ ghostbsd) NVM_OS=freebsd ;;2606+ # no `ID`, or one with no `nvm` OS of its own (DragonFly BSD): let2607+ # `uname -a` decide2608+ '' | dragonfly) NVM_OS='' ;;2609+ # any other `ID` is a Linux distribution's only if the kernel says so;2610+ # unlisted BSD and illumos derivatives (e.g. HardenedBSD, Helios) let2611+ # `uname -a` decide2612+ *)2613+ NVM_OS_KERNEL_PATH="${NVM_OS_KERNEL_OSTYPE:-/proc/sys/kernel/ostype}"2614+ if [ -r "${NVM_OS_KERNEL_PATH}" ] && IFS= read -r NVM_OS_KERNEL_TYPE < "${NVM_OS_KERNEL_PATH}" && [ "${NVM_OS_KERNEL_TYPE}" = 'Linux' ]; then2615+ NVM_OS=linux2616+ fi2617+ ;;2618+ esac2619+ fi2620+2621+ if [ -z "${NVM_OS-}" ]; then2622+ NVM_UNAME="$(command uname -a)"2569- case "${NVM_UNAME}" in2570- Linux\ *) NVM_OS=linux ;;2571- Darwin\ *) NVM_OS=darwin ;;2572- SunOS\ *) NVM_OS=sunos ;;2573- FreeBSD\ *) NVM_OS=freebsd ;;2574- OpenBSD\ *) NVM_OS=openbsd ;;2575- AIX\ *) NVM_OS=aix ;;2576- CYGWIN* | MSYS* | MINGW*) NVM_OS=win ;;2577- esac2623+ case "${NVM_UNAME}" in2624+ Linux\ *) NVM_OS=linux ;;2625+ Darwin\ *) NVM_OS=darwin ;;2626+ SunOS\ *) NVM_OS=sunos ;;2627+ FreeBSD\ *) NVM_OS=freebsd ;;2628+ OpenBSD\ *) NVM_OS=openbsd ;;2629+ AIX\ *) NVM_OS=aix ;;2630+ CYGWIN* | MSYS* | MINGW*) NVM_OS=win ;;2631+ esac2632+ fi2633+2634 nvm_echo "${NVM_OS-}"2635 }2636 2637 nvm_get_arch() {2924 unmodified lines