Partially drop redundant usage of "dry run" in Gradle plugin tests (#4776)

* Add task dependency helpers

* Use ProjectBuilder instead of dryRun for DetektPlainSpec

* Don't explicitly disable dryRun mode

dryRun mode is already disabled by default.

* Run real detekt task in GradleVersionSpec

* Run real detekt task in ConfigurationCacheSpec
This commit is contained in:
Matthew Haughton
2022-04-27 09:00:52 +10:00
committed by GitHub
parent dac5401949
commit 3e062dfb6f
6 changed files with 95 additions and 96 deletions

View File

@@ -4,24 +4,17 @@ import io.gitlab.arturbosch.detekt.testkit.DslTestBuilder
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
class ConfigurationCacheSpec {
@ParameterizedTest(name = "Given {0}, can be loaded from the configuration cache")
@CsvSource(
"regular invocation, 'detekt'",
"dry-run invocation, 'detekt,-Pdetekt-dry-run=true'",
)
@Suppress("UnusedPrivateMember") // `unused` is used in the parameterized test name
fun detektConfigCache(unused: String, arguments: String) {
@Test
fun `detekt task can be loaded from the configuration cache`() {
val gradleRunner = DslTestBuilder.kotlin().build()
// First run primes the cache
gradleRunner.runTasks("--configuration-cache", *arguments.split(',').toTypedArray())
gradleRunner.runTasks("--configuration-cache", "detekt")
// Second run reuses the cache
val result = gradleRunner.runTasks("--configuration-cache", *arguments.split(',').toTypedArray())
val result = gradleRunner.runTasks("--configuration-cache", "detekt")
assertThat(result.output).contains("Reusing configuration cache.")
}
@@ -36,7 +29,6 @@ class ConfigurationCacheSpec {
|}
"""
val gradleRunner = DslTestBuilder.kotlin()
.dryRun()
.withDetektConfig(detektConfig)
.build()
@@ -55,7 +47,6 @@ class ConfigurationCacheSpec {
@Test
fun `can be loaded from the configuration cache`() {
val gradleRunner = DslTestBuilder.kotlin()
.dryRun()
.build()
// First run primes the cache

View File

@@ -35,8 +35,7 @@ class DetektJvmSpec {
txt.destination = file("output-path2.txt")
}
}
""".trimIndent(),
dryRun = false
""".trimIndent()
).also {
it.setupProject()
}
@@ -70,8 +69,7 @@ class DetektJvmSpec {
txt.destination = file("output-path2.txt")
}
}
""".trimIndent(),
dryRun = false
""".trimIndent()
).also {
it.setupProject()
}

View File

@@ -1,78 +0,0 @@
package io.gitlab.arturbosch.detekt
import io.gitlab.arturbosch.detekt.testkit.DslGradleRunner
import io.gitlab.arturbosch.detekt.testkit.ProjectLayout
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class DetektPlainSpec {
@Nested
inner class `When detekt is applied before JVM plugin` {
val gradleRunner = DslGradleRunner(
projectLayout = ProjectLayout(numberOfSourceFilesInRootPerSourceDir = 1),
buildFileName = "build.gradle.kts",
mainBuildFileContent = """
plugins {
id("io.gitlab.arturbosch.detekt")
kotlin("jvm")
}
repositories {
mavenCentral()
mavenLocal()
}
detekt {
}
""".trimIndent(),
dryRun = true
).also { it.setupProject() }
@Test
fun `lazily adds detekt as a dependency of the 'check' task`() {
gradleRunner.runTasksAndCheckResult("check") { buildResult ->
assertThat(buildResult.task(":detekt")).isNotNull
}
}
}
@Nested
inner class `When applying detekt in a project` {
val gradleRunner = DslGradleRunner(
projectLayout = ProjectLayout(numberOfSourceFilesInRootPerSourceDir = 1),
buildFileName = "build.gradle.kts",
baselineFiles = listOf("detekt-baseline.xml"),
mainBuildFileContent = """
plugins {
kotlin("jvm")
id("io.gitlab.arturbosch.detekt")
}
repositories {
mavenCentral()
mavenLocal()
}
tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
reports {
sarif.enabled = true
txt.enabled = false
}
}
""".trimIndent(),
dryRun = true
).also { it.setupProject() }
@Test
fun `configures detekt plain task`() {
gradleRunner.runTasksAndCheckResult(":detekt") { buildResult ->
assertThat(buildResult.output).containsPattern("""--baseline \S*[/\\]detekt-baseline.xml """)
assertThat(buildResult.output).contains("--report xml:")
assertThat(buildResult.output).contains("--report sarif:")
assertThat(buildResult.output).doesNotContain("--report txt:")
assertThat(buildResult.output).doesNotContain("--classpath")
}
}
}
}

View File

@@ -15,7 +15,7 @@ class GradleVersionSpec {
@EnabledForJreRange(max = JAVA_13, disabledReason = "Gradle $gradleVersion unsupported on this Java version")
fun runsOnOldestSupportedGradleVersion() {
val builder = DslTestBuilder.kotlin()
val gradleRunner = builder.dryRun().withGradleVersion(gradleVersion).build()
val gradleRunner = builder.withGradleVersion(gradleVersion).build()
gradleRunner.runDetektTaskAndCheckResult { result ->
assertThat(result.task(":detekt")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

View File

@@ -0,0 +1,82 @@
package io.gitlab.arturbosch.detekt
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import io.gitlab.arturbosch.detekt.testkit.DslGradleRunner
import io.gitlab.arturbosch.detekt.testkit.ProjectLayout
import io.gitlab.arturbosch.detekt.testkit.dependenciesAsPaths
import org.assertj.core.api.Assertions.assertThat
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.repositories
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class DetektPlainSpec {
@Nested
inner class `When detekt is applied before JVM plugin` {
val gradleRunner = DslGradleRunner(
projectLayout = ProjectLayout(numberOfSourceFilesInRootPerSourceDir = 1),
buildFileName = "build.gradle.kts",
projectScript = {
apply<DetektPlugin>()
apply<KotlinPluginWrapper>() // org.jetbrains.kotlin.jvm
repositories {
mavenCentral()
mavenLocal()
}
configure<DetektExtension> {
}
},
).also { it.setupProject() }
@Test
fun `lazily adds detekt as a dependency of the 'check' task`() {
val project = gradleRunner.buildProject()
assertThat(project.tasks["check"].dependenciesAsPaths()).contains(":detekt")
}
}
@Nested
inner class `When applying detekt in a project` {
val gradleRunner = DslGradleRunner(
projectLayout = ProjectLayout(numberOfSourceFilesInRootPerSourceDir = 1),
buildFileName = "build.gradle.kts",
baselineFiles = listOf("detekt-baseline.xml"),
projectScript = {
apply<KotlinPluginWrapper>() // org.jetbrains.kotlin.jvm
apply<DetektPlugin>()
repositories {
mavenCentral()
mavenLocal()
}
tasks.withType(Detekt::class.java).configureEach {
it.reports { reports ->
reports.sarif.enabled = true
reports.txt.enabled = false
}
}
},
).also { it.setupProject() }
@Test
fun `configures detekt plain task`() {
val project = gradleRunner.buildProject()
val detektTask = project.tasks.getByPath("detekt") as Detekt
val argumentString = detektTask.arguments.get().joinToString(" ")
assertThat(argumentString).containsPattern("""--baseline \S*[/\\]detekt-baseline.xml """)
assertThat(argumentString).contains("--report xml:")
assertThat(argumentString).contains("--report sarif:")
assertThat(argumentString).doesNotContain("--report txt:")
assertThat(argumentString).doesNotContain("--classpath")
}
}
}

View File

@@ -0,0 +1,6 @@
package io.gitlab.arturbosch.detekt.testkit
import org.gradle.api.Task
fun Task.dependenciesAsNames() = this.taskDependencies.getDependencies(this).map { it.name }
fun Task.dependenciesAsPaths() = this.taskDependencies.getDependencies(this).map { it.path }