Skip to content

Commit c9933c5

Browse files
committed
Simplify assertions for JarPath
1 parent 2bbe80b commit c9933c5

File tree

5 files changed

+42
-34
lines changed

5 files changed

+42
-34
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import assertk.assertions.containsAtLeast
77
import assertk.assertions.exists
88
import assertk.assertions.isEqualTo
99
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
10+
import com.github.jengelman.gradle.plugins.shadow.util.getMainAttr
1011
import com.github.jengelman.gradle.plugins.shadow.util.isRegular
1112
import kotlin.io.path.appendText
1213
import kotlin.io.path.readText
@@ -48,7 +49,7 @@ class ApplicationTest : BasePluginTest() {
4849
"a2.properties",
4950
"myapp/Main.class",
5051
)
51-
transform { it.manifest.mainAttributes.getValue("Main-Class") }.isEqualTo("myapp.Main")
52+
getMainAttr("Main-Class").isEqualTo("myapp.Main")
5253
}
5354

5455
assertThat(path("build/install/myapp-shadow/bin/myapp")).all {

src/funcTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHA
99
import com.github.jengelman.gradle.plugins.shadow.util.Issue
1010
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
1111
import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries
12+
import com.github.jengelman.gradle.plugins.shadow.util.getMainAttr
1213
import java.net.URLClassLoader
1314
import kotlin.io.path.appendText
1415
import kotlin.io.path.writeText
@@ -111,7 +112,7 @@ class RelocationTest : BasePluginTest() {
111112
"junit/framework/TestSuite\$1.class",
112113
"junit/framework/TestSuite.class",
113114
)
114-
transform { it.manifest.mainAttributes.getValue("TEST-VALUE") }.isEqualTo("FOO")
115+
getMainAttr("TEST-VALUE").isEqualTo("FOO")
115116
}
116117
}
117118

src/funcTest/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPluginTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ class ShadowPluginTest : BasePluginTest() {
567567

568568
run(shadowJarTask)
569569

570-
val value = outputShadowJar.use { it.manifest.mainAttributes.getValue("Class-Path") }
570+
val value = outputShadowJar.use { it.getMainAttr("Class-Path") }
571571
assertThat(value).isNull()
572572
}
573573

@@ -591,7 +591,7 @@ class ShadowPluginTest : BasePluginTest() {
591591

592592
run(shadowJarTask)
593593

594-
val value = outputShadowJar.use { it.manifest.mainAttributes.getValue("Class-Path") }
594+
val value = outputShadowJar.use { it.getMainAttr("Class-Path") }
595595
assertThat(value).isEqualTo("/libs/a.jar junit-3.8.2.jar")
596596
}
597597

@@ -610,7 +610,7 @@ class ShadowPluginTest : BasePluginTest() {
610610

611611
run(shadowJarTask)
612612

613-
val value = outputShadowJar.use { it.manifest.mainAttributes.getValue("Class-Path") }
613+
val value = outputShadowJar.use { it.getMainAttr("Class-Path") }
614614
assertThat(value).isEqualTo("junit-3.8.2.jar")
615615
}
616616

src/funcTest/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformerTest.kt

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.jengelman.gradle.plugins.shadow.transformers
33
import assertk.assertThat
44
import assertk.assertions.isEqualTo
55
import com.github.jengelman.gradle.plugins.shadow.util.Issue
6+
import com.github.jengelman.gradle.plugins.shadow.util.getContent
67
import kotlin.io.path.appendText
78
import kotlin.io.path.writeText
89
import org.junit.jupiter.api.Test
@@ -22,8 +23,8 @@ class ServiceFileTransformerTest : BaseTransformerTest() {
2223
run(shadowJarTask)
2324

2425
assertThat(outputShadowJar).useAll {
25-
transform { it.getContent(ENTRY_SERVICES_SHADE) }.isEqualTo(CONTENT_ONE_TWO)
26-
transform { it.getContent(ENTRY_SERVICES_FOO) }.isEqualTo("one")
26+
getContent(ENTRY_SERVICES_SHADE).isEqualTo(CONTENT_ONE_TWO)
27+
getContent(ENTRY_SERVICES_FOO).isEqualTo("one")
2728
}
2829
}
2930

