From b2a43776944b1995b24aaf0532c4dd7c9887eebd Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Sat, 18 Jul 2020 11:11:41 -0700 Subject: [PATCH 1/5] [Fix] `install`: detect user shell and try shellrc file first --- install.sh | 13 +++++++++- test/install_script/nvm_detect_profile | 26 ++++++++++++++----- .../nvm_install_with_aliased_dot | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/install.sh b/install.sh index 0ea9a0a..99f69a6 100755 --- a/install.sh +++ b/install.sh @@ -233,7 +233,18 @@ nvm_detect_profile() { local DETECTED_PROFILE DETECTED_PROFILE='' - if [ -n "${BASH_VERSION-}" ]; then + # Detect the user's login shell + local DETECTED_SHELL + local SHELLRC + + DETECTED_SHELL="${SHELL##*/}" + SHELLRC="$HOME/.${DETECTED_SHELL}rc" + + if [ -n "${DETECTED_SHELL}" ]; then + if [ -f "$SHELLRC" ]; then + DETECTED_PROFILE="$SHELLRC" + fi + elif [ -n "${BASH_VERSION-}" ]; then if [ -f "$HOME/.bashrc" ]; then DETECTED_PROFILE="$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then diff --git a/test/install_script/nvm_detect_profile b/test/install_script/nvm_detect_profile index 54815ec..02339ef 100755 --- a/test/install_script/nvm_detect_profile +++ b/test/install_script/nvm_detect_profile @@ -2,6 +2,7 @@ setup () { HOME="." + SHELL='/bin/bash' NVM_ENV=testing \. ../../install.sh touch ".bashrc" touch ".bash_profile" @@ -12,6 +13,7 @@ setup () { cleanup () { unset HOME + unset SHELL unset NVM_ENV unset NVM_DETECT_PROFILE unset BASH_VERSION @@ -34,6 +36,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 +54,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,29 +96,29 @@ fi # return an empty value if everything fails # -# It should favor .profile if file exists -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +# 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)" 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; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; 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; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then die "nvm_detect_profile should have selected .bash_profile" 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 b8f05a5..d3f396f 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 () { shopt -s expand_aliases From d01b62b5940a36ad32c28937f95f67b82862b884 Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Sat, 18 Jul 2020 11:11:41 -0700 Subject: [PATCH 2/5] [Fix] `install`: detect user shell and try shellrc file first --- install.sh | 13 +++++++++- test/install_script/nvm_detect_profile | 26 ++++++++++++++----- .../nvm_install_with_aliased_dot | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/install.sh b/install.sh index 0ea9a0a..99f69a6 100755 --- a/install.sh +++ b/install.sh @@ -233,7 +233,18 @@ nvm_detect_profile() { local DETECTED_PROFILE DETECTED_PROFILE='' - if [ -n "${BASH_VERSION-}" ]; then + # Detect the user's login shell + local DETECTED_SHELL + local SHELLRC + + DETECTED_SHELL="${SHELL##*/}" + SHELLRC="$HOME/.${DETECTED_SHELL}rc" + + if [ -n "${DETECTED_SHELL}" ]; then + if [ -f "$SHELLRC" ]; then + DETECTED_PROFILE="$SHELLRC" + fi + elif [ -n "${BASH_VERSION-}" ]; then if [ -f "$HOME/.bashrc" ]; then DETECTED_PROFILE="$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then diff --git a/test/install_script/nvm_detect_profile b/test/install_script/nvm_detect_profile index 54815ec..02339ef 100755 --- a/test/install_script/nvm_detect_profile +++ b/test/install_script/nvm_detect_profile @@ -2,6 +2,7 @@ setup () { HOME="." + SHELL='/bin/bash' NVM_ENV=testing \. ../../install.sh touch ".bashrc" touch ".bash_profile" @@ -12,6 +13,7 @@ setup () { cleanup () { unset HOME + unset SHELL unset NVM_ENV unset NVM_DETECT_PROFILE unset BASH_VERSION @@ -34,6 +36,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 +54,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,29 +96,29 @@ fi # return an empty value if everything fails # -# It should favor .profile if file exists -NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" +# 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)" 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; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; 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; nvm_detect_profile)" +NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)" if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then die "nvm_detect_profile should have selected .bash_profile" 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 b8f05a5..d3f396f 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 () { shopt -s expand_aliases From 3cdedc97d8698b1c1d1b49d6f9724a835c159503 Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Mon, 20 Jul 2020 19:04:03 -0700 Subject: [PATCH 3/5] condense logic --- install.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index 99f69a6..5170b18 100755 --- a/install.sh +++ b/install.sh @@ -240,10 +240,8 @@ nvm_detect_profile() { DETECTED_SHELL="${SHELL##*/}" SHELLRC="$HOME/.${DETECTED_SHELL}rc" - if [ -n "${DETECTED_SHELL}" ]; then - if [ -f "$SHELLRC" ]; then - DETECTED_PROFILE="$SHELLRC" - fi + if [ -n "${DETECTED_SHELL}" ] && [ -f "$SHELLRC" ]; then + DETECTED_PROFILE="$SHELLRC" elif [ -n "${BASH_VERSION-}" ]; then if [ -f "$HOME/.bashrc" ]; then DETECTED_PROFILE="$HOME/.bashrc" From 9ffd330f59c96a7e70662837d21aa789a4e1dc1f Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Sat, 8 Aug 2020 18:19:52 -0700 Subject: [PATCH 4/5] 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 From 27432616bac1ab19b7d2751fe1280fd5d591b846 Mon Sep 17 00:00:00 2001 From: Jim Heald Date: Sat, 8 Aug 2020 18:38:42 -0700 Subject: [PATCH 5/5] unneccessary check for empty string --- install.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/install.sh b/install.sh index e70b49b..f342855 100755 --- a/install.sh +++ b/install.sh @@ -246,9 +246,7 @@ nvm_detect_profile() { USER_SHELL="$SHELL" fi - if [ -n "$SHELL" ]; then - USER_SHELL_NAME="${USER_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