From 516f4054efc2e23ac788a4569d07eb3278f67373 Mon Sep 17 00:00:00 2001 From: Zongle Wang Date: Sat, 4 Jan 2025 03:51:38 -0500 Subject: [PATCH] Port @Issue for Junit 5 (#1126) * Add Issue and IssueExtension * Replace issue links * Add ExtensionRegistryTests for IssueExtension * Enable junit.jupiter.extensions.autodetection.enabled for IssueExtension * Revert "Add ExtensionRegistryTests for IssueExtension" This reverts commit ff13d020 --- build.gradle.kts | 4 ++ .../gradle/plugins/shadow/RelocationTest.kt | 29 ++++++------- .../gradle/plugins/shadow/ShadowPluginTest.kt | 42 ++++++++++--------- .../gradle/plugins/shadow/util/Issue.kt | 31 ++++++++++++++ .../org.junit.jupiter.api.extension.Extension | 1 + 5 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/Issue.kt create mode 100644 src/intiTest/resources/META-INF/services/org.junit.jupiter.api.extension.Extension diff --git a/build.gradle.kts b/build.gradle.kts index 4fd1f40de..2c951a4d1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -106,6 +106,10 @@ val integrationTest by tasks.registering(Test::class) { testClassesDirs = intiTest.output.classesDirs classpath = intiTest.runtimeClasspath + // TODO: this should be moved into functionalTest after we migrated all functional tests to Kotlin. + // Required to enable `IssueExtension` for all tests. + systemProperty("junit.jupiter.extensions.autodetection.enabled", true) + val docsDir = file("src/docs") // Add src/docs as an input directory to trigger ManualCodeSnippetTests re-run on changes. inputs.dir(docsDir) diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt index a80671375..b0e002124 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt @@ -5,6 +5,7 @@ import assertk.assertThat import assertk.assertions.exists import assertk.assertions.isEqualTo import assertk.assertions.isInstanceOf +import com.github.jengelman.gradle.plugins.shadow.util.Issue import java.net.URLClassLoader import java.util.jar.JarFile import kotlin.io.path.appendText @@ -50,9 +51,9 @@ class RelocationTest : BasePluginTest() { ) } - /** - * https://github.com/GradleUp/shadow/issues/58 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/58", + ) @Test fun relocateDependencyFiles() { projectScriptPath.appendText( @@ -179,10 +180,10 @@ class RelocationTest : BasePluginTest() { ) } - /** - * https://github.com/GradleUp/shadow/issues/53 - * https://github.com/GradleUp/shadow/issues/55 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/53", + "https://github.com/GradleUp/shadow/issues/55", + ) @Test fun remapClassNamesForRelocatedFilesInProjectSource() { projectScriptPath.appendText( @@ -314,10 +315,10 @@ class RelocationTest : BasePluginTest() { ) } - /** - * https://github.com/GradleUp/shadow/issues/93 - * https://github.com/GradleUp/shadow/issues/114 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/93", + "https://github.com/GradleUp/shadow/issues/114", + ) @Test fun relocateResourceFiles() { repo.module("shadow", "dep", "1.0") @@ -363,9 +364,9 @@ class RelocationTest : BasePluginTest() { ) } - /** - * https://github.com/GradleUp/shadow/issues/294 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/294", + ) @Test fun doesNotErrorOnRelocatingJava9Classes() { projectScriptPath.appendText( diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPluginTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPluginTest.kt index e29791cf0..7aa7674ff 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPluginTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPluginTest.kt @@ -9,6 +9,7 @@ import assertk.assertions.isNull import assertk.assertions.isTrue import com.github.jengelman.gradle.plugins.shadow.legacy.LegacyShadowPlugin import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import com.github.jengelman.gradle.plugins.shadow.util.Issue import java.util.jar.JarFile import kotlin.io.path.appendText import kotlin.io.path.readText @@ -556,9 +557,9 @@ class ShadowPluginTest : BasePluginTest() { assertThat(attributes.getValue("Class-Path")).isNull() } - /** - * https://github.com/GradleUp/shadow/issues/65 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/65", + ) @Test fun addShadowConfigurationToClassPathInManifest() { projectScriptPath.appendText( @@ -581,9 +582,9 @@ class ShadowPluginTest : BasePluginTest() { assertThat(attributes.getValue("Class-Path")).isEqualTo("/libs/a.jar junit-3.8.2.jar") } - /** - * https://github.com/GradleUp/shadow/issues/92 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/92", + ) @Test fun doNotIncludeNullValueInClassPathWhenJarFileDoesNotContainClassPath() { projectScriptPath.appendText( @@ -601,9 +602,9 @@ class ShadowPluginTest : BasePluginTest() { assertThat(attributes.getValue("Class-Path")).isEqualTo("junit-3.8.2.jar") } - /** - * https://github.com/GradleUp/shadow/issues/203 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/203", + ) @Test fun supportZipCompressionStored() { projectScriptPath.appendText( @@ -625,9 +626,10 @@ class ShadowPluginTest : BasePluginTest() { /** * This spec requires > 15 minutes and > 8GB of disk space to run - * - * https://github.com/GradleUp/shadow/issues/143 */ + @Issue( + "https://github.com/GradleUp/shadow/issues/143", + ) @Disabled @Test fun checkLargeZipFilesWithZip64Enabled() { @@ -690,9 +692,9 @@ class ShadowPluginTest : BasePluginTest() { assertThat(result.output).contains("TestApp: Hello World! (foo)") } - /** - * https://github.com/GradleUp/shadow/issues/609 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/609", + ) @Test fun doesNotErrorWhenUsingApplicationMainClassProperty() { projectScriptPath.appendText( @@ -724,9 +726,9 @@ class ShadowPluginTest : BasePluginTest() { assertThat(result.output).contains("TestApp: Hello World! (foo)") } - /** - * https://github.com/GradleUp/shadow/pull/459 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/459", + ) @Test fun excludeGradleApiByDefault() { projectScriptPath.writeText( @@ -759,9 +761,9 @@ class ShadowPluginTest : BasePluginTest() { assertThat(entries.count { it.name.endsWith(".class") }).isEqualTo(1) } - /** - * https://github.com/GradleUp/shadow/issues/1070 - */ + @Issue( + "https://github.com/GradleUp/shadow/issues/1070", + ) @Test fun canRegisterCustomShadowJarTask() { val testShadowJarTask = "testShadowJar" diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/Issue.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/Issue.kt new file mode 100644 index 000000000..e7b936358 --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/Issue.kt @@ -0,0 +1,31 @@ +package com.github.jengelman.gradle.plugins.shadow.util + +import org.junit.jupiter.api.Tags +import org.junit.jupiter.api.extension.BeforeEachCallback +import org.junit.jupiter.api.extension.ExtensionContext +import org.junit.jupiter.api.extension.ExtensionContext.Namespace + +/** + * This is related to [spock.lang.Issue](https://github.com/spockframework/spock/blob/master/spock-core/src/main/java/spock/lang/Issue.java) but is used for JUnit 5 tests. + * + * @see [Tags] + */ +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +annotation class Issue(vararg val values: String) + +class IssueExtension : BeforeEachCallback { + override fun beforeEach(context: ExtensionContext) { + val issueAnnotation = context.requiredTestMethod.getAnnotation(Issue::class.java) ?: return + val store = context.getStore(Namespace.create(IssueExtension::class.java, context.requiredTestClass)) + store.put("tags", issueAnnotation.values.map(::issueLinkToTag)) + } +} + +private fun issueLinkToTag(link: String): String { + return issueLinkRegex.replace(link) { matchResult -> + "ISSUE-${matchResult.groupValues[1]}" + } +} + +private val issueLinkRegex = "https://github\\.com/[^/]+/[^/]+/issues/(\\d+)".toRegex() diff --git a/src/intiTest/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/src/intiTest/resources/META-INF/services/org.junit.jupiter.api.extension.Extension new file mode 100644 index 000000000..fd3002933 --- /dev/null +++ b/src/intiTest/resources/META-INF/services/org.junit.jupiter.api.extension.Extension @@ -0,0 +1 @@ +com.github.jengelman.gradle.plugins.shadow.util.IssueExtension