-
Notifications
You must be signed in to change notification settings - Fork 928
/
TestResultLogger.scala
159 lines (130 loc) · 6.44 KB
/
TestResultLogger.scala
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sbt
import sbt.Tests.Summary
/**
* Logs information about tests after they finish.
*
* Log output can be customised by providing a specialised instance of this
* trait via the `testResultLogger` setting.
*
* @since 0.13.5
*/
trait TestResultLogger {
/**
* Perform logging.
*
* @param log The target logger to write output to.
* @param results The test results about which to log.
* @param taskName The task about which we are logging. Eg. "my-module-b/test:test"
*/
def run(log: Logger, results: Tests.Output, taskName: String): Unit
/** Only allow invocation if certain criteria is met, else use another `TestResultLogger` (defaulting to nothing) . */
final def onlyIf(f: (Tests.Output, String) => Boolean, otherwise: TestResultLogger = TestResultLogger.Null) =
TestResultLogger.choose(f, this, otherwise)
/** Allow invocation unless a certain predicate passes, in which case use another `TestResultLogger` (defaulting to nothing) . */
final def unless(f: (Tests.Output, String) => Boolean, otherwise: TestResultLogger = TestResultLogger.Null) =
TestResultLogger.choose(f, otherwise, this)
}
object TestResultLogger {
/** A `TestResultLogger` that does nothing. */
val Null = const(_ => ())
/** SBT's default `TestResultLogger`. Use `copy()` to change selective portions. */
val Default = Defaults.Main()
/** Twist on the default which is completely silent when the subject module doesn't contain any tests. */
def SilentWhenNoTests = silenceWhenNoTests(Default)
/** Creates a `TestResultLogger` using a given function. */
def apply(f: (Logger, Tests.Output, String) => Unit): TestResultLogger =
new TestResultLogger {
override def run(log: Logger, results: Tests.Output, taskName: String) =
f(log, results, taskName)
}
/** Creates a `TestResultLogger` that ignores its input and always performs the same logging. */
def const(f: Logger => Unit) = apply((l, _, _) => f(l))
/**
* Selects a `TestResultLogger` based on a given predicate.
*
* @param t The `TestResultLogger` to choose if the predicate passes.
* @param f The `TestResultLogger` to choose if the predicate fails.
*/
def choose(cond: (Tests.Output, String) => Boolean, t: TestResultLogger, f: TestResultLogger) =
TestResultLogger((log, results, taskName) =>
(if (cond(results, taskName)) t else f).run(log, results, taskName))
/** Transforms the input to be completely silent when the subject module doesn't contain any tests. */
def silenceWhenNoTests(d: Defaults.Main) =
d.copy(
printStandard = d.printStandard.unless((results, _) => results.events.isEmpty),
printNoTests = Null
)
object Defaults {
/** SBT's default `TestResultLogger`. Use `copy()` to change selective portions. */
case class Main(
printStandard_? : Tests.Output => Boolean = Defaults.printStandard_?,
printSummary: TestResultLogger = Defaults.printSummary,
printStandard: TestResultLogger = Defaults.printStandard,
printFailures: TestResultLogger = Defaults.printFailures,
printNoTests: TestResultLogger = Defaults.printNoTests) extends TestResultLogger {
override def run(log: Logger, results: Tests.Output, taskName: String): Unit = {
def run(r: TestResultLogger): Unit = r.run(log, results, taskName)
run(printSummary)
if (printStandard_?(results))
run(printStandard)
if (results.events.isEmpty)
run(printNoTests)
else
run(printFailures)
results.overall match {
case TestResult.Error | TestResult.Failed => throw new TestsFailedException
case TestResult.Passed =>
}
}
}
val printSummary = TestResultLogger((log, results, _) => {
val multipleFrameworks = results.summaries.size > 1
for (Summary(name, message) <- results.summaries)
if (message.isEmpty)
log.debug("Summary for " + name + " not available.")
else {
if (multipleFrameworks) log.info(name)
log.info(message)
}
})
val printStandard_? : Tests.Output => Boolean =
results =>
// Print the standard one-liner statistic if no framework summary is defined, or when > 1 framework is in used.
results.summaries.size > 1 || results.summaries.headOption.forall(_.summaryText.isEmpty)
val printStandard = TestResultLogger((log, results, _) => {
val (skippedCount, errorsCount, passedCount, failuresCount, ignoredCount, canceledCount, pendingCount) =
results.events.foldLeft((0, 0, 0, 0, 0, 0, 0)) {
case ((skippedAcc, errorAcc, passedAcc, failureAcc, ignoredAcc, canceledAcc, pendingAcc), (name, testEvent)) =>
(skippedAcc + testEvent.skippedCount, errorAcc + testEvent.errorCount, passedAcc + testEvent.passedCount, failureAcc + testEvent.failureCount,
ignoredAcc + testEvent.ignoredCount, canceledAcc + testEvent.canceledCount, pendingAcc + testEvent.pendingCount)
}
val totalCount = failuresCount + errorsCount + skippedCount + passedCount
val base = s"Total $totalCount, Failed $failuresCount, Errors $errorsCount, Passed $passedCount"
val otherCounts = Seq("Skipped" -> skippedCount, "Ignored" -> ignoredCount, "Canceled" -> canceledCount, "Pending" -> pendingCount)
val extra = otherCounts.filter(_._2 > 0).map { case (label, count) => s", $label $count" }
val postfix = base + extra.mkString
results.overall match {
case TestResult.Error => log.error("Error: " + postfix)
case TestResult.Passed => log.info("Passed: " + postfix)
case TestResult.Failed => log.error("Failed: " + postfix)
}
})
val printFailures = TestResultLogger((log, results, _) => {
def select(resultTpe: TestResult.Value) = results.events collect {
case (name, tpe) if tpe.result == resultTpe =>
scala.reflect.NameTransformer.decode(name)
}
def show(label: String, level: Level.Value, tests: Iterable[String]): Unit =
if (tests.nonEmpty) {
log.log(level, label)
log.log(level, tests.mkString("\t", "\n\t", ""))
}
show("Passed tests:", Level.Debug, select(TestResult.Passed))
show("Failed tests:", Level.Error, select(TestResult.Failed))
show("Error during tests:", Level.Error, select(TestResult.Error))
})
val printNoTests = TestResultLogger((log, results, taskName) =>
log.info("No tests to run for " + taskName)
)
}
}