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

[gradle-plugin] use empty string as default value for reporter type #1458

Merged
merged 4 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}