Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 3 | setup () { |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 4 | HOME="." |
Qiangjun Ran | cadbbce | 2016-11-04 13:15:18 +0800 | [diff] [blame] | 5 | NVM_ENV=testing \. ../../install.sh |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 6 | touch ".bashrc" |
| 7 | touch ".bash_profile" |
| 8 | touch ".zshrc" |
| 9 | touch ".profile" |
| 10 | touch "test_profile" |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 11 | } |
| 12 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 13 | cleanup () { |
| 14 | unset HOME |
| 15 | unset NVM_ENV |
| 16 | unset NVM_DETECT_PROFILE |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 17 | unset BASH_VERSION |
| 18 | unset ZSH_VERSION |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 19 | unset -f setup cleanup die |
| 20 | rm -f ".bashrc" ".bash_profile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1 |
| 21 | } |
| 22 | |
Matthew Campbell | 4f4ff20 | 2016-01-02 11:16:50 -0500 | [diff] [blame] | 23 | die () { echo "$@" '$NVM_DETECT_PROFILE:' "$NVM_DETECT_PROFILE"; cleanup; exit 1; } |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 24 | |
| 25 | setup |
| 26 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 27 | # |
| 28 | # Confirm profile detection via $SHELL works and that $PROFILE overrides profile detection |
| 29 | # |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 30 | |
Spike Grobstein | 9854928 | 2018-05-03 08:22:07 -0700 | [diff] [blame] | 31 | # setting $PROFILE to /dev/null should return no detected profile |
| 32 | NVM_DETECT_PROFILE="$(PROFILE='/dev/null'; nvm_detect_profile)" |
| 33 | if [ -n "$NVM_DETECT_PROFILE" ]; then |
| 34 | die "nvm_detect_profile still detected a profile even though PROFILE=/dev/null" |
| 35 | fi |
| 36 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 37 | # .bashrc should be detected for bash |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 38 | NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 39 | if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then |
| 40 | die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash" |
| 41 | fi |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 42 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 43 | # $PROFILE should override .bashrc profile detection |
Matthew Campbell | 78fee86 | 2016-01-01 22:33:45 -0500 | [diff] [blame] | 44 | NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; PROFILE="test_profile"; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 45 | if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then |
| 46 | die "nvm_detect_profile ignored \$PROFILE" |
| 47 | fi |
| 48 | |
| 49 | # .zshrc should be detected for zsh |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 50 | NVM_DETECT_PROFILE="$(ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 51 | if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then |
| 52 | die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh" |
| 53 | fi |
| 54 | |
| 55 | # $PROFILE should override .zshrc profile detection |
Matthew Campbell | 78fee86 | 2016-01-01 22:33:45 -0500 | [diff] [blame] | 56 | NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh"; PROFILE="test_profile"; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 57 | if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then |
Peter Dave Hello | 7505710 | 2018-02-26 02:20:21 +0800 | [diff] [blame] | 58 | die "nvm_detect_profile ignored \$PROFILE" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 59 | fi |
David Mankin | 918fcb4 | 2015-09-15 23:34:02 +0000 | [diff] [blame] | 60 | |
| 61 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 62 | # |
| 63 | # Confirm $PROFILE is only returned when it points to a valid file |
| 64 | # |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 65 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 66 | # $PROFILE is a valid file |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 67 | NVM_DETECT_PROFILE="$(PROFILE="test_profile"; unset ZSH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 68 | if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then |
| 69 | die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file" |
| 70 | fi |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 71 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 72 | # $PROFILE is not a valid file |
| 73 | rm "test_profile" |
Matthew Campbell | 78fee86 | 2016-01-01 22:33:45 -0500 | [diff] [blame] | 74 | NVM_DETECT_PROFILE="$(PROFILE="test_profile"; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 75 | if [ "$NVM_DETECT_PROFILE" = "test_profile" ]; then |
| 76 | die "nvm_detect_profile picked \$PROFILE when it was an invalid file" |
| 77 | fi |
David Mankin | 918fcb4 | 2015-09-15 23:34:02 +0000 | [diff] [blame] | 78 | |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 79 | # |
| 80 | # When profile detection fails via both $PROFILE and $SHELL, profile detection should select based on the existence of |
| 81 | # one of the following files is the following order: .profile, .bashrc, .bash_profile, .zshrc and |
| 82 | # return an empty value if everything fails |
| 83 | # |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 84 | |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 85 | # It should favor .profile if file exists |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 86 | NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 87 | if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then |
Matthew Campbell | 4f4ff20 | 2016-01-02 11:16:50 -0500 | [diff] [blame] | 88 | die "nvm_detect_profile should have selected .profile" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 89 | fi |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 90 | |
Cristian Consonni | c37dbbe | 2015-07-19 02:01:06 +0200 | [diff] [blame] | 91 | # Otherwise, it should favor .bashrc if file exists |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 92 | rm ".profile" |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 93 | NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 94 | if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then |
| 95 | die "nvm_detect_profile should have selected .bashrc" |
| 96 | fi |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 97 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 98 | # Otherwise, it should favor .bash_profile if file exists |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 99 | rm ".bashrc" |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 100 | NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 101 | if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then |
| 102 | die "nvm_detect_profile should have selected .bash_profile" |
| 103 | fi |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 104 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 105 | # Otherwise, it should favor .zshrc if file exists |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 106 | rm ".bash_profile" |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 107 | NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 108 | if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then |
| 109 | die "nvm_detect_profile should have selected .zshrc" |
| 110 | fi |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 111 | |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 112 | # It should be empty if none is found |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 113 | rm ".zshrc" |
Brian M. Jemilo II | 68fe26a | 2018-04-09 14:54:28 -0500 | [diff] [blame] | 114 | NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" |
Matthew Campbell | 8dc53d4 | 2016-01-01 20:45:36 -0500 | [diff] [blame] | 115 | if [ ! -z "$NVM_DETECT_PROFILE" ]; then |
| 116 | die "nvm_detect_profile should have returned an empty value" |
| 117 | fi |
Xavier Cambar | b9f15b0 | 2014-10-29 13:05:11 +0100 | [diff] [blame] | 118 | |
| 119 | cleanup |