From 9593616b8c1730952b64e97389518f36d015e10a Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Sun, 1 Jan 2017 18:06:02 +0800 Subject: [PATCH] [Fix] NVM_PROFILE bash/zsh detection in installation --- install.sh | 21 ++++++++++++++----- .../install_script/nvm_profile_is_bash_or_zsh | 13 ++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100755 test/install_script/nvm_profile_is_bash_or_zsh diff --git a/install.sh b/install.sh index f185db6..7766aa5 100755 --- a/install.sh +++ b/install.sh @@ -14,6 +14,19 @@ nvm_latest_version() { echo "v0.33.1" } +nvm_profile_is_bash_or_zsh() { + local TEST_PROFILE + TEST_PROFILE="${1-}" + case "${TEST_PROFILE-}" in + *"/.bashrc" | *"/.bash_profile" | *"/.zshrc") + return + ;; + *) + return 1 + ;; + esac +} + # # Outputs the location to NVM depending on: # * The availability of $NVM_SOURCE @@ -305,11 +318,9 @@ nvm_do_install() { echo "=> Append the following lines to the correct file yourself:" command printf "${SOURCE_STR}" else - case "${NVM_PROFILE-}" in - ".bashrc" | ".bash_profile" | ".zshrc") - BASH_OR_ZSH=true - ;; - esac + if nvm_profile_is_bash_or_zsh "${NVM_PROFILE-}"; then + BASH_OR_ZSH=true + fi if ! command grep -qc '/nvm.sh' "$NVM_PROFILE"; then echo "=> Appending nvm source string to $NVM_PROFILE" command printf "${SOURCE_STR}" >> "$NVM_PROFILE" diff --git a/test/install_script/nvm_profile_is_bash_or_zsh b/test/install_script/nvm_profile_is_bash_or_zsh new file mode 100755 index 0000000..aae9cc3 --- /dev/null +++ b/test/install_script/nvm_profile_is_bash_or_zsh @@ -0,0 +1,13 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +NVM_ENV=testing \. ../../install.sh + +#nvm_profile_is_bash_or_zsh is available +type nvm_profile_is_bash_or_zsh > /dev/null 2>&1 || die 'nvm_profile_is_bash_or_zsh is not available' +nvm_profile_is_bash_or_zsh "/home/nvm/.bashrc" || die '/home/nvm/.bashrc is bash profile' +nvm_profile_is_bash_or_zsh "/home/nvm/.bash_profile" || die '/home/nvm/.bash_profile is bash profile' +nvm_profile_is_bash_or_zsh "/home/nvm/.zshrc" || die '/home/nvm/.zshrc is zsh profile' +if nvm_profile_is_bash_or_zsh "/home/nvm/.bash"; then die '/home/nvm/.bash is not bash nor zsh profile'; fi +if nvm_profile_is_bash_or_zsh "/home/nvm/.zsh" ; then die '/home/nvm/.zsh is not bash nor zsh profile'; fi