|
| 1 | +package io.sentry.kotlin.multiplatform.gradle |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import org.gradle.testkit.runner.GradleRunner |
| 5 | +import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading |
| 6 | +import org.gradle.testkit.runner.internal.io.SynchronizedOutputStream |
| 7 | +import org.junit.jupiter.api.Test |
| 8 | +import org.junit.jupiter.api.condition.EnabledOnOs |
| 9 | +import org.junit.jupiter.api.condition.OS |
| 10 | +import org.junit.jupiter.api.io.TempDir |
| 11 | +import java.io.ByteArrayOutputStream |
| 12 | +import java.io.File |
| 13 | +import java.io.OutputStreamWriter |
| 14 | + |
| 15 | +@EnabledOnOs(OS.MAC) |
| 16 | +class CocoaFrameworkLinkerIntegrationTest { |
| 17 | + /** |
| 18 | + * Verifies that the Cocoa linker is **not** configured when the task graph |
| 19 | + * contains only non-Apple targets. |
| 20 | + */ |
| 21 | + @Test |
| 22 | + fun `linker is not configured when only non-Apple tasks are requested`(@TempDir projectDir: File) { |
| 23 | + writeBuildFiles(projectDir) |
| 24 | + |
| 25 | + val output = ByteArrayOutputStream() |
| 26 | + defaultRunner(projectDir, output) |
| 27 | + .withArguments("compileKotlinJvm", "--dry-run", "--info") |
| 28 | + .build() |
| 29 | + |
| 30 | + assertThat(output.toString()) |
| 31 | + .contains("No Apple compile task scheduled for this build - skipping Sentry Cocoa framework linking") |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Verifies that the Cocoa linker **is** configured when at least one Apple |
| 36 | + * task is present in the task graph. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + fun `linker is configured when an Apple task is requested`(@TempDir projectDir: File) { |
| 40 | + writeBuildFiles(projectDir) |
| 41 | + |
| 42 | + val output = ByteArrayOutputStream() |
| 43 | + defaultRunner(projectDir, output) |
| 44 | + .withArguments("compileKotlinIosSimulatorArm64", "--dry-run", "--info") |
| 45 | + .build() |
| 46 | + |
| 47 | + assertThat(output.toString()) |
| 48 | + .contains("Set up Sentry Cocoa linking for target: iosSimulatorArm64") |
| 49 | + assertThat(output.toString()) |
| 50 | + .contains("Start resolving Sentry Cocoa framework paths for target: iosSimulatorArm64") |
| 51 | + } |
| 52 | + |
| 53 | + // --------------------------------------------------------------------- |
| 54 | + // test-fixture helpers |
| 55 | + // --------------------------------------------------------------------- |
| 56 | + |
| 57 | + private fun writeBuildFiles(dir: File) { |
| 58 | + // ----------------------------------------------------------------- |
| 59 | + // Create a fake XCFramework on disk so that the CustomPathStrategy |
| 60 | + // can resolve a valid framework path even on CI machines where SPM |
| 61 | + // (and hence DerivedData) is not available. |
| 62 | + // ----------------------------------------------------------------- |
| 63 | + val fakeFrameworkDir = File(dir, "Sentry-Dynamic.xcframework").apply { |
| 64 | + // Create minimal structure that satisfies path validation logic |
| 65 | + val archDirName = "ios-arm64_x86_64-simulator" // architecture used for iosSimulatorArm64 |
| 66 | + val archDir = File(this, archDirName) |
| 67 | + archDir.mkdirs() |
| 68 | + } |
| 69 | + |
| 70 | + File(dir, "settings.gradle").writeText("""rootProject.name = "fixture"""") |
| 71 | + |
| 72 | + val pluginClasspath = PluginUnderTestMetadataReading |
| 73 | + .readImplementationClasspath() |
| 74 | + .joinToString(", ") { "\"${it.absolutePath.replace('\\', '/')}\"" } |
| 75 | + |
| 76 | + File(dir, "build.gradle").writeText( |
| 77 | + """ |
| 78 | + buildscript { |
| 79 | + repositories { |
| 80 | + google() |
| 81 | + mavenCentral() |
| 82 | + gradlePluginPortal() |
| 83 | + } |
| 84 | + dependencies { |
| 85 | + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION" |
| 86 | + classpath files($pluginClasspath) |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + apply plugin: 'org.jetbrains.kotlin.multiplatform' |
| 91 | + apply plugin: 'io.sentry.kotlin.multiplatform.gradle' |
| 92 | +
|
| 93 | + repositories { |
| 94 | + google() |
| 95 | + mavenCentral() |
| 96 | + } |
| 97 | +
|
| 98 | + kotlin { |
| 99 | + jvm() // non-Apple target |
| 100 | + iosSimulatorArm64() // Apple target used in tests |
| 101 | + } |
| 102 | + |
| 103 | + // ----------------------------------------------------------------- |
| 104 | + // Configure the plugin to use the fake framework path created above. |
| 105 | + // This makes the CustomPathStrategy succeed immediately, bypassing the |
| 106 | + // DerivedData and manual search strategies that rely on an SPM setup. |
| 107 | + // ----------------------------------------------------------------- |
| 108 | + sentryKmp { |
| 109 | + linker { |
| 110 | + frameworkPath.set("${fakeFrameworkDir.absolutePath.replace('\\', '/')}") |
| 111 | + } |
| 112 | + } |
| 113 | + """.trimIndent() |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + /** Returns a pre-configured [GradleRunner] that logs into [out]. */ |
| 118 | + private fun defaultRunner(projectDir: File, out: ByteArrayOutputStream): GradleRunner = |
| 119 | + GradleRunner.create() |
| 120 | + .withProjectDir(projectDir) |
| 121 | + .withPluginClasspath() |
| 122 | + .withGradleVersion(org.gradle.util.GradleVersion.current().version) |
| 123 | + .forwardStdOutput(OutputStreamWriter(SynchronizedOutputStream(out))) |
| 124 | + .forwardStdError(OutputStreamWriter(SynchronizedOutputStream(out))) |
| 125 | + .withArguments("--stacktrace") |
| 126 | + |
| 127 | + private companion object { |
| 128 | + private const val KOTLIN_VERSION = "2.1.21" |
| 129 | + } |
| 130 | +} |
0 commit comments