Giter VIP home page Giter VIP logo

kotlin-compile-testing's Introduction

Kotlin Compile Testing

Maven Central GitHub Maintenance Generic badge Build Status Build status

A 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

  • Compile Kotlin and Java code in tests
  • Test annotation processors
  • Test compiler plugins
  • Test Kotlin code generation

Example

Create 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

  • Mixed-source sets: Compile Kotlin and Java source files in a single run
  • Annotation processing:
    • Run annotation processors on Kotlin and Java sources
    • Generate Kotlin and Java sources
    • Both Kotlin and Java sources have access to the generated sources
    • Provide your own instances of annotation processors directly to the compiler instead of letting the compiler create them with a service locator
    • Debug annotation processors: Since the compilation runs in the same process as your application, you can easily debug it instead of having to attach your IDE's debugger manually to the compilation process
  • Inherit classpath: Compiled sources have access to classes in your application
  • Project Jigsaw compatible: Kotlin-Compile-Testing works with JDK 8 as well as JDK 9 and later
  • JDK-crosscompilation: Provide your own JDK to compile the code against, instead of using the host application's JDK. This allows you to easily test your code on all JDK versions
  • Find dependencies automatically on the host classpath

Installation

The package is available on mavenCentral and jitpack.

Add dependency to your module's build.gradle file:

dependencies {
        // ...
	implementation 'com.github.tschuchortdev:kotlin-compile-testing:1.3.1'
}

Remember to leave a star if you found it useful

Compatible Compiler Versions

Kotlin-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 kotlin-compiler-embeddable or kotlin-annotation-processing-embeddable then they have to be the same version as the one used by Kotlin-Compile-Testing or there will be a transitive dependency conflict.

  • Current kotlin-compiler-embeddable version: 1.4.10

Because the internal APIs of the Kotlin compiler often change between versions, we can only support one kotlin-compiler-embeddable version at a time.

Kotlin Symbol Processing API Support

Kotlin 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 kapt does.

To test KSP processors, you need to use the KSP dependency:

dependencies {
    implementation 'com.github.tschuchortdev:kotlin-compile-testing-ksp:1.3.1'
}

This module adds a new function to the KotlinCompilation to specify KSP processors:

class MySymbolProcessor : SymbolProcessor {
    // implementation of the SymbolProcessor from the KSP API
}
val compilation = KotlinCompilation().apply {
    sources = listOf(source)
    symbolProcessors = listOf(MySymbolProcessor())
}
val result = compilation.compile()

All code generated by the KSP processor will be written into the KotlinCompilation.kspSourcesDir directory.

Projects that use Kotlin-Compile-Testing

License

Copyright (C) 2019 Thilo Schuchort

Licensed under the Mozilla Public License 2.0

For custom license agreements contact me directly

kotlin-compile-testing's People

Contributors

tschuchortdev avatar yigit avatar sullis avatar zacsweers avatar bnorm avatar rachelcarmena avatar evant avatar neetopia avatar tyvsmith avatar foso avatar smaugfm avatar ahmedmourad0 avatar neugartf avatar jamie-houston avatar 3flex avatar ansman avatar petedmarsh avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.