Skip to content
Myr

[Fix] `nvm_get_default_packages`: use `awk` for more reliable file processing

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

See https://github.com/nvm-sh/nvm/commit/db19450caafb37734a3302b24ac9fe2cde40638d Fixes #3382

authored by Jordan Harband · committed by Jordan Harband · parent f439acd · Browse files · 2 files+17 added−31 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+17 added−31 removed

  • tests:+1 added−1 removed
  • other:+16 added−30 removed

2 files+17 added−31 removed

modified:test/fast/Unit tests/nvm_get_default_packages+1 added-1 removed
72 unmodified lines
73rimraf74EOF7576DEFAULT_PKGS="$(nvm_get_default_packages 2>&1 >/dev/null)"77EXPECTED_PKGS="Only one package per line is allowed in the $FILE file. Please remove any lines with multiple space-separated values."77EXPECTED_PKGS="Only one package per line is allowed in \`${FILE}\`. Please remove any lines with multiple space-separated values."78[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"7980cleanup81
11 unmodified lines
modified:nvm.sh+16 added-30 removed
4434 unmodified lines
4435 esac4436}44374438nvm_get_default_packages() {4439 local NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"4439 local NVM_DEFAULT_PACKAGE_FILE4440 NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"4441 if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then4441 local DEFAULT_PACKAGES4442 DEFAULT_PACKAGES=''44434444 # Read lines from $NVM_DIR/default-packages4445 local line4446 # ensure a trailing newline4447 WORK=$(mktemp -d) || exit $?4448 # shellcheck disable=SC20644449 trap "command rm -rf '$WORK'" EXIT4450 # shellcheck disable=SC10034451 sed -e '$a\' "${NVM_DEFAULT_PACKAGE_FILE}" > "${WORK}/default-packages"4452 while IFS=' ' read -r line; do4453 # Skip empty lines.4454 [ -n "${line-}" ] || continue44554456 # Skip comment lines that begin with `#`.4457 [ "$(nvm_echo "${line}" | command cut -c1)" != "#" ] || continue44584459 # Fail on lines that have multiple space-separated words4460 case $line in4461 *\ *)4462 nvm_err "Only one package per line is allowed in the ${NVM_DIR}/default-packages file. Please remove any lines with multiple space-separated values."4463 return 14464 ;;4465 esac44664467 DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "4468 done < "${WORK}/default-packages"4469 echo "${DEFAULT_PACKAGES}" | command xargs4442 command awk -v filename="${NVM_DEFAULT_PACKAGE_FILE}" '4443 /^[[:space:]]*#/ { next } # Skip lines that begin with #4444 /^[[:space:]]*$/ { next } # Skip empty lines4445 /[[:space:]]/ && !/^[[:space:]]*#/ {4446 print "Only one package per line is allowed in `" filename "`. Please remove any lines with multiple space-separated values." > "/dev/stderr"4447 err = 14448 exit 14449 }4450 {4451 if (NR > 1 && !prev_space) printf " "4452 printf "%s", $04453 prev_space = 04454 }4455 ' "${NVM_DEFAULT_PACKAGE_FILE}"4456 fi4457}44584459nvm_install_default_packages() {
115 unmodified lines