Skip to content

Commit

Permalink
[gradle-plugin] use empty string as default value for reporter type (#…
Browse files Browse the repository at this point in the history
…1458)

### What's done:
* Use empty string as default value for reporter type
* Extract `createReporterFlag` method
* Add test
  • Loading branch information
petertrr authored Jul 19, 2022
1 parent 340a62e commit 59962bd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class DiktatExtension(
/**
* Type of the reporter to use
*/
var reporter: String = "plain"
var reporter: String = ""

/**
* Destination for reporter. If empty, will write to stdout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ open class DiktatJavaExecTaskBase @Inject constructor(
addInput(it)
}

add(createReporterFlag(diktatExtension))
add(reporterFlag(diktatExtension))
}
project.logger.debug("Setting JavaExec args to $args")
}
Expand Down Expand Up @@ -148,11 +148,14 @@ open class DiktatJavaExecTaskBase @Inject constructor(
@Suppress("FUNCTION_BOOLEAN_PREFIX")
override fun getIgnoreFailures(): Boolean = ignoreFailuresProp.getOrElse(false)

private fun createReporterFlag(diktatExtension: DiktatExtension): String {
val flag: StringBuilder = StringBuilder()

// appending the flag with the reporter
setReporter(diktatExtension, flag)
private fun reporterFlag(diktatExtension: DiktatExtension): String = buildString {
val reporterFlag = project.createReporterFlag(diktatExtension)
append(reporterFlag)
val isSarifReporterActive = reporterFlag.contains("sarif")
if (isSarifReporterActive) {
// need to set user.home specially for ktlint, so it will be able to put a relative path URI in SARIF
systemProperty("user.home", project.rootDir.toString())
}

val outFlag = when {
// githubActions should have higher priority than a custom input
Expand All @@ -165,40 +168,7 @@ open class DiktatJavaExecTaskBase @Inject constructor(
else -> ""
}

flag.append(outFlag)

return flag.toString()
}

private fun setReporter(diktatExtension: DiktatExtension, flag: java.lang.StringBuilder) {
val name = diktatExtension.reporter.trim()
val validReporters = listOf("sarif", "plain", "json", "html")
val reporterFlag = when {
diktatExtension.githubActions -> {
if (diktatExtension.reporter.isNotEmpty()) {
// githubActions should have higher priority than custom input
project.logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used")
}
"--reporter=sarif"
}
name.isEmpty() -> {
project.logger.info("Reporter name was not set. Using 'plain' reporter")
"--reporter=plain"
}
name !in validReporters -> {
project.logger.warn("Reporter name is invalid (provided value: [$name]). Falling back to 'plain' reporter")
"--reporter=plain"
}
else -> "--reporter=$name"
}

val isSarifReporterActive = reporterFlag.contains("sarif")
if (isSarifReporterActive) {
// need to set user.home specially for ktlint, so it will be able to put a relative path URI in SARIF
systemProperty("user.home", project.rootDir.toString())
}

flag.append(reporterFlag)
append(outFlag)
}

@Suppress("MagicNumber")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.cqfn.diktat.plugin.gradle

import groovy.lang.Closure
import org.gradle.api.Project

@Suppress(
"MISSING_KDOC_TOP_LEVEL",
Expand Down Expand Up @@ -35,3 +36,34 @@ class KotlinClosure1<in T : Any?, V : Any>(
)
fun <T> Any.closureOf(action: T.() -> Unit): Closure<Any?> =
KotlinClosure1(action, this, this)

/**
* Create CLI flag to select reporter based on [diktatExtension]
*
* @param diktatExtension project extension of type [DiktatExtension]
* @return CLI flag
*/
internal fun Project.createReporterFlag(diktatExtension: DiktatExtension): String {
val name = diktatExtension.reporter.trim()
val validReporters = listOf("sarif", "plain", "json", "html")
val reporterFlag = when {
diktatExtension.githubActions -> {
if (diktatExtension.reporter.isNotEmpty()) {
// githubActions should have higher priority than custom input
logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used")
}
"--reporter=sarif"
}
name.isEmpty() -> {
logger.info("Reporter name was not set. Using 'plain' reporter")
"--reporter=plain"
}
name !in validReporters -> {
logger.warn("Reporter name is invalid (provided value: [$name]). Falling back to 'plain' reporter")
"--reporter=plain"
}
else -> "--reporter=$name"
}

return reporterFlag
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ class DiktatGradlePluginTest {
}

@Test
fun `check that the right reporter dependency added`() {
fun `check default reporter type value`() {
val diktatExtension = project.extensions.getByName("diktat") as DiktatExtension
Assertions.assertTrue(diktatExtension.reporter == "plain")
Assertions.assertEquals("", diktatExtension.reporter)

val reporterFlag = project.createReporterFlag(diktatExtension)
Assertions.assertEquals("--reporter=plain", reporterFlag)
}
}

0 comments on commit 59962bd

Please sign in to comment.