forked from airbnb/epoxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateProcessorTestResources.kt
77 lines (64 loc) · 2.78 KB
/
UpdateProcessorTestResources.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
#!/usr/bin/env kscript
@file:DependsOn("org.jsoup:jsoup:1.13.1")
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.io.File
fun main() {
val testResultHtmlRegex = Regex("/build/reports/tests/.*/classes/.*\\.html")
File(".")
.walk()
.filter { it.isFile }
.filter { it.path.contains(testResultHtmlRegex) }
.forEach { updateTestClass(it) }
}
fun updateTestClass(testReport: File) {
val doc: Document = Jsoup.parse(testReport, "UTF-8")
// Failing processor tests have their output in a <pre></pre> block
doc.getElementsByTag("pre")
.filter { element ->
// A failing block contains the text "Source declared the same top-level types of an expected source, but
// didn't match exactly."
element.text().contains("Source declared the same top-level types of an expected source")
}.map { it.text() }
.forEach { failingTestText ->
updateIndividualTest(failingTestText)
}
}
private fun updateIndividualTest(failingTestText: String) {
val expectedFile = expectedFileRegex
.find(failingTestText)
?.groupValues
?.getOrNull(1)
?.let { filePath ->
// The test copies the source file to the build folder. We need to modify the original file to update its expected source
File(
filePath.replace(
"/build/intermediates/sourceFolderJavaResources/debug/",
"/src/test/resources/"
)
)
}
?.takeIf { it.isFile }
?: error("Count not find expected file in $failingTestText")
// The error message includes the source code that was generated.
// Actual Source:
//=================
// [code here]
//
// javaSources was: [com.google.testing.compile.JavaFileObjects$ResourceSourceJavaFileObject[file:/Users/elihart/repos/epoxy/epoxy-modelfactorytest/build/intermediates/sourceFolderJavaResources/debug/GroupPropMultipleSupportedAttributeDifferentNameModelView.java]]
// at com.airbnb.epoxy.ProcessorTestUtils.assertGeneration(ProcessorTestUtils.kt:33)
// ...
val actualSource = failingTestText.substringAfter(
"""
Actual Source:
=================
""".trimIndent()
).substringBefore("javaSources was:")
.substringBefore("object was:")
expectedFile.writeText(actualSource)
println("Updated test source ${expectedFile.path.substringAfter("/epoxy/")}")
}
// We expect to see a line like:
// Expected file: </Users/elihart/repos/epoxy/epoxy-modelfactorytest/build/intermediates/sourceFolderJavaResources/debug/AllTypesModelViewModel_.java>
// Which tells us where the original processor test file lives
val expectedFileRegex = Regex("Expected file: <(.*)>")