add implement of md5 and sha1 for MockAbsoluteFile (#2436)

* add impl of md5 and sha1

* add tests

* modify as required
This commit is contained in:
Eritque arcus 2023-01-18 04:42:56 -05:00 committed by GitHub
parent cc7f35519e
commit 0a7ebb7b4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 10 deletions

View File

@ -47,16 +47,29 @@ private fun MockServerRemoteFile.toMockAbsFile(
sha1: ByteArray = byteArrayOf() sha1: ByteArray = byteArrayOf()
): AbsoluteFile { ): AbsoluteFile {
val parent = this.parent.toMockAbsFolder(files) val parent = this.parent.toMockAbsFolder(files)
// todo md5 and sha return if (md5.isEmpty() || sha1.isEmpty()) {
return MockAbsoluteFile( asExternalResource().use { res ->
sha1, MockAbsoluteFile(
md5, if (sha1.isEmpty()) res.sha1 else sha1,
files, if (md5.isEmpty()) res.md5 else md5,
parent, files,
this.id, parent,
this.name, this.id,
parent.absolutePath.removeSuffix("/") + "/" + this.name this.name,
) parent.absolutePath.removeSuffix("/") + "/" + this.name
)
}
} else {
MockAbsoluteFile(
sha1,
md5,
files,
parent,
this.id,
this.name,
parent.absolutePath.removeSuffix("/") + "/" + this.name
)
}
} }
internal open class MockAbsoluteFolder( internal open class MockAbsoluteFolder(

View File

@ -22,9 +22,11 @@ import net.mamoe.mirai.mock.internal.serverfs.MockServerFileSystemImpl
import net.mamoe.mirai.mock.utils.simpleMemberInfo import net.mamoe.mirai.mock.utils.simpleMemberInfo
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.cast import net.mamoe.mirai.utils.cast
import net.mamoe.mirai.utils.md5
import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.nio.file.FileSystem import java.nio.file.FileSystem
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNotEquals import kotlin.test.assertNotEquals
@ -97,4 +99,34 @@ internal class AbsoluteFileTest : MockBotTestBase() {
assertEquals(true, file.exists()) assertEquals(true, file.exists())
assertNotEquals(null, folder.resolveFiles("test.txt").firstOrNull()) assertNotEquals(null, folder.resolveFiles("test.txt").firstOrNull())
} }
@Test
fun testMD5() = runTest {
val bytes = "test".toByteArray()
val file = bytes.toExternalResource().use { res ->
files.root.uploadNewFile("/test.txt", res)
}
assertContentEquals(bytes.md5(), file.md5)
}
@Test
fun testMD5WithResolve() = runTest {
val bytes = "test".toByteArray()
bytes.toExternalResource().use { res ->
files.root.uploadNewFile("/test.txt", res)
}
val file = files.root.resolveFiles("/test.txt").toList()
assertEquals(1, file.size)
assertContentEquals(bytes.md5(), file[0].md5)
}
@Test
fun testMD5WithIDResolve() = runTest {
val bytes = "test".toByteArray()
val absFile = bytes.toExternalResource().use { res ->
files.root.uploadNewFile("/test.txt", res)
}
val file = files.root.resolveFileById(absFile.id, true)!!
assertContentEquals(bytes.md5(), file.md5)
}
} }