Repository: GradleUp/shadow
Pull request #1172 (2025-01-18 13:37:38)

Be friend with `functionalTest`


Patch:
diff --git a/build.gradle.kts b/build.gradle.kts
index 362ced92..0feaab7b 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -105,8 +105,6 @@ testing.suites {
       }
     }
     dependencies {
-      // Seems we can't ref project() here due to some limitations of rootProject.
-      implementation(sourceSets.main.get().output)
       implementation(libs.apache.maven.modelBuilder)
       implementation(libs.moshi)
       implementation(libs.moshi.kotlin)
@@ -127,6 +125,15 @@ testing.suites {
   }
 }
 
+// This part should be placed after testing.suites to ensure the test sourceSets are created.
+kotlin.target.compilations {
+  val main by getting
+  val functionalTest by getting {
+    // TODO: https://youtrack.jetbrains.com/issue/KTIJ-7662
+    associateWith(main)
+  }
+}
+
 gradlePlugin {
   testSourceSets(
     sourceSets["functionalTest"],
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt
index 97557c31..b98ef3d6 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt
@@ -6,7 +6,10 @@ import java.io.File
 import java.io.InputStream
 import java.nio.charset.Charset
 import java.nio.file.NoSuchFileException
+import java.nio.file.Path
 import java.util.Properties
+import kotlin.io.path.inputStream
+import kotlin.io.path.toPath
 import org.gradle.api.file.RelativePath
 import org.gradle.api.internal.file.DefaultFileTreeElement
 import org.gradle.internal.file.Chmod
@@ -46,6 +49,12 @@ internal fun requireResourceAsStream(name: String): InputStream {
     ?: throw NoSuchFileException("Resource $name not found.")
 }
 
+internal fun requireResourceAsPath(name: String): Path {
+  val resource = Utils::class.java.classLoader.getResource(name)
+    ?: throw NoSuchFileException("Resource $name not found.")
+  return resource.toURI().toPath()
+}
+
 private val DummyFile = File("dummy")
 private val DummyChmod = Chmod { _, _ -> error("This is a dummy implementation.") }
 private val DummyStat = object : Stat {


Test patch:
diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt
index f1755ed5..3f4aee9d 100644
--- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt
+++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt
@@ -7,12 +7,12 @@ import assertk.assertions.doesNotContain
 import assertk.assertions.isEqualTo
 import assertk.assertions.isNotNull
 import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHADOW_JAR_TASK_NAME
+import com.github.jengelman.gradle.plugins.shadow.internal.requireResourceAsPath
 import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
 import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
 import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenRepository
 import com.github.jengelman.gradle.plugins.shadow.util.JarPath
 import java.io.Closeable
-import java.nio.file.NoSuchFileException
 import java.nio.file.Path
 import java.util.Properties
 import kotlin.io.path.ExperimentalPathApi
@@ -26,7 +26,6 @@ import kotlin.io.path.createTempDirectory
 import kotlin.io.path.deleteRecursively
 import kotlin.io.path.exists
 import kotlin.io.path.readText
-import kotlin.io.path.toPath
 import kotlin.io.path.writeText
 import org.gradle.testkit.runner.BuildResult
 import org.gradle.testkit.runner.GradleRunner
@@ -382,11 +381,5 @@ abstract class BasePluginTest {
     fun Assert<BuildResult>.taskOutcomeEquals(taskPath: String, expectedOutcome: TaskOutcome) {
       return transform { it.task(taskPath)?.outcome }.isNotNull().isEqualTo(expectedOutcome)
     }
-
-    fun requireResourceAsPath(name: String): Path {
-      val resource = this::class.java.classLoader.getResource(name)
-        ?: throw NoSuchFileException("Resource $name not found.")
-      return resource.toURI().toPath()
-    }
   }
 }
diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/caching/ShadowJarCachingTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/caching/ShadowJarCachingTest.kt
index 97f5d5c7..4d8f569a 100644
--- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/caching/ShadowJarCachingTest.kt
+++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/caching/ShadowJarCachingTest.kt
@@ -1,6 +1,7 @@
 package com.github.jengelman.gradle.plugins.shadow.caching
 
 import assertk.assertThat
+import com.github.jengelman.gradle.plugins.shadow.internal.MinimizeDependencyFilter
 import com.github.jengelman.gradle.plugins.shadow.util.Issue
 import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
 import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries
@@ -228,7 +229,7 @@ class ShadowJarCachingTest : BaseCachingTest() {
     projectScriptPath.appendText(
       """
         $shadowJar {
-          dependencyFilter = new com.github.jengelman.gradle.plugins.shadow.internal.MinimizeDependencyFilter(project)
+          dependencyFilter = new ${MinimizeDependencyFilter::class.java.name}(project)
         }
       """.trimIndent(),
     )
diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt
index e66ca134..2f56d018 100644
--- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt
+++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt
@@ -6,12 +6,12 @@ import assertk.assertions.isEqualTo
 import assertk.assertions.isNotEqualTo
 import assertk.assertions.isNotNull
 import assertk.assertions.isNull
+import com.github.jengelman.gradle.plugins.shadow.internal.requireResourceAsText
 import com.github.jengelman.gradle.plugins.shadow.util.Issue
 import com.github.jengelman.gradle.plugins.shadow.util.getStream
 import com.github.jengelman.gradle.plugins.shadow.util.isRegular
 import java.util.jar.Attributes
 import kotlin.io.path.appendText
-import kotlin.io.path.readText
 import kotlin.io.path.writeText
 import kotlin.reflect.KClass
 import org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor.PLUGIN_CACHE_FILE
@@ -77,7 +77,7 @@ class TransformersTest : BaseTransformerTest() {
   )
   @Test
   fun canMergeLog4j2PluginCacheFiles() {
-    val content = requireResourceAsPath(PLUGIN_CACHE_FILE).readText()
+    val content = requireResourceAsText(PLUGIN_CACHE_FILE)
     val one = buildJarOne {
       insert(PLUGIN_CACHE_FILE, content)
     }