Skip to content

Commit 6540277

Browse files
author
Oliver Weiler
authored
chore: Improve sdk env error handling (#1193)
1 parent 4b172a6 commit 6540277

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/main/bash/sdkman-env.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,25 @@ function __sdkman_load_env() {
5454
return 1
5555
fi
5656

57-
__sdkman_env_each_candidate "$sdkmanrc" "__sdk_use" &&
57+
__sdkman_env_each_candidate "$sdkmanrc" "__sdkman_check_and_use" &&
5858
SDKMAN_ENV=$PWD
5959
}
6060

61+
function __sdkman_check_and_use() {
62+
local -r candidate=$1
63+
local -r version=$2
64+
65+
if [[ ! -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}" ]]; then
66+
__sdkman_echo_red "Stop! $candidate $version is not installed."
67+
echo ""
68+
__sdkman_echo_yellow "Run 'sdk env install' to install it."
69+
70+
return 1
71+
fi
72+
73+
__sdk_use "$candidate" "$version"
74+
}
75+
6176
function __sdkman_create_env_file() {
6277
local sdkmanrc="$1"
6378

@@ -130,7 +145,7 @@ function __sdkman_env_each_candidate() {
130145
return 1
131146
fi
132147

133-
$func "${normalised_line%=*}" "${normalised_line#*=}"
148+
$func "${normalised_line%=*}" "${normalised_line#*=}" || return
134149
done < "$filepath"
135150
}
136151

src/test/groovy/sdkman/specs/EnvCommandSpec.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,29 @@ class EnvCommandSpec extends SdkmanEnvSpecification {
357357
}
358358
}
359359

360+
def "should issue an error when .sdkmanrc contains a candidate version which is not installed"() {
361+
given:
362+
bash = sdkmanBashEnvBuilder
363+
.withVersionCache("x.y.z")
364+
.withOfflineMode(true)
365+
.build()
366+
367+
new File(bash.workDir, ".sdkmanrc").text = "groovy=2.4.1"
368+
369+
bash.start()
370+
bash.execute("source $bootstrapScript")
371+
372+
when:
373+
bash.execute("sdk env")
374+
375+
then:
376+
verifyAll(bash) {
377+
status == 1
378+
output.contains("Stop! groovy 2.4.1 is not installed.")
379+
output.contains("Run 'sdk env install' to install it.")
380+
}
381+
}
382+
360383
def "should support blank lines, comments and inline comments"() {
361384
given:
362385
new FileTreeBuilder(candidatesDirectory).with {

0 commit comments

Comments
 (0)