Skip to content

Commit 8d13af2

Browse files
committed
fix NoSuchElementException in ViolationsUtil when no rules are evaluated
1 parent 3d96410 commit 8d13af2

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

nebula-archrules-gradle-plugin/src/main/kotlin/com/netflix/nebula/archrules/gradle/ViolationsUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ViolationsUtil {
5656

5757
@JvmStatic
5858
fun printSummary(resultMap: Map<Rule, List<RuleResult>>, output: StyledTextOutput) {
59-
val maxRuleNameLength = resultMap.keys.maxOf { it.ruleName().length }
59+
val maxRuleNameLength = resultMap.keys.maxOfOrNull { it.ruleName().length } ?: 1
6060
resultMap.forEach { (rule, results) ->
6161
val failures = results.filter { it.status() != RuleResultStatus.PASS }
6262
if (failures.isEmpty()) {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.netflix.nebula.archrules.gradle
2+
3+
import com.tngtech.archunit.lang.Priority
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.gradle.internal.logging.text.StyledTextOutput
6+
import org.junit.jupiter.api.Test
7+
8+
internal class ViolationsUtilTest {
9+
10+
@Test
11+
fun `test printSummary`() {
12+
val output = MockStyledTextOutput()
13+
val rule = Rule("RuleClass", "RuleName", "description", Priority.MEDIUM)
14+
val results = listOf(RuleResult(rule, "message", RuleResultStatus.PASS))
15+
ViolationsUtil.printSummary(mapOf(rule to results), output)
16+
assertThat(output.getOutput()).contains("RuleName MEDIUM (No failures)")
17+
}
18+
19+
@Test
20+
fun `test printSummary empty results`() {
21+
val output = MockStyledTextOutput()
22+
ViolationsUtil.printSummary(mapOf(), output)
23+
assertThat(output.getOutput()).isEmpty()
24+
}
25+
}
26+
27+
class MockStyledTextOutput : StyledTextOutput {
28+
private val output: StringBuilder = StringBuilder()
29+
fun getOutput(): String = output.toString()
30+
override fun append(c: Char): StyledTextOutput {
31+
output.append(c)
32+
return this
33+
}
34+
35+
override fun append(csq: CharSequence?): StyledTextOutput {
36+
output.append(csq)
37+
return this
38+
}
39+
40+
override fun append(
41+
csq: CharSequence?,
42+
start: Int,
43+
end: Int
44+
): StyledTextOutput {
45+
output.append(csq)
46+
return this
47+
}
48+
49+
override fun style(style: StyledTextOutput.Style?): StyledTextOutput {
50+
return this
51+
}
52+
53+
override fun withStyle(style: StyledTextOutput.Style?): StyledTextOutput {
54+
return this
55+
}
56+
57+
override fun text(text: Any?): StyledTextOutput {
58+
output.append(text)
59+
return this
60+
}
61+
62+
override fun println(text: Any): StyledTextOutput {
63+
output.append(text).append("\n")
64+
return this
65+
}
66+
67+
override fun format(
68+
pattern: String?,
69+
vararg args: Any?
70+
): StyledTextOutput {
71+
output.append(pattern?.format(*args))
72+
return this
73+
}
74+
75+
override fun formatln(
76+
pattern: String?,
77+
vararg args: Any?
78+
): StyledTextOutput {
79+
output.append(pattern?.format(*args)).append("\n")
80+
return this
81+
}
82+
83+
override fun println(): StyledTextOutput {
84+
output.append("\n")
85+
return this
86+
}
87+
88+
override fun exception(throwable: Throwable?): StyledTextOutput {
89+
TODO("Not yet implemented")
90+
}
91+
92+
}

0 commit comments

Comments
 (0)