diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ApplicationTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ApplicationTest.kt index 6fe3f2464..fe67d30ea 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ApplicationTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ApplicationTest.kt @@ -5,7 +5,8 @@ import assertk.assertions.contains import assertk.assertions.containsAtLeast import assertk.assertions.exists import assertk.assertions.isEqualTo -import java.util.jar.JarFile +import assertk.assertions.isNotEmpty +import com.github.jengelman.gradle.plugins.shadow.util.containsEntries import kotlin.io.path.appendText import kotlin.io.path.readText import kotlin.io.path.writeText @@ -39,13 +40,13 @@ class ApplicationTest : BasePluginTest() { assertThat(result.output).contains("Running application with JDK 17") assertThat(result.output).contains("TestApp: Hello World! (foo)") - val installedJar = path("build/install/myapp-shadow/lib/myapp-1.0-all.jar") - assertThat(installedJar).exists() - - assertContains(installedJar, listOf("a.properties", "a2.properties", "myapp/Main.class")) - - val jarFile = JarFile(installedJar.toFile()) - assertThat(jarFile.manifest.mainAttributes.getValue("Main-Class")) + val installedJar = jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar") + assertThat(installedJar).containsEntries( + "a.properties", + "a2.properties", + "myapp/Main.class", + ) + assertThat(installedJar.manifest.mainAttributes.getValue("Main-Class")) .isEqualTo("myapp.Main") path("build/install/myapp-shadow/bin/myapp").let { startScript -> @@ -84,7 +85,7 @@ class ApplicationTest : BasePluginTest() { run(ShadowApplicationPlugin.SHADOW_INSTALL_TASK_NAME) - assertThat(path("build/install/myapp-shadow/lib/myapp-1.0-all.jar")).exists() + assertThat(jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar").entries().toList()).isNotEmpty() } private fun prepare( diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt index 52f2e216a..2a1319432 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt @@ -5,9 +5,9 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHA import com.github.jengelman.gradle.plugins.shadow.tasks.JavaJarExec import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository +import com.github.jengelman.gradle.plugins.shadow.util.JarPath import java.nio.file.Path import java.util.Properties -import java.util.jar.JarFile import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.Path import kotlin.io.path.absolutePathString @@ -17,7 +17,7 @@ import kotlin.io.path.createFile import kotlin.io.path.createTempDirectory import kotlin.io.path.deleteRecursively import kotlin.io.path.exists -import kotlin.io.path.extension +import kotlin.io.path.isRegularFile import kotlin.io.path.readText import kotlin.io.path.toPath import kotlin.io.path.writeText @@ -123,19 +123,25 @@ abstract class BasePluginTest { val settingsScriptPath: Path get() = path("settings.gradle") - val outputShadowJar: Path - get() = path("build/libs/shadow-1.0-all.jar") + val outputShadowJar: JarPath + get() = jarPath("build/libs/shadow-1.0-all.jar") - val outputServerShadowJar: Path - get() = path("server/build/libs/server-1.0-all.jar") + val outputServerShadowJar: JarPath + get() = jarPath("server/build/libs/server-1.0-all.jar") + + fun jarPath(path: String): JarPath { + val realPath = root.resolve(path).also { + check(it.exists()) { "Path not found: $it" } + check(it.isRegularFile()) { "Path is not a regular file: $it" } + } + return JarPath(realPath) + } fun path(path: String): Path { return root.resolve(path).also { - val extension = it.extension - // Binary files should not be created, text files should be created. - if (it.exists() || extension == "jar" || extension == "zip") return@also - + if (it.exists()) return@also it.parent.createDirectories() + // We should create text file only if it doesn't exist. it.createFile() } } @@ -144,22 +150,6 @@ abstract class BasePluginTest { return AppendableMavenFileRepository(root.resolve(path)) } - fun assertContains(jarPath: Path, paths: List) { - JarFile(jarPath.toFile()).use { jar -> - paths.forEach { path -> - assert(jar.getJarEntry(path) != null) { "Jar file $jarPath does not contain entry $path" } - } - } - } - - fun assertDoesNotContain(jarPath: Path, paths: List) { - JarFile(jarPath.toFile()).use { jar -> - paths.forEach { path -> - assert(jar.getJarEntry(path) == null) { "Jar file $jarPath contains entry $path" } - } - } - } - private val runner: GradleRunner get() { return GradleRunner.create() diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.kt index 05857c983..79ed273a2 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.kt @@ -2,11 +2,11 @@ package com.github.jengelman.gradle.plugins.shadow import assertk.assertThat import assertk.assertions.contains -import assertk.assertions.exists import assertk.assertions.isEqualTo import assertk.assertions.isNotNull +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.deleteExisting import kotlin.io.path.writeText import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.TaskOutcome @@ -76,13 +76,12 @@ class ConfigurationCacheSpec : BasePluginTest() { outputShadowJar.deleteExisting() val result = run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("a.properties", "b.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "b.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("a2.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "a2.properties", ) result.assertCcReused() } @@ -101,14 +100,12 @@ class ConfigurationCacheSpec : BasePluginTest() { outputServerShadowJar.deleteExisting() val result = run(shadowJarTask) - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("server/Server.class", "junit/framework/Test.class"), + assertThat(outputServerShadowJar).containsEntries( + "server/Server.class", + "junit/framework/Test.class", ) - assertDoesNotContain( - outputServerShadowJar, - listOf("client/Client.class"), + assertThat(outputServerShadowJar).doesNotContainEntries( + "client/Client.class", ) result.assertCcReused() } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/FilteringTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/FilteringTest.kt index 08c2f4cf5..add31e8e6 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/FilteringTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/FilteringTest.kt @@ -1,9 +1,10 @@ package com.github.jengelman.gradle.plugins.shadow import assertk.assertThat -import assertk.assertions.exists import assertk.assertions.isEqualTo import assertk.assertions.isNotNull +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.readText import kotlin.io.path.writeText @@ -32,9 +33,10 @@ class FilteringTest : BasePluginTest() { @Test fun includeAllDependencies() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("a.properties", "a2.properties", "b.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "a2.properties", + "b.properties", ) } @@ -50,13 +52,12 @@ class FilteringTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("a.properties", "b.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "b.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("a2.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "a2.properties", ) } @@ -107,13 +108,14 @@ class FilteringTest : BasePluginTest() { assertThat(result.task(":shadowJar")).isNotNull() .transform { it.outcome }.isEqualTo(TaskOutcome.SUCCESS) - assertContains( - outputShadowJar, - listOf("a.properties", "a2.properties", "b.properties", "d.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "a2.properties", + "b.properties", + "d.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("c.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "c.properties", ) } @@ -134,13 +136,14 @@ class FilteringTest : BasePluginTest() { assertThat(result.task(":shadowJar")).isNotNull() .transform { it.outcome }.isEqualTo(TaskOutcome.SUCCESS) - assertContains( - outputShadowJar, - listOf("a2.properties", "b.properties", "c.properties", "d.properties"), + assertThat(outputShadowJar).containsEntries( + "a2.properties", + "b.properties", + "c.properties", + "d.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("a.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "a.properties", ) } @@ -168,13 +171,15 @@ class FilteringTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("d.properties", "shadow/Passed.class"), + assertThat(outputShadowJar).containsEntries( + "d.properties", + "shadow/Passed.class", ) - assertDoesNotContain( - outputShadowJar, - listOf("a.properties", "a2.properties", "b.properties", "c.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "a.properties", + "a2.properties", + "b.properties", + "c.properties", ) } @@ -190,14 +195,12 @@ class FilteringTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertDoesNotContain( - outputServerShadowJar, - listOf("client/Client.class"), + assertThat(outputServerShadowJar).doesNotContainEntries( + "client/Client.class", ) - assertContains( - outputServerShadowJar, - listOf("server/Server.class", "junit/framework/Test.class"), + assertThat(outputServerShadowJar).containsEntries( + "server/Server.class", + "junit/framework/Test.class", ) } @@ -213,14 +216,12 @@ class FilteringTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertDoesNotContain( - outputServerShadowJar, - listOf("junit/framework/Test.class"), + assertThat(outputServerShadowJar).doesNotContainEntries( + "junit/framework/Test.class", ) - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "server/Server.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "server/Server.class", ) } @@ -238,13 +239,12 @@ class FilteringTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("a.properties", "b.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "b.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("a2.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "a2.properties", ) } @@ -274,13 +274,14 @@ class FilteringTest : BasePluginTest() { } private fun commonAssertions() { - assertContains( - outputShadowJar, - listOf("a.properties", "a2.properties", "b.properties", "c.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "a2.properties", + "b.properties", + "c.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("d.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "d.properties", ) } } 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 b0e002124..6f4b18387 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 @@ -2,12 +2,12 @@ package com.github.jengelman.gradle.plugins.shadow import assertk.assertFailure 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 com.github.jengelman.gradle.plugins.shadow.util.containsEntries +import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries import java.net.URLClassLoader -import java.util.jar.JarFile import kotlin.io.path.appendText import kotlin.io.path.writeText import org.junit.jupiter.api.Test @@ -28,26 +28,23 @@ class RelocationTest : BasePluginTest() { ) run(shadowJarTask) - assertContains( - outputShadowJar, - listOf( - "META-INF/MANIFEST.MF", - "shadow/junit/textui/ResultPrinter.class", - "shadow/junit/textui/TestRunner.class", - "shadow/junit/framework/Assert.class", - "shadow/junit/framework/AssertionFailedError.class", - "shadow/junit/framework/ComparisonCompactor.class", - "shadow/junit/framework/ComparisonFailure.class", - "shadow/junit/framework/Protectable.class", - "shadow/junit/framework/Test.class", - "shadow/junit/framework/TestCase.class", - "shadow/junit/framework/TestFailure.class", - "shadow/junit/framework/TestListener.class", - "shadow/junit/framework/TestResult$1.class", - "shadow/junit/framework/TestResult.class", - "shadow/junit/framework/TestSuite$1.class", - "shadow/junit/framework/TestSuite.class", - ), + assertThat(outputShadowJar).containsEntries( + "META-INF/MANIFEST.MF", + "shadow/junit/textui/ResultPrinter.class", + "shadow/junit/textui/TestRunner.class", + "shadow/junit/framework/Assert.class", + "shadow/junit/framework/AssertionFailedError.class", + "shadow/junit/framework/ComparisonCompactor.class", + "shadow/junit/framework/ComparisonFailure.class", + "shadow/junit/framework/Protectable.class", + "shadow/junit/framework/Test.class", + "shadow/junit/framework/TestCase.class", + "shadow/junit/framework/TestFailure.class", + "shadow/junit/framework/TestListener.class", + "shadow/junit/framework/TestResult$1.class", + "shadow/junit/framework/TestResult.class", + "shadow/junit/framework/TestSuite$1.class", + "shadow/junit/framework/TestSuite.class", ) } @@ -73,51 +70,44 @@ class RelocationTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf( - "META-INF/MANIFEST.MF", - "a/ResultPrinter.class", - "a/TestRunner.class", - "b/Assert.class", - "b/AssertionFailedError.class", - "b/ComparisonCompactor.class", - "b/ComparisonFailure.class", - "b/Protectable.class", - "b/Test.class", - "b/TestCase.class", - "b/TestFailure.class", - "b/TestListener.class", - "b/TestResult\$1.class", - "b/TestResult.class", - "b/TestSuite\$1.class", - "b/TestSuite.class", - ), + assertThat(outputShadowJar).containsEntries( + "META-INF/MANIFEST.MF", + "a/ResultPrinter.class", + "a/TestRunner.class", + "b/Assert.class", + "b/AssertionFailedError.class", + "b/ComparisonCompactor.class", + "b/ComparisonFailure.class", + "b/Protectable.class", + "b/Test.class", + "b/TestCase.class", + "b/TestFailure.class", + "b/TestListener.class", + "b/TestResult\$1.class", + "b/TestResult.class", + "b/TestSuite\$1.class", + "b/TestSuite.class", ) - assertDoesNotContain( - outputShadowJar, - listOf( - "junit/textui/ResultPrinter.class", - "junit/textui/TestRunner.class", - "junit/framework/Assert.class", - "junit/framework/AssertionFailedError.class", - "junit/framework/ComparisonCompactor.class", - "junit/framework/ComparisonFailure.class", - "junit/framework/Protectable.class", - "junit/framework/Test.class", - "junit/framework/TestCase.class", - "junit/framework/TestFailure.class", - "junit/framework/TestListener.class", - "junit/framework/TestResult\$1.class", - "junit/framework/TestResult.class", - "junit/framework/TestSuite\$1.class", - "junit/framework/TestSuite.class", - ), + assertThat(outputShadowJar).doesNotContainEntries( + "junit/textui/ResultPrinter.class", + "junit/textui/TestRunner.class", + "junit/framework/Assert.class", + "junit/framework/AssertionFailedError.class", + "junit/framework/ComparisonCompactor.class", + "junit/framework/ComparisonFailure.class", + "junit/framework/Protectable.class", + "junit/framework/Test.class", + "junit/framework/TestCase.class", + "junit/framework/TestFailure.class", + "junit/framework/TestListener.class", + "junit/framework/TestResult\$1.class", + "junit/framework/TestResult.class", + "junit/framework/TestSuite\$1.class", + "junit/framework/TestSuite.class", ) - - val jarFile = JarFile(outputShadowJar.toFile()) - assertThat(jarFile.manifest.mainAttributes.getValue("TEST-VALUE")).isEqualTo("FOO") + assertThat(outputShadowJar.manifest.mainAttributes.getValue("TEST-VALUE")) + .isEqualTo("FOO") } @Test @@ -140,43 +130,34 @@ class RelocationTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf( - "a/ResultPrinter.class", - "b/Test.class", - "b/TestCase.class", - "b/TestFailure.class", - "b/TestListener.class", - "b/TestResult\$1.class", - "b/TestResult.class", - "b/TestSuite\$1.class", - "b/TestSuite.class", - ), + assertThat(outputShadowJar).containsEntries( + "a/ResultPrinter.class", + "b/Test.class", + "b/TestCase.class", + "b/TestFailure.class", + "b/TestListener.class", + "b/TestResult\$1.class", + "b/TestResult.class", + "b/TestSuite\$1.class", + "b/TestSuite.class", ) - assertDoesNotContain( - outputShadowJar, - listOf( - "a/TestRunner.class", - "b/Assert.class", - "b/AssertionFailedError.class", - "b/ComparisonCompactor.class", - "b/ComparisonFailure.class", - "b/Protectable.class", - ), + assertThat(outputShadowJar).doesNotContainEntries( + "a/TestRunner.class", + "b/Assert.class", + "b/AssertionFailedError.class", + "b/ComparisonCompactor.class", + "b/ComparisonFailure.class", + "b/Protectable.class", ) - assertContains( - outputShadowJar, - listOf( - "junit/textui/TestRunner.class", - "junit/framework/Assert.class", - "junit/framework/AssertionFailedError.class", - "junit/framework/ComparisonCompactor.class", - "junit/framework/ComparisonFailure.class", - "junit/framework/Protectable.class", - ), + assertThat(outputShadowJar).containsEntries( + "junit/textui/TestRunner.class", + "junit/framework/Assert.class", + "junit/framework/AssertionFailedError.class", + "junit/framework/ComparisonCompactor.class", + "junit/framework/ComparisonFailure.class", + "junit/framework/Protectable.class", ) } @@ -213,21 +194,15 @@ class RelocationTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf( - "shadow/ShadowTest.class", - "shadow/junit/Test.class", - "shadow/junit", - ), + assertThat(outputShadowJar).containsEntries( + "shadow/ShadowTest.class", + "shadow/junit/Test.class", + "shadow/junit", ) - assertDoesNotContain( - outputShadowJar, - listOf( - "junit/framework", - "junit/framework/Test.class", - ), + assertThat(outputShadowJar).doesNotContainEntries( + "junit/framework", + "junit/framework/Test.class", ) val classLoader = URLClassLoader( @@ -299,19 +274,14 @@ class RelocationTest : BasePluginTest() { run(":app:$shadowJarTask") - val appOutput = path("app/build/libs/app-all.jar") - assertThat(appOutput).exists() - - assertContains( - appOutput, - listOf( - "TEST", - "APP-TEST", - "test.properties", - "app/core/Core.class", - "app/App.class", - "app/junit/framework/Test.class", - ), + val appOutput = jarPath("app/build/libs/app-all.jar") + assertThat(appOutput).containsEntries( + "TEST", + "APP-TEST", + "test.properties", + "app/core/Core.class", + "app/App.class", + "app/junit/framework/Test.class", ) } @@ -346,21 +316,15 @@ class RelocationTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf( - "bar/Foo.class", - "bar/foo.properties", - "bar/dep.properties", - ), + assertThat(outputShadowJar).containsEntries( + "bar/Foo.class", + "bar/foo.properties", + "bar/dep.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf( - "foo/Foo.class", - "foo/foo.properties", - "foo/dep.properties", - ), + assertThat(outputShadowJar).doesNotContainEntries( + "foo/Foo.class", + "foo/foo.properties", + "foo/dep.properties", ) } 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 fc639d8e8..67f71634e 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 @@ -2,7 +2,6 @@ package com.github.jengelman.gradle.plugins.shadow import assertk.assertThat import assertk.assertions.contains -import assertk.assertions.exists import assertk.assertions.isEqualTo import assertk.assertions.isNotNull import assertk.assertions.isNull @@ -10,7 +9,9 @@ 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 com.github.jengelman.gradle.plugins.shadow.util.containsEntries +import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries +import com.github.jengelman.gradle.plugins.shadow.util.isRegular import kotlin.io.path.appendText import kotlin.io.path.readText import kotlin.io.path.toPath @@ -70,7 +71,7 @@ class ShadowPluginTest : BasePluginTest() { it.withGradleVersion("8.3") } - assertThat(outputShadowJar).exists() + assertThat(outputShadowJar).isRegular() } @Test @@ -97,7 +98,7 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() + assertThat(outputShadowJar).isRegular() } @Test @@ -123,16 +124,15 @@ class ShadowPluginTest : BasePluginTest() { ) run(shadowJarTask) - val outputShadowJar = path("build/libs/shadow.jar") - assertThat(outputShadowJar).exists() - assertContains( - outputShadowJar, - listOf("shadow/Passed.class", "junit/framework/Test.class"), + val outputShadowJar = jarPath("build/libs/shadow.jar") + + assertThat(outputShadowJar).containsEntries( + "shadow/Passed.class", + "junit/framework/Test.class", ) - assertDoesNotContain( - outputShadowJar, - listOf("/"), + assertThat(outputShadowJar).doesNotContainEntries( + "/", ) } @@ -142,10 +142,10 @@ class ShadowPluginTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "server/Server.class", "junit/framework/Test.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "server/Server.class", + "junit/framework/Test.class", ) } @@ -173,14 +173,12 @@ class ShadowPluginTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "server/Server.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "server/Server.class", ) - assertDoesNotContain( - outputServerShadowJar, - listOf("junit/framework/Test.class"), + assertThat(outputServerShadowJar).doesNotContainEntries( + "junit/framework/Test.class", ) } @@ -201,14 +199,12 @@ class ShadowPluginTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("server/Server.class", "junit/framework/Test.class"), + assertThat(outputServerShadowJar).containsEntries( + "server/Server.class", + "junit/framework/Test.class", ) - assertDoesNotContain( - outputServerShadowJar, - listOf("client/Client.class"), + assertThat(outputServerShadowJar).doesNotContainEntries( + "client/Client.class", ) } @@ -228,10 +224,9 @@ class ShadowPluginTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "server/Server.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "server/Server.class", ) } @@ -260,10 +255,10 @@ class ShadowPluginTest : BasePluginTest() { run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "server/Server.class", "junit/framework/TestCase.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "server/Server.class", + "junit/framework/TestCase.class", ) path("client/src/main/java/client/Client.java").writeText( @@ -273,12 +268,13 @@ class ShadowPluginTest : BasePluginTest() { """.trimIndent(), ) run(serverShadowJarTask) - assertThat(outputServerShadowJar).exists() + // TODO: I don't think junit classes should be in the output jar, but it's the test case // from https://github.com/GradleUp/shadow/pull/420, need to investigate more... - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "server/Server.class", "junit/framework/TestCase.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "server/Server.class", + "junit/framework/TestCase.class", ) } @@ -292,16 +288,17 @@ class ShadowPluginTest : BasePluginTest() { writeApiLibAndImplModules() run(":impl:$shadowJarTask") - val implOutput = path("impl/build/libs/impl-all.jar") - assertThat(implOutput).exists() - assertContains( - implOutput, - listOf("impl/SimpleEntity.class", "api/Entity.class", "api/UnusedEntity.class", "lib/LibEntity.class"), + val implOutput = jarPath("impl/build/libs/impl-all.jar") + assertThat(implOutput).containsEntries( + "impl/SimpleEntity.class", + "api/Entity.class", + "api/UnusedEntity.class", + "lib/LibEntity.class", ) - assertDoesNotContain( - implOutput, - listOf("junit/framework/Test.class", "lib/UnusedLibEntity.class"), + assertThat(implOutput).doesNotContainEntries( + "junit/framework/Test.class", + "lib/UnusedLibEntity.class", ) } @@ -324,22 +321,17 @@ class ShadowPluginTest : BasePluginTest() { ) run(":impl:$shadowJarTask") - val implOutput = path("impl/build/libs/impl-all.jar") - - assertThat(implOutput).exists() - assertContains( - implOutput, - listOf( - "impl/SimpleEntity.class", - "api/Entity.class", - "api/UnusedEntity.class", - "lib/LibEntity.class", - "lib/UnusedLibEntity.class", - ), - ) - assertDoesNotContain( - implOutput, - listOf("junit/framework/Test.class"), + + val implOutput = jarPath("impl/build/libs/impl-all.jar") + assertThat(implOutput).containsEntries( + "impl/SimpleEntity.class", + "api/Entity.class", + "api/UnusedEntity.class", + "lib/LibEntity.class", + "lib/UnusedLibEntity.class", + ) + assertThat(implOutput).doesNotContainEntries( + "junit/framework/Test.class", ) } @@ -348,22 +340,21 @@ class ShadowPluginTest : BasePluginTest() { writeShadowedClientAndServerModules() run(":server:jar") - val serverOutput = path("server/build/libs/server-1.0.jar") - val clientOutput = path("client/build/libs/client-all.jar") - assertThat(serverOutput).exists() - assertContains( - serverOutput, - listOf("server/Server.class"), + val serverOutput = jarPath("server/build/libs/server-1.0.jar") + assertThat(serverOutput).containsEntries( + "server/Server.class", ) - assertDoesNotContain( - serverOutput, - listOf("client/Client.class", "junit/framework/Test.class", "client/junit/framework/Test.class"), + assertThat(serverOutput).doesNotContainEntries( + "client/Client.class", + "junit/framework/Test.class", + "client/junit/framework/Test.class", ) - assertThat(clientOutput).exists() - assertContains( - clientOutput, - listOf("client/Client.class", "client/junit/framework/Test.class"), + + val clientOutput = jarPath("client/build/libs/client-all.jar") + assertThat(clientOutput).containsEntries( + "client/Client.class", + "client/junit/framework/Test.class", ) } @@ -372,21 +363,20 @@ class ShadowPluginTest : BasePluginTest() { writeShadowedClientAndServerModules() run(serverShadowJarTask) - val clientOutput = path("client/build/libs/client-all.jar") - assertThat(outputServerShadowJar).exists() - assertContains( - outputServerShadowJar, - listOf("client/Client.class", "client/junit/framework/Test.class", "server/Server.class"), + assertThat(outputServerShadowJar).containsEntries( + "client/Client.class", + "client/junit/framework/Test.class", + "server/Server.class", ) - assertDoesNotContain( - outputServerShadowJar, - listOf("junit/framework/Test.class"), + assertThat(outputServerShadowJar).doesNotContainEntries( + "junit/framework/Test.class", ) - assertThat(clientOutput).exists() - assertContains( - clientOutput, - listOf("client/Client.class", "client/junit/framework/Test.class"), + + val clientOutput = jarPath("client/build/libs/client-all.jar") + assertThat(clientOutput).containsEntries( + "client/Client.class", + "client/junit/framework/Test.class", ) } @@ -418,13 +408,17 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("shadow/Passed.class", "a.properties", "META-INF/a.properties"), + assertThat(outputShadowJar).containsEntries( + "shadow/Passed.class", + "a.properties", + "META-INF/a.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("META-INF/INDEX.LIST", "META-INF/a.SF", "META-INF/a.DSA", "META-INF/a.RSA", "module-info.class"), + assertThat(outputShadowJar).doesNotContainEntries( + "META-INF/INDEX.LIST", + "META-INF/a.SF", + "META-INF/a.DSA", + "META-INF/a.RSA", + "module-info.class", ) } @@ -444,13 +438,12 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("a.properties", "a2.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "a2.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("b.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "b.properties", ) } @@ -483,9 +476,11 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("api.properties", "implementation.properties", "runtimeOnly.properties", "implementation-dep.properties"), + assertThat(outputShadowJar).containsEntries( + "api.properties", + "implementation.properties", + "runtimeOnly.properties", + "implementation-dep.properties", ) } @@ -505,13 +500,12 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertContains( - outputShadowJar, - listOf("a.properties", "a2.properties"), + assertThat(outputShadowJar).containsEntries( + "a.properties", + "a2.properties", ) - assertDoesNotContain( - outputShadowJar, - listOf("b.properties"), + assertThat(outputShadowJar).doesNotContainEntries( + "b.properties", ) } @@ -535,9 +529,7 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - val entries = JarFile(outputShadowJar.toFile()).entries().toList() - assertThat(entries.size).isEqualTo(2) + assertThat(outputShadowJar.entries().toList().size).isEqualTo(2) } @Test @@ -552,9 +544,7 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - val attributes = JarFile(outputShadowJar.toFile()).manifest.mainAttributes - assertThat(attributes.getValue("Class-Path")).isNull() + assertThat(outputShadowJar.manifest.mainAttributes.getValue("Class-Path")).isNull() } @Issue( @@ -577,9 +567,8 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - val attributes = JarFile(outputShadowJar.toFile()).manifest.mainAttributes - assertThat(attributes.getValue("Class-Path")).isEqualTo("/libs/a.jar junit-3.8.2.jar") + assertThat(outputShadowJar.manifest.mainAttributes.getValue("Class-Path")) + .isEqualTo("/libs/a.jar junit-3.8.2.jar") } @Issue( @@ -597,9 +586,8 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - val attributes = JarFile(outputShadowJar.toFile()).manifest.mainAttributes - assertThat(attributes.getValue("Class-Path")).isEqualTo("junit-3.8.2.jar") + assertThat(outputShadowJar.manifest.mainAttributes.getValue("Class-Path")) + .isEqualTo("junit-3.8.2.jar") } @Issue( @@ -621,7 +609,7 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() + assertThat(outputShadowJar).isRegular() } /** @@ -755,9 +743,7 @@ class ShadowPluginTest : BasePluginTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val entries = JarFile(outputShadowJar.toFile()).entries().toList() + val entries = outputShadowJar.entries().toList() assertThat(entries.count { it.name.endsWith(".class") }).isEqualTo(1) } @@ -783,13 +769,11 @@ class ShadowPluginTest : BasePluginTest() { ) val result = run(testShadowJarTask) - val testJar = path("build/libs/shadow-1.0-tests.jar") assertThat(result.task(":$testShadowJarTask")).isNotNull() .transform { it.outcome }.isEqualTo(TaskOutcome.SUCCESS) - - assertThat(testJar).exists() - assertThat(JarFile(testJar.toFile()).getEntry("junit")).isNotNull() + val testJar = jarPath("build/libs/shadow-1.0-tests.jar") + assertThat(testJar.getEntry("junit")).isNotNull() } private fun writeShadowedClientAndServerModules() { diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/BaseTransformerTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/BaseTransformerTest.kt index 31e35640c..12b732786 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/BaseTransformerTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/BaseTransformerTest.kt @@ -3,7 +3,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.BasePluginTest import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar import java.nio.file.Path -import java.util.jar.JarFile import kotlin.io.path.writeText sealed class BaseTransformerTest : BasePluginTest() { @@ -56,13 +55,6 @@ sealed class BaseTransformerTest : BasePluginTest() { const val ENTRY_SERVICE_EXTENSION_MODULE = "META-INF/services/org.codehaus.groovy.runtime.ExtensionModule" const val ENTRY_GROOVY_EXTENSION_MODULE = "META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule" - fun getJarFileContents(jarPath: Path, entryName: String): String { - JarFile(jarPath.toFile()).use { jar -> - val entry = jar.getJarEntry(entryName) ?: error("Entry not found: $entryName") - return jar.getInputStream(entry).bufferedReader().readText() - } - } - inline fun transform( shadowJarBlock: String = "", transformerBlock: String = "", diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerTest.kt index 47a58eecb..aae881fba 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerTest.kt @@ -1,7 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import assertk.assertThat -import assertk.assertions.exists import assertk.assertions.isEqualTo import com.github.jengelman.gradle.plugins.shadow.util.Issue import kotlin.io.path.appendText @@ -23,13 +22,8 @@ class ServiceFileTransformerTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text1 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_SHADE) - assertThat(text1).isEqualTo(CONTENT_ONE_TWO) - - val text2 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_FOO) - assertThat(text2).isEqualTo("one") + assertThat(outputShadowJar.getContent(ENTRY_SERVICES_SHADE)).isEqualTo(CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(ENTRY_SERVICES_FOO)).isEqualTo("one") } @Test @@ -51,10 +45,7 @@ class ServiceFileTransformerTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text = getJarFileContents(outputShadowJar, ENTRY_FOO_SHADE) - assertThat(text).isEqualTo(CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(ENTRY_FOO_SHADE)).isEqualTo(CONTENT_ONE_TWO) } @Test @@ -72,13 +63,8 @@ class ServiceFileTransformerTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text1 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_SHADE) - assertThat(text1).isEqualTo(CONTENT_ONE_TWO) - - val text2 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_FOO) - assertThat(text2).isEqualTo("one") + assertThat(outputShadowJar.getContent(ENTRY_SERVICES_SHADE)).isEqualTo(CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(ENTRY_SERVICES_FOO)).isEqualTo("one") } @Test @@ -124,9 +110,7 @@ class ServiceFileTransformerTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text1 = getJarFileContents(outputShadowJar, "META-INF/services/java.sql.Driver") + val text1 = outputShadowJar.getContent("META-INF/services/java.sql.Driver") assertThat(text1).isEqualTo( """ oracle.jdbc.OracleDriver @@ -136,7 +120,7 @@ class ServiceFileTransformerTest : BaseTransformerTest() { """.trimIndent(), ) - val text2 = getJarFileContents(outputShadowJar, "META-INF/services/myapache.axis.components.compiler.Compiler") + val text2 = outputShadowJar.getContent("META-INF/services/myapache.axis.components.compiler.Compiler") assertThat(text2).isEqualTo( """ myapache.axis.components.compiler.Javac @@ -144,7 +128,7 @@ class ServiceFileTransformerTest : BaseTransformerTest() { """.trimIndent(), ) - val text3 = getJarFileContents(outputShadowJar, "META-INF/services/org.apache.commons.logging.LogFactory") + val text3 = outputShadowJar.getContent("META-INF/services/org.apache.commons.logging.LogFactory") assertThat(text3).isEqualTo( """ myapache.commons.logging.impl.LogFactoryImpl @@ -172,10 +156,7 @@ class ServiceFileTransformerTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text = getJarFileContents(outputShadowJar, ENTRY_FOO_SHADE) - assertThat(text).isEqualTo(CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(ENTRY_FOO_SHADE)).isEqualTo(CONTENT_ONE_TWO) } @Issue( @@ -207,9 +188,7 @@ class ServiceFileTransformerTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text = getJarFileContents(outputShadowJar, servicesShadowEntry) - assertThat(text).isEqualTo(CONTENT_THREE + "\n" + CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(servicesShadowEntry)) + .isEqualTo(CONTENT_THREE + "\n" + CONTENT_ONE_TWO) } } diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt index 85371752e..323e2811e 100644 --- a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/TransformersTest.kt @@ -1,14 +1,13 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import assertk.assertThat -import assertk.assertions.exists import assertk.assertions.isEqualTo import assertk.assertions.isNotNull import assertk.assertions.isNull import com.github.jengelman.gradle.plugins.shadow.util.Issue -import java.util.jar.JarInputStream +import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries +import com.github.jengelman.gradle.plugins.shadow.util.isRegular import kotlin.io.path.appendText -import kotlin.io.path.inputStream import kotlin.io.path.writeText import kotlin.reflect.KClass import org.junit.jupiter.api.Test @@ -36,10 +35,8 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text = getJarFileContents(outputShadowJar, ENTRY_TEST_PROPERTIES) - assertThat(text.trimIndent()).isEqualTo(CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(ENTRY_TEST_PROPERTIES).trimIndent()) + .isEqualTo(CONTENT_ONE_TWO) } @Test @@ -61,10 +58,8 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text = getJarFileContents(outputShadowJar, ENTRY_TEST_PROPERTIES) - assertThat(text.trimIndent()).isEqualTo(CONTENT_ONE_TWO) + assertThat(outputShadowJar.getContent(ENTRY_TEST_PROPERTIES).trimIndent()) + .isEqualTo(CONTENT_ONE_TWO) } @Test @@ -83,14 +78,10 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - JarInputStream(outputShadowJar.inputStream()).use { jis -> - val mf = jis.manifest - assertThat(mf).isNotNull() - assertThat(mf.mainAttributes.getValue("Test-Entry")).isEqualTo("PASSED") - assertThat(mf.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") - } + val mf = outputShadowJar.manifest + assertThat(mf).isNotNull() + assertThat(mf.mainAttributes.getValue("Test-Entry")).isEqualTo("PASSED") + assertThat(mf.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") } @Test @@ -116,15 +107,11 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - JarInputStream(outputShadowJar.inputStream()).use { jis -> - val mf = jis.manifest - assertThat(mf).isNotNull() - assertThat(mf.mainAttributes.getValue("Test-Entry")).isEqualTo("PASSED") - assertThat(mf.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") - assertThat(mf.mainAttributes.getValue("New-Entry")).isEqualTo("NEW") - } + val mf = outputShadowJar.manifest + assertThat(mf).isNotNull() + assertThat(mf.mainAttributes.getValue("Test-Entry")).isEqualTo("PASSED") + assertThat(mf.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") + assertThat(mf.mainAttributes.getValue("New-Entry")).isEqualTo("NEW") } @Test @@ -164,10 +151,7 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val text = getJarFileContents(outputShadowJar, propertiesXml) - assertThat(text.trimIndent()).isEqualTo( + assertThat(outputShadowJar.getContent(propertiesXml).trimIndent()).isEqualTo( """ @@ -202,24 +186,18 @@ class TransformersTest : BaseTransformerTest() { run("jar", shadowJarTask) - val jar = path("build/libs/shadow-1.0.jar") - assertThat(jar).exists() - assertThat(outputShadowJar).exists() - - JarInputStream(outputShadowJar.inputStream()).use { jis -> - val mf = jis.manifest - assertThat(mf).isNotNull() - assertThat(mf.mainAttributes.getValue("Test-Entry")).isEqualTo("PASSED") - assertThat(mf.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") - assertThat(mf.mainAttributes.getValue("New-Entry")).isEqualTo("NEW") - } - JarInputStream(jar.inputStream()).use { jis -> - val mf = jis.manifest - assertThat(mf).isNotNull() - assertThat(mf.mainAttributes.getValue("Test-Entry")).isEqualTo("FAILED") - assertThat(mf.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") - assertThat(mf.mainAttributes.getValue("New-Entry")).isNull() - } + val mf1 = outputShadowJar.manifest + assertThat(mf1).isNotNull() + assertThat(mf1.mainAttributes.getValue("Test-Entry")).isEqualTo("PASSED") + assertThat(mf1.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") + assertThat(mf1.mainAttributes.getValue("New-Entry")).isEqualTo("NEW") + + val outputJar = jarPath("build/libs/shadow-1.0.jar") + val mf2 = outputJar.manifest + assertThat(mf2).isNotNull() + assertThat(mf2.mainAttributes.getValue("Test-Entry")).isEqualTo("FAILED") + assertThat(mf2.mainAttributes.getValue("Main-Class")).isEqualTo("shadow.Main") + assertThat(mf2.mainAttributes.getValue("New-Entry")).isNull() } @Test @@ -254,9 +232,7 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val props = getJarFileContents(outputShadowJar, ENTRY_SERVICE_EXTENSION_MODULE).toProperties() + val props = outputShadowJar.getContent(ENTRY_SERVICE_EXTENSION_MODULE).toProperties() assertThat(props.getProperty("moduleName")).isEqualTo("MergedByShadowJar") assertThat(props.getProperty("moduleVersion")).isEqualTo("1.0.0") assertThat(props.getProperty("extensionClasses")) @@ -297,16 +273,16 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val props = getJarFileContents(outputShadowJar, ENTRY_GROOVY_EXTENSION_MODULE).toProperties() + val props = outputShadowJar.getContent(ENTRY_GROOVY_EXTENSION_MODULE).toProperties() assertThat(props.getProperty("moduleName")).isEqualTo("MergedByShadowJar") assertThat(props.getProperty("moduleVersion")).isEqualTo("1.0.0") assertThat(props.getProperty("extensionClasses")) .isEqualTo("com.acme.foo.FooExtension,com.acme.foo.BarExtension,com.acme.bar.SomeExtension,com.acme.bar.AnotherExtension") assertThat(props.getProperty("staticExtensionClasses")) .isEqualTo("com.acme.foo.FooStaticExtension,com.acme.bar.SomeStaticExtension") - assertDoesNotContain(outputShadowJar, listOf(ENTRY_SERVICE_EXTENSION_MODULE)) + assertThat(outputShadowJar).doesNotContainEntries( + ENTRY_SERVICE_EXTENSION_MODULE, + ) } @Test @@ -344,9 +320,7 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() - - val props = getJarFileContents(outputShadowJar, ENTRY_SERVICE_EXTENSION_MODULE).toProperties() + val props = outputShadowJar.getContent(ENTRY_SERVICE_EXTENSION_MODULE).toProperties() assertThat(props.getProperty("moduleName")).isEqualTo("MergedByShadowJar") assertThat(props.getProperty("moduleVersion")).isEqualTo("1.0.0") assertThat(props.getProperty("extensionClasses")) @@ -372,7 +346,7 @@ class TransformersTest : BaseTransformerTest() { run(shadowJarTask) - assertThat(outputShadowJar).exists() + assertThat(outputShadowJar).isRegular() } private companion object { diff --git a/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarPath.kt b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarPath.kt new file mode 100644 index 000000000..9f9ab7409 --- /dev/null +++ b/src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarPath.kt @@ -0,0 +1,47 @@ +package com.github.jengelman.gradle.plugins.shadow.util + +import assertk.Assert +import assertk.assertions.isNotEmpty +import assertk.fail +import java.nio.file.Path +import java.util.jar.JarFile +import kotlin.io.path.deleteExisting + +/** + * A wrapper for [JarFile] that also implements [Path]. + * + * We must declare some functions like [kotlin.io.path.deleteExisting] explicitly as they could not + * be delegated to [JarPath] type. + */ +class JarPath(val path: Path) : + JarFile(path.toFile()), + Path by path { + + fun deleteExisting() { + close() + path.deleteExisting() + } + + fun getContent(entryName: String): String { + val entry = getEntry(entryName) ?: error("Entry not found: $entryName") + return getInputStream(entry).bufferedReader().readText() + } +} + +/** + * Common regular assertions for [JarPath]. + */ +fun Assert.isRegular() = transform { it.entries().toList() }.isNotEmpty() + +fun Assert.containsEntries(vararg entries: String) = transform { actual -> + entries.forEach { entry -> + actual.getEntry(entry) ?: fail("Jar file ${actual.path} does not contain entry $entry") + } +} + +fun Assert.doesNotContainEntries(vararg entries: String) = transform { actual -> + entries.forEach { entry -> + actual.getEntry(entry) ?: return@forEach + fail("Jar file ${actual.path} contains entry $entry") + } +}