Skip to content

Commit

Permalink
add a test that repro google#2072 via a TriggerExceptionProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
asapha authored and ting-yuan committed Oct 2, 2024
1 parent ee09be1 commit 135e05f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,43 @@ class KMPImplementedIT(val useKSP2: Boolean) {
}
}

@Test
fun triggerException() {
Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows", ignoreCase = true))
val gradleRunner = GradleRunner.create().withProjectDir(project.root)
val path = "workload/src/commonMain/kotlin/com/example/FooBar.kt"
val file = File(project.root, path)

fun setup(shouldFail: Boolean) {
project.restore(path)

// Add an annotation that'll will make the processor trigger an exception.
if (shouldFail) {
file.writeText(
file.readText()
.replace("//@TriggerExceptionAnnotation", "@TriggerExceptionAnnotation")
)
}
}

// Start the kotlin daemon?
setup(shouldFail = false)
gradleRunner.withArguments("compileKotlinJvm").build()

// Make the 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"))
}
}

@Test
fun testWasm() {
Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows", ignoreCase = true))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example

annotation class TriggerExceptionAnnotation
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate

class TriggerExceptionProcessor(val codeGenerator: CodeGenerator, val logger: KSPLogger) : SymbolProcessor {
var invoked = false

override fun process(resolver: Resolver): List<KSAnnotated> {
if (invoked) {
return emptyList()
}
invoked = true

val symbols = resolver.getSymbolsWithAnnotation("com.example.TriggerExceptionAnnotation")
if (symbols.any()) {
logger.error("Exception triggered")
error("Exception triggered")
}

codeGenerator.createNewFile(
Dependencies(true),
"",
"NoTrigger",
"kt"
)

return emptyList()
}
}

class TriggerExceptionProvider : SymbolProcessorProvider {
override fun create(env: SymbolProcessorEnvironment): SymbolProcessor {
return TriggerExceptionProcessor(env.codeGenerator, env.logger)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
TestProcessorProvider
ValidateProcessorProvider
ErrorProcessorProvider
TriggerExceptionProvider

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example

//@TriggerExceptionAnnotation
class FooBar

0 comments on commit 135e05f

Please sign in to comment.