forked from bumptech/glide
/
SourceTestHelpers.kt
123 lines (105 loc) · 3.94 KB
/
SourceTestHelpers.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
package com.bumptech.glide.annotation.ksp.test
import com.bumptech.glide.annotation.ksp.GlideSymbolProcessorProvider
import com.google.common.truth.StringSubject
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspSourcesDir
import com.tschuchort.compiletesting.symbolProcessorProviders
import java.io.File
import java.io.FileNotFoundException
import org.intellij.lang.annotations.Language
internal class CompilationResult(
private val compilation: KotlinCompilation,
result: KotlinCompilation.Result,
) {
val exitCode = result.exitCode
val messages = result.messages
fun generatedAppGlideModuleContents() = readFile(findAppGlideModule())
fun allGeneratedFiles(): List<File> {
val allFiles = mutableListOf<File>()
val parentDir = generatedFilesParentDir()
if (parentDir != null) {
findAllFilesRecursive(parentDir, allFiles)
}
return allFiles
}
private fun findAllFilesRecursive(parent: File, allFiles: MutableList<File>) {
if (parent.isFile) {
allFiles.add(parent)
return
}
parent.listFiles()?.map { findAllFilesRecursive(it, allFiles) }
}
private fun generatedFilesParentDir(): File? {
var currentDir: File? = compilation.kspSourcesDir
listOf("kotlin", "com", "bumptech", "glide").forEach { directoryName ->
currentDir = currentDir?.listFiles()?.find { it.name.equals(directoryName) }
}
return currentDir
}
private fun readFile(file: File) = file.readLines().joinToString("\n")
private fun findAppGlideModule(): File {
return generatedFilesParentDir()?.listFiles()?.find { it.name.equals("GeneratedAppGlideModuleImpl.kt") }
?: throw FileNotFoundException(
"GeneratedAppGlideModuleImpl.kt was not generated or not generated in the expected" +
"location"
)
}
}
enum class SourceType {
KOTLIN,
JAVA
}
sealed interface TypedSourceFile {
fun sourceFile(): SourceFile
fun sourceType(): SourceType
}
internal class GeneratedSourceFile(
private val file: File, private val currentSourceType: SourceType,
) : TypedSourceFile {
override fun sourceFile(): SourceFile = SourceFile.fromPath(file)
// Hack alert: We use this class only for generated output of some previous compilation. We rely
// on the type in that previous compilation to select the proper source. The output however is
// always Kotlin, regardless of source. But we always want to include whatever the generated
// output is in the next step. That means we need our sourceType here to match the
// currentSourceType in the test.
override fun sourceType(): SourceType = currentSourceType
}
internal class KotlinSourceFile(
val name: String,
@Language("kotlin") val content: String,
) : TypedSourceFile {
override fun sourceFile() = SourceFile.kotlin(name, content)
override fun sourceType() = SourceType.KOTLIN
}
internal class JavaSourceFile(
val name: String,
@Language("java") val content: String,
) : TypedSourceFile {
override fun sourceFile() = SourceFile.java(name, content)
override fun sourceType() = SourceType.JAVA
}
internal interface PerSourceTypeTest {
val sourceType: SourceType
fun compileCurrentSourceType(
vararg sourceFiles: TypedSourceFile,
test: (input: CompilationResult) -> Unit = {},
) : CompilationResult {
val result =
compile(sourceFiles.filter { it.sourceType() == sourceType }.map { it.sourceFile() }.toList())
test(result)
return result
}
}
internal fun compile(sourceFiles: List<SourceFile>): CompilationResult {
require(sourceFiles.isNotEmpty())
val compilation =
KotlinCompilation().apply {
sources = sourceFiles
symbolProcessorProviders = listOf(GlideSymbolProcessorProvider())
inheritClassPath = true
}
val result = compilation.compile()
return CompilationResult(compilation, result)
}
fun StringSubject.hasSourceEqualTo(sourceContents: String) = isEqualTo(sourceContents.trimIndent())