-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BuildResultExtensions.kt
86 lines (71 loc) · 2.75 KB
/
BuildResultExtensions.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
// Named so that Groovy code can consume it as extension methods as well
// (see ExtensionModule in META-INF)
@file:JvmName("BuildResultExtensions")
package net.twisterrob.gradle.test
import org.gradle.testkit.runner.BuildResult
import org.junit.Assert.assertTrue
/**
* Matches a full line containing the regex.
* [expectedLineRegex] needs to include `.*` if the line beginning/end doesn't matter.
*/
fun BuildResult.assertHasOutputLine(expectedLineRegex: Regex) {
assertHasOutputLine(null, expectedLineRegex)
}
/**
* Matches a full line containing the text. An exact match will be performed against [expectedLine].
*/
fun BuildResult.assertHasOutputLine(expectedLine: String) {
assertHasOutputLine(null, expectedLine)
}
fun BuildResult.assertHasOutputLine(reason: String?, expectedLineRegex: Regex) {
assertRegex(reason, """(?m)^${expectedLineRegex.pattern}$""".toRegex())
}
fun BuildResult.assertHasOutputLine(reason: String?, expectedLine: String) {
assertRegex(reason, """(?m)^${Regex.escape(expectedLine)}$""".toRegex())
}
/**
* Matches a full line not containing the regex.
* [unexpectedLineRegex] needs to include `.*` if the line beginning/end doesn't matter.
*/
fun BuildResult.assertNoOutputLine(unexpectedLineRegex: Regex) {
assertNoOutputLine(null, unexpectedLineRegex)
}
/**
* Matches a full line containing the text. An exact match will be performed against [unexpectedLine].
* Use [#assertNoOutputLine(Regex)] with heading/trailing `.*` if parts don't matter.
*/
fun BuildResult.assertNoOutputLine(unexpectedLine: String) {
assertNoOutputLine(null, unexpectedLine)
}
fun BuildResult.assertNoOutputLine(reason: String?, unexpectedLineRegex: Regex) {
assertRegex(reason, """(?m)^${unexpectedLineRegex.pattern}$""".toRegex(), false)
}
fun BuildResult.assertNoOutputLine(reason: String?, unexpectedLine: String) {
assertRegex(reason, """(?m)^${Regex.escape(unexpectedLine)}$""".toRegex(), false)
}
private fun BuildResult.assertRegex(reason: String?, regex: Regex, positive: Boolean = true) {
val message = @Suppress("MultilineRawStringIndentation") """
${reason.orEmpty()}
Expected:
${if (positive) "" else "No match for "}${regex}
Actual:
${output.prependIndent("\t\t")}
""".trimIndent()
assertTrue(message, positive == regex.containsMatchIn(output))
}
val BuildResult.failReason: String
get() = findFailureBlock("What went wrong")
val BuildResult.failSuggestion: String
get() = findFailureBlock("Try")
val BuildResult.fullException: String
get() = findFailureBlock("Exception is")
fun BuildResult.findFailureBlock(label: String): String {
val fullLabel = "* ${label}:"
return output
.split(System.lineSeparator())
.dropWhile { it != fullLabel }
.drop(1)
.takeWhile { !it.startsWith("* ") }
.joinToString(System.lineSeparator())
.trim()
}