From 385196aed45e9fd4ce8c4cfffc7454591ea599a7 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 18 Jan 2025 08:19:47 +0800 Subject: [PATCH] Explicit MinimizeTest --- .../gradle/plugins/shadow/BasePluginTest.kt | 73 ----- .../gradle/plugins/shadow/JavaPluginTest.kt | 196 ------------ .../gradle/plugins/shadow/MinimizeTest.kt | 280 ++++++++++++++++++ 3 files changed, 280 insertions(+), 269 deletions(-) create mode 100644 src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/MinimizeTest.kt 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 bf956d417..7b9704155 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 @@ -216,79 +216,6 @@ abstract class BasePluginTest { ) } - fun writeApiLibAndImplModules() { - settingsScriptPath.appendText( - """ - include 'api', 'lib', 'impl' - """.trimIndent() + System.lineSeparator(), - ) - projectScriptPath.writeText("") - - path("lib/src/main/java/lib/LibEntity.java").writeText( - """ - package lib; - public interface LibEntity {} - """.trimIndent(), - ) - path("lib/src/main/java/lib/UnusedLibEntity.java").writeText( - """ - package lib; - public class UnusedLibEntity implements LibEntity {} - """.trimIndent(), - ) - path("lib/build.gradle").writeText( - """ - plugins { - id 'java' - } - """.trimIndent() + System.lineSeparator(), - ) - - path("api/src/main/java/api/Entity.java").writeText( - """ - package api; - public interface Entity {} - """.trimIndent(), - ) - path("api/src/main/java/api/UnusedEntity.java").writeText( - """ - package api; - import lib.LibEntity; - public class UnusedEntity implements LibEntity {} - """.trimIndent(), - ) - path("api/build.gradle").writeText( - """ - plugins { - id 'java' - } - dependencies { - implementation 'junit:junit:3.8.2' - implementation project(':lib') - } - """.trimIndent() + System.lineSeparator(), - ) - - path("impl/src/main/java/impl/SimpleEntity.java").writeText( - """ - package impl; - import api.Entity; - public class SimpleEntity implements Entity {} - """.trimIndent(), - ) - path("impl/build.gradle").writeText( - """ - ${getDefaultProjectBuildScript("java-library")} - dependencies { - api project(':api') - } - $shadowJar { - minimize() - } - """.trimIndent() + System.lineSeparator(), - ) - } - fun writeGradlePluginModule(legacy: Boolean) { val pluginId = "my.plugin" val pluginClass = "my.plugin.MyPlugin" diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt index a870adf68..723457fce 100644 --- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt @@ -150,202 +150,6 @@ class JavaPluginTest : BasePluginTest() { } } - /** - * 'Server' depends on 'Client'. 'junit' is independent. - * The minimize shall remove 'junit'. - */ - @Test - fun minimizeByKeepingOnlyTransitiveDependencies() { - writeClientAndServerModules( - serverShadowBlock = """ - minimize() - """.trimIndent(), - ) - path("server/src/main/java/server/Server.java").writeText( - """ - package server; - import client.Client; - public class Server { - // This is to make sure that 'Client' is not removed. - private final String client = Client.class.getName(); - } - """.trimIndent(), - ) - - run(serverShadowJarTask) - - assertThat(outputServerShadowJar).useAll { - containsEntries( - "client/Client.class", - "server/Server.class", - ) - doesNotContainEntries( - "junit/framework/Test.class", - ) - } - } - - /** - * 'Client', 'Server' and 'junit' are independent. - * 'junit' is excluded from the minimize step. - * The minimize step shall remove 'Client' but not 'junit'. - */ - @Test - fun excludeDependencyFromMinimize() { - writeClientAndServerModules( - serverShadowBlock = """ - minimize { - exclude(dependency('junit:junit:.*')) - } - """.trimIndent(), - ) - - run(serverShadowJarTask) - - assertThat(outputServerShadowJar).useAll { - containsEntries( - "server/Server.class", - "junit/framework/Test.class", - ) - doesNotContainEntries( - "client/Client.class", - ) - } - } - - /** - * 'Client', 'Server' and 'junit' are independent. - * Unused classes of 'client' and theirs dependencies shouldn't be removed. - */ - @Test - fun excludeProjectFromMinimize() { - writeClientAndServerModules( - serverShadowBlock = """ - minimize { - exclude(project(':client')) - } - """.trimIndent(), - ) - - run(serverShadowJarTask) - - assertThat(outputServerShadowJar).useAll { - containsEntries( - "client/Client.class", - "server/Server.class", - ) - } - } - - /** - * 'Client', 'Server' and 'junit' are independent. - * Unused classes of 'client' and theirs dependencies shouldn't be removed. - */ - @Test - fun excludeProjectFromMinimizeShallNotExcludeTransitiveDependenciesThatAreUsedInSubproject() { - writeClientAndServerModules( - serverShadowBlock = """ - minimize { - exclude(project(':client')) - } - """.trimIndent(), - ) - path("client/src/main/java/client/Client.java").writeText( - """ - package client; - import junit.framework.TestCase; - public class Client extends TestCase { - public static void main(String[] args) {} - } - """.trimIndent(), - ) - - run(serverShadowJarTask) - - assertThat(outputServerShadowJar).useAll { - containsEntries( - "client/Client.class", - "server/Server.class", - "junit/framework/TestCase.class", - ) - } - - path("client/src/main/java/client/Client.java").writeText( - """ - package client; - public class Client {} - """.trimIndent(), - ) - run(serverShadowJarTask) - - assertThat(outputServerShadowJar).useAll { - containsEntries( - "client/Client.class", - "server/Server.class", - "junit/framework/TestCase.class", - ) - } - } - - /** - * 'api' used as api for 'impl', and depended on 'lib'. 'junit' is independent. - * The minimize step shall remove 'junit', but not 'api'. - * Unused classes of 'api' and theirs dependencies also shouldn't be removed. - */ - @Test - fun useMinimizeWithDependenciesWithApiScope() { - writeApiLibAndImplModules() - - run(":impl:$SHADOW_JAR_TASK_NAME") - - assertThat(jarPath("impl/build/libs/impl-all.jar")).useAll { - containsEntries( - "impl/SimpleEntity.class", - "api/Entity.class", - "api/UnusedEntity.class", - "lib/LibEntity.class", - ) - doesNotContainEntries( - "junit/framework/Test.class", - "lib/UnusedLibEntity.class", - ) - } - } - - /** - * 'api' used as api for 'impl', and 'lib' used as api for 'api'. - * Unused classes of 'api' and 'lib' shouldn't be removed. - */ - @Test - fun useMinimizeWithTransitiveDependenciesWithApiScope() { - writeApiLibAndImplModules() - path("api/build.gradle").writeText( - """ - plugins { - id 'java-library' - } - dependencies { - api project(':lib') - } - """.trimIndent(), - ) - - run(":impl:$SHADOW_JAR_TASK_NAME") - - assertThat(jarPath("impl/build/libs/impl-all.jar")).useAll { - containsEntries( - "impl/SimpleEntity.class", - "api/Entity.class", - "api/UnusedEntity.class", - "lib/LibEntity.class", - "lib/UnusedLibEntity.class", - ) - doesNotContainEntries( - "junit/framework/Test.class", - ) - } - } - @Test fun dependOnProjectShadowJar() { writeShadowedClientAndServerModules() diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/MinimizeTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/MinimizeTest.kt new file mode 100644 index 000000000..d77b07180 --- /dev/null +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/MinimizeTest.kt @@ -0,0 +1,280 @@ +package com.github.jengelman.gradle.plugins.shadow + +import assertk.assertThat +import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHADOW_JAR_TASK_NAME +import com.github.jengelman.gradle.plugins.shadow.util.containsEntries +import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries +import kotlin.io.path.appendText +import kotlin.io.path.writeText +import org.junit.jupiter.api.Test + +class MinimizeTest : BasePluginTest() { + /** + * 'api' used as api for 'impl', and depended on 'lib'. 'junit' is independent. + * The minimize step shall remove 'junit', but not 'api'. + * Unused classes of 'api' and theirs dependencies also shouldn't be removed. + */ + @Test + fun useMinimizeWithDependenciesWithApiScope() { + writeApiLibAndImplModules() + + run(":impl:$SHADOW_JAR_TASK_NAME") + + assertThat(jarPath("impl/build/libs/impl-all.jar")).useAll { + containsEntries( + "impl/SimpleEntity.class", + "api/Entity.class", + "api/UnusedEntity.class", + "lib/LibEntity.class", + ) + doesNotContainEntries( + "junit/framework/Test.class", + "lib/UnusedLibEntity.class", + ) + } + } + + /** + * 'api' used as api for 'impl', and 'lib' used as api for 'api'. + * Unused classes of 'api' and 'lib' shouldn't be removed. + */ + @Test + fun useMinimizeWithTransitiveDependenciesWithApiScope() { + writeApiLibAndImplModules() + path("api/build.gradle").writeText( + """ + plugins { + id 'java-library' + } + dependencies { + api project(':lib') + } + """.trimIndent(), + ) + + run(":impl:$SHADOW_JAR_TASK_NAME") + + assertThat(jarPath("impl/build/libs/impl-all.jar")).useAll { + containsEntries( + "impl/SimpleEntity.class", + "api/Entity.class", + "api/UnusedEntity.class", + "lib/LibEntity.class", + "lib/UnusedLibEntity.class", + ) + doesNotContainEntries( + "junit/framework/Test.class", + ) + } + } + + /** + * 'Server' depends on 'Client'. 'junit' is independent. + * The minimize shall remove 'junit'. + */ + @Test + fun minimizeByKeepingOnlyTransitiveDependencies() { + writeClientAndServerModules( + serverShadowBlock = """ + minimize() + """.trimIndent(), + ) + path("server/src/main/java/server/Server.java").writeText( + """ + package server; + import client.Client; + public class Server { + // This is to make sure that 'Client' is not removed. + private final String client = Client.class.getName(); + } + """.trimIndent(), + ) + + run(serverShadowJarTask) + + assertThat(outputServerShadowJar).useAll { + containsEntries( + "client/Client.class", + "server/Server.class", + ) + doesNotContainEntries( + "junit/framework/Test.class", + ) + } + } + + /** + * 'Client', 'Server' and 'junit' are independent. + * 'junit' is excluded from the minimize step. + * The minimize step shall remove 'Client' but not 'junit'. + */ + @Test + fun excludeDependencyFromMinimize() { + writeClientAndServerModules( + serverShadowBlock = """ + minimize { + exclude(dependency('junit:junit:.*')) + } + """.trimIndent(), + ) + + run(serverShadowJarTask) + + assertThat(outputServerShadowJar).useAll { + containsEntries( + "server/Server.class", + "junit/framework/Test.class", + ) + doesNotContainEntries( + "client/Client.class", + ) + } + } + + /** + * 'Client', 'Server' and 'junit' are independent. + * Unused classes of 'client' and theirs dependencies shouldn't be removed. + */ + @Test + fun excludeProjectFromMinimize() { + writeClientAndServerModules( + serverShadowBlock = """ + minimize { + exclude(project(':client')) + } + """.trimIndent(), + ) + + run(serverShadowJarTask) + + assertThat(outputServerShadowJar).useAll { + containsEntries( + "client/Client.class", + "server/Server.class", + ) + } + } + + /** + * 'Client', 'Server' and 'junit' are independent. + * Unused classes of 'client' and theirs dependencies shouldn't be removed. + */ + @Test + fun excludeProjectFromMinimizeShallNotExcludeTransitiveDependenciesThatAreUsedInSubproject() { + writeClientAndServerModules( + serverShadowBlock = """ + minimize { + exclude(project(':client')) + } + """.trimIndent(), + ) + path("client/src/main/java/client/Client.java").writeText( + """ + package client; + import junit.framework.TestCase; + public class Client extends TestCase { + public static void main(String[] args) {} + } + """.trimIndent(), + ) + + run(serverShadowJarTask) + + assertThat(outputServerShadowJar).useAll { + containsEntries( + "client/Client.class", + "server/Server.class", + "junit/framework/TestCase.class", + ) + } + + path("client/src/main/java/client/Client.java").writeText( + """ + package client; + public class Client {} + """.trimIndent(), + ) + run(serverShadowJarTask) + + assertThat(outputServerShadowJar).useAll { + containsEntries( + "client/Client.class", + "server/Server.class", + "junit/framework/TestCase.class", + ) + } + } + + private fun writeApiLibAndImplModules() { + settingsScriptPath.appendText( + """ + include 'api', 'lib', 'impl' + """.trimIndent() + System.lineSeparator(), + ) + projectScriptPath.writeText("") + + path("lib/src/main/java/lib/LibEntity.java").writeText( + """ + package lib; + public interface LibEntity {} + """.trimIndent(), + ) + path("lib/src/main/java/lib/UnusedLibEntity.java").writeText( + """ + package lib; + public class UnusedLibEntity implements LibEntity {} + """.trimIndent(), + ) + path("lib/build.gradle").writeText( + """ + plugins { + id 'java' + } + """.trimIndent() + System.lineSeparator(), + ) + + path("api/src/main/java/api/Entity.java").writeText( + """ + package api; + public interface Entity {} + """.trimIndent(), + ) + path("api/src/main/java/api/UnusedEntity.java").writeText( + """ + package api; + import lib.LibEntity; + public class UnusedEntity implements LibEntity {} + """.trimIndent(), + ) + path("api/build.gradle").writeText( + """ + plugins { + id 'java' + } + dependencies { + implementation 'junit:junit:3.8.2' + implementation project(':lib') + } + """.trimIndent() + System.lineSeparator(), + ) + + path("impl/src/main/java/impl/SimpleEntity.java").writeText( + """ + package impl; + import api.Entity; + public class SimpleEntity implements Entity {} + """.trimIndent(), + ) + path("impl/build.gradle").writeText( + """ + ${getDefaultProjectBuildScript("java-library")} + dependencies { + api project(':api') + } + $shadowJar { + minimize() + } + """.trimIndent() + System.lineSeparator(), + ) + } +}