blob: b459afcce88d12114772ece21d489b3e4d1a0f76 [file] [log] [blame]
Xavier Cambarb9f15b02014-10-29 13:05:11 +01001#!/bin/sh
2
Xavier Cambarb9f15b02014-10-29 13:05:11 +01003setup () {
Matthew Campbell8dc53d42016-01-01 20:45:36 -05004 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 Cambarb9f15b02014-10-29 13:05:11 +010011}
12
Matthew Campbell8dc53d42016-01-01 20:45:36 -050013cleanup () {
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
21die () { echo "$@"; cleanup; exit 1; }
Xavier Cambarb9f15b02014-10-29 13:05:11 +010022
23setup
24
Matthew Campbell8dc53d42016-01-01 20:45:36 -050025#
26# Confirm profile detection via $SHELL works and that $PROFILE overrides profile detection
27#
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020028
Matthew Campbell8dc53d42016-01-01 20:45:36 -050029# .bashrc should be detected for bash
Matthew Campbell78fee862016-01-01 22:33:45 -050030NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; unset PROFILE; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050031if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
32 die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
33fi
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020034
Matthew Campbell8dc53d42016-01-01 20:45:36 -050035# $PROFILE should override .bashrc profile detection
Matthew Campbell78fee862016-01-01 22:33:45 -050036NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050037if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
38 die "nvm_detect_profile ignored \$PROFILE"
39fi
40
41# .zshrc should be detected for zsh
Matthew Campbell78fee862016-01-01 22:33:45 -050042NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh"; unset PROFILE; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050043if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
44 die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
45fi
46
47# $PROFILE should override .zshrc profile detection
Matthew Campbell78fee862016-01-01 22:33:45 -050048NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh"; PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050049if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
50 die "nvm_detect_profile ignored \$PROFILE"
51fi
David Mankin918fcb42015-09-15 23:34:02 +000052
53
Matthew Campbell8dc53d42016-01-01 20:45:36 -050054#
55# Confirm $PROFILE is only returned when it points to a valid file
56#
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020057
Matthew Campbell8dc53d42016-01-01 20:45:36 -050058# $PROFILE is a valid file
Matthew Campbell78fee862016-01-01 22:33:45 -050059NVM_DETECT_PROFILE="$(PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050060if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
61 die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file"
62fi
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020063
Matthew Campbell8dc53d42016-01-01 20:45:36 -050064# $PROFILE is not a valid file
65rm "test_profile"
Matthew Campbell78fee862016-01-01 22:33:45 -050066NVM_DETECT_PROFILE="$(PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050067if [ "$NVM_DETECT_PROFILE" = "test_profile" ]; then
68 die "nvm_detect_profile picked \$PROFILE when it was an invalid file"
69fi
David Mankin918fcb42015-09-15 23:34:02 +000070
Matthew Campbell8dc53d42016-01-01 20:45:36 -050071#
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 Cambarb9f15b02014-10-29 13:05:11 +010076
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020077# It should favor .profile if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -050078NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
79if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
80 die "nvm_detect_profile should have selected .profile ($NVM_DETECT_PROFILE) ($SHELL)"
81fi
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020082
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020083# Otherwise, it should favor .bashrc if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -050084rm ".profile"
85NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
86if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
87 die "nvm_detect_profile should have selected .bashrc"
88fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +010089
Xavier Cambarb9f15b02014-10-29 13:05:11 +010090# Otherwise, it should favor .bash_profile if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -050091rm ".bashrc"
92NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
93if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
94 die "nvm_detect_profile should have selected .bash_profile"
95fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +010096
Xavier Cambarb9f15b02014-10-29 13:05:11 +010097# Otherwise, it should favor .zshrc if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -050098rm ".bash_profile"
99NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
100if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
101 die "nvm_detect_profile should have selected .zshrc"
102fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100103
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100104# It should be empty if none is found
Matthew Campbell8dc53d42016-01-01 20:45:36 -0500105rm ".zshrc"
106NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
107if [ ! -z "$NVM_DETECT_PROFILE" ]; then
108 die "nvm_detect_profile should have returned an empty value"
109fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100110
111cleanup