[Fix] install.sh: ignore commented-out profile source lines
`nvm_do_install` decided whether the profile already loads nvm, and its bash completion, by grepping for `/nvm.sh` and `$NVM_DIR/bash_completion` anywhere in it, so a commented-out line, the usual way to disable nvm, counted as present, and reinstalling left the shell with no nvm. Only count a line whose first non-blank character isn't `#`. Fixes #1889. Signed-off-by: Vaibhav Cheruvu <vaibhavsharma.c@gmail.com> Co-authored-by: Vaibhav Cheruvu <vaibhavsharma.c@gmail.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
1 parent
7ab48a653b
commit
db03e5e1c3
2 files changed
+65
-2
No files matched your search
+2
-2
@@ -527,14 +527,14 @@ nvm_do_install() {
|
|||||||
if nvm_profile_is_bash_or_zsh "${NVM_PROFILE-}"; then
|
if nvm_profile_is_bash_or_zsh "${NVM_PROFILE-}"; then
|
||||||
BASH_OR_ZSH=true
|
BASH_OR_ZSH=true
|
||||||
fi
|
fi
|
||||||
if ! command grep -qc '/nvm.sh' "$NVM_PROFILE"; then
|
if ! command grep -qc '^[[:space:]]*[^#[:space:]].*/nvm\.sh' "$NVM_PROFILE"; then
|
||||||
nvm_echo "=> Appending nvm source string to $NVM_PROFILE"
|
nvm_echo "=> Appending nvm source string to $NVM_PROFILE"
|
||||||
command printf '%b' "${SOURCE_STR}" >> "$NVM_PROFILE"
|
command printf '%b' "${SOURCE_STR}" >> "$NVM_PROFILE"
|
||||||
else
|
else
|
||||||
nvm_echo "=> nvm source string already in ${NVM_PROFILE}"
|
nvm_echo "=> nvm source string already in ${NVM_PROFILE}"
|
||||||
fi
|
fi
|
||||||
# shellcheck disable=SC2016
|
# shellcheck disable=SC2016
|
||||||
if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then
|
if ${BASH_OR_ZSH} && ! command grep -qc '^[[:space:]]*[^#[:space:]].*\$NVM_DIR/bash_completion' "$NVM_PROFILE"; then
|
||||||
nvm_echo "=> Appending bash_completion source string to $NVM_PROFILE"
|
nvm_echo "=> Appending bash_completion source string to $NVM_PROFILE"
|
||||||
if [ -n "$(command tail -c 1 "$NVM_PROFILE")" ]; then
|
if [ -n "$(command tail -c 1 "$NVM_PROFILE")" ]; then
|
||||||
nvm_echo >> "$NVM_PROFILE"
|
nvm_echo >> "$NVM_PROFILE"
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
die () { echo "$@" ; cleanup ; exit 1; }
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
unset -f install_nvm_from_git install_nvm_as_script nvm_detect_profile nvm_has
|
||||||
|
unset -f setup cleanup die assert_install
|
||||||
|
unset NVM_ENV METHOD PROFILE
|
||||||
|
rm -rf "${TMPDIR_FOR_TEST-}" 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
NVM_ENV=testing \. ../../install.sh
|
||||||
|
|
||||||
|
# Mock installation functions to do nothing
|
||||||
|
install_nvm_from_git() { :; }
|
||||||
|
install_nvm_as_script() { :; }
|
||||||
|
|
||||||
|
# Mock nvm_has to return true for git
|
||||||
|
nvm_has() {
|
||||||
|
case "$1" in
|
||||||
|
git) return 0 ;;
|
||||||
|
*) return 1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
nvm_detect_profile() {
|
||||||
|
echo "${TEST_PROFILE}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TMPDIR_FOR_TEST="$(mktemp -d)"
|
||||||
|
TEST_PROFILE="${TMPDIR_FOR_TEST}/.bashrc"
|
||||||
|
mkdir -p "${TMPDIR_FOR_TEST}/nvm"
|
||||||
|
: > "${TMPDIR_FOR_TEST}/nvm/nvm.sh"
|
||||||
|
|
||||||
|
# `nvm_do_install` unsets every `nvm_` function when it finishes, so each run sources the installer and mocks afresh.
|
||||||
|
# args: the profile's content, then the message the install must print about it
|
||||||
|
assert_install() {
|
||||||
|
setup
|
||||||
|
printf '%s\n' "$1" > "${TEST_PROFILE}"
|
||||||
|
OUTPUT="$(PROFILE='' METHOD='' NVM_DIR="${TMPDIR_FOR_TEST}/nvm" nvm_do_install 2>&1)"
|
||||||
|
case "${OUTPUT}" in
|
||||||
|
*"$2"*) ;;
|
||||||
|
*) die "with a profile of >$1<, expected >$2<; got >${OUTPUT}<" ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
TAB="$(printf '\t')"
|
||||||
|
|
||||||
|
# a commented-out line loads nothing, however it is indented
|
||||||
|
assert_install '# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' 'Appending nvm source string'
|
||||||
|
assert_install ' # [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' 'Appending nvm source string'
|
||||||
|
assert_install "${TAB}"'# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' 'Appending nvm source string'
|
||||||
|
|
||||||
|
# an active line still counts, indented or not
|
||||||
|
assert_install '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' 'nvm source string already in'
|
||||||
|
assert_install ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' 'nvm source string already in'
|
||||||
|
|
||||||
|
# the same for the bash_completion line
|
||||||
|
assert_install ' # [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' 'Appending bash_completion source string'
|
||||||
|
|
||||||
|
cleanup
|
||||||
Reference in new issue
Block a user