Skip to content

Commit

Permalink
[core] Relocate ktor-io in mirai-core-api. Fix #2381
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Dec 4, 2022
1 parent 9f45cae commit 4065165
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mirai-core-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ kotlin {
implementation(project(":mirai-console-compiler-annotations"))
implementation(`kotlinx-serialization-protobuf`)
implementation(`kotlinx-atomicfu`)
compileOnly(`ktor-io`) // runtime from mirai-core-utils
relocateCompileOnly(`ktor-io_relocated`) // runtime from mirai-core-utils
}
}

Expand Down
1 change: 1 addition & 0 deletions mirai-deps-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ plugins {
dependencies {
implementation(gradleApi())
implementation(gradleKotlinDsl())
implementation(`kotlin-reflect`)
implementation(kotlin("gradle-plugin-api"))
implementation(kotlin("gradle-plugin"))
implementation(kotlin("stdlib"))
Expand Down
7 changes: 6 additions & 1 deletion mirai-deps-test/test/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ abstract class AbstractTest {
add("org.gradle.jvmargs=-Xmx512m")
add("-D")
add("file.encoding=UTF-8")
add("--stacktrace")
// add("--stacktrace")
add("--info")
})
.build()
}
Expand Down Expand Up @@ -165,6 +166,10 @@ abstract class AbstractTest {
}
dependencies {
testImplementation(kotlin("test"))
implementation(kotlin("reflect"))
testImplementation(kotlin("test-junit5"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
}
tasks.test {
useJUnitPlatform()
Expand Down
131 changes: 110 additions & 21 deletions mirai-deps-test/test/CoreShadowRelocationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CoreShadowRelocationTest : AbstractTest() {
private const val OkHttp = "okhttp3.OkHttp"
private const val OkIO = "okio.ByteString"

private fun relocated(string: String): String {
fun relocated(string: String): String {
return "net.mamoe.mirai.internal.deps.$string"
}
}
Expand Down Expand Up @@ -59,6 +59,7 @@ class CoreShadowRelocationTest : AbstractTest() {
-both(`ktor-client-okhttp`)
-both(`okhttp3-okhttp`)
-both(okio)
+relocated(`ExternalResource-input`)
}
applyCodeFragment(fragment)
buildFile.appendText(
Expand All @@ -80,6 +81,7 @@ class CoreShadowRelocationTest : AbstractTest() {
+relocated(`ktor-client-okhttp`)
+relocated(`okhttp3-okhttp`)
+relocated(okio)
+relocated(`ExternalResource-input`)
}
applyCodeFragment(fragment)
buildFile.appendText(
Expand Down Expand Up @@ -128,6 +130,7 @@ class CoreShadowRelocationTest : AbstractTest() {
-both(`ktor-client-okhttp`)
-both(`okhttp3-okhttp`)
-both(okio)
// +relocated(`ExternalResource-input`) // Will fail with no class def found error because there is no runtime ktor-io
}
applyCodeFragment(fragment)
buildFile.appendText(
Expand All @@ -151,6 +154,7 @@ class CoreShadowRelocationTest : AbstractTest() {
+relocated(`ktor-client-okhttp`)
+relocated(`okhttp3-okhttp`)
+relocated(okio)
+relocated(`ExternalResource-input`)
}
applyCodeFragment(fragment)

Expand All @@ -177,31 +181,81 @@ class CoreShadowRelocationTest : AbstractTest() {
"""
package test
import org.junit.jupiter.api.*
import java.lang.reflect.Method
import kotlin.reflect.jvm.kotlinFunction
import kotlin.test.assertTrue
import kotlin.test.assertFalse
private val Method.signature: String
get() = buildString {
append(kotlinFunction?.toString())
return@buildString
append(kotlinFunction?.visibility?.name?.lowercase())
append(kotlinFunction?.visibility?.name?.lowercase())
append(' ')
append(returnType.canonicalName)
append(' ')
append(name)
append('(')
for (parameter in parameters) {
append(parameter.type.canonicalName)
append(' ')
append(parameter.name)
append(", ")
}
if (parameterCount != 0) {
deleteAt(lastIndex)
deleteAt(lastIndex)
}
append(')')
}
class MyTest {
""".trimIndent()
).append("\n").append("\n")

class TestCase(
class ClassTestCase(
val name: String,
val qualifiedClassName: String,
)

class FunctionTestCase(
val name: String,
val qualifiedClassName: String,
val signature: String,
val relocated: (FunctionTestCase.() -> FunctionTestCase)? = null,
)

val `ktor-io` = TestCase("ktor-io ByteBufferChannel", ByteBufferChannel)
val `ktor-client-core` = TestCase("ktor-client-core HttpClient", HttpClient)
val `ktor-client-okhttp` = TestCase("ktor-client-core OkHttp", KtorOkHttp)
val `okhttp3-okhttp` = TestCase("okhttp3 OkHttp", OkHttp)
val okio = TestCase("okio ByteString", OkIO)
val `ktor-io` = ClassTestCase("ktor-io ByteBufferChannel", ByteBufferChannel)
val `ktor-client-core` = ClassTestCase("ktor-client-core HttpClient", HttpClient)
val `ktor-client-okhttp` = ClassTestCase("ktor-client-core OkHttp", KtorOkHttp)
val `okhttp3-okhttp` = ClassTestCase("okhttp3 OkHttp", OkHttp)
val okio = ClassTestCase("okio ByteString", OkIO)
val `ExternalResource-input` =
FunctionTestCase(
"ExternalResource_input",
"net.mamoe.mirai.utils.ExternalResource",
"fun net.mamoe.mirai.utils.ExternalResource.input(): io.ktor.utils.io.core.Input"
) original@{
FunctionTestCase(
"relocated ExternalResource_input",
"net.mamoe.mirai.utils.ExternalResource",
"fun net.mamoe.mirai.utils.ExternalResource.input(): ${relocated("io.ktor.utils.io.core.Input")}"
) {
this@original
}
}

class Relocated(
val testCase: TestCase
class RelocatedClassTestCase(
val testCase: ClassTestCase
)

class Both(
val testCase: TestCase
class BothClassTestCase(
val testCase: ClassTestCase
)


private fun appendHas(name: String, qualifiedClassName: String) {
private fun appendHasClass(name: String, qualifiedClassName: String) {
result.append(
"""
@Test
Expand All @@ -212,6 +266,30 @@ class CoreShadowRelocationTest : AbstractTest() {
).append("\n")
}

fun appendClassHasMethod(name: String, qualifiedClassName: String, methodSignature: String) {
result.append(
"""
@Test
fun `has ${name}`() {
val signatures = Class.forName("$qualifiedClassName").declaredMethods.map { it.signature }
assertTrue("All signatures: " + signatures.joinToString("\n")) { signatures.any { it == "$methodSignature" } }
}
""".trimIndent()
).append("\n")
}

fun appendClassMethodNotFound(name: String, qualifiedClassName: String, methodSignature: String) {
result.append(
"""
@Test
fun `has ${name}`() {
val signatures = Class.forName("$qualifiedClassName").declaredMethods.map { it.signature }
assertFalse("All signatures: " + signatures.joinToString("\n")) { signatures.any { it == "$methodSignature" } }
}
""".trimIndent()
).append("\n")
}

private fun appendNotFound(name: String, qualifiedClassName: String) {
result.append(
"""
Expand All @@ -227,33 +305,43 @@ class CoreShadowRelocationTest : AbstractTest() {
/**
* Asserts a class exists. Also asserts its relocated class does not exist.
*/
operator fun TestCase.unaryPlus() {
appendHas(name, qualifiedClassName)
operator fun FunctionTestCase.unaryPlus() {
appendClassHasMethod(name, qualifiedClassName, signature)
relocated?.invoke(this)?.let { inverted ->
appendClassMethodNotFound(inverted.name, inverted.qualifiedClassName, inverted.signature)
}
}

/**
* Asserts a class exists. Also asserts its relocated class does not exist.
*/
operator fun ClassTestCase.unaryPlus() {
appendHasClass(name, qualifiedClassName)
appendNotFound("relocated $name", Companion.relocated(qualifiedClassName))
}

/**
* Asserts a class does not exist.
*/
operator fun TestCase.unaryMinus() {
operator fun ClassTestCase.unaryMinus() {
appendNotFound(name, qualifiedClassName)
}


/**
* Asserts a relocated class exists. Also asserts the original class does not exist.
*/
operator fun Relocated.unaryPlus() {
operator fun RelocatedClassTestCase.unaryPlus() {
this.testCase.run {
appendHas("relocated $name", Companion.relocated(qualifiedClassName))
appendHasClass("relocated $name", Companion.relocated(qualifiedClassName))
appendNotFound(name, qualifiedClassName)
}
}

/**
* Asserts a relocated class does not exist.
*/
operator fun Relocated.unaryMinus() {
operator fun RelocatedClassTestCase.unaryMinus() {
this.testCase.run {
appendNotFound("relocated $name", Companion.relocated(qualifiedClassName))
}
Expand All @@ -262,13 +350,14 @@ class CoreShadowRelocationTest : AbstractTest() {
/**
* Asserts both the class and its relocated one do not exist.
*/
operator fun Both.unaryMinus() {
operator fun BothClassTestCase.unaryMinus() {
-this.testCase
-relocated(this.testCase)
}

fun relocated(testCase: TestCase): Relocated = Relocated(testCase)
fun both(testCase: TestCase) = Both(testCase)
fun relocated(testCase: ClassTestCase): RelocatedClassTestCase = RelocatedClassTestCase(testCase)
fun relocated(testCase: FunctionTestCase): FunctionTestCase = testCase.relocated!!(testCase)
fun both(testCase: ClassTestCase) = BothClassTestCase(testCase)

fun build(): String = result.append("\n}\n").toString()
}
Expand Down

0 comments on commit 4065165

Please sign in to comment.