Skip to content
Myr

[Fix] `default_packages`: work when the file lacks a trailing newline

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

Fixes #1995.

authored by Jordan Harband · committed by Jordan Harband · parent 5c117e6 · Browse files · 3 files+33 added−4 removed
This commit

State

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

Diff stats

3 files+33 added−4 removed

  • tests:+25 added−2 removed
  • other:+8 added−2 removed

3 files+33 added−4 removed

modified:test/fast/Unit tests/nvm_get_default_packages+17 added-2 removed
51 unmodified lines
5253setup5455cat > $FILE << EOF56rimraf57not~a~package~name58mkdirp59EOF60printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline6162DEFAULT_PKGS="$(nvm_get_default_packages)"63EXPECTED_PKGS='rimraf not~a~package~name mkdirp'64[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"6566cleanup6768setup6970cat > $FILE << EOF71object-inspect @ 1.0.272rimraf73EOF7475DEFAULT_PKGS="$(nvm_get_default_packages 2>&1 >/dev/null)"76EXPECTED_PKGS="Only one package per line is allowed in the $FILE file. Please remove any lines with multiple space-separated values."62[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"77[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"7879cleanup8081setup
1 unmodified line
83rm -rf $FILE8485DEFAULT_PKGS="$(nvm_get_default_packages)"86EXPECTED_PKGS=''72[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"87[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "5: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"8889touch $FILE9091cleanup
modified:test/installation_node/default-packages+8 added
28 unmodified lines
2930# commented-package3132stevemao/left-pad33daytime34EOF3536printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline3738nvm install v6.10.1 2>&139EXIT_CODE=$?40[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE"41
1 unmodified line
43if [ -z "$?" ]; then44 die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'' to exit with 0, got $?"45fi4647nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'48if [ -z "$?" ]; then49 die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'' to exit with 0, got $?"50fi5152cleanup5354setup55
68 unmodified lines
modified:nvm.sh+8 added-2 removed
3493 unmodified lines
3494 esac3495}34963497nvm_get_default_packages() {3498 if [ -f "${NVM_DIR}/default-packages" ]; then3498 local NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"3499 if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then3500 local DEFAULT_PACKAGES3501 DEFAULT_PACKAGES=''35023503 # Read lines from $NVM_DIR/default-packages3504 local line3505 # ensure a trailing newline3506 WORK=$(mktemp -d) || exit $?3507 trap "rm -rf '$WORK'" EXIT3508 # shellcheck disable=SC10033509 sed -e '$a\' "${NVM_DEFAULT_PACKAGE_FILE}" > "${WORK}/default-packages"3510 while IFS=' ' read -r line; do3511 # Skip empty lines.3512 [ -n "${line-}" ] || continue3513
8 unmodified lines
3522 ;;3523 esac35243525 DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "3520 done < "${NVM_DIR}/default-packages"3526 done < "${WORK}/default-packages"3527 echo "${DEFAULT_PACKAGES}" | xargs3528 fi3529}3530
97 unmodified lines