jvmMain.com.bkahlert.kommons.test.paths.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kommons-test Show documentation
Show all versions of kommons-test Show documentation
Kommons Test is a Kotlin Multiplatform Library to ease testing.
package com.bkahlert.kommons.test
import com.bkahlert.kommons.test.com.bkahlert.kommons.createTempFile
import java.net.URI
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import java.util.jar.JarOutputStream
import kotlin.io.path.outputStream
/**
* Creates an empty `jar` file in the system's temp directory.
*
* @see toNewJarFileSystem
*/
public fun Path.createTempJarFile(prefix: String? = null, suffix: String? = ".jar"): Path =
createTempFile(prefix, suffix).apply {
JarOutputStream(outputStream().buffered()).use { }
}
/**
* Attempts to create a [FileSystem] from this existing `jar` file.
*
* @see createTempJarFile
*/
public fun Path.toNewJarFileSystem(vararg env: Pair): FileSystem =
FileSystems.newFileSystem(URI.create("jar:${toUri()}"), env.toMap())
/**
* Creates an empty `jar` file system the default temp directory.
*
* @see createTempJarFile
* @see toNewJarFileSystem
*/
public fun Path.createTempJarFileSystem(base: String = "", extension: String = ".jar"): FileSystem =
createTempJarFile(base, extension).toNewJarFileSystem()