forked from google/ksp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix behavior for KSP error case with compilation.
respect returnOkOnError when withCompilation is set true.
- Loading branch information
Showing
12 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
integration-tests/src/test/kotlin/com/google/devtools/ksp/test/KSPCmdLineOptionsIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.google.devtools.ksp.test | ||
|
||
import org.gradle.testkit.runner.GradleRunner | ||
import org.jetbrains.kotlin.cli.common.ExitCode | ||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler | ||
import org.junit.Assert | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.PrintStream | ||
import java.net.URLClassLoader | ||
|
||
data class CompileResult(val exitCode: ExitCode, val output: String) | ||
|
||
class KSPCmdLineOptionsIT { | ||
@Rule | ||
@JvmField | ||
val project: TemporaryTestProject = TemporaryTestProject("cmd-options") | ||
|
||
private fun runCmdCompiler(pluginOptions: List<String>): CompileResult { | ||
val gradleRunner = GradleRunner.create().withProjectDir(project.root) | ||
gradleRunner.withArguments("clean", ":processors:build").build() | ||
val processorJar = File(project.root, "processors/build/libs/processors-1.0-SNAPSHOT.jar") | ||
val classLoader = URLClassLoader(arrayOf(processorJar.toURI().toURL()), javaClass.classLoader) | ||
val compiler = classLoader.loadClass(K2JVMCompiler::class.java.name).newInstance() as K2JVMCompiler | ||
val repoPath = "../build/repos/test/com/google/devtools/ksp/" | ||
val kspPluginId = "com.google.devtools.ksp.symbol-processing" | ||
val kspPluginJar = File("$repoPath/symbol-processing-cmdline/2.0.255-SNAPSHOT").listFiles()!!.filter { | ||
it.name.matches(Regex(".*-\\d.jar")) | ||
}.maxByOrNull { it.lastModified() }!! | ||
val kspApiJar = File("$repoPath/symbol-processing-api/2.0.255-SNAPSHOT").listFiles()!!.filter { | ||
it.name.matches(Regex(".*-\\d.jar")) | ||
}.maxByOrNull { it.lastModified() }!! | ||
val compilerArgs = mutableListOf( | ||
"-no-stdlib", | ||
"-Xplugin=${kspPluginJar.absolutePath}", | ||
"-Xplugin=${kspApiJar.absolutePath}", | ||
"-P", "plugin:$kspPluginId:apclasspath=${processorJar.absolutePath}", | ||
"-P", "plugin:$kspPluginId:projectBaseDir=${project.root}/build", | ||
"-P", "plugin:$kspPluginId:classOutputDir=${project.root}/build", | ||
"-P", "plugin:$kspPluginId:javaOutputDir=${project.root}/build/out", | ||
"-P", "plugin:$kspPluginId:kotlinOutputDir=${project.root}/build/out", | ||
"-P", "plugin:$kspPluginId:resourceOutputDir=${project.root}/build/out", | ||
"-P", "plugin:$kspPluginId:kspOutputDir=${project.root}/build/out", | ||
"-P", "plugin:$kspPluginId:cachesDir=${project.root}/build/out", | ||
"-P", "plugin:$kspPluginId:incremental=false", | ||
"-d", "${project.root}/build/out" | ||
) | ||
pluginOptions.forEach { | ||
compilerArgs.add("-P") | ||
compilerArgs.add("plugin:$kspPluginId:$it") | ||
} | ||
compilerArgs.add(File(project.root, "workload/src/main/kotlin/com/example/A.kt").absolutePath) | ||
val outStream = ByteArrayOutputStream() | ||
val exitCode = compiler.exec(PrintStream(outStream), *compilerArgs.toTypedArray()) | ||
return CompileResult(exitCode, outStream.toString()) | ||
} | ||
|
||
@Test | ||
fun testWithCompilationOnError() { | ||
val result = runCmdCompiler(listOf("apoption=error=true", "withCompilation=true")) | ||
val errors = result.output.lines().filter { it.startsWith("error: [ksp]") } | ||
val exitCode = result.exitCode | ||
Assert.assertTrue(exitCode == ExitCode.COMPILATION_ERROR) | ||
Assert.assertTrue( | ||
errors.any { | ||
it.startsWith("error: [ksp] java.lang.IllegalStateException: Error on request") | ||
} | ||
) | ||
} | ||
|
||
@Test | ||
fun testWithCompilationOnErrorOk() { | ||
val result = runCmdCompiler(listOf("apoption=error=true", "returnOkOnError=true", "withCompilation=true")) | ||
val errors = result.output.lines().filter { it.startsWith("error: [ksp]") } | ||
val exitCode = result.exitCode | ||
Assert.assertTrue(exitCode == ExitCode.OK) | ||
Assert.assertTrue( | ||
errors.any { | ||
it.startsWith("error: [ksp] java.lang.IllegalStateException: Error on request") | ||
} | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
integration-tests/src/test/resources/cmd-options/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} |
Empty file.
23 changes: 23 additions & 0 deletions
23
integration-tests/src/test/resources/cmd-options/processors/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
val kspVersion: String by project | ||
val testRepo: String by project | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
group = "com.example" | ||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven(testRepo) | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
|
||
dependencies { | ||
implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion") | ||
} | ||
|
||
sourceSets.main { | ||
java.srcDirs("src/main/kotlin") | ||
} |
37 changes: 37 additions & 0 deletions
37
integration-tests/src/test/resources/cmd-options/processors/src/main/kotlin/TestProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.* | ||
|
||
class TestProcessor : SymbolProcessor { | ||
lateinit var codeGenerator: CodeGenerator | ||
lateinit var logger: KSPLogger | ||
lateinit var options: Map<String, String> | ||
var rounds = 0 | ||
|
||
fun init( | ||
options: Map<String, String>, | ||
kotlinVersion: KotlinVersion, | ||
codeGenerator: CodeGenerator, | ||
logger: KSPLogger | ||
) { | ||
this.logger = logger | ||
this.options = options | ||
this.codeGenerator = codeGenerator | ||
} | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
if (options.containsKey("error")) { | ||
throw IllegalStateException("Error on request") | ||
} | ||
return emptyList() | ||
} | ||
} | ||
|
||
class TestProcessorProvider : SymbolProcessorProvider { | ||
override fun create( | ||
env: SymbolProcessorEnvironment | ||
): SymbolProcessor { | ||
return TestProcessor().apply { | ||
init(env.options, env.kotlinVersion, env.codeGenerator, env.logger) | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TestProcessorProvider |
19 changes: 19 additions & 0 deletions
19
integration-tests/src/test/resources/cmd-options/settings.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
pluginManagement { | ||
val kspVersion: String by settings | ||
val kotlinVersion: String by settings | ||
val testRepo: String by settings | ||
plugins { | ||
id("com.google.devtools.ksp") version kspVersion | ||
kotlin("jvm") version kotlinVersion | ||
} | ||
repositories { | ||
maven(testRepo) | ||
gradlePluginPortal() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
} | ||
|
||
rootProject.name = "cmd-options" | ||
|
||
include(":workload") | ||
include(":processors") |
19 changes: 19 additions & 0 deletions
19
integration-tests/src/test/resources/cmd-options/workload/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
val testRepo: String by project | ||
|
||
plugins { | ||
id("com.google.devtools.ksp") | ||
kotlin("jvm") | ||
} | ||
|
||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
maven(testRepo) | ||
mavenCentral() | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/") | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
ksp(project(":processors")) | ||
} |
4 changes: 4 additions & 0 deletions
4
integration-tests/src/test/resources/cmd-options/workload/src/main/kotlin/com/example/A.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.example | ||
|
||
fun main() { | ||
} |