Merge pull request #564 from xcambar/nvm_detect_profile
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Tests for PROFILE detection
authored by Jordan Harband · committed by Jordan Harband · parents fc17aaa, b9f15b0 · Browse files · 3 files·+100 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+100 added−26 removed
- tests:+69 added−1 removed
- other:+31 added−25 removed
Provenance
- Commit
6ec62fc- Tree
691adec- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent 1
fc17aaa- Parent 2
b9f15b0
3 files changed+100 added−26 removed
added:test/install_script/nvm_detect_profile+67 added
1+#!/bin/sh2+3+cleanup () {4+ unset -f setup cleanup die5+ unset _PROFILE6+ rm -f .bashrc .bash_profile .zshrc .profile test_profile > /dev/null 2>&17+}8+die () { echo $@ ; cleanup ; exit 1; }9+10+NVM_ENV=testing . ../../install.sh11+12+setup () {13+ touch .bashrc14+ touch .bash_profile15+ touch .zshrc16+ touch .profile17+ touch test_profile18+}19+20+#Let's hack $HOME21+HOME="."22+23+setup24+25+26+# $PROFILE points to a valid file, its path must be returned27+PROFILE="test_profile"28+_PROFILE=$(nvm_detect_profile)29+[ "_$_PROFILE" = "_$PROFILE" ] || die "nvm_detect_profile didn't pick \$PROFILE"30+31+# $PROFILE doesn't point to a valid file, its path must not be returned32+PROFILE="invalid_profile"33+_PROFILE=$(nvm_detect_profile)34+[ "_$_PROFILE" != "_$PROFILE" ] || die "nvm_detect_profile shouldn't pick \$PROFILE when it's not a valid file"35+36+37+# Below are tests for when $PROFILE is undefined38+rm test_profile39+unset PROFILE40+41+# It should favor .bashrc if file exists42+_PROFILE=$(nvm_detect_profile)43+[ "_$_PROFILE" = "_$HOME/.bashrc" ] || die "nvm_detect_profile should have selected .bashrc"44+45+rm .bashrc46+# Otherwise, it should favor .bash_profile if file exists47+_PROFILE=$(nvm_detect_profile)48+[ "_$_PROFILE" = "_$HOME/.bash_profile" ] || die "nvm_detect_profile should have selected .bash_profile"49+50+rm .bash_profile51+# Otherwise, it should favor .zshrc if file exists52+_PROFILE=$(nvm_detect_profile)53+[ "_$_PROFILE" = "_$HOME/.zshrc" ] || die "nvm_detect_profile should have selected .zshrc"54+55+rm .zshrc56+# Otherwise, it should favor .profile if file exists57+_PROFILE=$(nvm_detect_profile)58+[ "_$_PROFILE" = "_$HOME/.profile" ] || die "nvm_detect_profile should have selected .profile"59+60+rm .profile61+# It should be empty if none is found62+_PROFILE=$(nvm_detect_profile)63+[ -z "$_PROFILE" ] || die "nvm_detect_profile should have echo'ed an empty value"64+65+66+cleanup67+modified:test/install_script/nvm_reset+2 added-1 removed
1 #!/bin/sh2 3 cleanup () {4- unset -f safe_type4+ unset -f safe_type die cleanup5 }6 die () { echo $@ ; cleanup ; exit 1; }7 8 NVM_ENV=testing . ../../install.sh14 unmodified lines23 ! safe_type nvm_download || die 'nvm_download is still available'24 ! safe_type install_nvm_as_script || die 'install_nvm_as_script is still available'25 ! safe_type install_nvm_from_git || die 'install_nvm_from_git is still available'26 ! safe_type nvm_reset || die 'nvm_reset is still available'27+! safe_type nvm_detect_profile || die 'nvm_detect_profile is still available'28 29 cleanup30 modified:install.sh+31 added-25 removed
64 unmodified lines6566676869707172737475767778798081828384858687888990919219 unmodified lines112113114115969798991001011021031041051061071161171181191201111121131141151161171181211221231241251261271281291241251261301311321331281341351361371384 unmodified lines14314414514614114714814915064 unmodified lines return 1 }}
## 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 if nvm_has "git"; then19 unmodified lines fi
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
echo "=> Close and reopen your terminal to start using nvm"4 unmodified lines# Unsets the various functions defined# 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