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