blob: 54815ec7ecd7e94d4f0cdc4a848efe778f811f2f [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="."
Qiangjun Rancadbbce2016-11-04 13:15:18 +08005 NVM_ENV=testing \. ../../install.sh
Matthew Campbell8dc53d42016-01-01 20:45:36 -05006 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
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -050017 unset BASH_VERSION
18 unset ZSH_VERSION
Matthew Campbell8dc53d42016-01-01 20:45:36 -050019 unset -f setup cleanup die
20 rm -f ".bashrc" ".bash_profile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1
21}
22
Matthew Campbell4f4ff202016-01-02 11:16:50 -050023die () { echo "$@" '$NVM_DETECT_PROFILE:' "$NVM_DETECT_PROFILE"; cleanup; exit 1; }
Xavier Cambarb9f15b02014-10-29 13:05:11 +010024
25setup
26
Matthew Campbell8dc53d42016-01-01 20:45:36 -050027#
28# Confirm profile detection via $SHELL works and that $PROFILE overrides profile detection
29#
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020030
Spike Grobstein98549282018-05-03 08:22:07 -070031# setting $PROFILE to /dev/null should return no detected profile
32NVM_DETECT_PROFILE="$(PROFILE='/dev/null'; nvm_detect_profile)"
33if [ -n "$NVM_DETECT_PROFILE" ]; then
34 die "nvm_detect_profile still detected a profile even though PROFILE=/dev/null"
35fi
36
Matthew Campbell8dc53d42016-01-01 20:45:36 -050037# .bashrc should be detected for bash
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -050038NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050039if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
40 die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
41fi
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020042
Matthew Campbell8dc53d42016-01-01 20:45:36 -050043# $PROFILE should override .bashrc profile detection
Matthew Campbell78fee862016-01-01 22:33:45 -050044NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050045if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
46 die "nvm_detect_profile ignored \$PROFILE"
47fi
48
49# .zshrc should be detected for zsh
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -050050NVM_DETECT_PROFILE="$(ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050051if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
52 die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
53fi
54
55# $PROFILE should override .zshrc profile detection
Matthew Campbell78fee862016-01-01 22:33:45 -050056NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh"; PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050057if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
Peter Dave Hello75057102018-02-26 02:20:21 +080058 die "nvm_detect_profile ignored \$PROFILE"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050059fi
David Mankin918fcb42015-09-15 23:34:02 +000060
61
Matthew Campbell8dc53d42016-01-01 20:45:36 -050062#
63# Confirm $PROFILE is only returned when it points to a valid file
64#
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020065
Matthew Campbell8dc53d42016-01-01 20:45:36 -050066# $PROFILE is a valid file
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -050067NVM_DETECT_PROFILE="$(PROFILE="test_profile"; unset ZSH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050068if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
69 die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file"
70fi
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020071
Matthew Campbell8dc53d42016-01-01 20:45:36 -050072# $PROFILE is not a valid file
73rm "test_profile"
Matthew Campbell78fee862016-01-01 22:33:45 -050074NVM_DETECT_PROFILE="$(PROFILE="test_profile"; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050075if [ "$NVM_DETECT_PROFILE" = "test_profile" ]; then
76 die "nvm_detect_profile picked \$PROFILE when it was an invalid file"
77fi
David Mankin918fcb42015-09-15 23:34:02 +000078
Matthew Campbell8dc53d42016-01-01 20:45:36 -050079#
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 Cambarb9f15b02014-10-29 13:05:11 +010084
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020085# It should favor .profile if file exists
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -050086NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050087if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
Matthew Campbell4f4ff202016-01-02 11:16:50 -050088 die "nvm_detect_profile should have selected .profile"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050089fi
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020090
Cristian Consonnic37dbbe2015-07-19 02:01:06 +020091# Otherwise, it should favor .bashrc if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -050092rm ".profile"
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -050093NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -050094if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
95 die "nvm_detect_profile should have selected .bashrc"
96fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +010097
Xavier Cambarb9f15b02014-10-29 13:05:11 +010098# Otherwise, it should favor .bash_profile if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -050099rm ".bashrc"
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -0500100NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -0500101if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
102 die "nvm_detect_profile should have selected .bash_profile"
103fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100104
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100105# Otherwise, it should favor .zshrc if file exists
Matthew Campbell8dc53d42016-01-01 20:45:36 -0500106rm ".bash_profile"
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -0500107NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -0500108if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
109 die "nvm_detect_profile should have selected .zshrc"
110fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100111
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100112# It should be empty if none is found
Matthew Campbell8dc53d42016-01-01 20:45:36 -0500113rm ".zshrc"
Brian M. Jemilo II68fe26a2018-04-09 14:54:28 -0500114NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
Matthew Campbell8dc53d42016-01-01 20:45:36 -0500115if [ ! -z "$NVM_DETECT_PROFILE" ]; then
116 die "nvm_detect_profile should have returned an empty value"
117fi
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100118
119cleanup