fixed unzip tests and slightly modified LocalDriver
This commit is contained in:
parent
e1c8be66db
commit
8e304377c6
@ -19,19 +19,19 @@ internal class LocalFile(private val path: Path) : FileReader, FileWriter {
|
|||||||
override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes)
|
override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
private class LocalDirectory(private val root: Path, private val currentDir: Path) : Directory {
|
internal class LocalDirectory(private val root: Path, private val currentDir: Path) : Directory {
|
||||||
private fun child(child: String): Path = root / currentDir / child
|
private fun child(child: String): Path = root / currentDir / child
|
||||||
private fun child(child: Path): Path = root / currentDir / child
|
private fun child(child: Path): Path = root / currentDir / child
|
||||||
|
|
||||||
override fun close() {}
|
override fun close() {}
|
||||||
|
|
||||||
override suspend fun get(filename: String): FileReader = LocalFile(child(filename))
|
override suspend fun get(filename: String): LocalFile = LocalFile(child(filename))
|
||||||
|
|
||||||
override suspend fun get(filename: Path): FileReader = LocalFile(child(filename))
|
override suspend fun get(filename: Path): LocalFile = LocalFile(child(filename))
|
||||||
|
|
||||||
override suspend fun create(filename: String, ignoreIfExists: Boolean) {
|
override suspend fun create(filename: String, ignoreIfExists: Boolean) {
|
||||||
child(filename).parent.createDirectories()
|
val dir = child(filename)
|
||||||
|
dir.parent.createDirectories()
|
||||||
try {
|
try {
|
||||||
child(filename).createFile()
|
child(filename).createFile()
|
||||||
} catch (ex: java.nio.file.FileAlreadyExistsException) {
|
} catch (ex: java.nio.file.FileAlreadyExistsException) {
|
||||||
@ -41,18 +41,14 @@ private class LocalDirectory(private val root: Path, private val currentDir: Pat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun put(filename: String): FileWriter {
|
override suspend fun put(filename: String): LocalFile = get(filename)
|
||||||
val tmp = child(filename)
|
|
||||||
//tmp.toFile().setWritable(true)
|
|
||||||
return LocalFile(tmp)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
override suspend fun put(filename: Path): LocalFile = get(filename)
|
||||||
|
|
||||||
override suspend fun put(filename: Path): FileWriter = LocalFile(child(filename))
|
override suspend fun getSubdir(path: Path): LocalDirectory = LocalDirectory(root, currentDir / path)
|
||||||
|
|
||||||
override suspend fun getSubdir(path: Path): LocalDirectory = LocalDirectory(root, child(path))
|
|
||||||
override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): LocalDirectory {
|
override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): LocalDirectory {
|
||||||
val dir = child(dirname)
|
val dir = child(dirname)
|
||||||
|
dir.parent.createDirectories()
|
||||||
try {
|
try {
|
||||||
dir.createDirectory()
|
dir.createDirectory()
|
||||||
} catch (ex: java.nio.file.FileAlreadyExistsException) {
|
} catch (ex: java.nio.file.FileAlreadyExistsException) {
|
||||||
@ -60,7 +56,7 @@ private class LocalDirectory(private val root: Path, private val currentDir: Pat
|
|||||||
throw ex
|
throw ex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return LocalDirectory(root, dir)
|
return LocalDirectory(root, currentDir / dirname)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val path: Path
|
override val path: Path
|
||||||
|
@ -17,7 +17,7 @@ internal class LocalDriverTests {
|
|||||||
@BeforeTest
|
@BeforeTest
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
tempDir = createTempDirectory()
|
tempDir = createTempDirectory()
|
||||||
testSample = LocalDirectory(tempDir!!)
|
testSample = localStorage(tempDir!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -4,6 +4,7 @@ import kotlinx.coroutines.runBlocking
|
|||||||
|
|
||||||
import space.kscience.snark.storage.Directory
|
import space.kscience.snark.storage.Directory
|
||||||
import space.kscience.snark.storage.local.LocalDirectory
|
import space.kscience.snark.storage.local.LocalDirectory
|
||||||
|
import space.kscience.snark.storage.local.localStorage
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
@ -24,55 +25,40 @@ internal class UnzipTests {
|
|||||||
|
|
||||||
private suspend fun makeFile(dir: Directory, filename: String, content: ByteArray) {
|
private suspend fun makeFile(dir: Directory, filename: String, content: ByteArray) {
|
||||||
dir.create(filename)
|
dir.create(filename)
|
||||||
|
dir.put(filename).write(content)
|
||||||
val writter = dir.put(filename)
|
|
||||||
if (!(tempDir!! / Path("source") / Path(filename)).isRegularFile()) {
|
|
||||||
println("new shit")
|
|
||||||
}
|
|
||||||
writter.write(content)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun zipAll(directory: String, zipFile: String) {
|
private fun zipAll(directory: String, zipFile: String) {
|
||||||
val sourceFile = File(directory)
|
val sourceFile = File(directory)
|
||||||
|
|
||||||
ZipOutputStream(BufferedOutputStream( FileOutputStream(zipFile))).use {
|
ZipOutputStream(BufferedOutputStream( FileOutputStream(zipFile))).use {
|
||||||
it.use {
|
zipFiles(it, sourceFile, File.separator)
|
||||||
zipFiles(it, sourceFile, "")
|
it.closeEntry()
|
||||||
it.closeEntry()
|
it.close()
|
||||||
it.close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun zipFiles(zipOut: ZipOutputStream, sourceFile: File, parentDirPath: String) {
|
private fun zipFiles(zipOut: ZipOutputStream, sourceFile: File, parentDirname: String) {
|
||||||
|
|
||||||
val data = ByteArray(2048)
|
val data = ByteArray(2048)
|
||||||
|
|
||||||
for (f in sourceFile.listFiles()) {
|
for (f in sourceFile.listFiles()) {
|
||||||
if (f.isDirectory) {
|
if (f.isDirectory) {
|
||||||
val entry = ZipEntry(f.name + File.separator)
|
zipFiles(zipOut, f, parentDirname + f.name + File.separator)
|
||||||
entry.time = f.lastModified()
|
|
||||||
entry.isDirectory
|
|
||||||
entry.size = f.length()
|
|
||||||
zipOut.putNextEntry(entry)
|
|
||||||
zipFiles(zipOut, f, f.name)
|
|
||||||
} else {
|
} else {
|
||||||
if (!f.name.contains(".zip")) { //If folder contains a file with extension ".zip", skip it
|
FileInputStream(f).use { fi ->
|
||||||
FileInputStream(f).use { fi ->
|
BufferedInputStream(fi).use { origin ->
|
||||||
BufferedInputStream(fi).use { origin ->
|
var path = parentDirname + f.name
|
||||||
val path = parentDirPath + File.separator + f.name
|
val entry = ZipEntry(path.drop(1))
|
||||||
val entry = ZipEntry(path)
|
entry.time = f.lastModified()
|
||||||
entry.time = f.lastModified()
|
entry.size = f.length()
|
||||||
entry.isDirectory
|
zipOut.putNextEntry(entry)
|
||||||
entry.size = f.length()
|
while (true) {
|
||||||
zipOut.putNextEntry(entry)
|
val readBytes = origin.read(data)
|
||||||
while (true) {
|
if (readBytes == -1) {
|
||||||
val readBytes = origin.read(data)
|
break
|
||||||
if (readBytes == -1) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
zipOut.write(data, 0, readBytes)
|
|
||||||
}
|
}
|
||||||
|
zipOut.write(data, 0, readBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +69,7 @@ internal class UnzipTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testUnzip() = runBlocking {
|
fun testUnzip() = runBlocking {
|
||||||
val dir: Directory = LocalDirectory(tempDir!!)
|
val dir: Directory = localStorage(tempDir!!)
|
||||||
val source = dir.createSubdir("source")
|
val source = dir.createSubdir("source")
|
||||||
val target = dir.createSubdir("target")
|
val target = dir.createSubdir("target")
|
||||||
val bytes1 = byteArrayOf(0, 1, 2, 3)
|
val bytes1 = byteArrayOf(0, 1, 2, 3)
|
||||||
@ -101,6 +87,7 @@ internal class UnzipTests {
|
|||||||
unzip(archive_path, target)
|
unzip(archive_path, target)
|
||||||
|
|
||||||
val targetPath = tempDir!! / Path("target")
|
val targetPath = tempDir!! / Path("target")
|
||||||
|
println(targetPath)
|
||||||
val entries = targetPath.listDirectoryEntries()
|
val entries = targetPath.listDirectoryEntries()
|
||||||
|
|
||||||
assertEquals(3, entries.size)
|
assertEquals(3, entries.size)
|
||||||
|
Loading…
Reference in New Issue
Block a user