From ee09be15c0646f0ccc78dd5bf250d7de9641bfbc Mon Sep 17 00:00:00 2001 From: asapha Date: Sun, 29 Sep 2024 12:33:38 +0200 Subject: [PATCH] add a test that repro #2072 by using kotlin-inject --- .../devtools/ksp/test/KotlinInjectIT.kt | 61 +++++++++++++++++++ .../resources/kotlin-inject/build.gradle.kts | 12 ++++ .../kotlin-inject/settings.gradle.kts | 20 ++++++ .../kotlin-inject/workload/build.gradle.kts | 27 ++++++++ .../src/commonMain/kotlin/AppComponent.kt | 8 +++ .../src/commonMain/kotlin/CommonMain.kt | 1 + .../workload/src/jvmMain/kotlin/JvmMain.kt | 1 + 7 files changed, 130 insertions(+) create mode 100644 integration-tests/src/test/kotlin/com/google/devtools/ksp/test/KotlinInjectIT.kt create mode 100644 integration-tests/src/test/resources/kotlin-inject/build.gradle.kts create mode 100644 integration-tests/src/test/resources/kotlin-inject/settings.gradle.kts create mode 100644 integration-tests/src/test/resources/kotlin-inject/workload/build.gradle.kts create mode 100644 integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/AppComponent.kt create mode 100644 integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/CommonMain.kt create mode 100644 integration-tests/src/test/resources/kotlin-inject/workload/src/jvmMain/kotlin/JvmMain.kt diff --git a/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/KotlinInjectIT.kt b/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/KotlinInjectIT.kt new file mode 100644 index 0000000000..e370aef20c --- /dev/null +++ b/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/KotlinInjectIT.kt @@ -0,0 +1,61 @@ +package com.google.devtools.ksp.test + +import org.gradle.testkit.runner.GradleRunner +import org.junit.Assert +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import java.io.File + +@RunWith(Parameterized::class) +class KotlinInjectIT(val useKSP2: Boolean) { + @Rule + @JvmField + val project: TemporaryTestProject = TemporaryTestProject("kotlin-inject", useKSP2 = useKSP2) + + @Test + fun triggerException() { + val gradleRunner = GradleRunner.create().withProjectDir(project.root) + val path = "workload/src/commonMain/kotlin/AppComponent.kt" + val file = File(project.root, path) + + fun setup(shouldFail: Boolean) { + project.restore(path) + + // kotlin-inject will complain that Component classes can't be private + if (shouldFail) { + file + .apply { + writeText( + file.readText() + .replace("abstract class AppComponent", "private abstract class AppComponent") + ) + } + } + } + + // Start the kotlin daemon? + setup(shouldFail = false) + gradleRunner.withArguments("compileKotlinJvm").build() + + // Make a processor fail + setup(shouldFail = true) + gradleRunner.withArguments("compileKotlinJvm").buildAndFail() + + // Triggers the caching issue + setup(shouldFail = false) + gradleRunner.withArguments("compileKotlinJvm") + .buildAndFail().let { result -> + println("OUTPUT START") + println(result.output) + Assert.assertTrue(result.output.contains("id-to-file.tab] is already registered")) + } + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "KSP2={0}") + fun params() = listOf(arrayOf(true), arrayOf(false)) + } +} diff --git a/integration-tests/src/test/resources/kotlin-inject/build.gradle.kts b/integration-tests/src/test/resources/kotlin-inject/build.gradle.kts new file mode 100644 index 0000000000..8dd6566724 --- /dev/null +++ b/integration-tests/src/test/resources/kotlin-inject/build.gradle.kts @@ -0,0 +1,12 @@ +plugins { + kotlin("multiplatform") apply false +} + +val testRepo: String by project +allprojects { + repositories { + maven(testRepo) + mavenCentral() + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") + } +} diff --git a/integration-tests/src/test/resources/kotlin-inject/settings.gradle.kts b/integration-tests/src/test/resources/kotlin-inject/settings.gradle.kts new file mode 100644 index 0000000000..1ad8892c8d --- /dev/null +++ b/integration-tests/src/test/resources/kotlin-inject/settings.gradle.kts @@ -0,0 +1,20 @@ +pluginManagement { + val kotlinVersion: String by settings + val kspVersion: String by settings + + val testRepo: String by settings + plugins { + id("com.google.devtools.ksp") version kspVersion apply false + kotlin("multiplatform") version kotlinVersion apply false + } + repositories { + maven(testRepo) + gradlePluginPortal() + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") + } +} + +rootProject.name = "hmpp" + +include(":workload") +include(":test-processor") diff --git a/integration-tests/src/test/resources/kotlin-inject/workload/build.gradle.kts b/integration-tests/src/test/resources/kotlin-inject/workload/build.gradle.kts new file mode 100644 index 0000000000..f1defe1567 --- /dev/null +++ b/integration-tests/src/test/resources/kotlin-inject/workload/build.gradle.kts @@ -0,0 +1,27 @@ +plugins { + kotlin("multiplatform") + id("com.google.devtools.ksp") +} + +version = "1.0-SNAPSHOT" + +kotlin { + jvm { + withJava() + } + + sourceSets { + val commonMain by getting { + dependencies { + implementation("me.tatarka.inject:kotlin-inject-runtime:0.7.2") + } + } + + val jvmMain by getting { + } + } +} + +dependencies { + add("kspJvm", "me.tatarka.inject:kotlin-inject-compiler-ksp:0.7.2") +} diff --git a/integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/AppComponent.kt b/integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/AppComponent.kt new file mode 100644 index 0000000000..e7b3d18d67 --- /dev/null +++ b/integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/AppComponent.kt @@ -0,0 +1,8 @@ +@me.tatarka.inject.annotations.Component +abstract class AppComponent { + abstract val repo: Repository + +} + +@me.tatarka.inject.annotations.Inject +class Repository() diff --git a/integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/CommonMain.kt b/integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/CommonMain.kt new file mode 100644 index 0000000000..301f3efbab --- /dev/null +++ b/integration-tests/src/test/resources/kotlin-inject/workload/src/commonMain/kotlin/CommonMain.kt @@ -0,0 +1 @@ +class CommonMain diff --git a/integration-tests/src/test/resources/kotlin-inject/workload/src/jvmMain/kotlin/JvmMain.kt b/integration-tests/src/test/resources/kotlin-inject/workload/src/jvmMain/kotlin/JvmMain.kt new file mode 100644 index 0000000000..ffae14b727 --- /dev/null +++ b/integration-tests/src/test/resources/kotlin-inject/workload/src/jvmMain/kotlin/JvmMain.kt @@ -0,0 +1 @@ +class JvmMain