Skip to content

Commit 18d659e

Browse files
authored
Migrate functional tests to Kotlin and Junit part 3 (#1125)
* 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
1 parent dcb6620 commit 18d659e

File tree

13 files changed

+972
-1121
lines changed

13 files changed

+972
-1121
lines changed

src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/TransformerSpec.groovy

Lines changed: 0 additions & 746 deletions
This file was deleted.

src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ApplicationTest.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import assertk.assertions.contains
55
import assertk.assertions.containsAtLeast
66
import assertk.assertions.exists
77
import assertk.assertions.isEqualTo
8-
import java.util.jar.JarFile
8+
import assertk.assertions.isNotEmpty
9+
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
910
import kotlin.io.path.appendText
1011
import kotlin.io.path.readText
1112
import kotlin.io.path.writeText
@@ -39,13 +40,13 @@ class ApplicationTest : BasePluginTest() {
3940
assertThat(result.output).contains("Running application with JDK 17")
4041
assertThat(result.output).contains("TestApp: Hello World! (foo)")
4142

42-
val installedJar = path("build/install/myapp-shadow/lib/myapp-1.0-all.jar")
43-
assertThat(installedJar).exists()
44-
45-
assertContains(installedJar, listOf("a.properties", "a2.properties", "myapp/Main.class"))
46-
47-
val jarFile = JarFile(installedJar.toFile())
48-
assertThat(jarFile.manifest.mainAttributes.getValue("Main-Class"))
43+
val installedJar = jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar")
44+
assertThat(installedJar).containsEntries(
45+
"a.properties",
46+
"a2.properties",
47+
"myapp/Main.class",
48+
)
49+
assertThat(installedJar.manifest.mainAttributes.getValue("Main-Class"))
4950
.isEqualTo("myapp.Main")
5051

5152
path("build/install/myapp-shadow/bin/myapp").let { startScript ->
@@ -84,7 +85,7 @@ class ApplicationTest : BasePluginTest() {
8485

8586
run(ShadowApplicationPlugin.SHADOW_INSTALL_TASK_NAME)
8687

87-
assertThat(path("build/install/myapp-shadow/lib/myapp-1.0-all.jar")).exists()
88+
assertThat(jarPath("build/install/myapp-shadow/lib/myapp-1.0-all.jar").entries().toList()).isNotEmpty()
8889
}
8990

9091
private fun prepare(

src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/BasePluginTest.kt

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHA
55
import com.github.jengelman.gradle.plugins.shadow.tasks.JavaJarExec
66
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
77
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
8+
import com.github.jengelman.gradle.plugins.shadow.util.JarPath
89
import java.nio.file.Path
9-
import java.util.jar.JarFile
10+
import java.util.Properties
1011
import kotlin.io.path.ExperimentalPathApi
1112
import kotlin.io.path.Path
1213
import kotlin.io.path.absolutePathString
@@ -16,7 +17,7 @@ import kotlin.io.path.createFile
1617
import kotlin.io.path.createTempDirectory
1718
import kotlin.io.path.deleteRecursively
1819
import kotlin.io.path.exists
19-
import kotlin.io.path.extension
20+
import kotlin.io.path.isRegularFile
2021
import kotlin.io.path.readText
2122
import kotlin.io.path.toPath
2223
import kotlin.io.path.writeText
@@ -122,19 +123,25 @@ abstract class BasePluginTest {
122123
val settingsScriptPath: Path
123124
get() = path("settings.gradle")
124125

125-
val outputShadowJar: Path
126-
get() = path("build/libs/shadow-1.0-all.jar")
126+
val outputShadowJar: JarPath
127+
get() = jarPath("build/libs/shadow-1.0-all.jar")
127128

128-
val outputServerShadowJar: Path
129-
get() = path("server/build/libs/server-1.0-all.jar")
129+
val outputServerShadowJar: JarPath
130+
get() = jarPath("server/build/libs/server-1.0-all.jar")
131+
132+
fun jarPath(path: String): JarPath {
133+
val realPath = root.resolve(path).also {
134+
check(it.exists()) { "Path not found: $it" }
135+
check(it.isRegularFile()) { "Path is not a regular file: $it" }
136+
}
137+
return JarPath(realPath)
138+
}
130139

131140
fun path(path: String): Path {
132141
return root.resolve(path).also {
133-
val extension = it.extension
134-
// Binary files should not be created, text files should be created.
135-
if (it.exists() || extension == "jar" || extension == "zip") return@also
136-
142+
if (it.exists()) return@also
137143
it.parent.createDirectories()
144+
// We should create text file only if it doesn't exist.
138145
it.createFile()
139146
}
140147
}
@@ -143,22 +150,6 @@ abstract class BasePluginTest {
143150
return AppendableMavenFileRepository(root.resolve(path))
144151
}
145152

146-
fun assertContains(jarPath: Path, paths: List<String>) {
147-
JarFile(jarPath.toFile()).use { jar ->
148-
paths.forEach { path ->
149-
assert(jar.getJarEntry(path) != null) { "Jar file $jarPath does not contain entry $path" }
150-
}
151-
}
152-
}
153-
154-
fun assertDoesNotContain(jarPath: Path, paths: List<String>) {
155-
JarFile(jarPath.toFile()).use { jar ->
156-
paths.forEach { path ->
157-
assert(jar.getJarEntry(path) == null) { "Jar file $jarPath contains entry $path" }
158-
}
159-
}
160-
}
161-
162153
private val runner: GradleRunner
163154
get() {
164155
return GradleRunner.create()
@@ -328,6 +319,12 @@ abstract class BasePluginTest {
328319
tasks.named('$SHADOW_RUN_TASK_NAME', ${JavaJarExec::class.java.name})
329320
""".trimIndent()
330321

322+
fun String.toProperties(): Properties = Properties().apply { load(byteInputStream()) }
323+
324+
fun fromJar(vararg paths: Path): String {
325+
return paths.joinToString(System.lineSeparator()) { "from('${it.toUri().toURL().path}')" }
326+
}
327+
331328
fun BuildResult.assertNoDeprecationWarnings() = apply {
332329
output.lines().forEach {
333330
assert(!containsDeprecationWarning(it))

src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ConfigurationCacheSpec.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package com.github.jengelman.gradle.plugins.shadow
22

33
import assertk.assertThat
44
import assertk.assertions.contains
5-
import assertk.assertions.exists
65
import assertk.assertions.isEqualTo
76
import assertk.assertions.isNotNull
7+
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
8+
import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries
89
import kotlin.io.path.appendText
9-
import kotlin.io.path.deleteExisting
1010
import kotlin.io.path.writeText
1111
import org.gradle.testkit.runner.BuildResult
1212
import org.gradle.testkit.runner.TaskOutcome
@@ -76,13 +76,12 @@ class ConfigurationCacheSpec : BasePluginTest() {
7676
outputShadowJar.deleteExisting()
7777
val result = run(shadowJarTask)
7878

79-
assertContains(
80-
outputShadowJar,
81-
listOf("a.properties", "b.properties"),
79+
assertThat(outputShadowJar).containsEntries(
80+
"a.properties",
81+
"b.properties",
8282
)
83-
assertDoesNotContain(
84-
outputShadowJar,
85-
listOf("a2.properties"),
83+
assertThat(outputShadowJar).doesNotContainEntries(
84+
"a2.properties",
8685
)
8786
result.assertCcReused()
8887
}
@@ -101,14 +100,12 @@ class ConfigurationCacheSpec : BasePluginTest() {
101100
outputServerShadowJar.deleteExisting()
102101
val result = run(shadowJarTask)
103102

104-
assertThat(outputServerShadowJar).exists()
105-
assertContains(
106-
outputServerShadowJar,
107-
listOf("server/Server.class", "junit/framework/Test.class"),
103+
assertThat(outputServerShadowJar).containsEntries(
104+
"server/Server.class",
105+
"junit/framework/Test.class",
108106
)
109-
assertDoesNotContain(
110-
outputServerShadowJar,
111-
listOf("client/Client.class"),
107+
assertThat(outputServerShadowJar).doesNotContainEntries(
108+
"client/Client.class",
112109
)
113110
result.assertCcReused()
114111
}

src/intiTest/kotlin/com/github/jengelman/gradle/plugins/shadow/FilteringTest.kt

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.github.jengelman.gradle.plugins.shadow
22

33
import assertk.assertThat
4-
import assertk.assertions.exists
54
import assertk.assertions.isEqualTo
65
import assertk.assertions.isNotNull
6+
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
7+
import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries
78
import kotlin.io.path.appendText
89
import kotlin.io.path.readText
910
import kotlin.io.path.writeText
@@ -32,9 +33,10 @@ class FilteringTest : BasePluginTest() {
3233
@Test
3334
fun includeAllDependencies() {
3435
run(shadowJarTask)
35-
assertContains(
36-
outputShadowJar,
37-
listOf("a.properties", "a2.properties", "b.properties"),
36+
assertThat(outputShadowJar).containsEntries(
37+
"a.properties",
38+
"a2.properties",
39+
"b.properties",
3840
)
3941
}
4042

@@ -50,13 +52,12 @@ class FilteringTest : BasePluginTest() {
5052

5153
run(shadowJarTask)
5254

53-
assertContains(
54-
outputShadowJar,
55-
listOf("a.properties", "b.properties"),
55+
assertThat(outputShadowJar).containsEntries(
56+
"a.properties",
57+
"b.properties",
5658
)
57-
assertDoesNotContain(
58-
outputShadowJar,
59-
listOf("a2.properties"),
59+
assertThat(outputShadowJar).doesNotContainEntries(
60+
"a2.properties",
6061
)
6162
}
6263

@@ -107,13 +108,14 @@ class FilteringTest : BasePluginTest() {
107108

108109
assertThat(result.task(":shadowJar")).isNotNull()
109110
.transform { it.outcome }.isEqualTo(TaskOutcome.SUCCESS)
110-
assertContains(
111-
outputShadowJar,
112-
listOf("a.properties", "a2.properties", "b.properties", "d.properties"),
111+
assertThat(outputShadowJar).containsEntries(
112+
"a.properties",
113+
"a2.properties",
114+
"b.properties",
115+
"d.properties",
113116
)
114-
assertDoesNotContain(
115-
outputShadowJar,
116-
listOf("c.properties"),
117+
assertThat(outputShadowJar).doesNotContainEntries(
118+
"c.properties",
117119
)
118120
}
119121

@@ -134,13 +136,14 @@ class FilteringTest : BasePluginTest() {
134136

135137
assertThat(result.task(":shadowJar")).isNotNull()
136138
.transform { it.outcome }.isEqualTo(TaskOutcome.SUCCESS)
137-
assertContains(
138-
outputShadowJar,
139-
listOf("a2.properties", "b.properties", "c.properties", "d.properties"),
139+
assertThat(outputShadowJar).containsEntries(
140+
"a2.properties",
141+
"b.properties",
142+
"c.properties",
143+
"d.properties",
140144
)
141-
assertDoesNotContain(
142-
outputShadowJar,
143-
listOf("a.properties"),
145+
assertThat(outputShadowJar).doesNotContainEntries(
146+
"a.properties",
144147
)
145148
}
146149

@@ -168,13 +171,15 @@ class FilteringTest : BasePluginTest() {
168171

169172
run(shadowJarTask)
170173

171-
assertContains(
172-
outputShadowJar,
173-
listOf("d.properties", "shadow/Passed.class"),
174+
assertThat(outputShadowJar).containsEntries(
175+
"d.properties",
176+
"shadow/Passed.class",
174177
)
175-
assertDoesNotContain(
176-
outputShadowJar,
177-
listOf("a.properties", "a2.properties", "b.properties", "c.properties"),
178+
assertThat(outputShadowJar).doesNotContainEntries(
179+
"a.properties",
180+
"a2.properties",
181+
"b.properties",
182+
"c.properties",
178183
)
179184
}
180185

@@ -190,14 +195,12 @@ class FilteringTest : BasePluginTest() {
190195

191196
run(serverShadowJarTask)
192197

193-
assertThat(outputServerShadowJar).exists()
194-
assertDoesNotContain(
195-
outputServerShadowJar,
196-
listOf("client/Client.class"),
198+
assertThat(outputServerShadowJar).doesNotContainEntries(
199+
"client/Client.class",
197200
)
198-
assertContains(
199-
outputServerShadowJar,
200-
listOf("server/Server.class", "junit/framework/Test.class"),
201+
assertThat(outputServerShadowJar).containsEntries(
202+
"server/Server.class",
203+
"junit/framework/Test.class",
201204
)
202205
}
203206

@@ -213,14 +216,12 @@ class FilteringTest : BasePluginTest() {
213216

214217
run(serverShadowJarTask)
215218

216-
assertThat(outputServerShadowJar).exists()
217-
assertDoesNotContain(
218-
outputServerShadowJar,
219-
listOf("junit/framework/Test.class"),
219+
assertThat(outputServerShadowJar).doesNotContainEntries(
220+
"junit/framework/Test.class",
220221
)
221-
assertContains(
222-
outputServerShadowJar,
223-
listOf("client/Client.class", "server/Server.class"),
222+
assertThat(outputServerShadowJar).containsEntries(
223+
"client/Client.class",
224+
"server/Server.class",
224225
)
225226
}
226227

@@ -238,13 +239,12 @@ class FilteringTest : BasePluginTest() {
238239

239240
run(shadowJarTask)
240241

241-
assertContains(
242-
outputShadowJar,
243-
listOf("a.properties", "b.properties"),
242+
assertThat(outputShadowJar).containsEntries(
243+
"a.properties",
244+
"b.properties",
244245
)
245-
assertDoesNotContain(
246-
outputShadowJar,
247-
listOf("a2.properties"),
246+
assertThat(outputShadowJar).doesNotContainEntries(
247+
"a2.properties",
248248
)
249249
}
250250

@@ -274,13 +274,14 @@ class FilteringTest : BasePluginTest() {
274274
}
275275

276276
private fun commonAssertions() {
277-
assertContains(
278-
outputShadowJar,
279-
listOf("a.properties", "a2.properties", "b.properties", "c.properties"),
277+
assertThat(outputShadowJar).containsEntries(
278+
"a.properties",
279+
"a2.properties",
280+
"b.properties",
281+
"c.properties",
280282
)
281-
assertDoesNotContain(
282-
outputShadowJar,
283-
listOf("d.properties"),
283+
assertThat(outputShadowJar).doesNotContainEntries(
284+
"d.properties",
284285
)
285286
}
286287
}

0 commit comments

Comments
 (0)