Add integration test for Kotlin compiler plugin for Maven

This commit is contained in:
Sergey Mashkov
2016-10-10 16:16:02 +03:00
committed by Yan Zhulanow
parent f59377011c
commit 3149c75130
10 changed files with 333 additions and 0 deletions
@@ -0,0 +1,15 @@
[INFO] Kotlin Compiler version @snapshot@
[INFO] Compiling Kotlin sources from [/test-extension/src/main/kotlin]
[INFO] Module name is test-extension
[INFO] Kotlin Compiler version @snapshot@
[WARNING] No sources found skipping Kotlin compile
[INFO] Kotlin Compiler version @snapshot@
[INFO] Compiling Kotlin sources from [/use-test-extension/src/main/kotlin]
[INFO] Module name is use-test-extension
[INFO] Applicability test for project use-test-extension
[INFO] Applying plugin test-me
[INFO] Configuring test plugin with arguments
[INFO] Plugin applied
[INFO] Option value: my-special-value
[INFO] Kotlin Compiler version @snapshot@
[WARNING] No sources found skipping Kotlin compile
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-extensions-test</artifactId>
<version>1.1-SNAPSHOT</version>
<modules>
<module>test-extension</module>
<module>use-test-extension</module>
</modules>
<packaging>pom</packaging>
</project>
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kotlin-compiler-extensions-test</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-extension</artifactId>
<properties>
<kotlin.version>1.1-SNAPSHOT</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<id>process-classes</id>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
<execution>
<id>process-test-classes</id>
<goals>
<goal>generate-test-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,25 @@
package org.jetbrains.kotlin.test
import org.apache.maven.plugin.*
import org.apache.maven.project.*
import org.codehaus.plexus.component.annotations.*
import org.codehaus.plexus.logging.*
import org.jetbrains.kotlin.maven.*
@Component(role = KotlinMavenPluginExtension::class, hint = "test-me")
class MavenPluginComponent : KotlinMavenPluginExtension {
@Requirement
lateinit var logger: Logger
override fun isApplicable(project: MavenProject, execution: MojoExecution): Boolean {
logger.info("Applicability test for project ${project.artifactId}")
return true
}
override fun getPluginArguments(project: MavenProject, execution: MojoExecution): MutableList<String> {
logger.info("Configuring test plugin with arguments")
return mutableListOf("plugin:${TestCommandLineProcessor.TestPluginId}:${TestCommandLineProcessor.MyTestOption.name}=my-special-value")
}
}
@@ -0,0 +1,41 @@
package org.jetbrains.kotlin.test
import com.intellij.mock.*
import org.jetbrains.kotlin.cli.common.*
import org.jetbrains.kotlin.cli.common.messages.*
import org.jetbrains.kotlin.compiler.plugin.*
import org.jetbrains.kotlin.config.*
object TestPluginKeys {
val TestOption = CompilerConfigurationKey.create<String>("test option")!!
}
class TestCommandLineProcessor : CommandLineProcessor {
companion object {
val TestPluginId = "org.jetbrains.kotlin.test.test-plugin"
val MyTestOption = CliOption("test-option", "", "", true, false)
}
override val pluginId: String = TestPluginId
override val pluginOptions: Collection<CliOption> = listOf(MyTestOption)
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
when (option) {
MyTestOption -> {
configuration.put(TestPluginKeys.TestOption, value)
}
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
}
}
}
class TestKotlinPluginRegistrar : ComponentRegistrar {
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
val collector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)!!
val option = configuration.get(TestPluginKeys.TestOption)!!
collector.report(CompilerMessageSeverity.INFO, "Plugin applied", CompilerMessageLocation.NO_LOCATION)
collector.report(CompilerMessageSeverity.INFO, "Option value: $option", CompilerMessageLocation.NO_LOCATION)
}
}
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kotlin-compiler-extensions-test</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>use-test-extension</artifactId>
<properties>
<kotlin.version>1.1-SNAPSHOT</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>test-me</plugin>
</compilerPlugins>
<pluginArguments>
<argument>plugin:test-me:...=....</argument>
<!--<argument>plugin:allopen:annotations=ferfre</argument>-->
</pluginArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-extension</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,5 @@
package org.jetbrains.kotlin.test
fun main(args: Array<String>) {
println("OK")
}
@@ -0,0 +1,65 @@
import java.util.regex.Pattern
class State {
def currentPlugin = ""
def lines = []
}
def removePaths(String path, File basedir) {
while (basedir.parentFile != null) {
path = path.replace(basedir.absolutePath, "").replace(basedir.path, "")
basedir = basedir.parentFile
}
return path
}
System.out.flush()
def buildLogFile = new File(basedir, "build.log")
def pattern = Pattern.compile(/\[INFO\] --- ([^:]+):.*:\S+ \([^)]+\) @ [a-zA-Z_-]+ ---/)
State state = buildLogFile.readLines().collect { it.replaceAll("\\u001b[^m]*m", "") }.inject(new State()) { acc, line ->
def m = pattern.matcher(line)
if (m.find()) {
acc.currentPlugin = m.group(1)
} else if (line.startsWith("[INFO] Downloaded:") ||
line.startsWith("[INFO] Downloading:") ||
line.startsWith("Downloaded:") ||
line.startsWith("Downloading:") ||
line.startsWith("[INFO] PERF:")) {
// ignore line
} else if (acc.currentPlugin == "kotlin-maven-plugin") {
def filtered = removePaths(line, basedir).
replace("\\", "/").
replaceAll(/[0-9]+\s*ms/, "LLL ms").
trim().
replaceAll(/^\[[A-Z]+\]$/, "").
replaceAll(/version [0-9a-z-+\.]+/, "version @snapshot@")
if (filtered != "") {
acc.lines << filtered
}
}
acc
}
def expectedLog = new File(basedir, "expected.log").readLines().join("\n").trim()
def actualLog = state.lines.join("\n").trim()
if (expectedLog != actualLog) {
println "Expected and actual log differs!"
println ""
println "Actual log (from $buildLogFile):"
println actualLog
println ""
println "Expected log (from ${new File(basedir, "expected.log").absolutePath}):"
println expectedLog
println ""
return false
} else {
println "Log comparison succeeded"
}
return true