1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-01 04:10:12 +08:00

[mock] Filter files for MockAbsoluteFolder.resolveFiles, part of

This commit is contained in:
Him188 2023-03-19 12:22:33 +00:00
parent 9e5b282ee6
commit db3a0ad24a
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
2 changed files with 23 additions and 5 deletions
mirai-core-mock
src/internal/remotefile/absolutefile
test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -163,9 +163,11 @@ internal open class MockAbsoluteFolder(
if (path.isBlank()) throw IllegalArgumentException("path cannot be blank.")
if (!FileSystem.isLegal(path)) return emptyFlow()
if (path[0] == '/') return files.root.resolveFiles(path.removePrefix("/"))
return files.fileSystem.findByPath(absolutePath.removeSuffix("/") + "/" + path.removePrefix("/")).map {
it.toMockAbsFile(files)
}.asFlow()
return files.fileSystem.findByPath(absolutePath.removeSuffix("/") + "/" + path.removePrefix("/"))
.filter { it.isFile }
.map {
it.toMockAbsFile(files)
}.asFlow()
}
@JavaFriendlyAPI

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -11,6 +11,7 @@ package net.mamoe.mirai.mock.test
import com.google.common.jimfs.Configuration
import com.google.common.jimfs.Jimfs
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.toList
@ -23,6 +24,7 @@ import net.mamoe.mirai.mock.utils.simpleMemberInfo
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.cast
import net.mamoe.mirai.utils.md5
import net.mamoe.mirai.utils.runBIO
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.nio.file.FileSystem
@ -129,4 +131,18 @@ internal class AbsoluteFileTest : MockBotTestBase() {
val file = files.root.resolveFileById(absFile.id, true)!!
assertContentEquals(bytes.md5(), file.md5)
}
@Test
fun testResolveFiles() = runTest {
val file = runBIO {
kotlin.io.path.createTempFile("test", ".txt").toFile().apply {
writeText("test")
deleteOnExit()
}
}
file.toExternalResource().use {
group.files.root.uploadNewFile("/a/test.txt", it)
}
assertEquals(0, group.files.root.resolveFiles("/a").count())
}
}