diff --git a/src/main/bash/sdkman-init.sh b/src/main/bash/sdkman-init.sh index c6721980..4b483d3d 100644 --- a/src/main/bash/sdkman-init.sh +++ b/src/main/bash/sdkman-init.sh @@ -147,19 +147,27 @@ if [[ -z "$sdkman_curl_max_time" ]]; then sdkman_curl_max_time=10; fi # determine if up to date SDKMAN_VERSION_FILE="${SDKMAN_DIR}/var/version" if [[ "$sdkman_beta_channel" != "true" && -f "$SDKMAN_VERSION_FILE" && -z "$(find "$SDKMAN_VERSION_FILE" -mmin +$((60*24)))" ]]; then + __sdkman_echo_debug "Not refreshing version cache now..." SDKMAN_REMOTE_VERSION=$(cat "$SDKMAN_VERSION_FILE") else + __sdkman_echo_debug "Version cache needs updating..." if [[ "$sdkman_beta_channel" == "true" ]]; then - SDKMAN_REMOTE_VERSION=$(__sdkman_secure_curl_with_timeouts "${SDKMAN_LEGACY_API}/candidates/app/beta") + __sdkman_echo_debug "Refreshing version cache with BETA version." + VERSION_URL="${SDKMAN_LEGACY_API}/candidates/app/beta" else - SDKMAN_REMOTE_VERSION=$(__sdkman_secure_curl_with_timeouts "${SDKMAN_LEGACY_API}/candidates/app/stable") + __sdkman_echo_debug "Refreshing version cache with STABLE version." + VERSION_URL="${SDKMAN_LEGACY_API}/candidates/app/stable" fi - DETECT_HTML="$(echo "$SDKMAN_REMOTE_VERSION" | tr '[:upper:]' '[:lower:]' | grep 'html')" - if [[ -z "$SDKMAN_REMOTE_VERSION" || -n "$DETECT_HTML" ]]; then + + SDKMAN_REMOTE_VERSION=$(__sdkman_secure_curl_with_timeouts "$VERSION_URL") + if [[ -z "$SDKMAN_REMOTE_VERSION" || -n "$(echo "$SDKMAN_REMOTE_VERSION" | tr '[:upper:]' '[:lower:]' | grep 'html')" ]]; then + __sdkman_echo_debug "Version information corrupt or empty! Ignoring: $SDKMAN_REMOTE_VERSION" SDKMAN_REMOTE_VERSION="$SDKMAN_VERSION" + else - echo ${SDKMAN_REMOTE_VERSION} > "$SDKMAN_VERSION_FILE" + __sdkman_echo_debug "Overwriting version cache with: $SDKMAN_REMOTE_VERSION" + echo "${SDKMAN_REMOTE_VERSION}" > "$SDKMAN_VERSION_FILE" fi fi diff --git a/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy b/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy index 36a2469f..276256b9 100644 --- a/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy +++ b/src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy @@ -12,7 +12,7 @@ class SdkmanBashEnvBuilder { //optional fields with sensible defaults private Optional curlStub = Optional.empty() private List candidates = ['groovy', 'grails', 'java'] - private List availableCandidates = candidates + private List candidatesCache = candidates private boolean offlineMode = false private String broadcast = "This is a LIVE broadcast!" private String legacyService = "http://localhost:8080/1" @@ -20,7 +20,7 @@ class SdkmanBashEnvBuilder { private String sdkmanVersion = "5.0.0" private String jdkHome = "/path/to/my/jdk" private String httpProxy - private String versionFile + private String versionCache Map config = [ sdkman_auto_answer : 'false', @@ -47,8 +47,8 @@ class SdkmanBashEnvBuilder { this } - SdkmanBashEnvBuilder withAvailableCandidates(List candidates) { - this.availableCandidates = candidates + SdkmanBashEnvBuilder withCandidatesCache(List candidates) { + this.candidatesCache = candidates this } @@ -87,8 +87,8 @@ class SdkmanBashEnvBuilder { this } - SdkmanBashEnvBuilder withVersionFile(String version) { - this.versionFile = version + SdkmanBashEnvBuilder withVersionCache(String version) { + this.versionCache = version this } @@ -111,10 +111,10 @@ class SdkmanBashEnvBuilder { curlStub.map { cs -> cs.build() } initializeCandidates(sdkmanCandidatesDir, candidates) - initializeAvailableCandidates(sdkmanVarDir, availableCandidates) + initializeCandidatesCache(sdkmanVarDir, candidatesCache) initializeBroadcast(sdkmanVarDir, broadcast) initializeConfiguration(sdkmanEtcDir, config) - initializeVersionFile(sdkmanVarDir, versionFile) + initializeVersionCache(sdkmanVarDir, versionCache) primeInitScript(sdkmanBinDir) primeModuleScripts(sdkmanSrcDir) @@ -143,7 +143,7 @@ class SdkmanBashEnvBuilder { directory } - private initializeVersionFile(File folder, String version) { + private initializeVersionCache(File folder, String version) { if (version) { new File(folder, "version") << version } @@ -156,7 +156,7 @@ class SdkmanBashEnvBuilder { } } - private initializeAvailableCandidates(File folder, List candidates) { + private initializeCandidatesCache(File folder, List candidates) { def candidatesCache = new File(folder, "candidates") if (candidates) { candidatesCache << candidates.join(",") diff --git a/src/test/groovy/sdkman/specs/BetaChannelBootstrapSpec.groovy b/src/test/groovy/sdkman/specs/BetaChannelBootstrapSpec.groovy index a9cf9cfd..907619ac 100644 --- a/src/test/groovy/sdkman/specs/BetaChannelBootstrapSpec.groovy +++ b/src/test/groovy/sdkman/specs/BetaChannelBootstrapSpec.groovy @@ -9,10 +9,11 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { static final CLI_STABLE_ENDPOINT = "$LEGACY_API/candidates/app/stable" static final CLI_BETA_ENDPOINT = "$LEGACY_API/candidates/app/beta" - File versionFile + File versionCache def setup() { - versionFile = new File("${sdkmanDotDirectory}/var", "version") + versionCache = new File("${sdkmanDotDirectory}/var", "version") + sdkmanBashEnvBuilder.withCandidatesCache(["groovy"]) } void "should attempt immediate upgrade of stable to beta version if beta channel is first enabled"() { @@ -22,7 +23,7 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) .withConfiguration("sdkman_beta_channel", "true") - .withVersionFile("x.y.b") + .withVersionCache("x.y.b") .build() and: @@ -32,8 +33,8 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash.execute("source $bootstrapScript") then: - versionFile.exists() - versionFile.text.contains(betaVersion) + versionCache.exists() + versionCache.text.contains(betaVersion) } void "should attempt downgrade of beta to stable version if beta channel is first disabled"() { @@ -43,9 +44,9 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) .withConfiguration("sdkman_beta_channel", "false") - .withVersionFile("x.y.c") + .withVersionCache("x.y.c") .build() - versionFile.setLastModified(TWO_DAYS_AGO) + versionCache.setLastModified(TWO_DAYS_AGO) and: bash.start() @@ -54,8 +55,8 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash.execute("source $bootstrapScript") then: - versionFile.exists() - versionFile.text.contains(stableVersion) + versionCache.exists() + versionCache.text.contains(stableVersion) } void "should attempt immediate upgrade to new version of beta channel if available"() { @@ -65,7 +66,7 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) .withConfiguration("sdkman_beta_channel", "true") - .withVersionFile("x.y.c") + .withVersionCache("x.y.c") .build() and: @@ -75,8 +76,8 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash.execute("source $bootstrapScript") then: - versionFile.exists() - versionFile.text.contains(newerBetaVersion) + versionCache.exists() + versionCache.text.contains(newerBetaVersion) } void "should attempt upgrade to new version of stable channel if available"() { @@ -86,9 +87,9 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) .withConfiguration("sdkman_beta_channel", "false") - .withVersionFile("x.y.c") + .withVersionCache("x.y.c") .build() - versionFile.setLastModified(TWO_DAYS_AGO) + versionCache.setLastModified(TWO_DAYS_AGO) and: bash.start() @@ -97,7 +98,7 @@ class BetaChannelBootstrapSpec extends SdkmanEnvSpecification { bash.execute("source $bootstrapScript") then: - versionFile.exists() - versionFile.text.contains(newerStableVersion) + versionCache.exists() + versionCache.text.contains(newerStableVersion) } } diff --git a/src/test/groovy/sdkman/specs/CandidatesCacheBootstrapSpec.groovy b/src/test/groovy/sdkman/specs/CandidatesCacheBootstrapSpec.groovy index bb03a598..ed2f6e6a 100644 --- a/src/test/groovy/sdkman/specs/CandidatesCacheBootstrapSpec.groovy +++ b/src/test/groovy/sdkman/specs/CandidatesCacheBootstrapSpec.groovy @@ -2,9 +2,11 @@ package sdkman.specs import sdkman.support.SdkmanEnvSpecification +import static java.lang.System.currentTimeMillis + class CandidatesCacheBootstrapSpec extends SdkmanEnvSpecification { - static final DAY_AND_MINUTE_IN_MILLIS = 24 * 61 * 60 * 1000 + static final MORE_THAN_A_DAY_IN_MILLIS = 24 * 61 * 60 * 1000 static final LEGACY_API = "http://localhost:8080/1" static final LEGACY_CANDIDATES_ENDPOINT = "$LEGACY_API/candidates" @@ -14,167 +16,162 @@ class CandidatesCacheBootstrapSpec extends SdkmanEnvSpecification { static final CURRENT_API = "http://localhost:8080/2" static final CURRENT_CANDIDATES_ENDPOINT = "$CURRENT_API/candidates/all" - File candidatesFile + File candidatesCache def setup() { - candidatesFile = new File("${sdkmanDotDirectory}/var", "candidates") + candidatesCache = new File("${sdkmanDotDirectory}/var", "candidates") curlStub.primeWith(LEGACY_VERSIONS_STABLE_ENDPOINT, "echo x.y.y") .primeWith(LEGACY_VERSIONS_BETA_ENDPOINT, "echo x.y.z") sdkmanBashEnvBuilder.withConfiguration("sdkman_debug_mode", "true") } void "should not query server if unexpired candidates cache is found"() { - given: 'a working sdkman installation with candidates file' + given: bash = sdkmanBashEnvBuilder - .withAvailableCandidates(['gradle', 'sbt']) + .withCandidatesCache(['gradle', 'sbt']) .build() and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.exists() - candidatesFile.text.contains("gradle,sbt") + candidatesCache.exists() + candidatesCache.text.contains("gradle,sbt") } void "legacy should fetch and store candidates in cache if cache is empty"() { - given: 'a working sdkman installation without candidates' + given: curlStub.primeWith(LEGACY_CANDIDATES_ENDPOINT, "echo groovy,scala") bash = sdkmanBashEnvBuilder - .withAvailableCandidates([]) + .withCandidatesCache([]) .withLegacyService(LEGACY_API) .build() and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.exists() - candidatesFile.text.contains("groovy,scala") + candidatesCache.exists() + candidatesCache.text.contains("groovy,scala") } void "legacy should fetch candidates and refresh cache if older than a day"() { - given: 'a working sdkman installation with expired candidates' + given: curlStub.primeWith(LEGACY_CANDIDATES_ENDPOINT, "echo groovy,scala") bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) - .withAvailableCandidates(['groovy']) + .withCandidatesCache(['groovy']) .build() and: - def twoDaysAgo = System.currentTimeMillis() - DAY_AND_MINUTE_IN_MILLIS - candidatesFile.setLastModified(twoDaysAgo) + candidatesCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.exists() - candidatesFile.text.contains('groovy,scala') + candidatesCache.exists() + candidatesCache.text.contains('groovy,scala') } void "legacy should ignore candidates if api is offline"() { - given: 'a working sdkman installation with api down' + given: def candidates = ['groovy', 'scala'] curlStub.primeWith(LEGACY_CANDIDATES_ENDPOINT, "echo ''") bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) - .withAvailableCandidates(candidates) + .withCandidatesCache(candidates) .build() and: - def twoDaysAgo = System.currentTimeMillis() - DAY_AND_MINUTE_IN_MILLIS - candidatesFile.setLastModified(twoDaysAgo) + candidatesCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.text.contains(candidates.join(',')) + candidatesCache.text.contains(candidates.join(',')) } void "legacy should ignore candidates if api returns garbage"() { - given: 'a working sdkman installation with garbled api' + given: def candidates = ['groovy', 'scala'] curlStub.primeWith(LEGACY_CANDIDATES_ENDPOINT, "echo 'sorry'") bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) - .withAvailableCandidates(candidates) + .withCandidatesCache(candidates) .build() and: - def twoDaysAgo = System.currentTimeMillis() - DAY_AND_MINUTE_IN_MILLIS - candidatesFile.setLastModified(twoDaysAgo) + candidatesCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.text.contains(candidates.join(',')) + candidatesCache.text.contains(candidates.join(',')) } void "legacy should query legacy api if not subscribed to beta channel"() { - given: 'a working sdkman installation with expired candidates' + given: curlStub.primeWith(LEGACY_CANDIDATES_ENDPOINT, "echo groovy,scala") bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) .withCurrentService(CURRENT_API) .withConfiguration("sdkman_beta_channel", "false") - .withAvailableCandidates(['groovy']) + .withCandidatesCache(['groovy']) .build() and: - def twoDaysAgo = System.currentTimeMillis() - DAY_AND_MINUTE_IN_MILLIS - candidatesFile.setLastModified(twoDaysAgo) + candidatesCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.exists() - candidatesFile.text.contains('groovy,scala') + candidatesCache.exists() + candidatesCache.text.contains('groovy,scala') } void "current should query current api if subscribed to beta channel"() { - given: 'a working sdkman installation with expired candidates' + given: curlStub.primeWith(CURRENT_CANDIDATES_ENDPOINT, "echo groovy,java,scala") bash = sdkmanBashEnvBuilder .withLegacyService(LEGACY_API) .withCurrentService(CURRENT_API) .withConfiguration("sdkman_beta_channel", "true") - .withAvailableCandidates(['groovy']) + .withCandidatesCache(['groovy']) .build() and: - def twoDaysAgo = System.currentTimeMillis() - DAY_AND_MINUTE_IN_MILLIS - candidatesFile.setLastModified(twoDaysAgo) + candidatesCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) and: bash.start() - when: 'bootstrap the system' + when: bash.execute("source $bootstrapScript") then: - candidatesFile.exists() - candidatesFile.text.contains('groovy,java,scala') + candidatesCache.exists() + candidatesCache.text.contains('groovy,java,scala') } } diff --git a/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy b/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy index 3bf3830d..e8612481 100644 --- a/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy +++ b/src/test/groovy/sdkman/specs/CurrentCommandSpec.groovy @@ -34,7 +34,7 @@ class CurrentCommandSpec extends SdkmanEnvSpecification { bash = sdkmanBashEnvBuilder .withOfflineMode(false) - .withAvailableCandidates(allCandidates) + .withCandidatesCache(allCandidates) .withCandidates(installedCandidates.keySet().toList()) .build() diff --git a/src/test/groovy/sdkman/specs/InitialisationSpec.groovy b/src/test/groovy/sdkman/specs/InitialisationSpec.groovy index 3ab07674..4e241baa 100644 --- a/src/test/groovy/sdkman/specs/InitialisationSpec.groovy +++ b/src/test/groovy/sdkman/specs/InitialisationSpec.groovy @@ -12,9 +12,9 @@ class InitialisationSpec extends SdkmanEnvSpecification { def setup() { bash = sdkmanBashEnvBuilder - .withAvailableCandidates(allCandidates) + .withCandidatesCache(allCandidates) .withCandidates(allCandidates) - .withVersionFile("x.y.z") + .withVersionCache("x.y.z") .build() prepareCandidateDirectories(allCandidates) } diff --git a/src/test/groovy/sdkman/specs/SdkCompatibilitySpec.groovy b/src/test/groovy/sdkman/specs/SdkCompatibilitySpec.groovy index 1deaf752..5494a8f2 100644 --- a/src/test/groovy/sdkman/specs/SdkCompatibilitySpec.groovy +++ b/src/test/groovy/sdkman/specs/SdkCompatibilitySpec.groovy @@ -11,9 +11,9 @@ class SdkCompatibilitySpec extends SdkmanEnvSpecification { def setup() { bash = sdkmanBashEnvBuilder - .withAvailableCandidates(allCandidates) + .withCandidatesCache(allCandidates) .withCandidates(allCandidates) - .withVersionFile("x.y.z") + .withVersionCache("x.y.z") .build() } diff --git a/src/test/groovy/sdkman/specs/VersionCacheBootstrapSpec.groovy b/src/test/groovy/sdkman/specs/VersionCacheBootstrapSpec.groovy new file mode 100644 index 00000000..2af66fa3 --- /dev/null +++ b/src/test/groovy/sdkman/specs/VersionCacheBootstrapSpec.groovy @@ -0,0 +1,164 @@ +package sdkman.specs + +import sdkman.support.SdkmanEnvSpecification + +import static java.lang.System.currentTimeMillis + +class VersionCacheBootstrapSpec extends SdkmanEnvSpecification { + + static final MORE_THAN_A_DAY_IN_MILLIS = 24 * 61 * 60 * 1000 + + static final LEGACY_API = "http://localhost:8080/1" + static final CLI_VERSION_STABLE_ENDPOINT = "$LEGACY_API/candidates/app/stable" + static final CLI_VERSION_BETA_ENDPOINT = "$LEGACY_API/candidates/app/beta" + static final CANDIDATES_ENDPOINT = "$LEGACY_API/candidates" + + File versionCache + + def setup() { + versionCache = new File("${sdkmanDotDirectory}/var", "version") + curlStub.primeWith(CANDIDATES_ENDPOINT, "echo 'groovy,scala'") + } + + void "should store version cache if does not exist"() { + given: + curlStub.primeWith(CLI_VERSION_STABLE_ENDPOINT, "echo x.y.b") + bash = sdkmanBashEnvBuilder + .withLegacyService(LEGACY_API) + .build() + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + versionCache.exists() + versionCache.text.contains("x.y.b") + } + + void "should not query server if unexpired version cache is found"() { + given: + curlStub.primeWith(CLI_VERSION_STABLE_ENDPOINT, "sleep 50") //will timeout and fail if called + bash = sdkmanBashEnvBuilder + .withVersionCache("x.y.z") + .build() + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + versionCache.exists() + versionCache.text.contains("x.y.z") + } + + void "should refresh version cache if older than a day"() { + given: + curlStub.primeWith(CLI_VERSION_STABLE_ENDPOINT, "echo x.y.b") + bash = sdkmanBashEnvBuilder + .withLegacyService(LEGACY_API) + .withVersionCache("x.y.a") + .build() + + and: + versionCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + versionCache.exists() + versionCache.text.contains("x.y.b") + } + + void "should fallback and ignore version if version cache expired and api is offline"() { + given: + curlStub.primeWith(CLI_VERSION_STABLE_ENDPOINT, "echo ''") + bash = sdkmanBashEnvBuilder + .withLegacyService(LEGACY_API) + .withVersionCache("x.y.z") + .build() + + and: + versionCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + versionCache.text.contains("x.y.z") + } + + void "should not go offline if curl times out"() { + given: + curlStub.primeWith(CLI_VERSION_STABLE_ENDPOINT, "echo ''") + bash = sdkmanBashEnvBuilder + .withLegacyService(LEGACY_API) + .withVersionCache("x.y.z") + .build() + + and: + versionCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + !bash.output.contains("SDKMAN can't reach the internet so going offline.") + } + + void "should ignore version if api returns garbage"() { + given: + def sdkmanVersion = "x.y.z" + curlStub.primeWith(CLI_VERSION_STABLE_ENDPOINT, "echo 'sorry'") + bash = sdkmanBashEnvBuilder + .withLegacyService(LEGACY_API) + .withVersionCache(sdkmanVersion) + .build() + + and: + versionCache.setLastModified(currentTimeMillis() - MORE_THAN_A_DAY_IN_MILLIS) + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + versionCache.text.contains(sdkmanVersion) + } + + void "should always refresh version cache if on beta_channel"() { + given: + curlStub.primeWith(CLI_VERSION_BETA_ENDPOINT, "echo x.y.z") + bash = sdkmanBashEnvBuilder + .withLegacyService(LEGACY_API) + .withVersionCache("x.y.w") + .withConfiguration("sdkman_beta_channel", "true") + .build() + + and: + bash.start() + + when: + bash.execute("source $bootstrapScript") + + then: + versionCache.exists() + versionCache.text.contains("x.y.z") + } +} diff --git a/src/test/groovy/sdkman/specs/VersionFileBootstrapSpec.groovy b/src/test/groovy/sdkman/specs/VersionFileBootstrapSpec.groovy deleted file mode 100644 index 4ae06dd0..00000000 --- a/src/test/groovy/sdkman/specs/VersionFileBootstrapSpec.groovy +++ /dev/null @@ -1,127 +0,0 @@ -package sdkman.specs - -import sdkman.support.SdkmanEnvSpecification - -class VersionFileBootstrapSpec extends SdkmanEnvSpecification { - - static final LEGACY_API = "http://localhost:8080/1" - static final CLI_VERSION_ENDPOINT = "$LEGACY_API/candidates/app/stable" - - File versionFile - File candidatesFile - - def setup() { - versionFile = new File("${sdkmanDotDirectory}/var", "version") - candidatesFile = new File("${sdkmanDotDirectory}/var", "candidates") - } - - void "should store version file if does not exist"() { - given: 'a working sdkman installation without version file' - curlStub.primeWith(CLI_VERSION_ENDPOINT, "echo x.y.b") - bash = sdkmanBashEnvBuilder - .withLegacyService(LEGACY_API) - .build() - - and: - bash.start() - - when: 'bootstrap the system' - bash.execute("source $bootstrapScript") - - then: - versionFile.exists() - } - - void "should not query server if version file is found"() { - given: 'a working sdkman installation with version file' - bash = sdkmanBashEnvBuilder - .withVersionFile("x.y.z") - .build() - - and: - bash.start() - - when: 'bootstrap the system' - bash.execute("source $bootstrapScript") - - then: - versionFile.exists() - versionFile.text.contains("x.y.z") - } - - void "should query server for version and refresh if file is older than a day"() { - given: 'a working sdkman installation with expired version file' - curlStub.primeWith(CLI_VERSION_ENDPOINT, "echo x.y.b") - bash = sdkmanBashEnvBuilder - .withLegacyService(LEGACY_API) - .withVersionFile("x.y.a") - .build() - def twoDaysAgo = System.currentTimeMillis() - (24 * 61 * 60 * 1000) - versionFile.setLastModified(twoDaysAgo) - - and: - bash.start() - - when: 'bootstrap the system' - bash.execute("source $bootstrapScript") - - then: - versionFile.exists() - versionFile.text.contains("x.y.b") - } - - void "should ignore version if api is offline"() { - given: 'a working sdkman installation with api down' - def sdkmanVersion = "x.y.z" - curlStub.primeWith(CLI_VERSION_ENDPOINT, "echo ''") - bash = sdkmanBashEnvBuilder - .withLegacyService(LEGACY_API) - .withVersionFile(sdkmanVersion) - .build() - - and: - bash.start() - - when: 'bootstrap the system' - bash.execute("source $bootstrapScript") - - then: - versionFile.text.contains(sdkmanVersion) - } - - void "should not go offline if curl times out"() { - given: 'a working sdkman installation with api down' - curlStub.primeWith(CLI_VERSION_ENDPOINT, "echo ''") - bash = sdkmanBashEnvBuilder - .withLegacyService(LEGACY_API) - .build() - - and: - bash.start() - - when: 'bootstrap the system' - bash.execute("source $bootstrapScript") - - then: - !bash.output.contains("SDKMAN can't reach the internet so going offline.") - } - - void "should ignore version if api returns garbage"() { - given: 'a working sdkman installation with garbled api' - def sdkmanVersion = "x.y.z" - curlStub.primeWith(CLI_VERSION_ENDPOINT, "echo 'sorry'") - bash = sdkmanBashEnvBuilder - .withLegacyService(LEGACY_API) - .withVersionFile(sdkmanVersion) - .build() - - and: - bash.start() - - when: 'bootstrap the system' - bash.execute("source $bootstrapScript") - - then: - versionFile.text.contains(sdkmanVersion) - } -} diff --git a/src/test/groovy/sdkman/steps/initialisation_steps.groovy b/src/test/groovy/sdkman/steps/initialisation_steps.groovy index 3f45621e..7ab2e946 100644 --- a/src/test/groovy/sdkman/steps/initialisation_steps.groovy +++ b/src/test/groovy/sdkman/steps/initialisation_steps.groovy @@ -93,7 +93,7 @@ And(~'^an initialised environment$') {-> .withCurrentService(serviceUrlEnv) .withJdkHome(javaHome) .withHttpProxy(HTTP_PROXY) - .withVersionFile(sdkmanVersion) + .withVersionCache(sdkmanVersion) .withSdkmanVersion(sdkmanVersion) .build() } @@ -105,7 +105,7 @@ And(~'^an outdated initialised environment$') {-> .withCurrentService(serviceUrlEnv) .withJdkHome(javaHome) .withHttpProxy(HTTP_PROXY) - .withVersionFile(sdkmanVersionOutdated) + .withVersionCache(sdkmanVersionOutdated) .withSdkmanVersion(sdkmanVersionOutdated) .build()