From 0900ebbe78ffd813037f19453f741a2fee2d994b Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Thu, 21 Jan 2021 17:25:47 +0300 Subject: [PATCH 01/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Logic began --- diktat-gradle-plugin/build.gradle.kts | 2 ++ .../gradle/DiktatGradlePluginFunctionalTest.kt | 14 ++++++++++++++ .../diktat/plugin/gradle/DiktatExtension.kt | 5 +++++ .../diktat/plugin/gradle/DiktatGradlePlugin.kt | 17 ++++++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/diktat-gradle-plugin/build.gradle.kts b/diktat-gradle-plugin/build.gradle.kts index 5bf9b08466..8cc5708aaf 100644 --- a/diktat-gradle-plugin/build.gradle.kts +++ b/diktat-gradle-plugin/build.gradle.kts @@ -30,6 +30,8 @@ dependencies { exclude("com.pinterest.ktlint", "ktlint-ruleset-standard") } implementation("com.pinterest.ktlint:ktlint-reporter-plain:$ktlintVersion") + implementation("com.pinterest.ktlint:ktlint-reporter-json:$ktlintVersion") + implementation("com.pinterest.ktlint:ktlint-reporter-html:$ktlintVersion") implementation("org.cqfn.diktat:diktat-rules:$diktatVersion") testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index 1f7f3372d7..37992548fb 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -39,6 +39,18 @@ class DiktatGradlePluginFunctionalTest { ) } + @Test + fun `qqq`() { + val result = runDiktat(testProjectDir, shouldSucceed = false) + + val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") + requireNotNull(diktatCheckBuildResult) + Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) + Assertions.assertTrue( + result.output.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") + ) + } + @Test fun `should execute diktatCheck with explicit inputs`() { buildFile.appendText( @@ -149,6 +161,8 @@ class DiktatGradlePluginFunctionalTest { withGradleVersion("5.0") } + println("=====================" + testProjectDir.root.listFiles().size) + val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) 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 33f2521551..e5580ddfe6 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 @@ -18,6 +18,11 @@ open class DiktatExtension { */ var debug = false + /** + * Type of the reporter to use + */ + var reporterType: String = "plain" + /** * Path to diktat yml config file. Can be either absolute or relative to project's root directory. * Default value: `diktat-analysis.yml` in rootDir. diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index 97237e0b7f..15e985be83 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -1,6 +1,8 @@ package org.cqfn.diktat.plugin.gradle +import com.pinterest.ktlint.reporter.html.HtmlReporter import com.pinterest.ktlint.reporter.plain.PlainReporter +import com.pinterest.ktlint.reporter.json.JsonReporter import generated.DIKTAT_VERSION import generated.KTLINT_VERSION import org.gradle.api.Plugin @@ -22,7 +24,14 @@ class DiktatGradlePlugin : Plugin { } diktatConfigFile = project.rootProject.file("diktat-analysis.yml") excludes = project.files() - reporter = PlainReporter(System.out) + reporter = when(reporterType) { + "json" -> JsonReporter(System.out) + "html" -> HtmlReporter(System.out) + else -> { + project.logger.warn("Incorrect name, going to use plain reporter") + PlainReporter(System.out) + } + } } // only gradle 7+ (or maybe 6.8) will embed kotlin 1.4+, kx.serialization is incompatible with kotlin 1.3, so until then we have to use JavaExec wrapper @@ -39,6 +48,12 @@ class DiktatGradlePlugin : Plugin { ) })) configuration.dependencies.add(project.dependencies.create("org.cqfn.diktat:diktat-rules:$DIKTAT_VERSION")) + // Adding reporters to dependencies + when(diktatExtension.reporterType) { + "json" -> configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) + "html" -> configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-json:$KTLINT_VERSION")) + else -> configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-plain:$KTLINT_VERSION")) + } } project.registerDiktatCheckTask(diktatExtension, diktatConfiguration) From 1d374c0ceefae57682094c825dab54adbba5434d Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Thu, 21 Jan 2021 19:19:28 +0300 Subject: [PATCH 02/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../gradle/DiktatGradlePluginFunctionalTest.kt | 2 -- .../diktat/plugin/gradle/DiktatGradlePlugin.kt | 15 +-------------- .../plugin/gradle/DiktatJavaExecTaskBase.kt | 17 +++++++++++++++++ .../plugin/gradle/DiktatGradlePluginTest.kt | 7 +++++++ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index 37992548fb..142d707819 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -161,8 +161,6 @@ class DiktatGradlePluginFunctionalTest { withGradleVersion("5.0") } - println("=====================" + testProjectDir.root.listFiles().size) - val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index 15e985be83..f5b1f67626 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -24,14 +24,7 @@ class DiktatGradlePlugin : Plugin { } diktatConfigFile = project.rootProject.file("diktat-analysis.yml") excludes = project.files() - reporter = when(reporterType) { - "json" -> JsonReporter(System.out) - "html" -> HtmlReporter(System.out) - else -> { - project.logger.warn("Incorrect name, going to use plain reporter") - PlainReporter(System.out) - } - } + reporter = PlainReporter(System.out) } // only gradle 7+ (or maybe 6.8) will embed kotlin 1.4+, kx.serialization is incompatible with kotlin 1.3, so until then we have to use JavaExec wrapper @@ -48,12 +41,6 @@ class DiktatGradlePlugin : Plugin { ) })) configuration.dependencies.add(project.dependencies.create("org.cqfn.diktat:diktat-rules:$DIKTAT_VERSION")) - // Adding reporters to dependencies - when(diktatExtension.reporterType) { - "json" -> configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) - "html" -> configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-json:$KTLINT_VERSION")) - else -> configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-plain:$KTLINT_VERSION")) - } } project.registerDiktatCheckTask(diktatExtension, diktatConfiguration) 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 49d608b78a..af3e3894db 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 @@ -1,5 +1,8 @@ package org.cqfn.diktat.plugin.gradle +import com.pinterest.ktlint.reporter.html.HtmlReporter +import com.pinterest.ktlint.reporter.json.JsonReporter +import com.pinterest.ktlint.reporter.plain.PlainReporter import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_FIX_TASK import org.cqfn.diktat.ruleset.rules.DIKTAT_CONF_PROPERTY @@ -50,6 +53,20 @@ open class DiktatJavaExecTaskBase @Inject constructor( } else { main = "com.pinterest.ktlint.Main" } + diktatExtension.reporter = when(diktatExtension.reporterType) { + "json" -> { + diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) + JsonReporter(System.out) + } + "html" -> { + diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-json:$KTLINT_VERSION")) + HtmlReporter(System.out) + } + else -> { + diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-plain:$KTLINT_VERSION")) + PlainReporter(System.out) + } + } classpath = diktatConfiguration project.logger.debug("Setting diktatCheck classpath to ${diktatConfiguration.dependencies.toSet()}") if (diktatExtension.debug) { 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 7587186137..6ef8a6e8ad 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 @@ -32,4 +32,11 @@ class DiktatGradlePluginTest { Assertions.assertIterableEquals(project.fileTree("src").files, diktatExtension.inputs.files) Assertions.assertTrue(diktatExtension.inputs.files.isNotEmpty()) } + + @Test + fun `check that the right reporter dependency added`() { + val diktatExtension = project.extensions.getByName("diktat") as DiktatExtension + + Assertions.assertFalse(diktatExtension.reporterType == "plain") + } } From 68a5b0c74accff4a645b8b5a475e0a30baa6d56d Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Mon, 25 Jan 2021 11:52:01 +0300 Subject: [PATCH 03/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Added tests --- .../cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt | 1 + .../diktat/plugin/gradle/DiktatJavaExecTaskBase.kt | 7 ++++--- .../diktat/plugin/gradle/DiktatGradlePluginTest.kt | 4 ++-- .../diktat/plugin/gradle/DiktatJavaExecTaskTest.kt | 12 ++++++++++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index f5b1f67626..e00330397b 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -40,6 +40,7 @@ class DiktatGradlePlugin : Plugin { ) ) })) + configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-plain:$KTLINT_VERSION")) configuration.dependencies.add(project.dependencies.create("org.cqfn.diktat:diktat-rules:$DIKTAT_VERSION")) } 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 af3e3894db..50c83a2bcd 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 @@ -55,15 +55,16 @@ open class DiktatJavaExecTaskBase @Inject constructor( } diktatExtension.reporter = when(diktatExtension.reporterType) { "json" -> { - diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) + diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-json:$KTLINT_VERSION")) + diktatConfiguration.dependencies.remove(diktatConfiguration.dependencies.find { it.name == "ktlint-reporter-plain" }) JsonReporter(System.out) } "html" -> { - diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-json:$KTLINT_VERSION")) + diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) + diktatConfiguration.dependencies.remove(diktatConfiguration.dependencies.find { it.name == "ktlint-reporter-plain" }) HtmlReporter(System.out) } else -> { - diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-plain:$KTLINT_VERSION")) PlainReporter(System.out) } } 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 6ef8a6e8ad..d1a2a10887 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 @@ -36,7 +36,7 @@ class DiktatGradlePluginTest { @Test fun `check that the right reporter dependency added`() { val diktatExtension = project.extensions.getByName("diktat") as DiktatExtension - - Assertions.assertFalse(diktatExtension.reporterType == "plain") + Assertions.assertTrue(diktatExtension.reporterType == "plain") + Assertions.assertTrue(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-plain" }) } } diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 533c6ab497..331840b661 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -76,6 +76,18 @@ class DiktatJavaExecTaskTest { Assertions.assertEquals(File(project.projectDir.parentFile, "diktat-analysis.yml").absolutePath, task.systemProperties[DIKTAT_CONF_PROPERTY]) } + @Test + fun `check plain reporter type`() { + val task = project.registerDiktatTask { + inputs = project.files() + diktatConfigFile = project.file("../diktat-analysis.yml") + reporterType = "json" + } + Assertions.assertTrue(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-json" }) + Assertions.assertFalse(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-plain" }) + Assertions.assertEquals(File(project.projectDir.parentFile, "diktat-analysis.yml").absolutePath, task.systemProperties[DIKTAT_CONF_PROPERTY]) + } + @Test fun `check system property with multiproject build with default config`() { setupMultiProject() From 56c7621ebe5871847abb2773126b8cebaeca210a Mon Sep 17 00:00:00 2001 From: soWhoAmI Date: Mon, 25 Jan 2021 19:19:50 +0300 Subject: [PATCH 04/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- diktat-gradle-plugin/build.gradle.kts | 2 +- .../DiktatGradlePluginFunctionalTest.kt | 37 +++++++++++++- .../diktat/plugin/gradle/DiktatExtension.kt | 7 +++ .../plugin/gradle/DiktatGradlePlugin.kt | 3 -- .../plugin/gradle/DiktatJavaExecTaskBase.kt | 49 +++++++++++++------ .../plugin/gradle/DiktatGradlePluginTest.kt | 1 - .../plugin/gradle/DiktatJavaExecTaskTest.kt | 15 +++--- 7 files changed, 84 insertions(+), 30 deletions(-) diff --git a/diktat-gradle-plugin/build.gradle.kts b/diktat-gradle-plugin/build.gradle.kts index 8cc5708aaf..7e7383c260 100644 --- a/diktat-gradle-plugin/build.gradle.kts +++ b/diktat-gradle-plugin/build.gradle.kts @@ -31,7 +31,7 @@ dependencies { } implementation("com.pinterest.ktlint:ktlint-reporter-plain:$ktlintVersion") implementation("com.pinterest.ktlint:ktlint-reporter-json:$ktlintVersion") - implementation("com.pinterest.ktlint:ktlint-reporter-html:$ktlintVersion") + implementation("com.pinterest.ktlint:ktlint-reporter-checkstyle:$ktlintVersion") implementation("org.cqfn.diktat:diktat-rules:$diktatVersion") testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index 142d707819..42f748e9de 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -7,6 +7,7 @@ import org.gradle.testkit.runner.TaskOutcome import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import java.io.File @@ -28,6 +29,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should execute diktatCheck on default values`() { val result = runDiktat(testProjectDir, shouldSucceed = false) @@ -39,12 +41,38 @@ class DiktatGradlePluginFunctionalTest { ) } + /* + * Should fix output here + */ @Test - fun `qqq`() { + fun `should have json reporter files`() { + buildFile.appendText( + """${System.lineSeparator()} + diktat { + inputs = files("src/**/*.kt") + reporterType = "checkstyle" + output = "qqq.txt" + } + """.trimIndent() + ) val result = runDiktat(testProjectDir, shouldSucceed = false) val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) + File("test.txt").bufferedWriter().use { out -> + testProjectDir.root.walkTopDown().forEach { + out.write(it.name + "\n") + } + + out.write("\n===========\n") + + testProjectDir.root.walkTopDown().filter { it.name == "ktlint-report-in-checkstyle-format.xml" }.first { + it.readLines().forEach { + out.write(it + "\n") + } + true + } + } Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) Assertions.assertTrue( result.output.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") @@ -52,6 +80,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should execute diktatCheck with explicit inputs`() { buildFile.appendText( """${System.lineSeparator()} @@ -71,6 +100,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should execute diktatCheck with excludes`() { buildFile.appendText( """${System.lineSeparator()} @@ -88,6 +118,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should not run diktat with ktlint's default includes when no files match include patterns`() { buildFile.appendText( """${System.lineSeparator()} @@ -111,6 +142,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should not run diktat with ktlint's default includes when no files match include patterns - 2`() { buildFile.appendText( """${System.lineSeparator()} @@ -133,6 +165,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should execute diktatCheck with absolute paths`() { val path = testProjectDir.root .resolve("src/**/*.kt") @@ -156,6 +189,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should execute diktatCheck with gradle older than 6_4`() { val result = runDiktat(testProjectDir, shouldSucceed = false, arguments = listOf("--info")) { withGradleVersion("5.0") @@ -170,6 +204,7 @@ class DiktatGradlePluginFunctionalTest { } @Test + @Disabled fun `should respect ignoreFailures setting`() { buildFile.appendText( """${System.lineSeparator()} 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 e5580ddfe6..0abeabb7e8 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 @@ -2,6 +2,7 @@ package org.cqfn.diktat.plugin.gradle import com.pinterest.ktlint.core.Reporter import org.gradle.api.file.FileCollection +import org.gradle.api.tasks.Internal import java.io.File /** @@ -23,6 +24,12 @@ open class DiktatExtension { */ var reporterType: String = "plain" + /** + * Type of output + * Default: System.out + */ + var output: String = "out" + /** * Path to diktat yml config file. Can be either absolute or relative to project's root directory. * Default value: `diktat-analysis.yml` in rootDir. diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index e00330397b..97237e0b7f 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -1,8 +1,6 @@ package org.cqfn.diktat.plugin.gradle -import com.pinterest.ktlint.reporter.html.HtmlReporter import com.pinterest.ktlint.reporter.plain.PlainReporter -import com.pinterest.ktlint.reporter.json.JsonReporter import generated.DIKTAT_VERSION import generated.KTLINT_VERSION import org.gradle.api.Plugin @@ -40,7 +38,6 @@ class DiktatGradlePlugin : Plugin { ) ) })) - configuration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-plain:$KTLINT_VERSION")) configuration.dependencies.add(project.dependencies.create("org.cqfn.diktat:diktat-rules:$DIKTAT_VERSION")) } 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 50c83a2bcd..d9c87370d5 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 @@ -1,6 +1,6 @@ package org.cqfn.diktat.plugin.gradle -import com.pinterest.ktlint.reporter.html.HtmlReporter +import com.pinterest.ktlint.reporter.checkstyle.CheckStyleReporter import com.pinterest.ktlint.reporter.json.JsonReporter import com.pinterest.ktlint.reporter.plain.PlainReporter import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK @@ -20,6 +20,7 @@ import org.gradle.api.tasks.VerificationTask import org.gradle.util.GradleVersion import java.io.File +import java.io.PrintStream import javax.inject.Inject /** @@ -53,21 +54,6 @@ open class DiktatJavaExecTaskBase @Inject constructor( } else { main = "com.pinterest.ktlint.Main" } - diktatExtension.reporter = when(diktatExtension.reporterType) { - "json" -> { - diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-json:$KTLINT_VERSION")) - diktatConfiguration.dependencies.remove(diktatConfiguration.dependencies.find { it.name == "ktlint-reporter-plain" }) - JsonReporter(System.out) - } - "html" -> { - diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) - diktatConfiguration.dependencies.remove(diktatConfiguration.dependencies.find { it.name == "ktlint-reporter-plain" }) - HtmlReporter(System.out) - } - else -> { - PlainReporter(System.out) - } - } classpath = diktatConfiguration project.logger.debug("Setting diktatCheck classpath to ${diktatConfiguration.dependencies.toSet()}") if (diktatExtension.debug) { @@ -101,6 +87,37 @@ open class DiktatJavaExecTaskBase @Inject constructor( diktatExtension.excludes.files.forEach { addPattern(it, negate = true) } + + // Plain, checkstyle and json reporter are provided out of the box in ktlint + when (diktatExtension.reporterType) { + "json" -> { + add("--reporter=json") + diktatExtension.reporter = if (diktatExtension.output == "out") { + JsonReporter(System.out) + } else { +// require(File(diktatExtension.output).exists()) + JsonReporter(PrintStream(File(diktatExtension.output))) + } + } + "checkstyle" -> { + add("--reporter=checkstyle,output=ktlint-report-in-checkstyle-format.xml") + diktatExtension.reporter = if (diktatExtension.output == "out") { + CheckStyleReporter(System.out) + } else { +// require(File(diktatExtension.output).exists()) + CheckStyleReporter(PrintStream(File(project.projectDir.path + diktatExtension.output))) + } + } + else -> { + add("--reporter=plain") + diktatExtension.reporter = if (diktatExtension.output == "out") { + PlainReporter(System.out) + } else { +// require(File(diktatExtension.output).exists()) + PlainReporter(PrintStream(File(diktatExtension.output))) + } + } + } } logger.debug("Setting JavaExec args to $args") } 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 d1a2a10887..676e6237a0 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 @@ -37,6 +37,5 @@ class DiktatGradlePluginTest { fun `check that the right reporter dependency added`() { val diktatExtension = project.extensions.getByName("diktat") as DiktatExtension Assertions.assertTrue(diktatExtension.reporterType == "plain") - Assertions.assertTrue(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-plain" }) } } diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 331840b661..da54608bf9 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -23,7 +23,7 @@ class DiktatJavaExecTaskTest { @Test fun `check command line for various inputs`() { assertCommandLineEquals( - listOf(null, combinePathParts("src", "**", "*.kt")) + listOf(null, combinePathParts("src", "**", "*.kt"), "--reporter=plain") ) { inputs = project.files("src/**/*.kt") } @@ -32,7 +32,7 @@ class DiktatJavaExecTaskTest { @Test fun `check command line in debug mode`() { assertCommandLineEquals( - listOf(null, "--debug", combinePathParts("src", "**", "*.kt")) + listOf(null, "--debug", combinePathParts("src", "**", "*.kt"), "--reporter=plain") ) { inputs = project.files("src/**/*.kt") debug = true @@ -43,7 +43,7 @@ class DiktatJavaExecTaskTest { fun `check command line with excludes`() { assertCommandLineEquals( listOf(null, combinePathParts("src", "**", "*.kt"), - "!${combinePathParts("src", "main", "kotlin", "generated")}" + "!${combinePathParts("src", "main", "kotlin", "generated")}", "--reporter=plain" ) ) { inputs = project.files("src/**/*.kt") @@ -77,15 +77,14 @@ class DiktatJavaExecTaskTest { } @Test - fun `check plain reporter type`() { - val task = project.registerDiktatTask { + fun `check command line has reporter type`() { + assertCommandLineEquals( + listOf(null, "--reporter=json") + ) { inputs = project.files() diktatConfigFile = project.file("../diktat-analysis.yml") reporterType = "json" } - Assertions.assertTrue(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-json" }) - Assertions.assertFalse(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-plain" }) - Assertions.assertEquals(File(project.projectDir.parentFile, "diktat-analysis.yml").absolutePath, task.systemProperties[DIKTAT_CONF_PROPERTY]) } @Test From 7965ff1c67b18f2cf660bce6e7952610762ad1e4 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 11:42:11 +0300 Subject: [PATCH 05/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Logic remade * Added tests --- .../DiktatGradlePluginFunctionalTest.kt | 30 ++--------- .../diktat/plugin/gradle/DiktatExtension.kt | 8 +-- .../plugin/gradle/DiktatGradlePlugin.kt | 1 - .../plugin/gradle/DiktatJavaExecTaskBase.kt | 52 ++++++++----------- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 28 +++++++++- 5 files changed, 54 insertions(+), 65 deletions(-) diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index 42f748e9de..31627cc494 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -29,7 +29,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should execute diktatCheck on default values`() { val result = runDiktat(testProjectDir, shouldSucceed = false) @@ -50,8 +49,8 @@ class DiktatGradlePluginFunctionalTest { """${System.lineSeparator()} diktat { inputs = files("src/**/*.kt") - reporterType = "checkstyle" - output = "qqq.txt" + reporterType = "json" + output = "test.txt" } """.trimIndent() ) @@ -59,28 +58,15 @@ class DiktatGradlePluginFunctionalTest { val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) - File("test.txt").bufferedWriter().use { out -> - testProjectDir.root.walkTopDown().forEach { - out.write(it.name + "\n") - } - - out.write("\n===========\n") - - testProjectDir.root.walkTopDown().filter { it.name == "ktlint-report-in-checkstyle-format.xml" }.first { - it.readLines().forEach { - out.write(it + "\n") - } - true - } - } + Assertions.assertTrue(testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.firstOrNull() != null) Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) + val file = testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.first() Assertions.assertTrue( - result.output.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") + file.readLines().any { it.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") } ) } @Test - @Disabled fun `should execute diktatCheck with explicit inputs`() { buildFile.appendText( """${System.lineSeparator()} @@ -100,7 +86,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should execute diktatCheck with excludes`() { buildFile.appendText( """${System.lineSeparator()} @@ -118,7 +103,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should not run diktat with ktlint's default includes when no files match include patterns`() { buildFile.appendText( """${System.lineSeparator()} @@ -142,7 +126,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should not run diktat with ktlint's default includes when no files match include patterns - 2`() { buildFile.appendText( """${System.lineSeparator()} @@ -165,7 +148,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should execute diktatCheck with absolute paths`() { val path = testProjectDir.root .resolve("src/**/*.kt") @@ -189,7 +171,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should execute diktatCheck with gradle older than 6_4`() { val result = runDiktat(testProjectDir, shouldSucceed = false, arguments = listOf("--info")) { withGradleVersion("5.0") @@ -204,7 +185,6 @@ class DiktatGradlePluginFunctionalTest { } @Test - @Disabled fun `should respect ignoreFailures setting`() { buildFile.appendText( """${System.lineSeparator()} 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 0abeabb7e8..0ec9488b6b 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 @@ -28,7 +28,7 @@ open class DiktatExtension { * Type of output * Default: System.out */ - var output: String = "out" + var output: String = "" /** * Path to diktat yml config file. Can be either absolute or relative to project's root directory. @@ -41,12 +41,6 @@ open class DiktatExtension { */ lateinit var excludes: FileCollection - /** - * Ktlint's [Reporter] which will be used during run. - * Private until I find a way to configure it. - */ - internal lateinit var reporter: Reporter - /** * Paths that will be scanned for .kt(s) files */ diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index 97237e0b7f..6e0ac44c21 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -22,7 +22,6 @@ class DiktatGradlePlugin : Plugin { } diktatConfigFile = project.rootProject.file("diktat-analysis.yml") excludes = project.files() - reporter = PlainReporter(System.out) } // only gradle 7+ (or maybe 6.8) will embed kotlin 1.4+, kx.serialization is incompatible with kotlin 1.3, so until then we have to use JavaExec wrapper 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 d9c87370d5..89af4f863c 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 @@ -54,6 +54,9 @@ open class DiktatJavaExecTaskBase @Inject constructor( } else { main = "com.pinterest.ktlint.Main" } + if (diktatExtension.reporterType == "html") { + diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) + } classpath = diktatConfiguration project.logger.debug("Setting diktatCheck classpath to ${diktatConfiguration.dependencies.toSet()}") if (diktatExtension.debug) { @@ -88,36 +91,7 @@ open class DiktatJavaExecTaskBase @Inject constructor( addPattern(it, negate = true) } - // Plain, checkstyle and json reporter are provided out of the box in ktlint - when (diktatExtension.reporterType) { - "json" -> { - add("--reporter=json") - diktatExtension.reporter = if (diktatExtension.output == "out") { - JsonReporter(System.out) - } else { -// require(File(diktatExtension.output).exists()) - JsonReporter(PrintStream(File(diktatExtension.output))) - } - } - "checkstyle" -> { - add("--reporter=checkstyle,output=ktlint-report-in-checkstyle-format.xml") - diktatExtension.reporter = if (diktatExtension.output == "out") { - CheckStyleReporter(System.out) - } else { -// require(File(diktatExtension.output).exists()) - CheckStyleReporter(PrintStream(File(project.projectDir.path + diktatExtension.output))) - } - } - else -> { - add("--reporter=plain") - diktatExtension.reporter = if (diktatExtension.output == "out") { - PlainReporter(System.out) - } else { -// require(File(diktatExtension.output).exists()) - PlainReporter(PrintStream(File(diktatExtension.output))) - } - } - } + add(createReporterFlag(diktatExtension)) } logger.debug("Setting JavaExec args to $args") } @@ -142,6 +116,24 @@ 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() + + // Plain, checkstyle and json reporter are provided out of the box in ktlint + when(diktatExtension.reporterType) { + "json" -> flag.append("--reporter=json") + "html" -> flag.append("--reporter=html") + "checkstyle" -> flag.append("--reporter=checkstyle") + else -> flag.append("--reporter=plain") + } + + if (diktatExtension.output.isNotEmpty()) { + flag.append(",output=${diktatExtension.output}") + } + + return flag.toString() + } + @Suppress("MagicNumber") private fun isMainClassPropertySupported(gradleVersionString: String) = GradleVersion.version(gradleVersionString) >= GradleVersion.version("6.4") diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index da54608bf9..de7da38bdf 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -77,16 +77,40 @@ class DiktatJavaExecTaskTest { } @Test - fun `check command line has reporter type`() { + fun `check command line has reporter type and output`() { assertCommandLineEquals( - listOf(null, "--reporter=json") + listOf(null, "--reporter=json,output=some.txt") ) { inputs = project.files() diktatConfigFile = project.file("../diktat-analysis.yml") reporterType = "json" + output = "some.txt" } } + @Test + fun `check command line has reporter type without output`() { + assertCommandLineEquals( + listOf(null, "--reporter=json") + ) { + inputs = project.files() + diktatConfigFile = project.file("../diktat-analysis.yml") + reporterType = "json" + } + } + + @Test + fun `check that project has html dependency`() { + val task = project.registerDiktatTask { + inputs = project.files() + diktatConfigFile = project.file("../diktat-analysis.yml") + reporterType = "html" + } + + Assertions.assertTrue(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-html" }) + Assertions.assertEquals(File(project.projectDir.parentFile, "diktat-analysis.yml").absolutePath, task.systemProperties[DIKTAT_CONF_PROPERTY]) + } + @Test fun `check system property with multiproject build with default config`() { setupMultiProject() From 918ebfd833860e0b81f02da8fdf6ba6526a478c3 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 17:06:05 +0300 Subject: [PATCH 06/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../DiktatGradlePluginFunctionalTest.kt | 10 +++------ .../plugin/gradle/DiktatJavaExecTaskBase.kt | 21 +++++++++++++++---- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 11 ++++++++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index 31627cc494..7af8d18081 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -7,7 +7,6 @@ import org.gradle.testkit.runner.TaskOutcome import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import java.io.File @@ -40,9 +39,6 @@ class DiktatGradlePluginFunctionalTest { ) } - /* - * Should fix output here - */ @Test fun `should have json reporter files`() { buildFile.appendText( @@ -58,11 +54,11 @@ class DiktatGradlePluginFunctionalTest { val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) - Assertions.assertTrue(testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.firstOrNull() != null) Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) - val file = testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.first() + val file = testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.firstOrNull() + Assertions.assertTrue(file != null) Assertions.assertTrue( - file.readLines().any { it.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") } + file!!.readLines().any { it.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") } ) } 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 89af4f863c..9655edbcc9 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 @@ -1,14 +1,12 @@ package org.cqfn.diktat.plugin.gradle -import com.pinterest.ktlint.reporter.checkstyle.CheckStyleReporter -import com.pinterest.ktlint.reporter.json.JsonReporter -import com.pinterest.ktlint.reporter.plain.PlainReporter import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_FIX_TASK import org.cqfn.diktat.ruleset.rules.DIKTAT_CONF_PROPERTY import generated.DIKTAT_VERSION import generated.KTLINT_VERSION +import org.cqfn.diktat.ruleset.utils.log import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.provider.Property @@ -54,6 +52,7 @@ open class DiktatJavaExecTaskBase @Inject constructor( } else { main = "com.pinterest.ktlint.Main" } + // Plain, checkstyle and json reporter are provided out of the box in ktlint if (diktatExtension.reporterType == "html") { diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION")) } @@ -124,7 +123,21 @@ open class DiktatJavaExecTaskBase @Inject constructor( "json" -> flag.append("--reporter=json") "html" -> flag.append("--reporter=html") "checkstyle" -> flag.append("--reporter=checkstyle") - else -> flag.append("--reporter=plain") + else -> { + if (diktatExtension.reporterType.startsWith("custom")) { + val name = diktatExtension.reporterType.split(":")[1] + val jarPath = diktatExtension.reporterType.split(":")[2] + if (name.isEmpty() || jarPath.isEmpty()) { + log.warn("Either name or path to jar is not specified. Falling to plain reporter") + flag.append("--reporter=plain") + } else { + flag.append("--reporter=$name,artifact=$jarPath") + } + } else { + flag.append("--reporter=plain") + log.warn("Unknown reporter was specified. Falling back to plain reporter.") + } + } } if (diktatExtension.output.isNotEmpty()) { diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index de7da38bdf..c367cd2dfd 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -99,6 +99,17 @@ class DiktatJavaExecTaskTest { } } + @Test + fun `check command line has custom reporter type with output`() { + assertCommandLineEquals( + listOf(null, "--reporter=customName,artifact=customPath") + ) { + inputs = project.files() + diktatConfigFile = project.file("../diktat-analysis.yml") + reporterType = "custom:customName:customPath" + } + } + @Test fun `check that project has html dependency`() { val task = project.registerDiktatTask { From 5843f9af0127cc923a6072d626900f1ff95723d7 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 17:41:21 +0300 Subject: [PATCH 07/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../plugin/gradle/DiktatGradlePluginFunctionalTest.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index 7af8d18081..aeccc5caab 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -4,6 +4,7 @@ import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_T import org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder import org.gradle.testkit.runner.TaskOutcome +import org.jetbrains.kotlin.com.intellij.util.ObjectUtils.assertNotNull import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach @@ -55,10 +56,10 @@ class DiktatGradlePluginFunctionalTest { val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) - val file = testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.firstOrNull() - Assertions.assertTrue(file != null) + assertNotNull(testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.first()) + val file = assertNotNull(testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.first()) Assertions.assertTrue( - file!!.readLines().any { it.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") } + file.readLines().any { it.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") } ) } From d67ae58f9646ce5742ea910fd6a5fccb2828fe96 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 17:41:45 +0300 Subject: [PATCH 08/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- diktat-gradle-plugin/build.gradle.kts | 3 --- 1 file changed, 3 deletions(-) diff --git a/diktat-gradle-plugin/build.gradle.kts b/diktat-gradle-plugin/build.gradle.kts index 7e7383c260..fa1dc9540f 100644 --- a/diktat-gradle-plugin/build.gradle.kts +++ b/diktat-gradle-plugin/build.gradle.kts @@ -29,9 +29,6 @@ dependencies { implementation("com.pinterest.ktlint:ktlint-core:$ktlintVersion") { exclude("com.pinterest.ktlint", "ktlint-ruleset-standard") } - implementation("com.pinterest.ktlint:ktlint-reporter-plain:$ktlintVersion") - implementation("com.pinterest.ktlint:ktlint-reporter-json:$ktlintVersion") - implementation("com.pinterest.ktlint:ktlint-reporter-checkstyle:$ktlintVersion") implementation("org.cqfn.diktat:diktat-rules:$diktatVersion") testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") From caaaba7a354a9ed0d968bc87f275567f251eef2c Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 17:47:27 +0300 Subject: [PATCH 09/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../kotlin/org/cqfn/diktat/plugin/gradle/DiktatExtension.kt | 2 -- .../kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt | 1 - 2 files changed, 3 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 0ec9488b6b..6311944b17 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 @@ -1,8 +1,6 @@ package org.cqfn.diktat.plugin.gradle -import com.pinterest.ktlint.core.Reporter import org.gradle.api.file.FileCollection -import org.gradle.api.tasks.Internal import java.io.File /** diff --git a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt index 6e0ac44c21..8223efec9d 100644 --- a/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -1,6 +1,5 @@ package org.cqfn.diktat.plugin.gradle -import com.pinterest.ktlint.reporter.plain.PlainReporter import generated.DIKTAT_VERSION import generated.KTLINT_VERSION import org.gradle.api.Plugin From 0b42e86aa5c9f69608fbec73fe3aa4423fd51d8e Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 18:10:25 +0300 Subject: [PATCH 10/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt index aeccc5caab..cb9125b0b0 100644 --- a/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt +++ b/diktat-gradle-plugin/src/functionalTest/kotlin/org/cqfn/diktat/plugin/gradle/DiktatGradlePluginFunctionalTest.kt @@ -56,7 +56,6 @@ class DiktatGradlePluginFunctionalTest { val diktatCheckBuildResult = result.task(":$DIKTAT_CHECK_TASK") requireNotNull(diktatCheckBuildResult) Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome) - assertNotNull(testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.first()) val file = assertNotNull(testProjectDir.root.walkTopDown().filter { it.name == "test.txt" }.first()) Assertions.assertTrue( file.readLines().any { it.contains("[HEADER_MISSING_OR_WRONG_COPYRIGHT]") } From 4af01531ff3f0233c99694db691903e66c0cc0a5 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 18:25:24 +0300 Subject: [PATCH 11/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../plugin/gradle/DiktatJavaExecTaskBase.kt | 47 +++++++++++-------- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 11 +++-- 2 files changed, 36 insertions(+), 22 deletions(-) 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 9655edbcc9..b1e2a05c28 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 @@ -3,10 +3,10 @@ package org.cqfn.diktat.plugin.gradle import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_CHECK_TASK import org.cqfn.diktat.plugin.gradle.DiktatGradlePlugin.Companion.DIKTAT_FIX_TASK import org.cqfn.diktat.ruleset.rules.DIKTAT_CONF_PROPERTY +import org.cqfn.diktat.ruleset.utils.log import generated.DIKTAT_VERSION import generated.KTLINT_VERSION -import org.cqfn.diktat.ruleset.utils.log import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.provider.Property @@ -18,7 +18,6 @@ import org.gradle.api.tasks.VerificationTask import org.gradle.util.GradleVersion import java.io.File -import java.io.PrintStream import javax.inject.Inject /** @@ -119,24 +118,18 @@ open class DiktatJavaExecTaskBase @Inject constructor( val flag: StringBuilder = StringBuilder() // Plain, checkstyle and json reporter are provided out of the box in ktlint - when(diktatExtension.reporterType) { - "json" -> flag.append("--reporter=json") - "html" -> flag.append("--reporter=html") - "checkstyle" -> flag.append("--reporter=checkstyle") + when (diktatExtension.reporterType) { + "json" -> { + flag.append("--reporter=json") + } + "html" -> { + flag.append("--reporter=html") + } + "checkstyle" -> { + flag.append("--reporter=checkstyle") + } else -> { - if (diktatExtension.reporterType.startsWith("custom")) { - val name = diktatExtension.reporterType.split(":")[1] - val jarPath = diktatExtension.reporterType.split(":")[2] - if (name.isEmpty() || jarPath.isEmpty()) { - log.warn("Either name or path to jar is not specified. Falling to plain reporter") - flag.append("--reporter=plain") - } else { - flag.append("--reporter=$name,artifact=$jarPath") - } - } else { - flag.append("--reporter=plain") - log.warn("Unknown reporter was specified. Falling back to plain reporter.") - } + customReporter(diktatExtension, flag) } } @@ -147,6 +140,22 @@ open class DiktatJavaExecTaskBase @Inject constructor( return flag.toString() } + private fun customReporter(diktatExtension: DiktatExtension, flag: java.lang.StringBuilder) { + if (diktatExtension.reporterType.startsWith("custom")) { + val name = diktatExtension.reporterType.split(":")[1] + val jarPath = diktatExtension.reporterType.split(":")[2] + if (name.isEmpty() || jarPath.isEmpty()) { + log.warn("Either name or path to jar is not specified. Falling to plain reporter") + flag.append("--reporter=plain") + } else { + flag.append("--reporter=$name,artifact=$jarPath") + } + } else { + flag.append("--reporter=plain") + log.warn("Unknown reporter was specified. Falling back to plain reporter.") + } + } + @Suppress("MagicNumber") private fun isMainClassPropertySupported(gradleVersionString: String) = GradleVersion.version(gradleVersionString) >= GradleVersion.version("6.4") diff --git a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index c367cd2dfd..fa1c8a2840 100644 --- a/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/org/cqfn/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -91,7 +91,7 @@ class DiktatJavaExecTaskTest { @Test fun `check command line has reporter type without output`() { assertCommandLineEquals( - listOf(null, "--reporter=json") + listOf(null, "--reporter=json") ) { inputs = project.files() diktatConfigFile = project.file("../diktat-analysis.yml") @@ -102,7 +102,7 @@ class DiktatJavaExecTaskTest { @Test fun `check command line has custom reporter type with output`() { assertCommandLineEquals( - listOf(null, "--reporter=customName,artifact=customPath") + listOf(null, "--reporter=customName,artifact=customPath") ) { inputs = project.files() diktatConfigFile = project.file("../diktat-analysis.yml") @@ -118,7 +118,12 @@ class DiktatJavaExecTaskTest { reporterType = "html" } - Assertions.assertTrue(project.configurations.getByName("diktat").dependencies.any { it.name == "ktlint-reporter-html" }) + Assertions.assertTrue( + project + .configurations + .getByName("diktat") + .dependencies + .any { it.name == "ktlint-reporter-html" }) Assertions.assertEquals(File(project.projectDir.parentFile, "diktat-analysis.yml").absolutePath, task.systemProperties[DIKTAT_CONF_PROPERTY]) } From 6eaa275e03dafc7a5e6d0b52dcc13c4dd5d0c536 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 18:31:33 +0300 Subject: [PATCH 12/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- .../plugin/gradle/DiktatJavaExecTaskBase.kt | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) 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 b1e2a05c28..b705d03d0b 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 @@ -118,19 +118,11 @@ open class DiktatJavaExecTaskBase @Inject constructor( val flag: StringBuilder = StringBuilder() // Plain, checkstyle and json reporter are provided out of the box in ktlint - when (diktatExtension.reporterType) { - "json" -> { - flag.append("--reporter=json") - } - "html" -> { - flag.append("--reporter=html") - } - "checkstyle" -> { - flag.append("--reporter=checkstyle") - } - else -> { - customReporter(diktatExtension, flag) - } + when (diktatExtension.reporterType) { + "json" -> flag.append("--reporter=json") + "html" -> flag.append("--reporter=html") + "checkstyle" -> flag.append("--reporter=checkstyle") + else -> customReporter(diktatExtension, flag) } if (diktatExtension.output.isNotEmpty()) { From 4b8b6012f6439e14fd65fcfe0226ad4d6367d931 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 26 Jan 2021 18:54:34 +0300 Subject: [PATCH 13/13] feature/gradle-ktlint-reporter(#714) ### What's done: * Fixed bugs --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 0f3a8f5295..83569aa7c0 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,30 @@ diktat { } ``` +Also `diktat` extension has different reporters. You can specify `json`, `html`, `checkstyle`, `plain` (default) or your own custom reporter: +```kotlin +diktat { + reporter = "json" // "html", "checkstyle", "plain" +} +``` + +Example of your custom reporter: +```kotlin +diktat { + reporter = "custom:name:pathToJar" +} +``` +Name parameter is the name of your reporter and as the last parameter you should specify path to jar, which contains your reporter. +[Example of the junit custom reporter.](https://github.com/kryanod/ktlint-junit-reporter) + +You can also specify an output. +```kotlin +diktat { + reporter = "json" + output = "someFile.json" +} +``` + You can run diktat checks using task `diktatCheck` and automatically fix errors with tasks `diktatFix`. ## Customizations via `diktat-analysis.yml`