Skip to content

Commit

Permalink
Merge branch 'master' into vtchem-patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6675636b796f75676974687562 authored Jul 8, 2022
2 parents a239f81 + 739bd3a commit e110673
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
9 changes: 5 additions & 4 deletions diktat-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ repositories {
}

// default value is needed for correct gradle loading in IDEA; actual value from maven is used during build
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.43.0") as String
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "1.1.0"
// To debug gradle plugin, please set `diktatVersion` manually to the current maven project version.
val ktlintVersion = project.properties.getOrDefault("ktlintVersion", "0.46.1") as String
val diktatVersion = project.version.takeIf { it.toString() != Project.DEFAULT_VERSION } ?: "1.2.1"
val junitVersion = project.properties.getOrDefault("junitVersion", "5.8.1") as String
val jacocoVersion = project.properties.getOrDefault("jacocoVersion", "0.8.7") as String
dependencies {
Expand Down Expand Up @@ -107,7 +108,7 @@ val functionalTest = sourceSets.create("functionalTest") {
runtimeClasspath += output + compileClasspath
}
tasks.getByName<Test>("functionalTest") {
dependsOn("test")
shouldRunAfter("test")
testClassesDirs = functionalTest.output.classesDirs
classpath = functionalTest.runtimeClasspath
maxParallelForks = Runtime.getRuntime().availableProcessors()
Expand All @@ -131,7 +132,7 @@ jacocoTestKit {
applyTo("functionalTestRuntimeOnly", tasks.named("functionalTest"))
}
tasks.jacocoTestReport {
dependsOn(tasks.withType<Test>())
shouldRunAfter(tasks.withType<Test>())
executionData(
fileTree("$buildDir/jacoco").apply {
include("*.exec")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,26 @@ class DiktatGradlePluginGroovyFunctionalTest {
}

@Test
fun `should execute diktatCheck on default values`() {
fun `should execute diktatCheck with default values`() {
val result = runDiktat(testProjectDir, shouldSucceed = false)

val diktatCheckBuildResult = result.task(":${DiktatGradlePlugin.DIKTAT_CHECK_TASK}")
requireNotNull(diktatCheckBuildResult)
Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome)
Assertions.assertTrue(
result.output.contains("[FILE_NAME_MATCH_CLASS]")
assertDiktatExecuted(result)
}

@Test
fun `should execute diktatCheck with explicit configuration`() {
buildFile.appendText(
"""${System.lineSeparator()}
diktat {
inputs { it.include("src/**/*.kt") }
reporter = "plain"
diktatConfigFile = file(rootDir.path + "/diktat-analysis.yml")
}
""".trimIndent()
)

val result = runDiktat(testProjectDir, shouldSucceed = false)

assertDiktatExecuted(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package org.cqfn.diktat.plugin.gradle

import org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.Assertions
import java.io.File
import java.util.concurrent.atomic.AtomicInteger

Expand Down Expand Up @@ -71,3 +74,12 @@ private fun GradleRunner.withJaCoCo(number: Int) = apply {
}
}
}

fun assertDiktatExecuted(result: BuildResult) {
val diktatCheckBuildResult = result.task(":${DiktatGradlePlugin.DIKTAT_CHECK_TASK}")
requireNotNull(diktatCheckBuildResult)
Assertions.assertEquals(TaskOutcome.FAILED, diktatCheckBuildResult.outcome)
Assertions.assertTrue(
result.output.contains("[FILE_NAME_MATCH_CLASS]")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ open class DiktatExtension(
var reporter: String = "plain"

/**
* Type of output
* Default: System.out
* Destination for reporter. If empty, will write to stdout.
*/
var output: String = ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,32 @@ open class DiktatJavaExecTaskBase @Inject constructor(
return flag.toString()
}

@Suppress("SAY_NO_TO_VAR")
private fun setReporter(diktatExtension: DiktatExtension, flag: java.lang.StringBuilder) {
val name = diktatExtension.reporter.trim()
val validReporters = listOf("sarif", "plain", "json", "html")
var reporterFlag = if (name.isEmpty() || !validReporters.contains(name)) {
project.logger.warn("Reporter name $name was not specified or is invalid. Falling to 'plain' reporter")
"--reporter=plain"
} else {
"--reporter=$name"
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"
}

// githubActions should have higher priority than a custom input
if (diktatExtension.githubActions) {
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())
reporterFlag = "--reporter=sarif"
}

flag.append(reporterFlag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ class DiktatJavaExecTaskTest {
diktatConfigFile = project.file("../diktat-analysis.yml")
githubActions = true
}
val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatJavaExecTaskBase
Assertions.assertEquals(
project.rootDir.toString(),
task.systemProperties["user.home"]
)
}

@Test
Expand All @@ -137,6 +142,22 @@ class DiktatJavaExecTaskTest {
}
}

@Test
fun `should set system property with SARIF reporter`() {
assertCommandLineEquals(
listOf(null, "--reporter=sarif")
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
reporter = "sarif"
}
val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatJavaExecTaskBase
Assertions.assertEquals(
project.rootDir.toString(),
task.systemProperties["user.home"]
)
}

@Test
fun `check system property with multiproject build with default config`() {
setupMultiProject()
Expand Down
3 changes: 2 additions & 1 deletion examples/gradle-groovy-dsl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ repositories {
}

diktat {
inputs { include ("src/**/*.kt") }
inputs { it.include ("src/**/*.kt") }
diktatConfigFile = file(rootDir.path + "/diktat-analysis.yml")
}

0 comments on commit e110673

Please sign in to comment.