-
Notifications
You must be signed in to change notification settings - Fork 105
/
BaseProcessorTest.kt
142 lines (120 loc) 路 5.04 KB
/
BaseProcessorTest.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.airbnb.android.showkase_processor_testing
import com.airbnb.android.showkase.processor.ShowkaseProcessor
import com.airbnb.android.showkase.processor.ShowkaseProcessorProvider
import com.google.common.io.Resources
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspArgs
import com.tschuchort.compiletesting.kspSourcesDir
import com.tschuchort.compiletesting.symbolProcessorProviders
import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import java.io.File
/**
* Temporarily set this to true to have the test runner update test resource file expected outputs
* instead of failing tests on mismatch. Use this to easily update expected outputs.
*/
const val UPDATE_TEST_OUTPUTS = true
abstract class BaseProcessorTest {
@Rule
@JvmField
val testNameRule = TestNameRule()
enum class Mode {
KSP, KAPT
}
/**
* Collects the files in the "input" directory of this test's resources directory
* and compiles them with Kotlin, returning the result.
*/
protected fun compileInputs(
modes: List<Mode> = listOf(Mode.KSP, Mode.KAPT),
options: MutableMap<String, String> = mutableMapOf(),
onCompilation: (mode: Mode, compilation: KotlinCompilation, result: KotlinCompilation.Result) -> Unit
) {
val testResourcesDir = getTestResourcesDirectory(getRootResourcesDir())
val inputDir = File(testResourcesDir, "input")
inputDir.mkdirs()
modes.forEach { mode ->
val compilation = KotlinCompilation().apply {
kotlincArguments = kotlincArguments + "-Xexplicit-api=strict"
sources = inputDir.listFiles()?.toList().orEmpty().map { SourceFile.fromPath(it) }
when (mode) {
Mode.KSP -> {
symbolProcessorProviders = listOf(ShowkaseProcessorProvider())
kspArgs = options
}
Mode.KAPT -> {
annotationProcessors = listOf(ShowkaseProcessor())
kaptArgs = options
}
}
inheritClassPath = true
messageOutputStream = System.out // see diagnostics in real time
}
val result = compilation.compile()
onCompilation(mode, compilation, result)
}
}
protected fun assertCompilationFails(errorMessage: String) {
compileInputs { _, _, result ->
assertThat(result.exitCode)
.isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
assertThat(result.messages)
.contains(errorMessage)
}
}
protected fun compileInputsAndVerifyOutputs(
modes:List<Mode> = listOf(Mode.KSP, Mode.KAPT),
options: MutableMap<String, String> = mutableMapOf(),
) {
compileInputs(modes = modes, options = options) { mode, compilation, result ->
result.assertGeneratedSources(mode, compilation)
}
}
/**
* Collects the files in the "output" directory of this test's resources directory
* and validates that they match the generated sources of this compilation result.
*/
protected fun KotlinCompilation.Result.assertGeneratedSources(mode: Mode, compilation: KotlinCompilation) {
assertThat(exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
val testResourcesDir = getTestResourcesDirectory(getRootResourcesDir())
val outputDir = File(testResourcesDir, "output")
if (UPDATE_TEST_OUTPUTS) {
outputDir.deleteRecursively()
}
outputDir.mkdirs()
val generatedSources = when (mode) {
Mode.KSP -> compilation.kspSourcesDir.walk().filter { it.isFile }.toList()
Mode.KAPT -> sourcesGeneratedByAnnotationProcessor
}
if (UPDATE_TEST_OUTPUTS) {
generatedSources.forEach {
println("Generated: ${it.name}")
it.copyTo(File(outputDir, it.name))
}
} else {
assertThat(generatedSources.size)
.isEqualTo(outputDir.listFiles()?.size ?: 0)
generatedSources.forEach { actualFile ->
println("Generated: ${actualFile.name}")
val expectedFile = File(outputDir, actualFile.name)
assertThat(expectedFile).exists()
assertThat(actualFile).hasSameTextualContentAs(expectedFile)
}
}
}
private fun getRootResourcesDir(): File {
val path = Resources.getResource("")
.path
.substringBefore("/build/")
.plus("/src/test/resources")
return File(path)
}
protected open fun getTestResourcesDirectory(rootResourcesDir: File): File {
val methodName = testNameRule
.methodName
.replace(" ", "_")
val className = testNameRule.className.substringAfterLast(".")
return File(rootResourcesDir, "$className/${methodName}")
}
}