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

7018 fix #7025

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion detekt-gradle-plugin/api/detekt-gradle-plugin.api
Expand Up @@ -130,7 +130,6 @@ public abstract interface class io/gitlab/arturbosch/detekt/extensions/DetektExt
public abstract fun getIgnoredVariants ()Lorg/gradle/api/provider/ListProperty;
public abstract fun getParallel ()Lorg/gradle/api/provider/Property;
public abstract fun getReportsDir ()Lorg/gradle/api/file/DirectoryProperty;
public abstract fun getSource ()Lorg/gradle/api/file/ConfigurableFileCollection;
public abstract fun getToolVersion ()Lorg/gradle/api/provider/Property;
}

Expand Down
Expand Up @@ -35,12 +35,12 @@ class ConfigurationCacheSpec {
.build()

// First run primes the cache
val storeCacheResult = gradleRunner.runTasks("--configuration-cache", "detektBaseline")
val storeCacheResult = gradleRunner.runTasks("--configuration-cache", "detektBaselineMainSourceSet")

assertThat(storeCacheResult.output).contains("Configuration cache entry stored.")

// Second run reuses the cache
val reuseCacheResult = gradleRunner.runTasks("--configuration-cache", "detektBaseline")
val reuseCacheResult = gradleRunner.runTasks("--configuration-cache", "detektBaselineMainSourceSet")

assertThat(reuseCacheResult.output).contains("Reusing configuration cache.")
}
Expand Down
Expand Up @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test

