Skip to content

Commit

Permalink
Truncates long messages for console reports
Browse files Browse the repository at this point in the history
Prevents line breaks and long messages from breaking report structure
  • Loading branch information
gfreivasc committed Jan 26, 2022
1 parent b5b96c4 commit b6bee2b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Expand Up @@ -11,6 +11,7 @@ import io.gitlab.arturbosch.detekt.api.Detektion
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.api.RuleSetId
import io.gitlab.arturbosch.detekt.api.ThresholdedCodeSmell
import kotlin.text.Typography.ellipsis

internal fun defaultReportMapping(reportId: String) = when (reportId) {
TxtOutputReport::class.java.simpleName -> "txt"
Expand Down Expand Up @@ -41,17 +42,14 @@ internal fun printFindings(findings: Map<String, List<Finding>>): String {
}
}

private fun Finding.detailed() = when (this) {
is ThresholdedCodeSmell -> "$id - $metric - [${messageOrDescription()}] at ${location.compact()}"
else -> "$id - [${messageOrDescription()}] at ${location.compact()}"
}

const val BUILD = "build"
const val EXCLUDE_CORRECTABLE = "excludeCorrectable"

const val DETEKT_OUTPUT_REPORT_PATHS_KEY = "detekt.output.report.paths.key"
const val DETEKT_OUTPUT_REPORT_BASE_PATH_KEY = "detekt.output.report.base.path"

private const val REPORT_MESSAGE_SIZE_LIMIT = 80

fun Config.excludeCorrectable(): Boolean = subConfig(BUILD).valueOrDefault(EXCLUDE_CORRECTABLE, false)

fun Detektion.filterEmptyIssues(config: Config): Map<RuleSetId, List<Finding>> {
Expand All @@ -74,3 +72,18 @@ fun Detektion.filterAutoCorrectedIssues(config: Config): Map<RuleSetId, List<Fin
}
return filteredFindings
}

private fun Finding.truncatedMessage(): String {
val message = messageOrDescription()
.replace(Regex("\\s+"), " ")
.trim()
return when {
message.length > REPORT_MESSAGE_SIZE_LIMIT -> "${message.take(REPORT_MESSAGE_SIZE_LIMIT)}($ellipsis)"
else -> message
}
}

private fun Finding.detailed(): String = when (this) {
is ThresholdedCodeSmell -> "$id - $metric - [${truncatedMessage()}] at ${location.compact()}"
else -> "$id - [${truncatedMessage()}] at ${location.compact()}"
}
Expand Up @@ -6,7 +6,9 @@ import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.core.reporting.AutoCorrectableIssueAssert
import io.gitlab.arturbosch.detekt.core.reporting.decolorized
import io.gitlab.arturbosch.detekt.test.TestDetektion
import io.gitlab.arturbosch.detekt.test.createEntity
import io.gitlab.arturbosch.detekt.test.createFinding
import io.gitlab.arturbosch.detekt.test.createIssue
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
Expand Down Expand Up @@ -69,6 +71,23 @@ class FindingsReportSpec {
val report = FindingsReport()
AutoCorrectableIssueAssert.isReportNull(report)
}

@Test
fun `truncates long message`() {
val expectedContent = readResourceContent("/reporting/long-messages-report.txt")
val longMessage = "This is just a long message that should be truncated after a given " +
"threshold is reached."
val multilineMessage = "A multiline\n\r\tmessage.\t "
val detektion = object : TestDetektion() {
override val findings: Map<String, List<Finding>> = mapOf(
"Ruleset" to listOf(
createFinding(createIssue("LongRule"), createEntity("File.kt"), longMessage),
createFinding(createIssue("MultilineRule"), createEntity("File.kt"), multilineMessage),
),
)
}
assertThat(subject.render(detektion)?.decolorized()).isEqualTo(expectedContent)
}
}
}

Expand Down
@@ -0,0 +1,5 @@
Ruleset - 10min debt
LongRule - [This is just a long message that should be truncated after a given threshold is (…)] at File.kt:1:1
MultilineRule - [A multiline message.] at File.kt:1:1

Overall debt: 10min

0 comments on commit b6bee2b

Please sign in to comment.