forked from google/ksp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KSPCmdLineOptionsIT.kt
85 lines (79 loc) · 3.95 KB
/
KSPCmdLineOptionsIT.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.google.devtools.ksp.test
import org.gradle.testkit.runner.GradleRunner
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.net.URLClassLoader
data class CompileResult(val exitCode: ExitCode, val output: String)
class KSPCmdLineOptionsIT {
@Rule
@JvmField
val project: TemporaryTestProject = TemporaryTestProject("cmd-options")
private fun runCmdCompiler(pluginOptions: List<String>): CompileResult {
val gradleRunner = GradleRunner.create().withProjectDir(project.root)
gradleRunner.withArguments("clean", ":processors:build").build()
val processorJar = File(project.root, "processors/build/libs/processors-1.0-SNAPSHOT.jar")
val classLoader = URLClassLoader(arrayOf(processorJar.toURI().toURL()), javaClass.classLoader)
val compiler = classLoader.loadClass(K2JVMCompiler::class.java.name).newInstance() as K2JVMCompiler
val repoPath = "../build/repos/test/com/google/devtools/ksp/"
val kspPluginId = "com.google.devtools.ksp.symbol-processing"
val kspPluginJar = File("$repoPath/symbol-processing-cmdline/2.0.255-SNAPSHOT").listFiles()!!.filter {
it.name.matches(Regex(".*-\\d.jar"))
}.maxByOrNull { it.lastModified() }!!
val kspApiJar = File("$repoPath/symbol-processing-api/2.0.255-SNAPSHOT").listFiles()!!.filter {
it.name.matches(Regex(".*-\\d.jar"))
}.maxByOrNull { it.lastModified() }!!
val compilerArgs = mutableListOf(
"-no-stdlib",
"-Xplugin=${kspPluginJar.absolutePath}",
"-Xplugin=${kspApiJar.absolutePath}",
"-P", "plugin:$kspPluginId:apclasspath=${processorJar.absolutePath}",
"-P", "plugin:$kspPluginId:projectBaseDir=${project.root}/build",
"-P", "plugin:$kspPluginId:classOutputDir=${project.root}/build",
"-P", "plugin:$kspPluginId:javaOutputDir=${project.root}/build/out",
"-P", "plugin:$kspPluginId:kotlinOutputDir=${project.root}/build/out",
"-P", "plugin:$kspPluginId:resourceOutputDir=${project.root}/build/out",
"-P", "plugin:$kspPluginId:kspOutputDir=${project.root}/build/out",
"-P", "plugin:$kspPluginId:cachesDir=${project.root}/build/out",
"-P", "plugin:$kspPluginId:incremental=false",
"-d", "${project.root}/build/out"
)
pluginOptions.forEach {
compilerArgs.add("-P")
compilerArgs.add("plugin:$kspPluginId:$it")
}
compilerArgs.add(File(project.root, "workload/src/main/kotlin/com/example/A.kt").absolutePath)
val outStream = ByteArrayOutputStream()
val exitCode = compiler.exec(PrintStream(outStream), *compilerArgs.toTypedArray())
return CompileResult(exitCode, outStream.toString())
}
@Test
fun testWithCompilationOnError() {
val result = runCmdCompiler(listOf("apoption=error=true", "withCompilation=true"))
val errors = result.output.lines().filter { it.startsWith("error: [ksp]") }
val exitCode = result.exitCode
Assert.assertTrue(exitCode == ExitCode.COMPILATION_ERROR)
Assert.assertTrue(
errors.any {
it.startsWith("error: [ksp] java.lang.IllegalStateException: Error on request")
}
)
}
@Test
fun testWithCompilationOnErrorOk() {
val result = runCmdCompiler(listOf("apoption=error=true", "returnOkOnError=true", "withCompilation=true"))
val errors = result.output.lines().filter { it.startsWith("error: [ksp]") }
val exitCode = result.exitCode
Assert.assertTrue(exitCode == ExitCode.OK)
Assert.assertTrue(
errors.any {
it.startsWith("error: [ksp] java.lang.IllegalStateException: Error on request")
}
)
}
}