class CreateBaselineTaskDslSpec {
@Test
fun `detektBaseline task can be executed when baseline file is specified`() {
fun `detektBaselineMainSourceSet task can be executed when baseline file is specified`() {
val baselineFilename = "baseline.xml"

val detektConfig = """
Expand All @@ -26,15 +26,15 @@ class CreateBaselineTaskDslSpec {
.withDetektConfig(detektConfig)
.build()

gradleRunner.runTasksAndCheckResult("detektBaseline") { result ->
assertThat(result.task(":detektBaseline")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(projectFile(baselineFilename)).exists()
gradleRunner.runTasksAndCheckResult("detektBaselineMainSourceSet") { result ->
assertThat(result.task(":detektBaselineMainSourceSet")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(projectFile("baseline-mainSourceSet.xml")).exists()
assertThat(projectFile(DEFAULT_BASELINE_FILENAME)).doesNotExist()
}
}

@Test
fun `detektBaseline task can be executed when baseline file is not specified`() {
fun `detektBaselineMainSourceSet task can be executed when baseline file is not specified`() {
val detektConfig = """
detekt {
}
Expand All @@ -49,11 +49,11 @@ class CreateBaselineTaskDslSpec {
.withDetektConfig(detektConfig)
.build()

gradleRunner.runTasksAndCheckResult("detektBaseline") { result ->
assertThat(result.task(":detektBaseline")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
gradleRunner.runTasksAndCheckResult("detektBaselineMainSourceSet") { result ->
assertThat(result.task(":detektBaselineMainSourceSet")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(projectFile(DEFAULT_BASELINE_FILENAME)).exists()
}
}
}

private const val DEFAULT_BASELINE_FILENAME = "detekt-baseline.xml"
private const val DEFAULT_BASELINE_FILENAME = "detekt-baseline-mainSourceSet.xml"
Expand Up @@ -48,25 +48,25 @@ class DetektReportMergeSpec {
val gradleRunner = DslGradleRunner(projectLayout, builder.gradleBuildName, buildFileContent)
gradleRunner.setupProject()
gradleRunner.runTasksAndExpectFailure("detekt", "sarifReportMerge", "--continue") { result ->
assertThat(result.output).contains("FAILURE: Build completed with 2 failures.")
assertThat(result.output).contains("FAILURE: Build completed with 4 failures.")
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child1:detekt'.
Execution failed for task ':child1:detektMainSourceSet'.
> Analysis failed with 2 issues.
""".trimIndent()
)
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child2:detekt'.
Execution failed for task ':child2:detektMainSourceSet'.
> Analysis failed with 4 issues.
""".trimIndent()
)
assertThat(projectFile("build/reports/detekt/detekt.sarif")).doesNotExist()
assertThat(projectFile("build/reports/detekt/mainSourceSet.sarif")).doesNotExist()
assertThat(projectFile("build/reports/detekt/merge.sarif")).exists()
assertThat(projectFile("build/reports/detekt/merge.sarif").readText())
.contains("\"ruleId\": \"detekt.style.MagicNumber\"")
projectLayout.submodules.forEach {
assertThat(projectFile("${it.name}/build/reports/detekt/detekt.sarif")).exists()
assertThat(projectFile("${it.name}/build/reports/detekt/mainSourceSet.sarif")).exists()
}
}
}
Expand Down Expand Up @@ -111,25 +111,25 @@ class DetektReportMergeSpec {
val gradleRunner = DslGradleRunner(projectLayout, builder.gradleBuildName, buildFileContent)
gradleRunner.setupProject()
gradleRunner.runTasksAndExpectFailure("detekt", "xmlReportMerge", "--continue") { result ->
assertThat(result.output).contains("FAILURE: Build completed with 2 failures.")
assertThat(result.output).contains("FAILURE: Build completed with 4 failures.")
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child1:detekt'.
Execution failed for task ':child1:detektMainSourceSet'.
> Analysis failed with 2 issues.
""".trimIndent()
)
assertThat(result.output).containsIgnoringWhitespaces(
"""
Execution failed for task ':child2:detekt'.
Execution failed for task ':child2:detektMainSourceSet'.
> Analysis failed with 4 issues.
""".trimIndent()
)
assertThat(projectFile("build/reports/detekt/detekt.xml")).doesNotExist()
assertThat(projectFile("build/reports/detekt/mainSourceSet.xml")).doesNotExist()
assertThat(projectFile("build/reports/detekt/merge.xml")).exists()
assertThat(projectFile("build/reports/detekt/merge.xml").readText())
.contains("<error column=\"31\" line=\"4\"")
projectLayout.submodules.forEach {
assertThat(projectFile("${it.name}/build/reports/detekt/detekt.xml")).exists()
assertThat(projectFile("${it.name}/build/reports/detekt/mainSourceSet.xml")).exists()
}
}
}
Expand Down
Expand Up @@ -3,7 +3,6 @@ package io.gitlab.arturbosch.detekt
import io.gitlab.arturbosch.detekt.extensions.DetektReportType
import io.gitlab.arturbosch.detekt.extensions.loadDetektVersion
import io.gitlab.arturbosch.detekt.testkit.DslTestBuilder.Companion.kotlin
import io.gitlab.arturbosch.detekt.testkit.ProjectLayout
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.DisplayName
Expand All @@ -29,25 +28,25 @@ class DetektTaskDslSpec {

@Test
fun `enables xml report to default location`() {
val xmlReportFile = gradleRunner.projectFile("build/reports/detekt/detekt.xml")
val xmlReportFile = gradleRunner.projectFile("build/reports/detekt/mainSourceSet.xml")
assertThat(result.output).contains("--report xml:$xmlReportFile")
}

@Test
fun `enables html report to default location`() {
val htmlReportFile = gradleRunner.projectFile("build/reports/detekt/detekt.html")
val htmlReportFile = gradleRunner.projectFile("build/reports/detekt/mainSourceSet.html")
assertThat(result.output).contains("--report html:$htmlReportFile")
}

@Test
fun `enables text report to default location`() {
val textReportFile = gradleRunner.projectFile("build/reports/detekt/detekt.txt")
val textReportFile = gradleRunner.projectFile("build/reports/detekt/mainSourceSet.txt")
assertThat(result.output).contains("--report txt:$textReportFile")
}

@Test
fun `enables sarif report to default location`() {
val sarifReportFile = gradleRunner.projectFile("build/reports/detekt/detekt.sarif")
val sarifReportFile = gradleRunner.projectFile("build/reports/detekt/mainSourceSet.sarif")
assertThat(result.output).contains("--report sarif:$sarifReportFile")
}

Expand All @@ -58,7 +57,8 @@ class DetektTaskDslSpec {
val file2 = gradleRunner.projectFile("src/test/java/My1Root0Class.kt")
val file3 = gradleRunner.projectFile("src/main/kotlin/My2Root0Class.kt")
val file4 = gradleRunner.projectFile("src/test/kotlin/My3Root0Class.kt")
assertThat(result.output).contains("--input $file1,$file2,$file3,$file4 ")
assertThat(result.output).contains("--input $file3,$file1 ")
assertThat(result.output).contains("--input $file4,$file2 ")
}
}

Expand Down Expand Up @@ -128,39 +128,6 @@ class DetektTaskDslSpec {
}
}

@Nested
inner class `with custom input directories` {
val customSrc1 = "gensrc/kotlin"
val customSrc2 = "src/main/kotlin"
private val builder = kotlin().dryRun()

private val config = """
detekt {
source.setFrom(files("$customSrc1", "$customSrc2", "folder_that_does_not_exist"))
}
""".trimIndent()

private val projectLayout = ProjectLayout(1, srcDirs = listOf(customSrc1, customSrc2))
private val gradleRunner = builder
.withProjectLayout(projectLayout)
.withDetektConfig(config)
.build()
private val result = gradleRunner.runDetektTask()

@Test
fun `sets input parameter to absolute filenames of all source files`() {
val file1 = gradleRunner.projectFile("$customSrc1/My0Root0Class.kt")
val file2 = gradleRunner.projectFile("$customSrc2/My1Root0Class.kt")
val expectedInputParam = "--input $file1,$file2"
assertThat(result.output).contains(expectedInputParam)
}

@Test
fun `ignores input directories that do not exist`() {
assertThat(result.output).doesNotContain("folder_that_does_not_exist")
}
}

@Nested
inner class `with custom reports dir` {
private val config = """
Expand All @@ -174,25 +141,25 @@ class DetektTaskDslSpec {

@Test
fun `configures xml report to custom directory`() {
val xmlReportFile = gradleRunner.projectFile("build/detekt-reports/detekt.xml")
val xmlReportFile = gradleRunner.projectFile("build/detekt-reports/mainSourceSet.xml")
assertThat(result.output).contains("--report xml:$xmlReportFile")
}

@Test
fun `configures html report to custom directory`() {
val htmlReportFile = gradleRunner.projectFile("build/detekt-reports/detekt.html")
val htmlReportFile = gradleRunner.projectFile("build/detekt-reports/mainSourceSet.html")
assertThat(result.output).contains("--report html:$htmlReportFile")
}

@Test
fun `configures text report to custom directory`() {
val textReportFile = gradleRunner.projectFile("build/detekt-reports/detekt.txt")
val textReportFile = gradleRunner.projectFile("build/detekt-reports/mainSourceSet.txt")
assertThat(result.output).contains("--report txt:$textReportFile")
}

@Test
fun `configures sarif report to custom directory`() {
val sarifReportFile = gradleRunner.projectFile("build/detekt-reports/detekt.sarif")
val sarifReportFile = gradleRunner.projectFile("build/detekt-reports/mainSourceSet.sarif")
assertThat(result.output).contains("--report sarif:$sarifReportFile")
}
}
Expand All @@ -204,15 +171,15 @@ class DetektTaskDslSpec {
reportsDir = file("build/detekt-reports")
}

tasks.detekt {
tasks.detektMainSourceSet {
reports {
xml.outputLocation.set(file("build/xml-reports/custom-detekt.xml"))
}
}
""".trimIndent()
private val builder = kotlin().dryRun()
private val gradleRunner = builder.withDetektConfig(config).build()
private val result = gradleRunner.runDetektTask()
private val result = gradleRunner.runTasks("detektMainSourceSet")

@Test
fun `configures xml report to specific absolute filename`() {
Expand All @@ -222,21 +189,21 @@ class DetektTaskDslSpec {

@Test
fun `configures html report to default name in custom directory`() {
val htmlReportFile = gradleRunner.projectFile("build/detekt-reports/detekt.html")
val htmlReportFile = gradleRunner.projectFile("build/detekt-reports/mainSourceSet.html")
assertThat(result.output).contains("--report html:$htmlReportFile")
}

@Test
fun `configures text report to default name in custom directory`() {
val textReportFile = gradleRunner.projectFile("build/detekt-reports/detekt.txt")
val textReportFile = gradleRunner.projectFile("build/detekt-reports/mainSourceSet.txt")
assertThat(result.output).contains("--report txt:$textReportFile")
}
}

@Nested
inner class `with disabled reports` {
private val config = """
tasks.detekt {
tasks.detektMainSourceSet {
reports {
xml.required.set(false)
html {
Expand All @@ -256,7 +223,7 @@ class DetektTaskDslSpec {
""".trimIndent()
private val builder = kotlin().dryRun()
private val gradleRunner = builder.withDetektConfig(config).build()
private val result = gradleRunner.runDetektTask()
private val result = gradleRunner.runTasks("detektMainSourceSet")

@Test
fun `no report param is set`() {
Expand All @@ -269,7 +236,7 @@ class DetektTaskDslSpec {
@Nested
inner class `configured correctly` {
private val config = """
tasks.detekt {
tasks.detektMainSourceSet {
reports {
custom {
reportId = "customXml"
Expand Down Expand Up @@ -476,7 +443,7 @@ class DetektTaskDslSpec {
inner class `with cmdline args` {
private val builder = kotlin().dryRun()
private val gradleRunner = builder.build()
private val result = gradleRunner.runDetektTask("--auto-correct")
private val result = gradleRunner.runDetektTask("detektMainSourceSet", "--auto-correct")

@Test
fun `enables auto correcting`() {
Expand Down
Expand Up @@ -14,7 +14,6 @@ class DetektTaskGroovyDslSpec {
toolVersion = "1.0.0.RC8"
ignoreFailures = true
failOnSeverity = io.gitlab.arturbosch.detekt.extensions.FailOnSeverity.Info
source.setFrom(file("src/main/kotlin"))
baseline = file("path/to/baseline.xml")
basePath = projectDir
config.setFrom("config/detekt/detekt.yml")
Expand Down