Testing tools for kotlinc and kapt

Overview

kompile-testing Build Status

A library for testing kotlinc compilation with kotlin annotation processors(kapt) which is strongly inspired by google/compile-testing.

kompile-testing gives you an easy way to assert not only annotation processors can handle incorrect inputs and prints the correct errors but also the generated Kotlin file is correctly formed.

NOTE: Current library's status is still in development especially due to performance issue. Use at your own risk:D

Installation

latestVersion is Download

dependencies {
    testImplementation "org.permissionsdispatcher:kompile-testing:{latestVersion}"
}

Usage

A simple example that tests compiling a source file succeeded and make assertions about generated file is:

kotlinc()
    .withProcessors(YourProcessor())
    .addKotlin("input.kt", """
        import kompile.testing.TestAnnotation

        @TestAnnotation
        class TestClass
        """.trimIndent())
        .compile()
        .succeededWithoutWarnings()
        .generatedFile("generatedKtFile.kt")
        .hasSourceEquivalentTo("""
            class GeneratedKtFile
        """.trimIndent())

You can also test that errors or warnings were reported.

The following tests compiling a source file with an annotation processor reported an error:

kotlinc()
    .withProcessors(YourProcessor())
    .addKotlin("input.kt", """
        import kompile.testing.TestAnnotation

        @TestAnnotation
        class TestClass
        """.trimIndent())
        .compile()
        .failed()
        .withErrorContaining("Expected error message here.")

License

Copyright (C) 2019 permissions-dispatcher contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • IOException when compiling file

    IOException when compiling file

    Tried running:

    @Test
        fun firstTest() {
    
            //val inFiles = dirToObjectFiles("com.github.alexdochioiu.test1")
    
            kotlinc()
                .withProcessors(TimeTurnerProcessor())
                .addKotlin(
                    "input.kt", """
            package com.github.alexdochioiu.example
    
            import android.arch.lifecycle.Lifecycle
            import com.github.alexdochioiu.timeturner.GlobalSurvivor
            import com.github.alexdochioiu.timeturner.Survivor
            import com.github.alexdochioiu.timeturner.SurvivorBinding
            import java.util.*
    
            /**
             * Created by Alexandru Iustin Dochioiu on 30-Jan-19
             */
            @Survivor
            class TestPresenter(lifecycle: Lifecycle) {
    
                @GlobalSurvivor
                var survivingElements: MutableList<String> = ArrayList()
    
                @GlobalSurvivor
                var myInt: Int = 2
    
                @GlobalSurvivor
                var myBool: Boolean = true
    
                @GlobalSurvivor
                var myStr = "Test"
    
                init {
                }
            }
            """.trimIndent()
                )
                .compile()
                .succeeded()
    
        }
    
    java.io.IOException: Directory 'C:\Users\JeeFo\AppData\Local\Temp\tmp9008736054030136230.tmp\unzippedAar\C:\Users\JeeFo\.gradle\caches\modules-2\files-2.1\com.android.support\appcompat-v7\28.0.0\132586ec59604a86703796851a063a0ac61f697b' could not be created
    
    	at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:361)
    	at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:324)
    	at org.apache.commons.io.FileUtils.copyToFile(FileUtils.java:1529)
    	at org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1506)
    	at kompile.testing.Compiler.extractJarFromAar(Compiler.kt:143)
    	at kompile.testing.Compiler.access$extractJarFromAar(Compiler.kt:22)
    	at kompile.testing.Compiler$classpathFiles$2.invoke(Compiler.kt:130)
    	at kompile.testing.Compiler$classpathFiles$2.invoke(Compiler.kt:22)
    	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    	at kompile.testing.Compiler.getClasspathFiles(Compiler.kt)
    	at kompile.testing.Compiler.fullClasspath(Compiler.kt:95)
    	at kompile.testing.Compiler.compile(Compiler.kt:58)
    	at com.github.alexdochioiu.timeturnerprocessor.TimeTurnerProcessorTest.firstTest(TimeTurnerProcessorTest.kt:70)
    

    P.S. I am running Android Studio 3.3.1 on Windows 10

    bug 
    opened by AlexDochioiu 5
  • Apache license violation and original copyright omission

    Apache license violation and original copyright omission

    The file https://github.com/permissions-dispatcher/kompile-testing/blob/master/src/main/java/kompile/testing/Compiler.kt is derived from https://github.com/square/moshi/blob/master/kotlin/codegen/src/test/java/com/squareup/moshi/kotlin/codegen/KotlinCompilerCall.kt but does not retain the copyright designation and license information in the derived work. In order to use this code, you must comply with item 4 of the Apache license (the license of the original work).

    opened by JakeWharton 3
  • Support Filer API-generated files

    Support Filer API-generated files

    I simply changed kapt.kotlin.generated to the same directory where Filer API creates files so that both old way and Filer API are supported. We could keep them separate but I think eventually all APs should migrate to Filer API.

    opened by technoir42 2
  • Error regarding tools.jar during test run

    Error regarding tools.jar during test run

    I have this test

    class SampleTest {
      @Test
      fun testTriggersErrorWhenAppliedToClass() {
        kotlinc()
          .withProcessors(ModelGeneratorProcessor())
          .addKotlin("TestModel.kt", """
            fun main() {
            
            }
          """.trimIndent())
          .compile()
          .failed()
          .withErrorContaining("blah")
      }
    }
    

    It fails with

    org.junit.ComparisonFailure: null 
    Expected :blah
    Actual   :error: [kapt] 'com.sun.tools.javac.util.Context' class can't be found ('tools.jar' is absent in the plugin classpath). Kapt won't work.
    

    I am using kotlin 1.3.21, The build.gradle contains:

      testImplementation("org.permissionsdispatcher:kompile-testing:0.1.2")
      testImplementation(files(Jvm.current().toolsJar)) // tried to add this, didn't help
    

    Any hints on what do I do wrong?

    question 
    opened by dimsuz 5
  • Improve performance

    Improve performance

    Currently library's performance is not so good because there're so many File I/O. We should improve that by introducing on-memory based File manipulation system.

    ref: https://github.com/google/compile-testing/blob/master/src/main/java/com/google/testing/compile/InMemoryJavaFileManager.java

    enhancement help wanted 
    opened by hotchemi 1
