Skip to content

Commit

Permalink
Creates extension method for console report
Browse files Browse the repository at this point in the history
Reverts changes to `CodeSmell::compact` method to keep other reports
untouched.
  • Loading branch information
gfreivasc committed Jan 18, 2022
1 parent 0a9dd93 commit ede8fa2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Expand Up @@ -24,7 +24,7 @@ open class CodeSmell(

override val id: String = issue.id

override fun compact(): String = "$id - ${messageWithLocation()}"
override fun compact(): String = "$id - ${entity.compact()}"

override fun compactWithSignature(): String = compact() + " - Signature=" + entity.signature

Expand All @@ -39,8 +39,6 @@ open class CodeSmell(
}

override fun messageOrDescription(): String = message.ifEmpty { issue.description }

private fun messageWithLocation() = "[${messageOrDescription()}] at ${location.compact()}"
}

/**
Expand Down Expand Up @@ -100,9 +98,7 @@ open class ThresholdedCodeSmell(
val threshold: Int
get() = metric.threshold

override fun compact(): String = "$id - $metric - ${messageWithLocation()}"
override fun compact(): String = "$id - $metric - ${entity.compact()}"

override fun messageOrDescription(): String = message.ifEmpty { issue.description }

private fun messageWithLocation() = "[${messageOrDescription()}] at ${location.compact()}"
}
Expand Up @@ -10,6 +10,7 @@ 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

internal fun defaultReportMapping(reportId: String) = when (reportId) {
TxtOutputReport::class.java.simpleName -> "txt"
Expand All @@ -30,7 +31,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 @@ -40,6 +41,11 @@ 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"

Expand Down
Expand Up @@ -20,7 +20,7 @@ class TxtOutputReportSpec : Spek({
it("renders one") {
val report = TxtOutputReport()
val detektion = TestDetektion(createFinding())
val renderedText = "TestSmell - [TestMessage] at TestFile.kt:1:1 - Signature=TestEntitySignature\n"
val renderedText = "TestSmell - [TestEntity] at TestFile.kt:1:1 - Signature=TestEntitySignature\n"
assertThat(report.render(detektion)).isEqualTo(renderedText)
}

Expand All @@ -32,9 +32,9 @@ class TxtOutputReportSpec : Spek({
createFinding(ruleName = "TestSmellC")
)
val renderedText = """
TestSmellA - [TestMessage] at TestFile.kt:1:1 - Signature=TestEntitySignature
TestSmellB - [TestMessage] at TestFile.kt:1:1 - Signature=TestEntitySignature
TestSmellC - [TestMessage] at TestFile.kt:1:1 - Signature=TestEntitySignature
TestSmellA - [TestEntity] at TestFile.kt:1:1 - Signature=TestEntitySignature
TestSmellB - [TestEntity] at TestFile.kt:1:1 - Signature=TestEntitySignature
TestSmellC - [TestEntity] at TestFile.kt:1:1 - Signature=TestEntitySignature
""".trimIndent()
assertThat(report.render(detektion)).isEqualTo(renderedText)
Expand Down

0 comments on commit ede8fa2

Please sign in to comment.