@@ -66,8 +67,8 @@ class ServiceFileTransformerTest : BaseTransformerTest() {
6667
run(shadowJarTask)
6768

6869
assertThat(outputShadowJar).useAll {
69-
transform { it.getContent(ENTRY_SERVICES_SHADE) }.isEqualTo(CONTENT_ONE_TWO)
70-
transform { it.getContent(ENTRY_SERVICES_FOO) }.isEqualTo("one")
70+
getContent(ENTRY_SERVICES_SHADE).isEqualTo(CONTENT_ONE_TWO)
71+
getContent(ENTRY_SERVICES_FOO).isEqualTo("one")
7172
}
7273
}
7374

@@ -124,29 +125,26 @@ class ServiceFileTransformerTest : BaseTransformerTest() {
124125
run(shadowJarTask)
125126

126127
assertThat(outputShadowJar).useAll {
127-
transform { it.getContent("META-INF/services/java.sql.Driver") }
128-
.isEqualTo(
129-
"""
130-
oracle.jdbc.OracleDriver
131-
myapache.hive.jdbc.HiveDriver
132-
myapache.derby.jdbc.AutoloadedDriver
133-
com.mysql.jdbc.Driver
134-
""".trimIndent(),
135-
)
136-
transform { it.getContent("META-INF/services/myapache.axis.components.compiler.Compiler") }
137-
.isEqualTo(
138-
"""
139-
myapache.axis.components.compiler.Javac
140-
org.apache.axis.components.compiler.Jikes
141-
""".trimIndent(),
142-
)
143-
transform { it.getContent("META-INF/services/org.apache.commons.logging.LogFactory") }
144-
.isEqualTo(
145-
"""
146-
myapache.commons.logging.impl.LogFactoryImpl
147-
org.mortbay.log.Factory
148-
""".trimIndent(),
149-
)
128+
getContent("META-INF/services/java.sql.Driver").isEqualTo(
129+
"""
130+
oracle.jdbc.OracleDriver
131+
myapache.hive.jdbc.HiveDriver
132+
myapache.derby.jdbc.AutoloadedDriver
133+
com.mysql.jdbc.Driver
134+
""".trimIndent(),
135+
)
136+
getContent("META-INF/services/myapache.axis.components.compiler.Compiler").isEqualTo(
137+
"""
138+
myapache.axis.components.compiler.Javac
139+
org.apache.axis.components.compiler.Jikes
140+
""".trimIndent(),
141+
)
142+
getContent("META-INF/services/org.apache.commons.logging.LogFactory").isEqualTo(
143+
"""
144+
myapache.commons.logging.impl.LogFactoryImpl
145+
org.mortbay.log.Factory
146+
""".trimIndent(),
147+
)
150148
}
151149
}
152150

src/funcTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/JarPath.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class JarPath(val path: Path) :
2323
path.deleteExisting()
2424
}
2525

26+
fun getMainAttr(name: String): String? {
27+
return manifest.mainAttributes.getValue(name)
28+
}
29+
2630
fun getContent(entryName: String): String {
2731
val entry = getEntry(entryName) ?: error("Entry not found: $entryName")
2832
return getInputStream(entry).bufferedReader().use { it.readText() }
@@ -38,13 +42,17 @@ fun Assert<JarPath>.isRegular() = all {
3842
given { it.use(block = {}) }
3943
}
4044

41-
fun Assert<JarPath>.containsEntries(vararg entries: String) = transform { actual ->
45+
fun Assert<JarPath>.getContent(entryName: String) = transform { it.getContent(entryName) }
46+
47+
fun Assert<JarPath>.getMainAttr(name: String) = transform { it.getMainAttr(name) }
48+
49+
fun Assert<JarPath>.containsEntries(vararg entries: String) = given { actual ->
4250
entries.forEach { entry ->
4351
actual.getEntry(entry) ?: fail("Jar file ${actual.path} does not contain entry $entry")
4452
}
4553
}
4654

47-
fun Assert<JarPath>.doesNotContainEntries(vararg entries: String) = transform { actual ->
55+
fun Assert<JarPath>.doesNotContainEntries(vararg entries: String) = given { actual ->
4856
entries.forEach { entry ->
4957
actual.getEntry(entry) ?: return@forEach
5058
fail("Jar file ${actual.path} contains entry $entry")

0 commit comments

Comments
 (0)