From d6adc321d145e0b6229e839c76aeccd33151636a Mon Sep 17 00:00:00 2001 From: Yingtao Liu Date: Fri, 3 Nov 2023 21:36:14 -0700 Subject: [PATCH] change loader logic --- .../PartiQLSchemaInferencerTests.kt | 5 +-- partiql-planner/build.gradle.kts | 35 +++++++++++++++++ .../planner/test/PartiQLTestProvider.kt | 38 ++++++++++++++----- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/partiql-lang/src/test/kotlin/org/partiql/lang/planner/transforms/PartiQLSchemaInferencerTests.kt b/partiql-lang/src/test/kotlin/org/partiql/lang/planner/transforms/PartiQLSchemaInferencerTests.kt index c278c91d3b..50a30082e7 100644 --- a/partiql-lang/src/test/kotlin/org/partiql/lang/planner/transforms/PartiQLSchemaInferencerTests.kt +++ b/partiql-lang/src/test/kotlin/org/partiql/lang/planner/transforms/PartiQLSchemaInferencerTests.kt @@ -54,6 +54,7 @@ import kotlin.test.assertNotNull import kotlin.test.assertTrue class PartiQLSchemaInferencerTests { + private val provider = PartiQLTestProvider() init { // load test inputs @@ -130,9 +131,7 @@ class PartiQLSchemaInferencerTests { fun testCaseWhens(tc: TestCase) = runTest(tc) companion object { - private val provider = PartiQLTestProvider - - private val root = PartiQLTestProvider.javaClass.getResource("/catalogs/default")!!.toURI().toPath().pathString + private val root = PartiQLTestProvider().javaClass.getResource("/catalogs/default")!!.toURI().toPath().pathString private val PLUGINS = listOf(LocalPlugin()) diff --git a/partiql-planner/build.gradle.kts b/partiql-planner/build.gradle.kts index 5cade1ebb4..c940b93074 100644 --- a/partiql-planner/build.gradle.kts +++ b/partiql-planner/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.dokka.utilities.relativeTo + /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * @@ -34,6 +36,39 @@ dependencies { testFixturesImplementation(project(":partiql-spi")) } +tasks.register("generateResourcePath") { + doLast { + val resourceDir = file("src/testFixtures/resources") + val outDir = File("$buildDir/resources/testFixtures") + val fileName = "resource_path.txt" + val pathFile = File(outDir, fileName) + println(pathFile.absolutePath) + if (pathFile.exists()) { + println("exists") + pathFile.writeText("") // clean up existing text + } + println("if statement completed") + resourceDir.walk().forEach { file -> + println(file) + if (!file.isDirectory) { + if (file.extension == "ion" || file.extension == "sql") { + val toAppend = file.toURI().relativeTo(resourceDir.toURI()) + pathFile.appendText("$toAppend\n") + } + } + } + + sourceSets { + testFixtures { + resources { + this.srcDirs += pathFile + } + } + } + } +} + tasks.processTestResources { + dependsOn("generateResourcePath") from("src/testFixtures/resources") } diff --git a/partiql-planner/src/testFixtures/kotlin/org/partiql/planner/test/PartiQLTestProvider.kt b/partiql-planner/src/testFixtures/kotlin/org/partiql/planner/test/PartiQLTestProvider.kt index bd231075f3..7d583210e8 100644 --- a/partiql-planner/src/testFixtures/kotlin/org/partiql/planner/test/PartiQLTestProvider.kt +++ b/partiql-planner/src/testFixtures/kotlin/org/partiql/planner/test/PartiQLTestProvider.kt @@ -15,13 +15,13 @@ package org.partiql.planner.test import java.io.File +import java.io.InputStream import java.nio.file.Path -import kotlin.io.path.toPath /** * The PartiQLTestProvider is a simple utility for indexing SQL statements within files for re-use across library tests. */ -object PartiQLTestProvider { +class PartiQLTestProvider { /** * Backing map for test input lookup. @@ -29,18 +29,34 @@ object PartiQLTestProvider { private val map: MutableMap = mutableMapOf() /** - * Default database of test inputs. + * Retrieve of path information. + * This is primarily for Non-IDE build, i.e., GITHUB build. Where resource are loading from jar. + * It is very tricky to retrieve files from jar. */ - private val default = this::class.java.getResource("/inputs")!!.toURI().toPath() + private val default = { this::class.java.getResourceAsStream("/resource_path.txt")!! } /** * Load test groups from a directory. */ public fun load(root: Path? = null) { - val dir = (root ?: default).toFile() - dir.listFiles { f -> f.isDirectory }!!.map { - for (test in load(it)) { - map[test.key] = test + if (root != null) { + val dir = root.toFile() + dir.listFiles { f -> f.isDirectory }!!.map { + for (test in load(it)) { + map[test.key] = test + } + } + } else { + default().reader().forEachLine { path -> + val pathSteps = path.split("/") + val outMostDir = pathSteps.first() + if (outMostDir == "inputs") { + val group = pathSteps[pathSteps.size - 2] + val resource = this::class.java.getResourceAsStream("/$path")!! + for (test in load(group, resource)) { + map[test.key] = test + } + } } } } @@ -66,11 +82,13 @@ object PartiQLTestProvider { private fun load(dir: File) = dir.listFiles()!!.flatMap { load(dir.name, it) } // load all tests in a file - private fun load(group: String, file: File): List { + private fun load(group: String, file: File): List = load(group, file.inputStream()) + + private fun load(group: String, inputStream: InputStream): List { val tests = mutableListOf() var name = "" val statement = StringBuilder() - for (line in file.readLines()) { + for (line in inputStream.reader().readLines()) { // start of test if (line.startsWith("--#[") and line.endsWith("]")) {