From c00cad1ad0685029a5d08c134e750852c8eab5b0 Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Mon, 11 Nov 2024 15:41:14 -0800 Subject: [PATCH] Enable both KSP1 and KSP2 in gradle plugin tests --- .../ksp/gradle/GradleCompilationTest.kt | 32 ++++++++++++++++--- .../ProcessorClasspathConfigurationsTest.kt | 14 ++++++-- .../ksp/gradle/SourceSetConfigurationsTest.kt | 14 ++++++-- .../gradle/testing/KspIntegrationTestRule.kt | 7 ++-- .../ksp/gradle/testing/TestProject.kt | 4 ++- 5 files changed, 59 insertions(+), 12 deletions(-) diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt index d0fe401401..22f17be1c8 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/GradleCompilationTest.kt @@ -32,15 +32,25 @@ import org.junit.Assume import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class GradleCompilationTest(val useKSP2: Boolean) { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "KSP2={0}") + fun params() = listOf(arrayOf(true), arrayOf(false)) + } -class GradleCompilationTest { @Rule @JvmField val tmpDir = TemporaryFolder() @Rule @JvmField - val testRule = KspIntegrationTestRule(tmpDir) + val testRule = KspIntegrationTestRule(tmpDir, useKSP2) @Test fun errorMessageFailsCompilation() { @@ -239,8 +249,8 @@ class GradleCompilationTest { ) testRule.appModule.dependencies.addAll( listOf( - artifact(configuration = "ksp", "androidx.room:room-compiler:2.4.2"), - artifact(configuration = "implementation", "androidx.room:room-runtime:2.4.2") + artifact(configuration = "ksp", "androidx.room:room-compiler:2.6.1"), + artifact(configuration = "implementation", "androidx.room:room-runtime:2.6.1") ) ) testRule.appModule.buildFileAdditions.add( @@ -267,6 +277,13 @@ class GradleCompilationTest { } } } + tasks.withType().configureEach { + doFirst { + kspConfig.processorOptions.get().forEach { (key, value) -> + println("apoption=${'$'}key=${'$'}value") + } + } + } """.trimIndent() ) @@ -312,6 +329,7 @@ class GradleCompilationTest { @Test fun commandLineArgumentIsIncludedInApoptionsWhenAddedInKspTask() { Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows", ignoreCase = true)) + Assume.assumeFalse(useKSP2) testRule.setupAppAsAndroidApp() testRule.appModule.dependencies.addAll( listOf( @@ -366,6 +384,11 @@ class GradleCompilationTest { println("HAS LIBRARY: ${'$'}{it.path}") } } + tasks.withType().configureEach { + kspConfig.libraries.files.forEach { + println("HAS LIBRARY: ${'$'}{it.path}") + } + } } """.trimIndent() ) @@ -389,6 +412,7 @@ class GradleCompilationTest { @Test fun changingKsp2AtRuntime() { + Assume.assumeFalse(useKSP2) testRule.setupAppAsJvmApp() testRule.appModule.buildFileAdditions.add( """ diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt index 713eaf4c09..01db0b609e 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/ProcessorClasspathConfigurationsTest.kt @@ -21,15 +21,25 @@ import com.google.devtools.ksp.gradle.testing.KspIntegrationTestRule import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.junit.runners.Parameterized + +@RunWith(Parameterized::class) +class ProcessorClasspathConfigurationsTest(val useKSP2: Boolean) { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "KSP2={0}") + fun params() = listOf(arrayOf(true), arrayOf(false)) + } -class ProcessorClasspathConfigurationsTest { @Rule @JvmField val tmpDir = TemporaryFolder() @Rule @JvmField - val testRule = KspIntegrationTestRule(tmpDir) + val testRule = KspIntegrationTestRule(tmpDir, useKSP2) private val kspConfigs by lazy { """configurations.matching { it.name.startsWith("ksp") && !it.name.endsWith("ProcessorClasspath") }""" diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/SourceSetConfigurationsTest.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/SourceSetConfigurationsTest.kt index e0506ae756..7cb1320029 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/SourceSetConfigurationsTest.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/SourceSetConfigurationsTest.kt @@ -30,16 +30,26 @@ import com.google.devtools.ksp.symbol.KSClassDeclaration import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.junit.runners.Parameterized import java.io.File -class SourceSetConfigurationsTest { +@RunWith(Parameterized::class) +class SourceSetConfigurationsTest(val useKSP2: Boolean) { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "KSP2={0}") + fun params() = listOf(arrayOf(true), arrayOf(false)) + } + @Rule @JvmField val tmpDir = TemporaryFolder() @Rule @JvmField - val testRule = KspIntegrationTestRule(tmpDir) + val testRule = KspIntegrationTestRule(tmpDir, useKSP2) @Test fun configurationsForJvmApp() { diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt index baf69f5d83..1186b83884 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/KspIntegrationTestRule.kt @@ -29,7 +29,8 @@ import kotlin.reflect.KClass * Test must call [setupAppAsAndroidApp] or [setupAppAsJvmApp] before using the [runner]. */ class KspIntegrationTestRule( - private val tmpFolder: TemporaryFolder + private val tmpFolder: TemporaryFolder, + private val useKSP2: Boolean ) : TestWatcher() { /** * Initialized when the test starts. @@ -144,7 +145,7 @@ class KspIntegrationTestRule( """ android { namespace = "com.example.kspandroidtestapp" - compileSdk = 31 + compileSdk = 34 defaultConfig { minSdk = 24 } @@ -168,6 +169,6 @@ class KspIntegrationTestRule( override fun starting(description: Description) { super.starting(description) - testProject = TestProject(tmpFolder.newFolder(), testConfig) + testProject = TestProject(tmpFolder.newFolder(), testConfig, useKSP2) } } diff --git a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/TestProject.kt b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/TestProject.kt index 0084410eaf..c82f951ae6 100644 --- a/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/TestProject.kt +++ b/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/TestProject.kt @@ -23,7 +23,8 @@ import java.io.File */ class TestProject( val rootDir: File, - val testConfig: TestConfig + val testConfig: TestConfig, + val useKSP2: Boolean, ) { val processorModule = TestModule( rootDir.resolve("processor") @@ -59,6 +60,7 @@ class TestProject( val contents = """ kotlin.jvm.target.validation.mode=warning + ksp.useKSP2=$useKSP2 """.trimIndent() rootDir.resolve("gradle.properties").appendText(contents) }