|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package aws.sdk.kotlin.gradle.kmp |
| 6 | + |
| 7 | +import org.gradle.api.Project |
| 8 | +import org.gradle.api.tasks.Exec |
| 9 | +import org.gradle.kotlin.dsl.withType |
| 10 | +import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest |
| 11 | +import org.jetbrains.kotlin.konan.target.HostManager |
| 12 | + |
| 13 | +/** |
| 14 | + * Disables standalone mode in simulator tests since it causes issues with TLS. |
| 15 | + * This means we need to manage the simulator state ourselves (booting, shutting down). |
| 16 | + * https://youtrack.jetbrains.com/issue/KT-38317 |
| 17 | + */ |
| 18 | +public fun Project.configureIosSimulatorTasks() { |
| 19 | + val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 15" |
| 20 | + |
| 21 | + val xcrun = "/usr/bin/xcrun" |
| 22 | + |
| 23 | + tasks.register("bootIosSimulatorDevice", Exec::class.java) { |
| 24 | + isIgnoreExitValue = true |
| 25 | + commandLine(xcrun, "simctl", "boot", simulatorDeviceName) |
| 26 | + |
| 27 | + doLast { |
| 28 | + val result = executionResult.get() |
| 29 | + val code = result.exitValue |
| 30 | + if (code != 148 && code != 149) { // ignore "simulator already running" errors |
| 31 | + result.assertNormalExitValue() |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + tasks.register("shutdownIosSimulatorDevice", Exec::class.java) { |
| 37 | + isIgnoreExitValue = true |
| 38 | + mustRunAfter(tasks.withType<KotlinNativeSimulatorTest>()) |
| 39 | + commandLine(xcrun, "simctl", "shutdown", simulatorDeviceName) |
| 40 | + |
| 41 | + doLast { |
| 42 | + val result = executionResult.get() |
| 43 | + if (result.exitValue != 405) { // ignore "simulator already shutdown" errors |
| 44 | + result.assertNormalExitValue() |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + tasks.withType<KotlinNativeSimulatorTest>().configureEach { |
| 50 | + if (!HostManager.hostIsMac) { |
| 51 | + return@configureEach |
| 52 | + } |
| 53 | + |
| 54 | + dependsOn("bootIosSimulatorDevice") |
| 55 | + finalizedBy("shutdownIosSimulatorDevice") |
| 56 | + |
| 57 | + standalone.set(false) |
| 58 | + device.set(simulatorDeviceName) |
| 59 | + } |
| 60 | +} |
0 commit comments