[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
Provenance
- Commit
c28f2b7- Tree
dac5103- Author
Cassidy Marble <cassidy@marble.sh>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
f296b8d
2 files changed+72 added−2 removed
modified:test/fast/Unit tests/nvm_die_on_prefix+51 added
199 unmodified lines200 [ "_$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 )203 204+# nvm_npmrc_bad_news_bears matches `^(prefix|globalconfig) *=`205+NPMRC="${TEST_DIR}/bad-news.npmrc"206+printf 'prefix=x\n' > "${NPMRC}"207+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix=x should be bad news'208+printf 'prefix =x\n' > "${NPMRC}"209+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix =x should be bad news'210+printf 'prefix =x\n' > "${NPMRC}"211+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix =x should be bad news'212+printf 'prefix =\n' > "${NPMRC}"213+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix = should be bad news'214+printf 'foo=1\nprefix = y\n' > "${NPMRC}"215+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a matching line after a non-matching line should be bad news'216+printf 'prefix=x' > "${NPMRC}"217+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix=x without a trailing newline should be bad news'218+printf 'globalconfig=x\n' > "${NPMRC}"219+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'globalconfig=x should be bad news'220+printf 'globalconfig = x\n' > "${NPMRC}"221+nvm_npmrc_bad_news_bears "${NPMRC}" || die 'globalconfig = x should be bad news'222+223+printf 'prefixfoo=x\n' > "${NPMRC}"224+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefixfoo=x should not be bad news'225+printf 'globalconfigfoo=x\n' > "${NPMRC}"226+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'globalconfigfoo=x should not be bad news'227+printf 'prefix x=x\n' > "${NPMRC}"228+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'prefix x=x should not be bad news'229+printf 'prefix\t=x\n' > "${NPMRC}"230+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a tab before = should not be bad news'231+printf ' prefix=x\n' > "${NPMRC}"232+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a leading space should not be bad news'233+printf 'Prefix=x\n' > "${NPMRC}"234+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'an uppercase key should not be bad news'235+printf 'prefix\n' > "${NPMRC}"236+! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a bare prefix should not be bad news'237+printf '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'243+244+# 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'254+255 cleanupmodified:nvm.sh+21 added-2 removed
3327 unmodified lines3328 }3329 3330 nvm_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