diff --git a/core/src/testFixtures/kotlin/com/alecstrong/sql/psi/test/fixtures/FixturesTest.kt b/core/src/testFixtures/kotlin/com/alecstrong/sql/psi/test/fixtures/FixturesTest.kt index 9585d591..cb8953c3 100644 --- a/core/src/testFixtures/kotlin/com/alecstrong/sql/psi/test/fixtures/FixturesTest.kt +++ b/core/src/testFixtures/kotlin/com/alecstrong/sql/psi/test/fixtures/FixturesTest.kt @@ -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, @@ -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> = + listFiles()?.filter { it.isDirectory }?.map { arrayOf(it.name, it) } ?: emptyList() + +fun loadFolderFromResources(target: File) = object : PropertyDelegateProvider>> { + override fun provideDelegate(thisRef: Any, property: KProperty<*>): List> = + 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(), null).use { @@ -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 {