Skip to content

Commit

Permalink
Adds javaAP precondition to KotlinPoet integration
Browse files Browse the repository at this point in the history
Also adds tests for the changes and marks tests which skip backends it
does not target with `AssumptionViolatedException`.
  • Loading branch information
gcvasconcelos-picpay committed Mar 10, 2021
1 parent 92977af commit bcf7ef7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.squareup.kotlinpoet.BOOLEAN
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.TypeSpec as KTypeSpec
import org.junit.Test
import org.junit.AssumptionViolatedException
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

Expand Down Expand Up @@ -118,7 +119,9 @@ class GeneratedCodeMatchTest internal constructor(
@Test
fun successfulGeneratedKotlinCodeMatch() {
// java environment will not generate kotlin files
if (runTest.toString() == "java") return
if (runTest.toString() == "java") {
throw AssumptionViolatedException("javaAP won't generate kotlin code.")
}

val file = FileSpec.builder("foo.bar", "Baz")
.addType(KTypeSpec.classBuilder("Baz").build())
Expand All @@ -138,7 +141,9 @@ class GeneratedCodeMatchTest internal constructor(
@Test
fun missingGeneratedKotlinCode_mismatch() {
// java environment will not generate kotlin files
if (runTest.toString() == "java") return
if (runTest.toString() == "java") {
throw AssumptionViolatedException("javaAP won't generate kotlin code.")
}

val generated = FileSpec.builder("foo.bar", "Baz")
.addType(
Expand Down Expand Up @@ -180,4 +185,36 @@ class GeneratedCodeMatchTest internal constructor(
)
assertThat(result.exceptionOrNull()).hasMessageThat().contains(mismatch.toString())
}

@Test
fun missingGeneratedKotlinCode_javaAP() {
if (runTest.toString() != "java") {
throw AssumptionViolatedException("Testing scenario for javaAP only.")
}

val file = FileSpec.builder("foo.bar", "Baz")
.addType(KTypeSpec.classBuilder("Baz").build())
.build()

val result = runCatching {
runTest { invocation ->
if (invocation.processingEnv.findTypeElement("foo.bar.Baz") == null) {
invocation.processingEnv.filer.write(file)
}
invocation.assertCompilationResult {
generatedSource(
Source.kotlin("foo/bar/Baz.kt", file.toString())
)
}
}
}

assertThat(result.exceptionOrNull())
.hasCauseThat()
.hasMessageThat()
.contains(
"Could not generate kotlin file foo/bar/Baz.kt. The annotation processing " +
"environment is not set to generate Kotlin files."
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import androidx.room.compiler.processing.XFiler
import com.squareup.javapoet.JavaFile
import com.squareup.kotlinpoet.FileSpec
import javax.annotation.processing.Filer
import javax.annotation.processing.ProcessingEnvironment

internal class JavacFiler(val filer: Filer) : XFiler {
internal class JavacFiler(val processingEnv: ProcessingEnvironment) : XFiler {
override fun write(javaFile: JavaFile) {
javaFile.writeTo(filer)
javaFile.writeTo(processingEnv.filer)
}

override fun write(fileSpec: FileSpec) {
fileSpec.writeTo(filer)
require(processingEnv.options.containsKey("kapt.kotlin.generated")) {
val filePath = fileSpec.packageName.replace('.', '/')
"Could not generate kotlin file $filePath/${fileSpec.name}.kt. The " +
"annotation processing environment is not set to generate Kotlin files."
}
fileSpec.writeTo(processingEnv.filer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class JavacProcessingEnv(
JavacProcessingEnvMessager(delegate)
}

override val filer = JavacFiler(delegate.filer)
override val filer = JavacFiler(delegate)

override val options: Map<String, String>
get() = delegate.options
Expand Down

0 comments on commit bcf7ef7

Please sign in to comment.