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

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

Merged
merged 5 commits into from Apr 26, 2022
Merged
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
Expand Up @@ -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.")
}
Expand All @@ -36,7 +29,6 @@ class ConfigurationCacheSpec {
|}
"""
val gradleRunner = DslTestBuilder.kotlin()
.dryRun()
.withDetektConfig(detektConfig)
.build()

Expand All @@ -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
Expand Down
Expand Up @@ -35,8 +35,7 @@ class DetektJvmSpec {
txt.destination = file("output-path2.txt")
}
}
""".trimIndent(),
dryRun = false
""".trimIndent()
).also {
it.setupProject()
}
Expand Down Expand Up @@ -70,8 +69,7 @@ class DetektJvmSpec {
txt.destination = file("output-path2.txt")
}
}
""".trimIndent(),
dryRun = false
""".trimIndent()
).also {
it.setupProject()
}
Expand Down

This file was deleted.

Expand Up @@ -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)
}
Expand Down
@@ -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")
}
}
}
@@ -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 }