Yokan / rebuild
Course 1 8624561

TestFiles.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/files/TestFiles.kt

test / fixture62 lines
1package eu.friedcutlet.yokan.services.files23import eu.friedcutlet.yokan.database.DbPageUrl45/** A filesystem that stores nothing: writes vanish and every path is absent. */6internal object NoFiles : LocalFileService {7    override val downloadRoot = "/downloads"8    override val backupRoot = "/backups"9    override val cacheRoot = "/cache"10    override fun writeBytes(path: String, bytes: ByteArray) = Unit11    override fun readBytes(path: String) = ByteArray(0)12    override fun canonicalPath(path: String) = path13    override fun exists(path: String) = false14    override fun deleteRecursively(path: String) = true15    override fun directorySize(path: String) = 0L16    override fun openLocation(path: String) = false17    override fun listLocalEntries(root: String) = emptyList<LocalEntry>()18    override fun listFiles(root: String) = emptyList<LocalFileEntry>()19}2021/**22 * Map-backed filesystem with directory semantics derived from `/`-separated paths.23 * Methods are `open` so tests can inject failures by subclassing.24 */25internal open class MemoryFiles : LocalFileService {26    override val downloadRoot = "/downloads"27    override val backupRoot = "/backups"28    override val cacheRoot = "/cache"29    val files = linkedMapOf<String, ByteArray>()30    val paths: Set<String> get() = files.keys3132    override fun writeBytes(path: String, bytes: ByteArray) {33        files[path] = bytes.copyOf()34    }3536    override fun readBytes(path: String): ByteArray = files[path]?.copyOf() ?: ByteArray(0)37    override fun canonicalPath(path: String): String? = path38    override fun exists(path: String): Boolean = path in files || files.keys.any { it.startsWith("$path/") }3940    override fun deleteRecursively(path: String): Boolean {41        files.keys.removeAll { it == path || it.startsWith("$path/") }42        return true43    }4445    override fun directorySize(path: String): Long =46        files.filterKeys { it == path || it.startsWith("$path/") }.values.sumOf { it.size.toLong() }4748    override fun openLocation(path: String) = false49    override fun listLocalEntries(root: String): List<LocalEntry>? = emptyList()50    override fun listFiles(root: String): List<LocalFileEntry> =51        files.filterKeys { it.substringBeforeLast('/') == root }52            .map { (path, bytes) -> LocalFileEntry(path, path.substringAfterLast('/'), bytes.size.toLong(), 0L) }53}5455/** Archives that contain nothing and cannot be packaged. */56internal object NoArchives : LocalArchiveService {57    override fun listPages(archivePath: String) = emptyList<DbPageUrl>()58    override fun listEpubChapters(archivePath: String) = emptyList<LocalEpubChapter>()59    override fun readEpubChapter(archivePath: String, index: Int) = ""60    override fun readEpubResource(archivePath: String, resourcePath: String): ByteArray? = null61    override fun zipDirectoryToCbz(directoryPath: String, deleteSource: Boolean): String? = null62}63

Search chapters, reference and source filenames.