Skip to content
Myr

[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

2 files+68 added−0 removed

added:test/install_script/nvm_do_install_profile_without_trailing_newline+65 added
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45cleanup() {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}1213setup() {14 NVM_ENV=testing \. ../../install.sh1516 # Mock installation functions to do nothing17 install_nvm_from_git() { :; }18 install_nvm_as_script() { :; }19 nvm_check_global_modules() { :; }20 nvm_install_node() { :; }2122 # 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}3132setup3334TMPDIR_FOR_TEST="$(mktemp -d)"35TEST_PROFILE="${TMPDIR_FOR_TEST}/.bashrc"36EXPECTED_PROFILE="${TMPDIR_FOR_TEST}/expected"37COMPLETION_LINE='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion'3839# 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.41assert_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}4950#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#54command printf '%s\n%s' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" > "${TEST_PROFILE}"55command printf '%s\n%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" "${COMPLETION_LINE}" > "${EXPECTED_PROFILE}"56assert_profile 'bash_completion line was not appended on its own line, ending with a newline'5758#59# Test: when the profile already ends with a newline, no blank line is added before the bash_completion line60#61command printf '%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" > "${TEST_PROFILE}"62command printf '%s\n%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" "${COMPLETION_LINE}" > "${EXPECTED_PROFILE}"63assert_profile 'a profile ending with a newline got an extra blank line before the bash_completion line'6465cleanup
modified:install.sh+3 added
488 unmodified lines
489 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 fi
37 unmodified lines