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

Remove dependency on mavenLocal() for functionalTests #6060

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions detekt-gradle-plugin/build.gradle.kts
Expand Up @@ -11,8 +11,7 @@ plugins {
`java-test-fixtures`
idea
alias(libs.plugins.pluginPublishing)
// We use this published version of the detekt plugin to self analyse this project.
id("io.gitlab.arturbosch.detekt") version "1.22.0"
alias(libs.plugins.detekt.self.inspection)
}

repositories {
Expand Down Expand Up @@ -140,13 +139,23 @@ tasks {
property("detektVersion", project.version)
property("detektCompilerPluginVersion", project.version)
}
val writeDetektTestVersionProperties by registering(WriteProperties::class) {
description = "Write the properties file with the detekt version to be used by functional tests of the plugin."
encoding = "UTF-8"
destinationFile.set(file("$buildDir/detekt-test-versions.properties"))
property("detektTestVersion", libs.versions.detekt.self.inspection.get())
}

processResources {
from(writeDetektVersionProperties)
}

processTestResources {
from(writeDetektVersionProperties)
from(writeDetektVersionProperties, writeDetektTestVersionProperties)
}

named("processFunctionalTestResources", ProcessResources::class) {
from(writeDetektVersionProperties, writeDetektTestVersionProperties)
}

check {
Expand Down
Expand Up @@ -739,7 +739,6 @@ private fun createGradleRunnerAndSetupProject(
repositories {
mavenCentral()
google()
mavenLocal()
}
}
""".trimIndent(),
Expand Down
Expand Up @@ -20,7 +20,6 @@ class DetektJvmSpec {
}
repositories {
mavenCentral()
mavenLocal()
}
detekt {
reports {
Expand Down Expand Up @@ -59,7 +58,6 @@ class DetektJvmSpec {
}
repositories {
mavenCentral()
mavenLocal()
}
tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
reports {
Expand Down
Expand Up @@ -350,7 +350,6 @@ private fun setupProject(projectLayoutAction: ProjectLayout.() -> Unit): DslGrad
repositories {
mavenCentral()
google()
mavenLocal()
}
}
""".trimIndent(),
Expand Down
@@ -1,7 +1,7 @@
package io.gitlab.arturbosch.detekt

import io.gitlab.arturbosch.detekt.extensions.DetektReportType
import io.gitlab.arturbosch.detekt.extensions.loadDetektVersion
import io.gitlab.arturbosch.detekt.internal.loadDetektVersion
import io.gitlab.arturbosch.detekt.testkit.DslTestBuilder.Companion.kotlin
import io.gitlab.arturbosch.detekt.testkit.ProjectLayout
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -530,7 +530,11 @@ class DetektTaskDslSpec {
}
""".trimIndent()
private val builder = kotlin().dryRun()
private val gradleRunner = builder.withDetektConfig(config).build()
private val gradleRunner = builder
.withDetektConfig(config)
// For this specific test we want to make sure that the version used is the one from the DSL we provide.
.withCustomArgs("-Pdetekt.internal.tool.version.override=")
.build()
private val result = gradleRunner.runTasks("dependencies", "--offline", "--configuration", "detekt")

@Test
Expand Down
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt

import io.gitlab.arturbosch.detekt.testkit.loadTestDetektVersion
import io.gitlab.arturbosch.detekt.testkit.withResourceDir
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.GradleRunner
Expand All @@ -11,7 +12,7 @@ class JvmSpec {
val result = GradleRunner.create()
.withResourceDir("jvm")
.withPluginClasspath()
.withArguments("detektMain")
.withArguments("detektMain", "-Pdetekt.internal.tool.version.override=${loadTestDetektVersion()}")
.buildAndFail()

assertThat(result.output).contains("failed with 3 weighted issues.")
Expand Down
Expand Up @@ -157,7 +157,6 @@ class ReportMergeSpec {
repositories {
mavenCentral()
google()
mavenLocal()
}
}

Expand Down
Expand Up @@ -5,7 +5,6 @@ plugins {

repositories {
mavenCentral()
mavenLocal()
}

tasks.detektMain {
Expand Down
Expand Up @@ -96,7 +96,12 @@ class DetektPlugin : Plugin<Project> {
configuration.isCanBeConsumed = false

configuration.defaultDependencies { dependencySet ->
val version = extension.toolVersion
val versionOverride = (project.findProperty(DETEKT_INTERNAL_TEST_VERSION) as? String)
val version = if ((versionOverride)?.isNotBlank() == true) {
versionOverride
} else {
extension.toolVersion
}
dependencySet.add(project.dependencies.create("io.gitlab.arturbosch.detekt:detekt-cli:$version"))
}
}
Expand Down Expand Up @@ -131,6 +136,7 @@ class DetektPlugin : Plugin<Project> {

internal const val DETEKT_ANDROID_DISABLED_PROPERTY = "detekt.android.disabled"
internal const val DETEKT_MULTIPLATFORM_DISABLED_PROPERTY = "detekt.multiplatform.disabled"
internal const val DETEKT_INTERNAL_TEST_VERSION = "detekt.internal.tool.version.override"
}
}

Expand Down
@@ -1,14 +1,12 @@
package io.gitlab.arturbosch.detekt.extensions

import io.gitlab.arturbosch.detekt.internal.loadDetektVersion
import org.gradle.api.Action
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.quality.CodeQualityExtension
import org.gradle.api.provider.Property
import java.io.File
import java.io.InputStream
import java.net.URL
import java.util.Properties
import javax.inject.Inject

open class DetektExtension @Inject constructor(objects: ObjectFactory) : CodeQualityExtension() {
Expand Down Expand Up @@ -125,36 +123,3 @@ open class DetektExtension @Inject constructor(objects: ObjectFactory) : CodeQua
const val DEFAULT_COMPILER_PLUGIN_ENABLED = true
}
}

internal fun loadDetektVersion(classLoader: ClassLoader): String {
// Other Gradle plugins can also have a versions.properties.
val distinctVersions = classLoader
.getResources("detekt-versions.properties")
.toList()
.mapNotNull { versions ->
Properties().run {
load(versions.openSafeStream())
getProperty("detektVersion")
}
}
.distinct()
return distinctVersions.singleOrNull() ?: error(
"You're importing two detekt plugins which have different versions. " +
"(${distinctVersions.joinToString()}) Make sure to align the versions."
)
}

// Copy-paste from io.github.detekt.utils.openSafeStream in Resources.kt.
// Can't use that function, because gradle-plugin is minimising dependencies: see #4748.
private fun URL.openSafeStream(): InputStream {
return openConnection()
/*
* Due to https://bugs.openjdk.java.net/browse/JDK-6947916 and https://bugs.openjdk.java.net/browse/JDK-8155607,
* it is necessary to disallow caches to maintain stability on JDK 8 and 11 (and possibly more).
* Otherwise, simultaneous invocations of detekt in the same VM can fail spuriously. A similar bug is referenced in
* https://github.com/detekt/detekt/issues/3396. The performance regression is likely unnoticeable.
* Due to https://github.com/detekt/detekt/issues/4332 it is included for all JDKs.
*/
.apply { useCaches = false }
.getInputStream()
}
@@ -0,0 +1,42 @@
package io.gitlab.arturbosch.detekt.internal

import java.io.InputStream
import java.net.URL
import java.util.Properties

fun loadDetektVersion(
classLoader: ClassLoader,
propertyFileName: String = "detekt-versions.properties",
propertyName: String = "detektVersion"
): String {
// Other Gradle plugins can also have a versions.properties.
val distinctVersions = classLoader
.getResources(propertyFileName)
.toList()
.mapNotNull { versions ->
Properties().run {
load(versions.openSafeStream())
getProperty(propertyName)
}
}
.distinct()
return distinctVersions.singleOrNull() ?: error(
"You're importing two detekt plugins which have different versions. " +
"(${distinctVersions.joinToString()}) Make sure to align the versions."
)
}

// Copy-paste from io.github.detekt.utils.openSafeStream in Resources.kt.
// Can't use that function, because gradle-plugin is minimising dependencies: see #4748.
private fun URL.openSafeStream(): InputStream {
return openConnection()
/*
* Due to https://bugs.openjdk.java.net/browse/JDK-6947916 and https://bugs.openjdk.java.net/browse/JDK-8155607,
* it is necessary to disallow caches to maintain stability on JDK 8 and 11 (and possibly more).
* Otherwise, simultaneous invocations of detekt in the same VM can fail spuriously. A similar bug is referenced in
* https://github.com/detekt/detekt/issues/3396. The performance regression is likely unnoticeable.
* Due to https://github.com/detekt/detekt/issues/4332 it is included for all JDKs.
*/
.apply { useCaches = false }
.getInputStream()
}
Expand Up @@ -24,6 +24,7 @@ constructor(
val gradleVersionOrNone: String? = null,
val dryRun: Boolean = false,
val jvmArgs: String = "-Xmx2g -XX:MaxMetaspaceSize=1g",
val customArgs: String = "",
val projectScript: Project.() -> Unit = {}
) {

Expand Down Expand Up @@ -139,6 +140,10 @@ constructor(
if (dryRun) {
add("-Pdetekt-dry-run=true")
}
add("-Pdetekt.internal.tool.version.override=${loadTestDetektVersion()}")
if (customArgs.isNotBlank()) {
add(customArgs)
}
addAll(tasks.toList())
}

Expand Down
Expand Up @@ -12,7 +12,6 @@ abstract class DslTestBuilder {
@Language("gradle.kts")
val gradleRepositories = """
repositories {
mavenLocal()
mavenCentral()
}
""".trimIndent()
Expand All @@ -23,6 +22,7 @@ abstract class DslTestBuilder {
private var baselineFile: String? = null
private var configFile: String? = null
private var gradleVersion: String? = null
private var customArgs: String = ""
private var dryRun: Boolean = false

fun withDetektConfig(@Language("gradle.kts") config: String): DslTestBuilder {
Expand Down Expand Up @@ -50,6 +50,11 @@ abstract class DslTestBuilder {
return this
}

fun withCustomArgs(args: String): DslTestBuilder {
customArgs = args
return this
}

fun dryRun(): DslTestBuilder {
dryRun = true
return this
Expand All @@ -63,6 +68,7 @@ abstract class DslTestBuilder {
configFileOrNone = configFile,
baselineFiles = baselineFile?.let { listOf(it) }.orEmpty(),
gradleVersionOrNone = gradleVersion,
customArgs = customArgs,
dryRun = dryRun,
)
runner.setupProject()
Expand Down
@@ -1,5 +1,7 @@
package io.gitlab.arturbosch.detekt.testkit

import io.gitlab.arturbosch.detekt.DetektPlugin
import io.gitlab.arturbosch.detekt.internal.loadDetektVersion
import org.gradle.api.Task
import org.intellij.lang.annotations.Language

Expand Down Expand Up @@ -30,3 +32,10 @@ fun String.reIndent(level: Int = 0, baseIndent: Int = 3): String =

fun joinGradleBlocks(@Language("gradle.kts") vararg blocks: String): String =
blocks.joinToString(separator = "\n\n")

fun loadTestDetektVersion(): String =
loadDetektVersion(
DetektPlugin::class.java.classLoader,
"detekt-test-versions.properties",
"detektTestVersion"
)
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Expand Up @@ -5,6 +5,8 @@ kotlin = "1.8.21"
ktlint = "0.49.0"
junit = "5.9.3"
contester = "0.2.0"
# Version used to self-inpsect and format the Gradle Plugin.
cortinico marked this conversation as resolved.
Show resolved Hide resolved
detekt-self-inspection = "1.22.0"

[libraries]
githubRelease-gradle = "com.github.breadmoirai:github-release:2.4.1"
Expand Down Expand Up @@ -53,3 +55,4 @@ download = { id = "de.undercouch.download", version = "5.4.0" }
gradleVersions = { id = "com.github.ben-manes.versions", version = "0.46.0" }
pluginPublishing = { id = "com.gradle.plugin-publish", version = "1.2.0" }
shadow = { id = "com.github.johnrengelman.shadow", version = "8.1.1" }
detekt-self-inspection = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt-self-inspection" }