Skip to content

Commit

Permalink
Handle empty ruleId's in summary of violations
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-dingemans committed Dec 11, 2022
1 parent b2e6c37 commit 1355aeb
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PlainSummaryReporter(
}
} else {
ruleViolationCountNoAutocorrection
.merge(err.ruleId, 1) { previousValue, _ ->
.merge(err.causedBy(), 1) { previousValue, _ ->
previousValue + 1
}
}
Expand All @@ -47,6 +47,13 @@ public class PlainSummaryReporter(
.map { out.println(" ${it.first}: ${it.second}") }
}

private fun LintError.causedBy() =
when {
ruleId.isNotEmpty() -> ruleId
detail.startsWith(NOT_A_VALID_KOTLIN_FILE) -> NOT_A_VALID_KOTLIN_FILE
else -> "Unknown"
}

private companion object {
val COUNT_DESC_AND_RULE_ID_ASC_COMPARATOR =
kotlin
Expand All @@ -56,5 +63,7 @@ public class PlainSummaryReporter(
.thenComparator { left, right ->
compareValuesBy(left, right) { it.first }
}

const val NOT_A_VALID_KOTLIN_FILE = "Not a valid Kotlin file"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,50 @@ class PlainSummaryReporterTest {
""".trimIndent().replace("\n", System.lineSeparator()),
)
}

@Test
fun `Report other violations`() {
val out = ByteArrayOutputStream()
val reporter = PlainSummaryReporter(PrintStream(out, true))
reporter.onLintError(
"file-1.kt",
LintError(
18,
51,
"",
"Not a valid Kotlin file (18:51 unexpected tokens (use ';' to separate expressions on the same line)) (cannot be auto-corrected) ()",
),
false,
)
reporter.onLintError(
"file-2.kt",
LintError(
18,
51,
"",
"Not a valid Kotlin file (18:51 unexpected tokens (use ';' to separate expressions on the same line)) (cannot be auto-corrected) ()",
),
false,
)
reporter.onLintError(
"file-3.kt",
LintError(
18,
51,
"",
"Something else",
),
false,
)
reporter.afterAll()

assertThat(String(out.toByteArray())).isEqualTo(
"""
|Count (descending) of errors not autocorrected by rule:
| Not a valid Kotlin file: 2
| Unknown: 1
|
""".trimMargin().replace("\n", System.lineSeparator()),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class PlainReporter(
)
}
ruleViolationCount
.merge(err.ruleId, 1) { previousValue, _ ->
.merge(err.causedBy(), 1) { previousValue, _ ->
previousValue + 1
}
}
Expand Down Expand Up @@ -85,6 +85,13 @@ public class PlainReporter(
this
}

private fun LintError.causedBy() =
when {
ruleId.isNotEmpty() -> ruleId
detail.startsWith(NOT_A_VALID_KOTLIN_FILE) -> NOT_A_VALID_KOTLIN_FILE
else -> "Unknown"
}

private companion object {
val COUNT_DESC_AND_RULE_ID_ASC_COMPARATOR =
kotlin
Expand All @@ -94,6 +101,8 @@ public class PlainReporter(
.thenComparator { left, right ->
compareValuesBy(left, right) { it.first }
}

const val NOT_A_VALID_KOTLIN_FILE = "Not a valid Kotlin file"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test

class PlainReporterTest {
@Test
fun testReportGeneration() {
fun `Report normal rule violations`() {
val out = ByteArrayOutputStream()
val reporter = PlainReporter(PrintStream(out, true))
reporter.onLintError(
Expand Down Expand Up @@ -81,6 +81,56 @@ class PlainReporterTest {
)
}

@Test
fun `Report other violations`() {
val out = ByteArrayOutputStream()
val reporter = PlainReporter(PrintStream(out, true))
reporter.onLintError(
"file-1.kt",
LintError(
18,
51,
"",
"Not a valid Kotlin file (18:51 unexpected tokens (use ';' to separate expressions on the same line)) (cannot be auto-corrected) ()",
),
false,
)
reporter.onLintError(
"file-2.kt",
LintError(
18,
51,
"",
"Not a valid Kotlin file (18:51 unexpected tokens (use ';' to separate expressions on the same line)) (cannot be auto-corrected) ()",
),
false,
)
reporter.onLintError(
"file-3.kt",
LintError(
18,
51,
"",
"Something else",
),
false,
)
reporter.afterAll()

assertThat(String(out.toByteArray())).isEqualTo(
"""
|file-1.kt:18:51: Not a valid Kotlin file (18:51 unexpected tokens (use ';' to separate expressions on the same line)) (cannot be auto-corrected) () ()
|file-2.kt:18:51: Not a valid Kotlin file (18:51 unexpected tokens (use ';' to separate expressions on the same line)) (cannot be auto-corrected) () ()
|file-3.kt:18:51: Something else ()
|
|Summary error count (descending) by rule:
| Not a valid Kotlin file: 2
| Unknown: 1
|
""".trimMargin().replace("\n", System.lineSeparator()),
)
}

@Test
fun testColoredOutput() {
val out = ByteArrayOutputStream()
Expand Down

0 comments on commit 1355aeb

Please sign in to comment.