[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
Provenance
- Commit
db19450- Tree
1497cf9- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
5c117e6
3 files changed+33 added−4 removed
modified:test/fast/Unit tests/nvm_get_default_packages+17 added-2 removed
51 unmodified lines52 53 setup54 55 cat > $FILE << EOF56+rimraf57+not~a~package~name58+mkdirp59+EOF60+printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline61+62+DEFAULT_PKGS="$(nvm_get_default_packages)"63+EXPECTED_PKGS='rimraf not~a~package~name mkdirp'64+[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"65+66+cleanup67+68+setup69+70+cat > $FILE << EOF71 object-inspect @ 1.0.272 rimraf73 EOF74 75 DEFAULT_PKGS="$(nvm_get_default_packages 2>&1 >/dev/null)"76 EXPECTED_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}<"78 79 cleanup80 81 setup1 unmodified line83 rm -rf $FILE84 85 DEFAULT_PKGS="$(nvm_get_default_packages)"86 EXPECTED_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}<"88 89 touch $FILE90 91 cleanupmodified:test/installation_node/default-packages+8 added
28 unmodified lines29 30 # commented-package31 32 stevemao/left-pad33+daytime34 EOF35 36+printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline37+38 nvm install v6.10.1 2>&139 EXIT_CODE=$?40 [ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE"41 1 unmodified line43 if [ -z "$?" ]; then44 die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'' to exit with 0, got $?"45 fi46 47+nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'48+if [ -z "$?" ]; then49+ die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'' to exit with 0, got $?"50+fi51+52 cleanup53 54 setup55 68 unmodified linesmodified:nvm.sh+8 added-2 removed
3493 unmodified lines3494 esac3495 }3496 3497 nvm_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=''3502 3503 # 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 lines3522 ;;3523 esac3524 3525 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