Skip to content
Myr

[Perf] `nvm_npmrc_bad_news_bears`: scan npmrc files without forking `grep`

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 f296b8d · Browse files · 2 files+72 added−2 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+72 added−2 removed

  • tests:+51 added−0 removed
  • other:+21 added−2 removed

2 files+72 added−2 removed

modified:test/fast/Unit tests/nvm_die_on_prefix+51 added
199 unmodified lines
200 [ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not error with '$EXPECTED_OUTPUT'; got '$CAPTURED_STDERR'"201 [ "_$CAPTURED_EXIT_CODE" = "_10" ] || die "'nvm_die_on_prefix 0 foo' with user .npmrc that has globalconfig did not exit with 10; got '$CAPTURED_EXIT_CODE'"202)203204# nvm_npmrc_bad_news_bears matches `^(prefix|globalconfig) *=`205NPMRC="${TEST_DIR}/bad-news.npmrc"206printf 'prefix=x\n' > "${NPMRC}"207nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix=x should be bad news'208printf 'prefix =x\n' > "${NPMRC}"209nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix =x should be bad news'210printf 'prefix =x\n' > "${NPMRC}"211nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix =x should be bad news'212printf 'prefix =\n' > "${NPMRC}"213nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix = should be bad news'214printf 'foo=1\nprefix = y\n' > "${NPMRC}"215nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a matching line after a non-matching line should be bad news'216printf 'prefix=x' > "${NPMRC}"217nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix=x without a trailing newline should be bad news'218printf 'globalconfig=x\n' > "${NPMRC}"219nvm_npmrc_bad_news_bears "${NPMRC}" || die 'globalconfig=x should be bad news'220printf 'globalconfig = x\n' > "${NPMRC}"221nvm_npmrc_bad_news_bears "${NPMRC}" || die 'globalconfig = x should be bad news'222223printf 'prefixfoo=x\n' > "${NPMRC}"224! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefixfoo=x should not be bad news'225printf 'globalconfigfoo=x\n' > "${NPMRC}"226! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'globalconfigfoo=x should not be bad news'227printf 'prefix x=x\n' > "${NPMRC}"228! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix x=x should not be bad news'229printf 'prefix\t=x\n' > "${NPMRC}"230! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a tab before = should not be bad news'231printf ' prefix=x\n' > "${NPMRC}"232! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a leading space should not be bad news'233printf 'Prefix=x\n' > "${NPMRC}"234! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'an uppercase key should not be bad news'235printf 'prefix\n' > "${NPMRC}"236! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a bare prefix should not be bad news'237printf 'prefix \n' > "${NPMRC}"238! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'trailing spaces without = should not be bad news'239: > "${NPMRC}"240! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'an empty npmrc should not be bad news'241! nvm_npmrc_bad_news_bears || die 'a missing argument should not be bad news'242! nvm_npmrc_bad_news_bears "${TEST_DIR}/nonexistent.npmrc" || die 'a missing file should not be bad news'243244# the npmrc scan must not fork `grep`245(246 rm -f "${TEST_DIR}/grep-was-called"247 nvm_grep() { : > "${TEST_DIR}/grep-was-called"; return 1; }248 printf 'globalconfig = nope\n' > "${TEST_DIR}/probe.npmrc"249 NVM_PROBE_EXIT=0250 nvm_npmrc_bad_news_bears "${TEST_DIR}/probe.npmrc" || NVM_PROBE_EXIT=$?251 [ ! -e "${TEST_DIR}/grep-was-called" ] || exit 10252 [ "${NVM_PROBE_EXIT}" = 0 ] || exit 11253) || die 'nvm_npmrc_bad_news_bears must read npmrc files without calling nvm_grep'254255cleanup
modified:nvm.sh+21 added-2 removed
3327 unmodified lines
3328}33293330nvm_npmrc_bad_news_bears() {3331 local NVM_NPMRC3332 local NVM_NPMRC_LINE3333 local NVM_NPMRC_KEY3334 NVM_NPMRC="${1-}"3333 if [ -n "${NVM_NPMRC}" ] && [ -f "${NVM_NPMRC}" ] && nvm_grep -Ee '^(prefix|globalconfig) *=' <"${NVM_NPMRC}" >/dev/null; then3334 return 03335 if [ -n "${NVM_NPMRC}" ] && [ -f "${NVM_NPMRC}" ]; then3336 # this is fork-free on purpose: it runs up to four times on every `nvm use`/`nvm install`3337 # avoid grepping or sub-shells as much as possible here.3338 while IFS= read -r NVM_NPMRC_LINE || [ -n "${NVM_NPMRC_LINE}" ]; do3339 case "${NVM_NPMRC_LINE}" in3340 *=*)3341 NVM_NPMRC_KEY="${NVM_NPMRC_LINE%%=*}"3342 ;;3343 *)3344 continue3345 ;;3346 esac3347 NVM_NPMRC_KEY="${NVM_NPMRC_KEY%"${NVM_NPMRC_KEY##*[! ]}"}"3348 case "${NVM_NPMRC_KEY}" in3349 prefix | globalconfig)3350 return 03351 ;;3352 esac3353 done < "${NVM_NPMRC}"3354 fi3355 return 13356}3357
2148 unmodified lines