Skip to content

Commit

Permalink
refactor: Create module with shared utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed Dec 22, 2020
1 parent 144cc62 commit b02f229
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package flank.scripts.utils
package flank.common

import org.rauschig.jarchivelib.ArchiveFormat
import org.rauschig.jarchivelib.ArchiverFactory
import org.rauschig.jarchivelib.CompressionType
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream


fun File.extract(
destination: File,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
package flank.scripts.utils
package flank.common

import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.OutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream

fun unzip(src: File, dst: File = src.parentFile) {
print("* Unzipping: $src to $dst - ")
ZipFile(src).use { zipFile ->
zipFile.entries().asSequence().forEach { zipEntry ->
val outputFile = File(dst, zipEntry.name)

if (zipEntry.isDirectory) {
outputFile.mkdirs()
return@forEach
}

outputFile.parentFile.mkdirs()
fun unzipFile(zipFileName: File, unzipPath: String): List<File> {
println("Unzipping: ${zipFileName.absolutePath} to $unzipPath")
return ZipFile(zipFileName).unzipTo(unzipPath)
}

zipFile.getInputStream(zipEntry) useCopyTo outputFile.outputStream()
private fun ZipFile.unzipTo(unzipPath: String) = use { zipFile ->
zipFile.entries()
.asSequence()
.fold(listOf<File>()) { unzippedFiles, zipFileEntry ->
unzippedFiles + zipFileEntry.saveToFile(zipFile, unzipPath)
}
}

private fun ZipEntry.saveToFile(zipFile: ZipFile, unzipPath: String): File {
val outputFile = File(unzipPath, name)
if(isDirectory) {
outputFile.mkdirs()
} else {
zipFile.getInputStream(this).use { zipEntryInput -> zipEntryInput.toFile(outputFile) }
}
println("OK")

return outputFile
}

private infix fun InputStream.useCopyTo(output: OutputStream) =
use { zipEntryInput -> output.use { output -> zipEntryInput.copyTo(output) } }
private fun InputStream.toFile(destinationFile: File) {
destinationFile.outputStream().use { output -> copyTo(output) }
}

fun zip(src: File, dst: File) {
print("* Zipping: $src to $dst - ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ftl.log
package flank.common

import com.google.common.truth.Truth
import ftl.test.util.TestHelper.normalizeLineEnding
import org.junit.After
import org.junit.Rule
import org.junit.Test
Expand All @@ -11,7 +10,8 @@ class OutputLoggerTest {

@Rule
@JvmField
val systemOutRule: SystemOutRule = SystemOutRule().enableLog().muteForSuccessfulTests()
val systemOutRule: SystemOutRule = SystemOutRule()
.enableLog().muteForSuccessfulTests()

private val simpleMessage = "print for simple"
private val detailedMessage = "print for detailed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ftl.util
package flank.common

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert
import org.junit.Test
import java.io.File
import java.nio.file.Files
Expand All @@ -19,7 +18,7 @@ internal class FilesTest {
createSymbolicLink(expectedDestination, testFile)

// then
assertTrue(Files.isSymbolicLink(expectedDestination))
Assert.assertTrue(Files.isSymbolicLink(expectedDestination))

// clean
testFile.toFile().delete()
Expand All @@ -33,11 +32,11 @@ internal class FilesTest {
val testDestination = Paths.get(Files.createTempDirectory("temp").toString(), "settings.gradle.kts")

// when
download(testSource, testDestination)
downloadFile(testSource, testDestination)

// then
assertTrue(testDestination.toFile().exists())
assertTrue(testDestination.toFile().length() > 0)
Assert.assertTrue(testDestination.toFile().exists())
Assert.assertTrue(testDestination.toFile().length() > 0)
}

@Test
Expand All @@ -62,8 +61,8 @@ internal class FilesTest {
)

// then
assertTrue(resultTrue)
assertFalse(resultFalse)
Assert.assertTrue(resultTrue)
Assert.assertFalse(resultFalse)

// clean
testDirectory.toFile().deleteRecursively()
Expand Down
29 changes: 0 additions & 29 deletions test_runner/src/main/kotlin/ftl/util/Zip.kt

This file was deleted.

0 comments on commit b02f229

Please sign in to comment.