From 9ffd330f59c96a7e70662837d21aa789a4e1dc1f Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Sat, 8 Aug 2020 18:19:52 -0700 Subject: [PATCH] Better shell detection, and better test handling --- install.sh | 61 +++++++++++++++++++------- test/install_script/nvm_detect_profile | 10 +++-- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/install.sh b/install.sh index 5170b18..e70b49b 100755 --- a/install.sh +++ b/install.sh @@ -234,24 +234,55 @@ nvm_detect_profile() { DETECTED_PROFILE='' # Detect the user's login shell - local DETECTED_SHELL - local SHELLRC + local USER_SHELL + local USER_SHELL_NAME + USER_SHELL='' - DETECTED_SHELL="${SHELL##*/}" - SHELLRC="$HOME/.${DETECTED_SHELL}rc" - - if [ -n "${DETECTED_SHELL}" ] && [ -f "$SHELLRC" ]; then - DETECTED_PROFILE="$SHELLRC" - elif [ -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" + # 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 + if [ -n "$SHELL" ]; then + USER_SHELL_NAME="${USER_SHELL##*/}" + fi + + # 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 02339ef..ee511c0 100755 --- a/test/install_script/nvm_detect_profile +++ b/test/install_script/nvm_detect_profile @@ -4,6 +4,7 @@ setup () { HOME="." SHELL='/bin/bash' NVM_ENV=testing \. ../../install.sh + NVM_TESTING='yes' touch ".bashrc" touch ".bash_profile" touch ".zshrc" @@ -96,22 +97,23 @@ fi # return an empty value if everything fails # -# It should favor .profile if other detection methods fail and file exists and -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" +# 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" fi # Otherwise, it should favor .bashrc if file exists rm ".profile" -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then die "nvm_detect_profile should have selected .bashrc" fi # Otherwise, it should favor .bash_profile if file exists rm ".bashrc" -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then die "nvm_detect_profile should have selected .bash_profile" fi