Skip to content

Commit

Permalink
Adds issue details to findings on FindingsReport and FileBasedFinding…
Browse files Browse the repository at this point in the history
…sReporter (#4464)

* Changes FindingsReporter detail level

Achieved different levels of detailing by changing the format in the
implementation of compact() method for CodeSmell and
ThresholdedCodeSmell.

* Updates docs with reporting changes

* Adapts tests to findings reporter changes

* Creates extension method for console report

Reverts changes to `CodeSmell::compact` method to keep other reports
untouched.

* Truncates long messages for console reports

Prevents line breaks and long messages from breaking report structure
  • Loading branch information
gfreivasc committed Jan 31, 2022
1 parent 2d00cab commit 0b36ff0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 15 deletions.
Expand Up @@ -129,7 +129,7 @@ class RunnerSpec {

@Test
fun `writes no build related output to output printer`() {
assertThat(outPrintStream.toString()).doesNotContain("test - [Poko]")
assertThat(outPrintStream.toString()).doesNotContain("test - [A failure]")
}

@Test
Expand Down Expand Up @@ -158,7 +158,7 @@ class RunnerSpec {

@Test
fun `writes output to output printer`() {
assertThat(outPrintStream.toString()).contains("test - [Poko]")
assertThat(outPrintStream.toString()).contains("test - [A failure]")
}

@Test
Expand Down
Expand Up @@ -17,7 +17,7 @@ class TestProvider : RuleSetProvider {
}

class TestRule : Rule() {
override val issue = Issue("test", Severity.Minor, "", Debt.FIVE_MINS)
override val issue = Issue("test", Severity.Minor, "A failure", Debt.FIVE_MINS)
override fun visitClass(klass: KtClass) {
if (klass.name == "Poko") {
report(CodeSmell(issue, Entity.from(klass), issue.description))
Expand Down
Expand Up @@ -10,6 +10,8 @@ import io.gitlab.arturbosch.detekt.api.Debt
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 All @@ -30,7 +32,7 @@ internal fun printFindings(findings: Map<String, List<Finding>>): String {
append("$key - $debt debt\n")
issues.forEach {
append("\t")
append(it.compact().yellow())
append(it.detailed().yellow())
append("\n")
}
}
Expand All @@ -46,6 +48,8 @@ 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 @@ -68,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
6 changes: 3 additions & 3 deletions detekt-core/src/test/resources/reporting/findings-report.txt
@@ -1,7 +1,7 @@
Ruleset1 - 10min debt
TestSmell - [TestEntity] at TestFile.kt:1:1
TestSmell - [TestEntity] at TestFile.kt:1:1
TestSmell - [TestMessage] at TestFile.kt:1:1
TestSmell - [TestMessage] at TestFile.kt:1:1
Ruleset2 - 5min debt
TestSmell - [TestEntity] at TestFile.kt:1:1
TestSmell - [TestMessage] at TestFile.kt:1:1

Overall debt: 15min
@@ -1,7 +1,7 @@
File1.kt - 10min debt
TestSmell - [TestEntity] at File1.kt:1:1
TestSmell - [TestEntity] at File1.kt:1:1
TestSmell - [TestMessage] at File1.kt:1:1
TestSmell - [TestMessage] at File1.kt:1:1
File2.kt - 5min debt
TestSmell - [TestEntity] at File2.kt:1:1
TestSmell - [TestMessage] at File2.kt:1:1

Overall debt: 15min
@@ -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
10 changes: 5 additions & 5 deletions docs/pages/reporting.md
Expand Up @@ -17,11 +17,11 @@ Similar to the console output, each line of the txt output represents a finding
finding signature to help edit [baseline files](gettingstarted/gradle.md).

```
EmptyFunctionBlock - [apply] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:14:42 - Signature=DetektPlugin.kt$DetektPlugin${ }
NoUnusedImports - [] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:9:1 - Signature=io.gitlab.arturbosch.detekt.DetektPlugin.kt:9
NoUnusedImports - [] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:10:1 - Signature=io.gitlab.arturbosch.detekt.DetektPlugin.kt:10
NoConsecutiveBlankLines - [] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:86:1 - Signature=io.gitlab.arturbosch.detekt.DetektPlugin.kt:86
UnusedPrivateMember - [registerDetektJvmTasks] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:17:5 - Signature=DetektPlugin.kt$DetektPlugin$private fun Project.registerDetektJvmTasks(extension: DetektExtension)
EmptyFunctionBlock - [This empty block of code can be removed.] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:14:42 - Signature=DetektPlugin.kt$DetektPlugin${ }
NoUnusedImports - [Unused import] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:9:1 - Signature=io.gitlab.arturbosch.detekt.DetektPlugin.kt:9
NoUnusedImports - [Unused import] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:10:1 - Signature=io.gitlab.arturbosch.detekt.DetektPlugin.kt:10
NoConsecutiveBlankLines - [Needless blank line(s)] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:86:1 - Signature=io.gitlab.arturbosch.detekt.DetektPlugin.kt:86
UnusedPrivateMember - [Private function registerDetektJvmTasks is unused.] at /user/home/detekt/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/DetektPlugin.kt:17:5 - Signature=DetektPlugin.kt$DetektPlugin$private fun Project.registerDetektJvmTasks(extension: DetektExtension)
```

### HTML
Expand Down

0 comments on commit 0b36ff0

Please sign in to comment.