Skip to content

Commit 9bcb112

Browse files
CopilotGoooler
andcommitted
Fix skipStringConstants per-relocator behavior: use continue instead of return
Co-authored-by: Goooler <10363352+Goooler@users.noreply.github.com>
1 parent 8d995b2 commit 9bcb112

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

  • src
    • main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal
    • test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Relocators.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private fun Set<Relocator>.realMap(name: String, mapLiterals: Boolean): String {
3535

3636
for (relocator in this) {
3737
if (mapLiterals && relocator.skipStringConstants) {
38-
return name
38+
continue
3939
} else if (relocator.canRelocateClass(newName)) {
4040
return prefix + relocator.relocateClass(newName) + suffix
4141
} else if (relocator.canRelocatePath(newName)) {

src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocatorsTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.jengelman.gradle.plugins.shadow.relocation
33
import assertk.assertThat
44
import assertk.assertions.isEqualTo
55
import com.github.jengelman.gradle.plugins.shadow.internal.mapName
6+
import org.junit.jupiter.api.Test
67
import org.junit.jupiter.params.ParameterizedTest
78
import org.junit.jupiter.params.provider.Arguments
89
import org.junit.jupiter.params.provider.MethodSource
@@ -17,6 +18,30 @@ class RelocatorsTest {
1718
assertThat(actual).isEqualTo(expected)
1819
}
1920

21+
/**
22+
* Verifies that a relocator with [Relocator.skipStringConstants] = true is skipped individually
23+
* when mapping literals, rather than short-circuiting the entire set of relocators.
24+
*/
25+
@Test
26+
fun skipStringConstantsIsPerRelocator() {
27+
val skippingRelocator =
28+
SimpleRelocator("org.package", "shadow.org.package", skipStringConstants = true)
29+
val normalRelocator = SimpleRelocator("com.example", "shadow.com.example")
30+
val relocators = linkedSetOf(skippingRelocator, normalRelocator)
31+
32+
// The skipping relocator cannot match "com.example.Foo", but the normal one can.
33+
// Ensure the normal relocator is still applied even though the first one has
34+
// skipStringConstants.
35+
assertThat(relocators.mapName("com.example.Foo", mapLiterals = true))
36+
.isEqualTo("shadow.com.example.Foo")
37+
// When mapLiterals=true, the skipping relocator should not apply to its own pattern.
38+
assertThat(relocators.mapName("org.package.Bar", mapLiterals = true))
39+
.isEqualTo("org.package.Bar")
40+
// When mapLiterals=false, the skipping relocator applies normally.
41+
assertThat(relocators.mapName("org.package.Bar", mapLiterals = false))
42+
.isEqualTo("shadow.org.package.Bar")
43+
}
44+
2045
private companion object {
2146
val primitiveTypes = setOf('B', 'C', 'D', 'F', 'I', 'J', 'S', 'Z')
2247

0 commit comments

Comments
 (0)