Skip to content

Commit

Permalink
Use empty string as default value for reporter type
Browse files Browse the repository at this point in the history
### What's done:
* Extract `createReporterFlag` method
* Add test
  • Loading branch information
petertrr committed Jul 19, 2022
1 parent cf81b04 commit 67ca69c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
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 @@ -40,7 +40,9 @@ class DiktatGradlePluginTest {
@Test
fun `check default reporter type value`() {
val diktatExtension = project.extensions.getByName("diktat") as DiktatExtension
// fixme: verify that correct reporter flag is built from this setting.
Assertions.assertEquals("", diktatExtension.reporter)

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

0 comments on commit 67ca69c

Please sign in to comment.