在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tschuchortdev/kotlin-compile-testing开源软件地址(OpenSource Url):https://github.com/tschuchortdev/kotlin-compile-testing开源编程语言(OpenSource Language):Kotlin 97.5%开源软件介绍(OpenSource Introduction):Kotlin Compile TestingA library for in-process compilation of Kotlin and Java code, in the spirit of Google Compile Testing. For example, you can use this library to test your annotation processor or compiler plugin. Use Cases
ExampleCreate sources class TestEnvClass {}
@Test
fun `test my annotation processor`() {
val kotlinSource = SourceFile.kotlin(
"KClass.kt", """
class KClass {
fun foo() {
// Classes from the test environment are visible to the compiled sources
val testEnvClass = TestEnvClass()
}
}
"""
)
val javaSource = SourceFile.java(
"JClass.java", """
public class JClass {
public void bar() {
// compiled Kotlin classes are visible to Java sources
KClass kClass = new KClass();
}
}
""") Configure compilation val result = KotlinCompilation().apply {
sources = listOf(kotlinSource, javaSource)
// pass your own instance of an annotation processor
annotationProcessors = listOf(MyAnnotationProcessor())
// pass your own instance of a compiler plugin
compilerPlugins = listOf(MyComponentRegistrar())
commandLineProcessors = listOf(MyCommandlineProcessor())
inheritClassPath = true
messageOutputStream = System.out // see diagnostics in real time
}.compile() Assert results assertThat(result.exitCode).isEqualTo(ExitCode.OK)
// Test diagnostic output of compiler
assertThat(result.messages).contains("My annotation processor was called")
// Load compiled classes and inspect generated code through reflection
val kClazz = result.classLoader.loadClass("KClass")
assertThat(kClazz).hasDeclaredMethods("foo")
} Features
InstallationThe package is available on Maven Central. Add dependency to your module's dependencies {
// ...
testImplementation 'com.github.tschuchortdev:kotlin-compile-testing:1.4.9'
} Remember to leave a star if you found it useful Compatible Compiler VersionsKotlin-Compile-Testing is compatible with all local compiler versions. It does not matter what compiler you use to compile your project. However, if your project or any of its dependencies depend directly on compiler artifacts such as
Because the internal APIs of the Kotlin compiler often change between versions, we can only support one Kotlin Symbol Processing API SupportKotlin Symbol Processing (KSP) is a new annotation processing pipeline that builds on top of the
plugin architecture of the Kotlin Compiler, instead of delegating to javac as To test KSP processors, you need to use the KSP dependency: dependencies {
testImplementation 'com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.9'
} This module adds a new function to the class MySymbolProcessorProvider : SymbolProcessorProvider {
// implementation of the SymbolProcessorProvider from the KSP API
}
val compilation = KotlinCompilation().apply {
sources = listOf(source)
symbolProcessorProviders = listOf(MySymbolProcessorProvider())
}
val result = compilation.compile() All code generated by the KSP processor will be written into the Projects that use Kotlin-Compile-Testing
Java 16 compatibilityWith the release of Java 16 the access control of the new Jigsaw module system is starting to be enforced by the JVM. Unfortunately, this impacts kotlin-compile-testing because KAPT still tries to access classes of javac that are not exported by the jdk.compiler module, leading to errors such as:
To mitigate this problem, you have to add the following code to your module's if (JavaVersion.current() >= JavaVersion.VERSION_16) {
test {
jvmArgs(
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
)
}
} or for Kotlin DSL if (JavaVersion.current() >= JavaVersion.VERSION_16) {
tasks.withType<Test>().all {
jvmArgs(
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
)
}
} Since the kotlin compilation tests run in the same process as the test runner, these options have to be added manually and can not be set automatically by the kotlin-compile-testing library. LicenseCopyright (C) 2021 Thilo Schuchort Licensed under the Mozilla Public License 2.0 For custom license agreements contact me directly |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论