Skip to content
Myr

[Perf] `nvm_die_on_prefix`: detect the OS only when it is needed

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Cassidy Marble · committed by Jordan Harband · parent c7ba8d2 · Browse files · 2 files+18 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−3 removed

  • tests:+14 added−0 removed
  • other:+4 added−3 removed

2 files+18 added−3 removed

modified:test/fast/Unit tests/nvm_die_on_prefix+14 added
47 unmodified lines
4849OUTPUT="$(nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"50[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' was not a noop when prefix is good; got '$OUTPUT'"5152# `nvm_get_os` runs `uname`; it is only needed when an `NPM_CONFIG_PREFIX`53# variable is set, so with none set it must not run at all54MOCK_BIN="${TEST_DIR}/bin"55mkdir -p "${MOCK_BIN}"56{57 echo '#!/bin/sh'58 echo 'echo "uname was called" >&2'59 echo 'exit 1'60} > "${MOCK_BIN}/uname"61chmod +x "${MOCK_BIN}/uname"62unset NPM_CONFIG_PREFIX npm_config_prefix npm_CONFIG_PREFIX63OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_die_on_prefix 0 foo "$(nvm_version_dir new)" 2>&1)"64[ -z "$OUTPUT" ] || die "'nvm_die_on_prefix' detected the OS (ran uname) when no NPM_CONFIG_PREFIX was set; got '$OUTPUT'"6566mkdir -p "$(nvm_version_dir new)"67ln -s "$(nvm_version_dir new)" "$TEST_DIR/node"6869npm() {
135 unmodified lines
modified:nvm.sh+4 added-3 removed
3363 unmodified lines
3364 nvm_err 'Run `unset PREFIX` to unset it.'3365 return 33366 fi33673368 local NVM_OS3369 NVM_OS="$(nvm_get_os)"33703368 # npm normalizes NPM_CONFIG_-prefixed env vars3369 # https://github.com/npm/npmconf/blob/22827e4038d6eebaafeb5c13ed2b92cf97b8fb82/npmconf.js#L331-L3483370 # https://github.com/npm/npm/blob/5e426a78ca02d0044f8dd26e0c5f881217081cbd/lib/config/core.js#L343-L3593371 #
1 unmodified line
3373 # until none are left.3374 local NVM_NPM_CONFIG_x_PREFIX_ENV3375 NVM_NPM_CONFIG_x_PREFIX_ENV="$(command awk 'BEGIN { for (name in ENVIRON) if (toupper(name) == "NPM_CONFIG_PREFIX") { print name; break } }')"3376 if [ -n "${NVM_NPM_CONFIG_x_PREFIX_ENV-}" ]; then3377 # `$NVM_OS` is only used to translate Windows paths, but detecting the OS3378 # shells out to `uname`, so it is deferred until it is actually needed3379 local NVM_OS3380 NVM_OS="$(nvm_get_os)"3381 local NVM_CONFIG_VALUE3382 eval "NVM_CONFIG_VALUE=\"\$${NVM_NPM_CONFIG_x_PREFIX_ENV}\""3383 if [ -n "${NVM_CONFIG_VALUE-}" ] && [ "_${NVM_OS}" = "_win" ]; then3384 NVM_CONFIG_VALUE="$(cd "$NVM_CONFIG_VALUE" 2>/dev/null && pwd)"
2102 unmodified lines