Skip to content

Commit f3de906

Browse files
marc0derclaude
andcommitted
fix: preserve global default during sdk env install
Closes #1457 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d1c4e8b commit f3de906

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/main/bash/sdkman-env.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,33 @@ function __sdkman_setup_env() {
3939
return 1
4040
fi
4141

42-
sdkman_auto_answer="true" USE="n" __sdkman_env_each_candidate "$sdkmanrc" "__sdk_install"
42+
__sdkman_env_each_candidate "$sdkmanrc" "__sdkman_install_keeping_default"
4343
__sdkman_load_env "$sdkmanrc"
4444
}
4545

46+
function __sdkman_install_keeping_default() {
47+
local candidate version current_link previous_default
48+
candidate="$1"
49+
version="$2"
50+
current_link="${SDKMAN_CANDIDATES_DIR}/${candidate}/current"
51+
previous_default=""
52+
53+
if [[ -L "$current_link" ]]; then
54+
previous_default="$(basename "$(readlink "$current_link")")"
55+
fi
56+
57+
sdkman_auto_answer="true" USE="n" __sdk_install "$candidate" "$version" || return 1
58+
59+
if [[ -n "$previous_default" && -L "$current_link" ]]; then
60+
local new_default
61+
new_default="$(basename "$(readlink "$current_link")")"
62+
if [[ "$new_default" != "$previous_default" ]]; then
63+
rm -f "$current_link"
64+
ln -s "$previous_default" "$current_link"
65+
fi
66+
fi
67+
}
68+
4669
function __sdkman_load_env() {
4770
local sdkmanrc="$1"
4871

src/test/groovy/sdkman/steps/stub_steps.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download$') {
2626
primeEndpointWithString("/hooks/post/${candidate}/${version}/${UnixUtils.inferPlatform()}", postInstallationHookSuccess())
2727
}
2828

29-
And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download with checksum "([^"]*)" using algorithm "([^"]*)"$') {
29+
And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download with a perturbing post-installation hook$') { String candidate, String version ->
30+
primeEndpointWithString("/candidates/validate/${candidate}/${version}/${UnixUtils.inferPlatform()}", "valid")
31+
primeDownloadFor(SERVICE_UP_URL, candidate, version, UnixUtils.inferPlatform())
32+
primeEndpointWithString("/hooks/post/${candidate}/${version}/${UnixUtils.inferPlatform()}", postInstallationHookPerturbing())
33+
}
34+
35+
And(~'^the candidate "([^"]*)" version "([^"]*)" is available for download with checksum "([^"]*)" using algorithm "([^"]*)"$') {
3036
String candidate, String version, String checksum, String algorithm ->
3137
primeEndpointWithString("/candidates/validate/${candidate}/${version}/${UnixUtils.inferPlatform()}", "valid")
3238
primeDownloadFor(SERVICE_UP_URL, candidate, version, UnixUtils.inferPlatform(), ["X-Sdkman-Checksum-${algorithm}": "${checksum}"])

src/test/groovy/sdkman/stubs/HookResponses.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ function __sdkman_post_installation_hook {
2020
echo "Post-installation hook failure"
2121
return 1
2222
}
23+
'''
24+
}
25+
26+
static postInstallationHookPerturbing() {
27+
'''\
28+
#!/usr/bin/env bash
29+
function __sdkman_post_installation_hook {
30+
mv -f $binary_input $zip_output
31+
unset sdkman_auto_answer
32+
echo "Post-installation hook success"
33+
return 0
34+
}
2335
'''
2436
}
2537
}

src/test/resources/features/per_project_configuration.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ Feature: Per-project configuration
3838
And the candidate "groovy" version "2.4.1" is in use
3939
And the candidate "groovy" version "2.0.5" should be the default
4040

41+
Scenario: The env install subcommand preserves the global default when the post-installation hook perturbs shell state
42+
Given the file ".sdkmanrc" exists and contains "groovy=2.4.1"
43+
And the candidate "groovy" version "2.0.5" is already installed and default
44+
And the candidate "groovy" version "2.4.1" is available for download with a perturbing post-installation hook
45+
And the system is bootstrapped
46+
When I enter "sdk env install"
47+
Then I see "Done installing!"
48+
And the candidate "groovy" version "2.4.1" is installed
49+
And the candidate "groovy" version "2.0.5" should be the default
50+
4151
Scenario: The env install subcommand is issued without an sdkman project configuration present
4252
Given the system is bootstrapped
4353
When I enter "sdk env install"

0 commit comments

Comments
 (0)