|
| 1 | +package sdkman.specs |
| 2 | + |
| 3 | +import sdkman.support.SdkmanEnvSpecification |
| 4 | + |
| 5 | +import java.time.Instant |
| 6 | + |
| 7 | +import static java.time.temporal.ChronoUnit.DAYS |
| 8 | + |
| 9 | +class SelfupdateSpec extends SdkmanEnvSpecification { |
| 10 | + static final String CANDIDATES_API = "http://localhost:8080/2" |
| 11 | + static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id" |
| 12 | + static final String VERSION_ENDPOINT = "$CANDIDATES_API/broker/download/sdkman/version/stable" |
| 13 | + |
| 14 | + def setup() { |
| 15 | + curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155") |
| 16 | + curlStub.primeWith(VERSION_ENDPOINT, "echo 5.0.0") |
| 17 | + } |
| 18 | + |
| 19 | + def "should list selfupdate as a valid command when the selfupdate feature is toggled on"() { |
| 20 | + given: |
| 21 | + bash = sdkmanBashEnvBuilder |
| 22 | + .withConfiguration("sdkman_selfupdate_feature", selfUpdateFeature) |
| 23 | + .build() |
| 24 | + |
| 25 | + bash.start() |
| 26 | + bash.execute("source $bootstrapScript") |
| 27 | + |
| 28 | + when: |
| 29 | + bash.execute("sdk help") |
| 30 | + |
| 31 | + then: |
| 32 | + verifyOutput(bash.output) |
| 33 | + |
| 34 | + where: |
| 35 | + selfUpdateFeature | verifyOutput |
| 36 | + "false" | { !it.contains("selfupdate") } |
| 37 | + "true" | { it.contains("selfupdate") } |
| 38 | + } |
| 39 | + |
| 40 | + def "should source sdkman-selfupdate.sh when the selfupdate feature is toggled on"() { |
| 41 | + given: |
| 42 | + bash = sdkmanBashEnvBuilder |
| 43 | + .withConfiguration("sdkman_selfupdate_feature", selfupdateFeature) |
| 44 | + .build() |
| 45 | + |
| 46 | + bash.start() |
| 47 | + bash.execute("source $bootstrapScript") |
| 48 | + |
| 49 | + when: |
| 50 | + bash.execute("sdk selfupdate") |
| 51 | + |
| 52 | + then: |
| 53 | + verifyOutput(bash.output) |
| 54 | + |
| 55 | + where: |
| 56 | + selfupdateFeature | verifyOutput |
| 57 | + "false" | { it.contains("Invalid command: selfupdate") } |
| 58 | + "true" | { it.contains("No update available at this time.") } |
| 59 | + } |
| 60 | + |
| 61 | + def "should perform an autoupdate when the selfupdate feature is toggled on and autoupdate is enabled"() { |
| 62 | + given: |
| 63 | + new File("$sdkmanDotDirectory/var/delay_upgrade").with { |
| 64 | + parentFile.mkdirs() |
| 65 | + createNewFile() |
| 66 | + lastModified = Instant.now().minus(2, DAYS).toEpochMilli() |
| 67 | + } |
| 68 | + |
| 69 | + bash = sdkmanBashEnvBuilder |
| 70 | + .withSdkmanVersion("4.0.0") |
| 71 | + .withConfiguration("sdkman_selfupdate_feature", selfupdateFeature) |
| 72 | + .withConfiguration("sdkman_auto_update", autoUpdateEnabled) |
| 73 | + .withConfiguration("sdkman_auto_answer", "true") |
| 74 | + .build() |
| 75 | + |
| 76 | + bash.start() |
| 77 | + bash.execute("source $bootstrapScript") |
| 78 | + |
| 79 | + when: |
| 80 | + bash.execute("sdk version") |
| 81 | + |
| 82 | + then: |
| 83 | + verifyOutput(bash.output) |
| 84 | + |
| 85 | + where: |
| 86 | + selfupdateFeature | autoUpdateEnabled | verifyOutput |
| 87 | + "true" | "true" | { it.contains("ATTENTION: A new version of SDKMAN is available...") } |
| 88 | + "true" | "false" | { !it.contains("ATTENTION: A new version of SDKMAN is available...") } |
| 89 | + "false" | "true" | { !it.contains("ATTENTION: A new version of SDKMAN is available...") } |
| 90 | + } |
| 91 | +} |
0 commit comments