Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new ConsoleReport format #4027

Merged
merged 5 commits into from Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,35 @@
package io.gitlab.arturbosch.detekt.core.reporting.console

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.ConsoleReport
import io.gitlab.arturbosch.detekt.api.Detektion
import io.gitlab.arturbosch.detekt.api.SingleAssign
import io.gitlab.arturbosch.detekt.core.reporting.filterEmptyIssues

/**
* Contains a clear read of the console report, where each line contains location, messages and issue id.
* See: https://detekt.github.io/detekt/configurations.html#console-reports
*/
class ReadableFindingsReport : ConsoleReport() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we refactor FindingsReport and this class to a single hierarchy? Apparently the difference is just the (Map<String, List<Finding>>) -> String function that they're invoking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call this ReadableFindingsReport. Is the other FindingsReport not readable? I would prefer to call it ConciseFindingsReport.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name to LiteFindingsReport as suggested above.


private var config: Config by SingleAssign()

override val priority: Int = 40

override fun init(config: Config) {
this.config = config
}

override fun render(detektion: Detektion): String? {
val issues = detektion.filterEmptyIssues(config)
if (issues.isEmpty()) {
return null
}
return buildString {
issues.values.flatten().forEach { finding ->
append("${finding.location.compact()}: ${finding.messageOrDescription()} [${finding.issue.id}]")
chao2zhang marked this conversation as resolved.
Show resolved Hide resolved
appendLine()
}
}
}
}
@@ -1,5 +1,6 @@
io.gitlab.arturbosch.detekt.core.reporting.console.ComplexityReport
io.gitlab.arturbosch.detekt.core.reporting.console.FindingsReport
io.gitlab.arturbosch.detekt.core.reporting.console.FileBasedFindingsReport
io.gitlab.arturbosch.detekt.core.reporting.console.ReadableFindingsReport
io.gitlab.arturbosch.detekt.core.reporting.console.NotificationReport
io.gitlab.arturbosch.detekt.core.reporting.console.ProjectStatisticsReport
1 change: 1 addition & 0 deletions detekt-core/src/main/resources/default-detekt-config.yml
Expand Up @@ -38,6 +38,7 @@ console-reports:
- 'NotificationReport'
# - 'FindingsReport'
- 'FileBasedFindingsReport'
- 'ReadableFindingsReport'

output-reports:
active: true
Expand Down
@@ -0,0 +1,53 @@
package io.gitlab.arturbosch.detekt.core.reporting.console

import io.github.detekt.test.utils.readResourceContent
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.core.reporting.AutoCorrectableIssueAssert
import io.gitlab.arturbosch.detekt.test.TestDetektion
import io.gitlab.arturbosch.detekt.test.createFinding
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

class ReadableFindingsReportSpec : Spek({

val subject by memoized { createFindingsReport() }

describe("findings report") {
it("reports non-empty findings") {
assertThat(
subject
.render(
TestDetektion(
createFinding("SpacingBetweenPackageAndImports"),
createFinding("UnnecessarySafeCall")
)
)
).isEqualTo(readResourceContent("/reporting/readable-findings-report.txt"))
}

it("reports no findings") {
val detektion = TestDetektion()
assertThat(subject.render(detektion)).isNull()
}

it("reports no findings with rule set containing no smells") {
val detektion = object : TestDetektion() {
override val findings: Map<String, List<Finding>> = mapOf(
Pair("Ruleset", emptyList())
)
}
assertThat(subject.render(detektion)).isNull()
}

it("should not add auto corrected issues to report") {
val report = FindingsReport()
AutoCorrectableIssueAssert.isReportNull(report)
}
}
})

private fun createFindingsReport() = ReadableFindingsReport().apply {
init(Config.empty)
}
@@ -0,0 +1,2 @@
TestFile.kt:1:1: TestMessage [SpacingBetweenPackageAndImports]
TestFile.kt:1:1: TestMessage [UnnecessarySafeCall]
Expand Up @@ -116,6 +116,7 @@ object ConfigPrinter : DocumentationPrinter<List<RuleSetPage>> {
- 'NotificationReport'
# - 'FindingsReport'
- 'FileBasedFindingsReport'
- 'ReadableFindingsReport'
""".trimIndent()

private fun defaultOutputReportsConfiguration(): String = """
Expand Down
1 change: 1 addition & 0 deletions docs/pages/configurations.md
Expand Up @@ -98,6 +98,7 @@ console-reports:
# - 'NotificationReport'
# - 'FindingsReport'
# - 'FileBasedFindingsReport'
# - 'ReadableFindingsReport'
```

**ProjectStatisticsReport** contains metrics and statistics concerning the analyzed project sorted by priority.
Expand Down