Skip to content
Myr

[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

4 files+224 added−11 removed

modified:test/fast/Unit tests/nvm_get_arch+4 added
19 unmodified lines
20# Sets the PATH for these tests to include the symlinks to the mocked21# binaries22export PATH=".:${PATH}"2324# `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 exist26export NVM_OS_RELEASE="${MOCKS_DIR}/nonexistent-os-release"2728# 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.31setup_mock_arch() {
76 unmodified lines
modified:test/fast/Unit tests/nvm_get_arch alpine+4 added
13 unmodified lines
1415MOCKS_DIR="$(pwd)/../../mocks"16export PATH=".:${PATH}"1718# `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 exist20export NVM_OS_RELEASE="${MOCKS_DIR}/nonexistent-os-release"2122# 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 lines
added:test/fast/Unit tests/nvm_get_os+149 added
1#!/bin/sh23TEST_PWD=$(pwd)4TEST_DIR="${TEST_PWD}/nvm_get_os_tmp"56: nvm.sh7\. ../../../nvm.sh89cleanup() {10 rm -rf "${TEST_DIR}"11}1213die() {14 echo "$@"15 cleanup16 exit 117}1819[ ! -e "${TEST_DIR}" ] && mkdir "${TEST_DIR}"2021OS_RELEASE="${TEST_DIR}/os-release"22MISSING_OS_RELEASE="${TEST_DIR}/nonexistent-os-release"2324# a `uname` that reports that it ran (and fails), so that any test can prove25# whether `nvm_get_os` forked it26MOCK_BIN="${TEST_DIR}/bin"27mkdir -p "${MOCK_BIN}"28{29 echo '#!/bin/sh'30 echo 'echo "uname was called" >&2'31 echo 'exit 1'32} > "${MOCK_BIN}/uname"33chmod +x "${MOCK_BIN}/uname"3435# 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 host37KERNEL_LINUX="${TEST_DIR}/ostype-linux"38echo 'Linux' > "${KERNEL_LINUX}"39assert_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}5556printf 'NAME=Ubuntu\nID=ubuntu\n' > "${OS_RELEASE}"57assert_os_release 'a distro-style os-release' 'linux'5859printf 'NAME="Arch Linux"\nID=arch\n' > "${OS_RELEASE}"60assert_os_release 'an os-release with a quoted NAME' 'linux'6162printf 'NAME=FreeBSD\nVERSION=14.0-RELEASE\nID=freebsd\n' > "${OS_RELEASE}"63assert_os_release 'the FreeBSD 13.2+ os-release' 'freebsd'6465printf 'ID=freebsd' > "${OS_RELEASE}"66assert_os_release 'an os-release whose ID has no trailing newline' 'freebsd'6768printf 'NAME=MSYS2\nID=msys2\nPRETTY_NAME="MSYS2"\nID_LIKE="cygwin arch"\n' > "${OS_RELEASE}"69assert_os_release 'the MSYS2 os-release' 'win'7071printf '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}"72assert_os_release 'the Oracle Solaris 11.4 os-release' 'sunos'7374printf '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}"75assert_os_release 'the OmniOS r151048 os-release' 'sunos'7677printf 'ID=openindiana\nID_LIKE="illumos solaris"\n' > "${OS_RELEASE}"78assert_os_release 'an OpenIndiana os-release' 'sunos'7980printf 'ID=smartos\n' > "${OS_RELEASE}"81assert_os_release 'a SmartOS os-release' 'sunos'8283printf 'ID=illumos\n' > "${OS_RELEASE}"84assert_os_release 'an illumos os-release' 'sunos'8586printf 'ID=ghostbsd\nID_LIKE=freebsd\n' > "${OS_RELEASE}"87assert_os_release 'a GhostBSD os-release' 'freebsd'8889printf 'ID=freebsd # a comment\n' > "${OS_RELEASE}"90assert_os_release 'an ID followed by whitespace and a comment' 'freebsd'9192# the file is read, not sourced: nothing in it runs93printf 'exit 3\nID=ubuntu\n' > "${OS_RELEASE}"94assert_os_release 'an os-release containing shell code' 'linux'9596# 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"101chmod +x "${MOCK_BIN}/uname"102103assert_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}120121printf 'NAME=OnlyAName\n' > "${OS_RELEASE}"122assert_uname_os 'Linux host 6.1.0 #1 SMP x86_64 GNU/Linux' "${OS_RELEASE}" 'linux'123assert_uname_os 'Darwin host 23.1.0 arm64' "${OS_RELEASE}" 'darwin'124assert_uname_os 'SunOS host 5.11' "${OS_RELEASE}" 'sunos'125assert_uname_os 'FreeBSD host 14.0-RELEASE' "${OS_RELEASE}" 'freebsd'126assert_uname_os 'OpenBSD host 7.4' "${OS_RELEASE}" 'openbsd'127assert_uname_os 'AIX host 3 7' "${OS_RELEASE}" 'aix'128assert_uname_os 'CYGWIN_NT-10.0 host' "${OS_RELEASE}" 'win'129assert_uname_os 'MINGW64_NT-10.0 host' "${OS_RELEASE}" 'win'130assert_uname_os 'MSYS_NT-10.0 host' "${OS_RELEASE}" 'win'131assert_uname_os 'Darwin host 23.1.0 arm64' "${MISSING_OS_RELEASE}" 'darwin'132133# a pre-set `ID` in the environment must not be mistaken for the file's `ID`134ID=1000135export ID136assert_uname_os 'Darwin host 23.1.0 arm64' "${OS_RELEASE}" 'darwin'137unset ID138139# DragonFly BSD has no `nvm` OS of its own: fall back to `uname -a` as before140printf 'NAME="DragonFly"\nID=dragonfly\n' > "${OS_RELEASE}"141assert_uname_os 'DragonFly host 6.4-RELEASE' "${OS_RELEASE}" ''142143# an unlisted `ID` on a non-Linux kernel is not assumed to be Linux144printf 'NAME=HardenedBSD\nID=hardenedbsd\n' > "${OS_RELEASE}"145assert_uname_os 'FreeBSD host 14.0-STABLE' "${OS_RELEASE}" 'freebsd'146printf 'NAME=Helios\nID=helios\n' > "${OS_RELEASE}"147assert_uname_os 'SunOS host 5.11' "${OS_RELEASE}" 'sunos'148149cleanup
modified:nvm.sh+67 added-11 removed
2561 unmodified lines
2562 fi2563}25642565nvm_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=''25772578 # `/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 fi26202621 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 fi26332634 nvm_echo "${NVM_OS-}"2635}26362637nvm_get_arch() {
2924 unmodified lines