Owner
PermissionsDispatcher
PermissionsDispatcher
📒 NotyKT is a complete 💎Kotlin-stack (Backend + Android) 📱 application built to demonstrate the use of Modern development tools with best practices implementation🦸.

NotyKT ??️ NotyKT is the complete Kotlin-stack note taking ??️ application ?? built to demonstrate a use of Kotlin programming language in server-side

Shreyas Patil 1.4k Dec 26, 2022
Shreyas Patil 2.2k Jan 4, 2023
Kotlin-phoenix - A set of tools aimed to bridge Phoenix with the Kotlin Multiplatform world

Kotlin Phoenix This project is aimed to allow developers to work with Phoenix Ch

Adrien Jacquier Bret 5 Sep 21, 2022
A starter project to build command-line tools in Kotlin Multiplatform

A starter project to build command-line tools in Kotlin Multiplatform Contains a re-implementation of a real world CLI tool: git-standup Installation

null 0 May 2, 2022
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles

This repository serves as template and demo for building android applications for scale. It is suited for large teams where individuals can work independently on feature wise and layer wise reducing the dependency on each other.

Devrath 11 Oct 21, 2022
Tools & tips to learn about recomposition in Jetpack Compose

⚡ Recomposition in Jetpack Compose List of practical tips and code snippets to avoid unnecessary recomposition in Jetpack Compose. This is an active r

Aida Issayeva 62 Nov 22, 2022
Tools for Kotlin/Kscript to easy write shell command line in kotlin code

Kscript Tools Easy way to run shell command line in kotlin and other tools Usage Used in kscript: @file:DependsOn("com.sealwu:kscript-tools:1.0.2") Us

Seal 4 Dec 12, 2022
Nice and simple DSL for Espresso Compose UI testing in Kotlin

Kakao Compose Nice and simple DSL for Espresso Compose in Kotlin Benefits Readability Reusability Extensible DSL How to use it Create Screen Create yo

null 74 Dec 26, 2022
A seed and demo about how to do end-to-end testing of a Dataflow pipeline

dataflow-e2e-demo This is a demo and a seed project to show how you can end-to-end test a Dataflow pipeline. You can find more about by follwing this

null 0 Dec 18, 2021
Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.

Kata Maxibon for Kotlin. We are here to practice property based testing. We are going to use KotlinTest to write our tests. We are going to practice p

Karumi 44 Oct 3, 2022
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

Karumi 61 Nov 20, 2022
Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.

KataScreenshot in Kotlin We are here to practice UI testing using screenshot tests for Android. We are going to use Espresso to interact with the Appl

Karumi 76 Nov 20, 2022
Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.

KataSuperHeroes in Kotlin We are here to practice UI Testing. We are going to use Espresso to interact with the Application UI. We are going to use Ko

Karumi 86 Nov 20, 2022
Kotlin Unit Testing Examples

Kotlin Unit Testing Examples Table of Contents Application Gradle, Kotlin & Groovy Junit4 Junit5 KotlinTest Spek Mockito Mockito-Kotlin Mockk Strikt T

Jarosław 110 Dec 11, 2022
Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3

TO-DO Notes - Code for 5.1-5.3 Testing Codelab Code for the Advanced Android Kotlin Testing Codelab 5.1-5.3 Introduction TO-DO Notes is an app where y

Jorge M 1 Jun 7, 2022
Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

JetBrains 10k Jan 7, 2023
A counter down timer for android which supports both dark and light mode and Persian text and digit.

FlipTimerView A counter down timer for android which supports both dark and light mode and Persian text and digit. English Perisan Getting started Ste

Arezoo Nazer 7 Jul 17, 2022