Skip to content

Commit

Permalink
Migrate functional tests to Kotlin and Junit part 3 (GradleUp#1125)
Browse files Browse the repository at this point in the history
* 1/2 of TransformerSpec

* 2/2 of TransformerSpec

* Refine TransformersTest

* Split BaseTransformerTest

* Split ServiceFileTransformerTest

* Cleanups

* Add `JarPath` to simplify assertions

* Split AppendingTransformerTest

* Split GroovyExtensionModuleTransformerTest

* Rename transformerShouldNotHaveDeprecatedBehaviours to otherTransformers

* Cleanups
  • Loading branch information
Goooler authored Jan 5, 2025
1 parent dcb6620 commit 18d659e
Show file tree
Hide file tree
Showing 13 changed files with 972 additions and 1,121 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +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.jar.JarFile
import java.util.Properties
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.Path
import kotlin.io.path.absolutePathString
Expand All @@ -16,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
Expand Down Expand Up @@ -122,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()
}
}
Expand All @@ -143,22 +150,6 @@ abstract class BasePluginTest {
return AppendableMavenFileRepository(root.resolve(path))
}

fun assertContains(jarPath: Path, paths: List<String>) {
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<String>) {
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()
Expand Down Expand Up @@ -328,6 +319,12 @@ abstract class BasePluginTest {
tasks.named('$SHADOW_RUN_TASK_NAME', ${JavaJarExec::class.java.name})
""".trimIndent()

fun String.toProperties(): Properties = Properties().apply { load(byteInputStream()) }

fun fromJar(vararg paths: Path): String {
return paths.joinToString(System.lineSeparator()) { "from('${it.toUri().toURL().path}')" }
}

fun BuildResult.assertNoDeprecationWarnings() = apply {
output.lines().forEach {
assert(!containsDeprecationWarning(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
)
}

Expand All @@ -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",
)
}

Expand Down Expand Up @@ -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",
)
}

Expand All @@ -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",
)
}

Expand Down Expand Up @@ -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",
)
}

Expand All @@ -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",
)
}

Expand All @@ -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",
)
}

Expand All @@ -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",
)
}

Expand Down Expand Up @@ -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",
)
}
}
Loading

0 comments on commit 18d659e

Please sign in to comment.