Merge pull request #564 from xcambar/nvm_detect_profile

Tests for PROFILE detection
master
Jordan Harband 2014-10-30 10:24:53 -07:00
commit 6ec62fcc79
3 changed files with 100 additions and 26 deletions

View File

@ -66,6 +66,26 @@ install_nvm_as_script() {
}
}
#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
nvm_detect_profile() {
if [ -f "$PROFILE" ]; then
echo "$PROFILE"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
elif [ -f "$HOME/.profile" ]; then
echo "$HOME/.profile"
fi
}
nvm_do_install() {
if [ -z "$METHOD" ]; then
# Autodetect install method
@ -93,39 +113,25 @@ nvm_do_install() {
echo
# Detect profile file if not specified as environment variable (eg: PROFILE=~/.myprofile).
if [ -z "$PROFILE" ]; then
if [ -f "$HOME/.bashrc" ]; then
PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.profile" ]; then
PROFILE="$HOME/.profile"
fi
fi
local NVM_PROFILE
NVM_PROFILE=$(nvm_detect_profile)
SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
if [ -z "$PROFILE" ] || [ ! -f "$PROFILE" ] ; then
if [ -z "$PROFILE" ]; then
echo "=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
echo "=> Create one of them and run this script again"
else
echo "=> Profile $PROFILE not found"
echo "=> Create it (touch $PROFILE) and run this script again"
fi
if [ -z "$NVM_PROFILE" ] ; then
echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
echo "=> Create one of them and run this script again"
echo "=> Create it (touch $NVM_PROFILE) and run this script again"
echo " OR"
echo "=> Append the following lines to the correct file yourself:"
printf "$SOURCE_STR"
echo
else
if ! grep -qc 'nvm.sh' "$PROFILE"; then
echo "=> Appending source string to $PROFILE"
printf "$SOURCE_STR\n" >> "$PROFILE"
if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
echo "=> Appending source string to $NVM_PROFILE"
printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
else
echo "=> Source string already in $PROFILE"
echo "=> Source string already in $NVM_PROFILE"
fi
fi
@ -138,7 +144,7 @@ nvm_do_install() {
# during the execution of the install script
#
nvm_reset() {
unset -f nvm_do_install nvm_has nvm_download install_nvm_as_script install_nvm_from_git nvm_reset
unset -f nvm_do_install nvm_has nvm_download install_nvm_as_script install_nvm_from_git nvm_reset nvm_detect_profile
}
[ "_$NVM_ENV" = "_testing" ] || nvm_do_install

View File

@ -0,0 +1,67 @@
#!/bin/sh
cleanup () {
unset -f setup cleanup die
unset _PROFILE
rm -f .bashrc .bash_profile .zshrc .profile test_profile > /dev/null 2>&1
}
die () { echo $@ ; cleanup ; exit 1; }
NVM_ENV=testing . ../../install.sh
setup () {
touch .bashrc
touch .bash_profile
touch .zshrc
touch .profile
touch test_profile
}
#Let's hack $HOME
HOME="."
setup
# $PROFILE points to a valid file, its path must be returned
PROFILE="test_profile"
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" = "_$PROFILE" ] || die "nvm_detect_profile didn't pick \$PROFILE"
# $PROFILE doesn't point to a valid file, its path must not be returned
PROFILE="invalid_profile"
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" != "_$PROFILE" ] || die "nvm_detect_profile shouldn't pick \$PROFILE when it's not a valid file"
# Below are tests for when $PROFILE is undefined
rm test_profile
unset PROFILE
# It should favor .bashrc if file exists
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" = "_$HOME/.bashrc" ] || die "nvm_detect_profile should have selected .bashrc"
rm .bashrc
# Otherwise, it should favor .bash_profile if file exists
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" = "_$HOME/.bash_profile" ] || die "nvm_detect_profile should have selected .bash_profile"
rm .bash_profile
# Otherwise, it should favor .zshrc if file exists
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" = "_$HOME/.zshrc" ] || die "nvm_detect_profile should have selected .zshrc"
rm .zshrc
# Otherwise, it should favor .profile if file exists
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" = "_$HOME/.profile" ] || die "nvm_detect_profile should have selected .profile"
rm .profile
# It should be empty if none is found
_PROFILE=$(nvm_detect_profile)
[ -z "$_PROFILE" ] || die "nvm_detect_profile should have echo'ed an empty value"
cleanup

View File

@ -1,7 +1,7 @@
#!/bin/sh
cleanup () {
unset -f safe_type
unset -f safe_type die cleanup
}
die () { echo $@ ; cleanup ; exit 1; }
@ -24,6 +24,7 @@ nvm_reset
! safe_type install_nvm_as_script || die 'install_nvm_as_script is still available'
! safe_type install_nvm_from_git || die 'install_nvm_from_git is still available'
! safe_type nvm_reset || die 'nvm_reset is still available'
! safe_type nvm_detect_profile || die 'nvm_detect_profile is still available'
cleanup