Skip to content

Commit 470554a

Browse files
authored
Fix cached candidate server api error-handling bug in sdk update (#749)
* Fix cached candidate server api error-handling bug * Simplify code in update scripts
1 parent 4a2e6a9 commit 470554a

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

src/main/bash/sdkman-update.sh

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,11 @@ function __sdk_update() {
2121
__sdkman_echo_debug "Using candidates endpoint: $candidates_uri"
2222

2323
local fresh_candidates_csv=$(__sdkman_secure_curl_with_timeouts "$candidates_uri")
24-
local detect_html="$(echo "$fresh_candidates" | tr '[:upper:]' '[:lower:]' | grep 'html')"
25-
26-
local fresh_candidates=("")
27-
local cached_candidates=("")
28-
29-
if [[ "$zsh_shell" == 'true' ]]; then
30-
fresh_candidates=( ${(s:,:)fresh_candidates_csv} )
31-
cached_candidates=( ${(s:,:)SDKMAN_CANDIDATES_CSV} )
32-
else
33-
OLD_IFS="$IFS"
34-
IFS=","
35-
fresh_candidates=(${fresh_candidates_csv})
36-
cached_candidates=(${SDKMAN_CANDIDATES_CSV})
37-
IFS="$OLD_IFS"
38-
fi
3924

4025
__sdkman_echo_debug "Local candidates: $SDKMAN_CANDIDATES_CSV"
4126
__sdkman_echo_debug "Fetched candidates: $fresh_candidates_csv"
4227

43-
if [[ -n "$fresh_candidates_csv" && -z "$detect_html" ]]; then
28+
if [[ -n "${fresh_candidates_csv}" ]] && ! grep -iq 'html' <<< "${fresh_candidates_csv}"; then
4429
# legacy bash workaround
4530
if [[ "$bash_shell" == 'true' && "$BASH_VERSINFO" -lt 4 ]]; then
4631
__sdkman_legacy_bash_message
@@ -50,9 +35,15 @@ function __sdk_update() {
5035

5136
__sdkman_echo_debug "Fresh and cached candidate lengths: ${#fresh_candidates_csv} ${#SDKMAN_CANDIDATES_CSV}"
5237

53-
local combined_candidates diff_candidates
38+
local fresh_candidates combined_candidates diff_candidates
39+
40+
if [[ "${zsh_shell}" == 'true' ]]; then
41+
fresh_candidates=(${(s:,:)fresh_candidates_csv})
42+
else
43+
IFS=',' read -a fresh_candidates <<< "${fresh_candidates_csv}"
44+
fi
5445

55-
combined_candidates=("${fresh_candidates[@]}" "${cached_candidates[@]}")
46+
combined_candidates=("${fresh_candidates[@]}" "${SDKMAN_CANDIDATES[@]}")
5647

5748
diff_candidates=($(printf $'%s\n' "${combined_candidates[@]}" | sort | uniq -u))
5849

@@ -65,7 +56,7 @@ function __sdk_update() {
6556
__sdkman_echo_green "\nAdding new candidates(s): ${delta[*]}"
6657
fi
6758

68-
delta=("${cached_candidates[@]}" "${diff_candidates[@]}")
59+
delta=("${SDKMAN_CANDIDATES[@]}" "${diff_candidates[@]}")
6960
delta=($(printf $'%s\n' "${delta[@]}" | sort | uniq -d))
7061
if ((${#delta[@]})); then
7162
__sdkman_echo_green "\nRemoving obsolete candidates(s): ${delta[*]}"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sdkman.specs
2+
3+
import sdkman.support.SdkmanEnvSpecification
4+
5+
class CandidatesCacheUpdateFailureSpec extends SdkmanEnvSpecification {
6+
7+
static final String CANDIDATES_API = "http://localhost:8080/2"
8+
9+
static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
10+
static final String CANDIDATES_ALL_ENDPOINT = "$CANDIDATES_API/candidates/all"
11+
12+
File candidatesCache
13+
14+
def setup() {
15+
candidatesCache = new File("${sdkmanDotDirectory}/var", "candidates")
16+
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
17+
.primeWith(CANDIDATES_ALL_ENDPOINT, "echo html")
18+
sdkmanBashEnvBuilder.withConfiguration("sdkman_debug_mode", "true")
19+
}
20+
21+
void "should not update candidates if error downloading candidate list"() {
22+
given:
23+
bash = sdkmanBashEnvBuilder
24+
.withCandidates([])
25+
.build()
26+
27+
and:
28+
bash.start()
29+
30+
when:
31+
bash.execute("source $bootstrapScript")
32+
bash.execute("sdk update")
33+
34+
then:
35+
!bash.output.contains('Fresh and cached candidate lengths')
36+
}
37+
}

0 commit comments

Comments
 (0)