[Fix] `install.sh`: start a new line before appending to a profile that does not end with one
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #3951
authored by lazerg · committed by Jordan Harband · parent d55a55c · Browse files · 2 files·+68 added−0 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+68 added−0 removed
- tests:+65 added−0 removed
- other:+3 added−0 removed
Provenance
- Commit
e45c2df- Tree
d61da70- Author
lazerg <lazerg2@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
d55a55c
2 files changed+68 added−0 removed
added:test/install_script/nvm_do_install_profile_without_trailing_newline+65 added
1+#!/bin/sh2+3+die () { echo "$@" ; cleanup ; exit 1; }4+5+cleanup() {6+ unset -f install_nvm_from_git install_nvm_as_script nvm_detect_profile nvm_has7+ unset -f nvm_check_global_modules nvm_install_node8+ unset -f setup cleanup die assert_profile9+ unset NVM_ENV METHOD PROFILE10+ rm -rf "${TMPDIR_FOR_TEST-}" 2>/dev/null11+}12+13+setup() {14+ NVM_ENV=testing \. ../../install.sh15+16+ # Mock installation functions to do nothing17+ install_nvm_from_git() { :; }18+ install_nvm_as_script() { :; }19+ nvm_check_global_modules() { :; }20+ nvm_install_node() { :; }21+22+ # Mock nvm_has to return true for git23+ nvm_has() {24+ case "$1" in25+ git) return 0 ;;26+ xcode-select) return 1 ;;27+ *) return 1 ;;28+ esac29+ }30+}31+32+setup33+34+TMPDIR_FOR_TEST="$(mktemp -d)"35+TEST_PROFILE="${TMPDIR_FOR_TEST}/.bashrc"36+EXPECTED_PROFILE="${TMPDIR_FOR_TEST}/expected"37+COMPLETION_LINE='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion'38+39+# runs the installer against $TEST_PROFILE, and compares the whole file, byte for byte, with $EXPECTED_PROFILE.40+# `nvm_do_install` unsets every `nvm_` function when it finishes, so each run sources the installer and mocks afresh.41+assert_profile() {42+ setup43+ nvm_detect_profile() {44+ echo "${TEST_PROFILE}"45+ }46+ PROFILE='' METHOD='' NVM_DIR="$(cd ../.. && pwd)" nvm_do_install > /dev/null 2>&147+ command cmp -s "${TEST_PROFILE}" "${EXPECTED_PROFILE}" || die "$1; got: >$(command cat "${TEST_PROFILE}")<"48+}49+50+#51+# Test: when the profile already sources nvm.sh but does not end with a newline,52+# the bash_completion line must be appended on its own line, and the profile must end with a newline53+#54+command printf '%s\n%s' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" > "${TEST_PROFILE}"55+command printf '%s\n%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" "${COMPLETION_LINE}" > "${EXPECTED_PROFILE}"56+assert_profile 'bash_completion line was not appended on its own line, ending with a newline'57+58+#59+# Test: when the profile already ends with a newline, no blank line is added before the bash_completion line60+#61+command printf '%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" > "${TEST_PROFILE}"62+command printf '%s\n%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" "${COMPLETION_LINE}" > "${EXPECTED_PROFILE}"63+assert_profile 'a profile ending with a newline got an extra blank line before the bash_completion line'64+65+cleanupmodified:install.sh+3 added
488 unmodified lines489 fi490 # shellcheck disable=SC2016491 if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then492 nvm_echo "=> Appending bash_completion source string to $NVM_PROFILE"493+ if [ -n "$(command tail -c 1 "$NVM_PROFILE")" ]; then494+ nvm_echo >> "$NVM_PROFILE"495+ fi496 command printf '%b' "$COMPLETION_STR" >> "$NVM_PROFILE"497 else498 nvm_echo "=> bash_completion source string already in ${NVM_PROFILE}"499 fi37 unmodified lines