From 59962bddbe70c3531c022d70f5e08baa31aba742 Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Tue, 19 Jul 2022 20:18:40 +0300 Subject: [PATCH] [gradle-plugin] use empty string as default value for reporter type (#1458) ### What's done: * Use empty string as default value for reporter type * Extract `createReporterFlag` method * Add test --- .../diktat/plugin/gradle/DiktatExtension.kt | 2 +- .../plugin/gradle/DiktatJavaExecTaskBase.kt | 50 ++++--------------- .../org/cqfn/diktat/plugin/gradle/Utils.kt | 32 ++++++++++++ .../plugin/gradle/DiktatGradlePluginTest.kt | 7 ++- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt index f2d4c5e1c9..5b939f8f3e 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt @@ -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. diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt index b0a08d3d2a..a33ede567a 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt @@ -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") } @@ -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 @@ -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") diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/Utils.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/Utils.kt index 87c992be99..bc5a7bf1eb 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/Utils.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/Utils.kt @@ -7,6 +7,7 @@ package org.cqfn.diktat.plugin.gradle import groovy.lang.Closure +import org.gradle.api.Project @Suppress( "MISSING_KDOC_TOP_LEVEL", @@ -35,3 +36,34 @@ class KotlinClosure1( ) fun Any.closureOf(action: T.() -> Unit): Closure = 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 +} diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginTest.kt index dbf5acb88c..376111138d 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginTest.kt @@ -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) } }