diff --git a/src/main/bash/sdkman-env.sh b/src/main/bash/sdkman-env.sh index 28b541c6..0c29bbb7 100644 --- a/src/main/bash/sdkman-env.sh +++ b/src/main/bash/sdkman-env.sh @@ -39,10 +39,33 @@ function __sdkman_setup_env() { return 1 fi - sdkman_auto_answer="true" USE="n" __sdkman_env_each_candidate "$sdkmanrc" "__sdk_install" + __sdkman_env_each_candidate "$sdkmanrc" "__sdkman_install_keeping_default" __sdkman_load_env "$sdkmanrc" } +function __sdkman_install_keeping_default() { + local candidate version current_link previous_default + candidate="$1" + version="$2" + current_link="${SDKMAN_CANDIDATES_DIR}/${candidate}/current" + previous_default="" + + if [[ -L "$current_link" ]]; then + previous_default="$(basename "$(readlink "$current_link")")" + fi + + sdkman_auto_answer="true" USE="n" __sdk_install "$candidate" "$version" || return 1 + + if [[ -n "$previous_default" && -L "$current_link" ]]; then + local new_default + new_default="$(basename "$(readlink "$current_link")")" + if [[ "$new_default" != "$previous_default" ]]; then + rm -f "$current_link" + ln -s "$previous_default" "$current_link" + fi + fi +} + function __sdkman_load_env() { local sdkmanrc="$1" diff --git a/src/test/groovy/sdkman/steps/stub_steps.groovy b/src/test/groovy/sdkman/steps/stub_steps.groovy index f2594366..9ceeb193 100644 --- a/src/test/groovy/sdkman/steps/stub_steps.groovy +++ b/src/test/groovy/sdkman/steps/stub_steps.groovy @@ -26,7 +26,13 @@ And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download$') { primeEndpointWithString("/hooks/post/${candidate}/${version}/${UnixUtils.inferPlatform()}", postInstallationHookSuccess()) } -And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download with checksum "([^"]*)" using algorithm "([^"]*)"$') { +And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download with a perturbing post-installation hook$') { String candidate, String version -> + primeEndpointWithString("/candidates/validate/${candidate}/${version}/${UnixUtils.inferPlatform()}", "valid") + primeDownloadFor(SERVICE_UP_URL, candidate, version, UnixUtils.inferPlatform()) + primeEndpointWithString("/hooks/post/${candidate}/${version}/${UnixUtils.inferPlatform()}", postInstallationHookPerturbing()) +} + +And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download with checksum "([^"]*)" using algorithm "([^"]*)"$') { String candidate, String version, String checksum, String algorithm -> primeEndpointWithString("/candidates/validate/${candidate}/${version}/${UnixUtils.inferPlatform()}", "valid") primeDownloadFor(SERVICE_UP_URL, candidate, version, UnixUtils.inferPlatform(), ["X-Sdkman-Checksum-${algorithm}": "${checksum}"]) diff --git a/src/test/groovy/sdkman/stubs/HookResponses.groovy b/src/test/groovy/sdkman/stubs/HookResponses.groovy index 8757a564..951d797f 100644 --- a/src/test/groovy/sdkman/stubs/HookResponses.groovy +++ b/src/test/groovy/sdkman/stubs/HookResponses.groovy @@ -20,6 +20,18 @@ function __sdkman_post_installation_hook { echo "Post-installation hook failure" return 1 } +''' + } + + static postInstallationHookPerturbing() { + '''\ +#!/usr/bin/env bash +function __sdkman_post_installation_hook { + mv -f $binary_input $zip_output + unset sdkman_auto_answer + echo "Post-installation hook success" + return 0 +} ''' } } diff --git a/src/test/resources/features/per_project_configuration.feature b/src/test/resources/features/per_project_configuration.feature index d932fb78..7af24006 100644 --- a/src/test/resources/features/per_project_configuration.feature +++ b/src/test/resources/features/per_project_configuration.feature @@ -38,6 +38,16 @@ Feature: Per-project configuration And the candidate "groovy" version "2.4.1" is in use And the candidate "groovy" version "2.0.5" should be the default + Scenario: The env install subcommand preserves the global default when the post-installation hook perturbs shell state + Given the file ".sdkmanrc" exists and contains "groovy=2.4.1" + And the candidate "groovy" version "2.0.5" is already installed and default + And the candidate "groovy" version "2.4.1" is available for download with a perturbing post-installation hook + And the system is bootstrapped + When I enter "sdk env install" + Then I see "Done installing!" + And the candidate "groovy" version "2.4.1" is installed + And the candidate "groovy" version "2.0.5" should be the default + Scenario: The env install subcommand is issued without an sdkman project configuration present Given the system is bootstrapped When I enter "sdk env install"