Skip to content

Commit

Permalink
Add helper method for loading folder from resources based on variable…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
hfhbd committed Nov 5, 2023
1 parent fb53ff3 commit c415f22
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.copyToRecursively
import kotlin.io.path.div
import kotlin.io.path.toPath
import kotlin.properties.PropertyDelegateProvider
import kotlin.reflect.KProperty

abstract class FixturesTest(
val name: String,
Expand Down Expand Up @@ -113,23 +115,25 @@ abstract class FixturesTest(
}

companion object {
init {
loadFolderFromResources("fixtures", target = File("build"))
}

@JvmStatic
protected val ansiFixtures = File("build/fixtures").listFiles()
.filter { it.isDirectory }
.map { arrayOf(it.name, it) }
protected val ansiFixtures = loadFolderFromResources("fixtures", target = File("build")).toParameter()
}
}

fun File.toParameter(): List<Array<out Any>> =
listFiles()?.filter { it.isDirectory }?.map { arrayOf(it.name, it) } ?: emptyList()

fun loadFolderFromResources(target: File) = object : PropertyDelegateProvider<Any, List<Array<out Any>>> {
override fun provideDelegate(thisRef: Any, property: KProperty<*>): List<Array<out Any>> =
loadFolderFromResources(property.name, target).toParameter()
}

@OptIn(ExperimentalPathApi::class)
fun Any.loadFolderFromResources(path: String, target: File) {
File(target, path).apply { if (exists()) deleteRecursively() }
fun Any.loadFolderFromResources(path: String, target: File): File {
val targetFile = File(target, path).apply { if (exists()) deleteRecursively() }
val resourcesUri = javaClass.getResource("/$path")?.toURI()
requireNotNull(resourcesUri) {
"/$path not found in resources"
"/$path not found in resources."
}
when (resourcesUri.scheme) {
"jar" -> FileSystems.newFileSystem(resourcesUri, emptyMap<String, Nothing>(), null).use {
Expand All @@ -138,6 +142,7 @@ fun Any.loadFolderFromResources(path: String, target: File) {
"file" -> resourcesUri.toPath().copyToRecursively(target.toPath() / path, overwrite = true, followLinks = false)
else -> error("Unsupported scheme ${resourcesUri.scheme} of $resourcesUri")
}
return targetFile
}

private fun formatErrorList(errors: List<String>): String {
Expand Down

0 comments on commit c415f22

Please sign in to comment.