From ec8906b284d000b414da78855be90d7ae8152b77 Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Thu, 5 Dec 2019 19:30:26 -0800 Subject: [PATCH] [Fix] `install.sh`: do not log when user has requested no profile modifications [Tests] `install.sh`: add tests for PROFILE=/dev/null profile skip Verify that when PROFILE="/dev/null" is set: - The "Profile not found" warning is suppressed - Profile modification is skipped as expected Co-authored-by: Wes Todd Co-authored-by: Jordan Harband --- install.sh | 5 +- test/install_script/nvm_install_profile_skip | 62 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 test/install_script/nvm_install_profile_skip diff --git a/install.sh b/install.sh index 3dce7e0..6e1b2e9 100755 --- a/install.sh +++ b/install.sh @@ -428,7 +428,10 @@ nvm_do_install() { COMPLETION_STR='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion\n' BASH_OR_ZSH=false - if [ -z "${NVM_PROFILE-}" ] ; then + if [ "${PROFILE-}" = '/dev/null' ] ; then + # the user has specifically requested NOT to have nvm touch their profile + echo + elif [ -z "${NVM_PROFILE-}" ] ; then local TRIED_PROFILE if [ -n "${PROFILE}" ]; then TRIED_PROFILE="${NVM_PROFILE} (as defined in \$PROFILE), " diff --git a/test/install_script/nvm_install_profile_skip b/test/install_script/nvm_install_profile_skip new file mode 100755 index 0000000..dce6888 --- /dev/null +++ b/test/install_script/nvm_install_profile_skip @@ -0,0 +1,62 @@ +#!/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 + unset NVM_ENV METHOD PROFILE +} + +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 (to take the git path) + nvm_has() { + case "$1" in + git) return 0 ;; + xcode-select) return 1 ;; + *) return 1 ;; + esac + } + + # Mock nvm_detect_profile to return empty (no profile found) + nvm_detect_profile() { + echo "" + } +} + +setup + +# +# Test: When PROFILE="/dev/null", no "Profile not found" warning should appear +# + +OUTPUT="$(PROFILE='/dev/null' METHOD='' NVM_DIR="$(mktemp -d)" nvm_do_install 2>&1)" +if echo "$OUTPUT" | grep -q "Profile not found"; then + die "nvm_do_install should NOT show 'Profile not found' when PROFILE=/dev/null, got: $OUTPUT" +fi + +# +# Test: When PROFILE is empty/unset, the "Profile not found" warning SHOULD appear +# + +OUTPUT="$(PROFILE='' METHOD='' NVM_DIR="$(mktemp -d)" nvm_do_install 2>&1)" +if ! echo "$OUTPUT" | grep -q "Profile not found"; then + die "nvm_do_install should show 'Profile not found' when PROFILE is empty, got: $OUTPUT" +fi + +# +# Test: When PROFILE points to a non-existent file, the "Profile not found" warning SHOULD appear +# + +OUTPUT="$(PROFILE='/nonexistent/profile' METHOD='' NVM_DIR="$(mktemp -d)" nvm_do_install 2>&1)" +if ! echo "$OUTPUT" | grep -q "Profile not found"; then + die "nvm_do_install should show 'Profile not found' when PROFILE points to nonexistent file, got: $OUTPUT" +fi + +cleanup