Skip to content

Commit

Permalink
gradle-plugin: fix behavior in githubActions mode (#1229)
Browse files Browse the repository at this point in the history
### What's done:
* Correctly set system property
* Rename report; store reports in build directory
* Tests

Closes #1213
  • Loading branch information
petertrr authored Mar 17, 2022
1 parent aa3c22b commit 755b6b7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import org.gradle.api.tasks.util.PatternSet
import org.gradle.util.GradleVersion

import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import javax.inject.Inject

/**
Expand Down Expand Up @@ -162,7 +164,11 @@ open class DiktatJavaExecTaskBase @Inject constructor(

val outFlag = when {
// githubActions should have higher priority than a custom input
diktatExtension.githubActions -> ",output=${project.projectDir}/${project.name}"
diktatExtension.githubActions -> {
val reportDir = Files.createDirectories(Paths.get("${project.buildDir}/reports/diktat"))
outputs.dir(reportDir)
",output=${reportDir.resolve("diktat.sarif")}"
}
diktatExtension.output.isNotEmpty() -> ",output=${diktatExtension.output}"
else -> ""
}
Expand All @@ -186,7 +192,7 @@ open class DiktatJavaExecTaskBase @Inject constructor(
// githubActions should have higher priority than a custom input
if (diktatExtension.githubActions) {
// need to set user.home specially for ktlint, so it will be able to put a relative path URI in SARIF
System.setProperty("user.home", project.projectDir.toString())
systemProperty("user.home", project.projectDir.toString())
reporterFlag = "--reporter=sarif"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ class DiktatJavaExecTaskTest {
}
}

@Test
fun `check command line in githubActions mode`() {
val path = project.file("${project.buildDir}/reports/diktat/diktat.sarif")
assertCommandLineEquals(
listOf(null, "--reporter=sarif,output=$path")
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
githubActions = true
}
}

@Test
fun `githubActions mode should have higher precedence over explicit reporter`() {
val path = project.file("${project.buildDir}/reports/diktat/diktat.sarif")
assertCommandLineEquals(
listOf(null, "--reporter=sarif,output=$path")
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
githubActions = true
reporter = "json"
output = "report.json"
}
}

@Test
fun `check system property with multiproject build with default config`() {
setupMultiProject()
Expand Down

0 comments on commit 755b6b7

Please sign in to comment.