Skip to content

Commit cd22c84

Browse files
menaechmiljharb
authored andcommitted
[New] install.sh: add $ZDOTDIR to zsh search
Fixes nvm-sh#3128
1 parent d648a3b commit cd22c84

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

install.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ nvm_detect_profile() {
296296
DETECTED_PROFILE="$HOME/.bash_profile"
297297
fi
298298
elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
299-
if [ -f "$HOME/.zshrc" ]; then
300-
DETECTED_PROFILE="$HOME/.zshrc"
301-
elif [ -f "$HOME/.zprofile" ]; then
302-
DETECTED_PROFILE="$HOME/.zprofile"
299+
if [ -f "${ZDOTDIR:-${HOME}}/.zshrc" ]; then
300+
DETECTED_PROFILE="${ZDOTDIR:-${HOME}}/.zshrc"
301+
elif [ -f "${ZDOTDIR:-${HOME}}/.zprofile" ]; then
302+
DETECTED_PROFILE="${ZDOTDIR:-${HOME}}/.zprofile"
303303
fi
304304
fi
305305

test/install_script/nvm_detect_profile

+29-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
setup () {
44
HOME="."
55
NVM_ENV=testing \. ../../install.sh
6+
ZDOTDIR="$HOME/zdotdir"
7+
mkdir -p zdotdir
68
touch ".bashrc"
79
touch ".bash_profile"
810
touch ".zprofile"
911
touch ".zshrc"
1012
touch ".profile"
1113
touch "test_profile"
14+
touch "zdotdir/.zshrc"
15+
touch "zdotdir/.zprofile"
1216
}
1317

1418
cleanup () {
@@ -17,7 +21,9 @@ cleanup () {
1721
unset NVM_DETECT_PROFILE
1822
unset SHELL
1923
unset -f setup cleanup die
24+
unset ZDOTDIR
2025
rm -f ".bashrc" ".bash_profile" ".zprofile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1
26+
rm -rf zdot>&1
2127
}
2228

2329
die () { echo "$@" '$NVM_DETECT_PROFILE:' "$NVM_DETECT_PROFILE"; cleanup; exit 1; }
@@ -46,8 +52,14 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
4652
die "nvm_detect_profile ignored \$PROFILE"
4753
fi
4854

49-
# .zshrc should be detected for zsh
55+
# zdotdir/.zshrc should be detected for zsh
5056
NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; nvm_detect_profile)"
57+
if [ "$NVM_DETECT_PROFILE" != "$ZDOTDIR/.zshrc" ]; then
58+
die "nvm_detect_profile didn't pick \$ZDOTDIR/.zshrc for zsh"
59+
fi
60+
61+
# .zshrc should be detected for zsh
62+
NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; unset ZDOTDIR; nvm_detect_profile)"
5163
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
5264
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
5365
fi
@@ -58,7 +70,6 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
5870
die "nvm_detect_profile ignored \$PROFILE"
5971
fi
6072

61-
6273
#
6374
# Confirm $PROFILE is only returned when it points to a valid file
6475
#
@@ -102,16 +113,30 @@ if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
102113
die "nvm_detect_profile should have selected .bash_profile"
103114
fi
104115

105-
# Otherwise, it should favor .zprofile if file exists
116+
# Otherwise, it should favor zdotdir/.zprofile if file exists
106117
rm ".bash_profile"
107118
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
119+
if [ "$NVM_DETECT_PROFILE" != "$ZDOTDIR/.zprofile" ]; then
120+
die "nvm_detect_profile should have selected zdotdir/.zprofile"
121+
fi
122+
123+
# Otherwise, it should favor .zprofile if file exists
124+
rm "zdotdir/.zprofile"
125+
NVM_DETECT_PROFILE="$(unset SHELL; unset ZDOTDIR; nvm_detect_profile)"
108126
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zprofile" ]; then
109127
die "nvm_detect_profile should have selected .zprofile"
110128
fi
111129

112-
# Otherwise, it should favor .zshrc if file exists
130+
# Otherwise, it should favor zdotdir/.zshrc if file exists
113131
rm ".zprofile"
114132
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
133+
if [ "$NVM_DETECT_PROFILE" != "$ZDOTDIR/.zshrc" ]; then
134+
die "nvm_detect_profile should have selected zdotdir/.zshrc"
135+
fi
136+
137+
# Otherwise, it should favor .zshrc if file exists
138+
rm "zdotdir/.zshrc"
139+
NVM_DETECT_PROFILE="$(unset SHELL; unset ZDOTDIR; nvm_detect_profile)"
115140
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
116141
die "nvm_detect_profile should have selected .zshrc"
117142
fi

0 commit comments

Comments
 (0)