diff --git a/install.sh b/install.sh index 50fbd3a..2a2baa1 100755 --- a/install.sh +++ b/install.sh @@ -267,16 +267,54 @@ nvm_detect_profile() { local DETECTED_PROFILE DETECTED_PROFILE='' - if [ -n "${BASH_VERSION-}" ]; then - if [ -f "$HOME/.bashrc" ]; then - DETECTED_PROFILE="$HOME/.bashrc" - elif [ -f "$HOME/.bash_profile" ]; then - DETECTED_PROFILE="$HOME/.bash_profile" - fi - elif [ -n "${ZSH_VERSION-}" ]; then - DETECTED_PROFILE="$HOME/.zshrc" + # Detect the user's login shell + local USER_SHELL + local USER_SHELL_NAME + USER_SHELL='' + + # If we're not testing, try to get shell from passwd + # Otherwise, try the SHELL variable + if [ "$NVM_TESTING" != 'yes' ]; then + USER_SHELL=$(getent passwd $(whoami) | cut -d: -f7) + elif [ -n "$SHELL" ]; then + USER_SHELL="$SHELL" fi + USER_SHELL_NAME="${USER_SHELL##*/}" + + # First try to find the config file based on the shell name + if [ -n "${USER_SHELL_NAME}" ]; then + case "${USER_SHELL_NAME}" in + bash) + if [ -f "$HOME/.bashrc" ]; then + DETECTED_PROFILE="$HOME/.bashrc" + elif [ -f "$HOME/.bash_profile" ]; then + DETECTED_PROFILE="$HOME/.bash_profile" + fi + ;; + zsh) + if [ -f "$HOME/.zshrc" ]; then + DETECTED_PROFILE="$HOME/.zshrc" + elif [ -f "$HOME/.zprofile" ]; then + DETECTED_PROFILE="$HOME/.zprofile" + fi + ;; + ksh*) + if [ -f "$HOME/.kshrc" ]; then + DETECTED_PROFILE="$HOME/.kshrc" + fi + ;; + mksh) + if [ -f "$HOME/.mkshrc" ]; then + DETECTED_PROFILE="$HOME/.mkshrc" + fi + ;; + *) + ;; + esac + fi + + # Now brute force if [ -z "$DETECTED_PROFILE" ]; then for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc" do diff --git a/test/install_script/nvm_detect_profile b/test/install_script/nvm_detect_profile index 54815ec..ee511c0 100755 --- a/test/install_script/nvm_detect_profile +++ b/test/install_script/nvm_detect_profile @@ -2,7 +2,9 @@ setup () { HOME="." + SHELL='/bin/bash' NVM_ENV=testing \. ../../install.sh + NVM_TESTING='yes' touch ".bashrc" touch ".bash_profile" touch ".zshrc" @@ -12,6 +14,7 @@ setup () { cleanup () { unset HOME + unset SHELL unset NVM_ENV unset NVM_DETECT_PROFILE unset BASH_VERSION @@ -34,6 +37,12 @@ if [ -n "$NVM_DETECT_PROFILE" ]; then die "nvm_detect_profile still detected a profile even though PROFILE=/dev/null" fi +# .bashrc should be detected if the shell is bash +NVM_DETECT_PROFILE="$(unset PROFILE; nvm_detect_profile)" +if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then + die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash" +fi + # .bashrc should be detected for bash NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then @@ -46,8 +55,14 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then die "nvm_detect_profile ignored \$PROFILE" fi +# .zshrc should be detected if the shell is zsh +NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" +if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then + die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh" +fi + # .zshrc should be detected for zsh -NVM_DETECT_PROFILE="$(ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh" fi @@ -82,7 +97,8 @@ fi # return an empty value if everything fails # -# It should favor .profile if file exists +# It should favor .profile if other detection methods fail and file exists and +SHELL="nonsense" NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then die "nvm_detect_profile should have selected .profile" @@ -104,7 +120,7 @@ fi # Otherwise, it should favor .zshrc if file exists rm ".bash_profile" -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then die "nvm_detect_profile should have selected .zshrc" fi diff --git a/test/install_script/nvm_install_with_aliased_dot b/test/install_script/nvm_install_with_aliased_dot index e1b19a1..f8ca5e0 100755 --- a/test/install_script/nvm_install_with_aliased_dot +++ b/test/install_script/nvm_install_with_aliased_dot @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash setup() { # Needed to avoid to checkout the repo to the latest nvm version, losing the commits of the current PR