fix: preserve global default during sdk env install

Closes #1457

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Marco Vermeulen
2026-04-28 11:37:10 +01:00
committed by Marco Vermeulen
parent d1c4e8b1d8
commit f3de90665f
4 changed files with 53 additions and 2 deletions

View File

@@ -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"

View File

@@ -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}"])

View File

@@ -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
}
'''
}
}

View File

@@ -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"