From 83a7379cc446118179bda7efa602f6931b972254 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Mon, 6 Dec 2021 18:54:48 +0300 Subject: [PATCH] [Native][tests] Fix: Provide correct path to K/N stdlib in compiled tests --- .../blackboxtest/NativeStdlibBlackBoxTest.kt | 3 +- .../support/NativeBlackBoxTestSupport.kt | 16 +++-- .../group/PredefinedTestCaseGroupProvider.kt | 63 ++++++++++--------- .../support/group/PredefinedTestCases.kt | 8 ++- 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeStdlibBlackBoxTest.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeStdlibBlackBoxTest.kt index 416d24faadc..7ef6a0d06de 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeStdlibBlackBoxTest.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeStdlibBlackBoxTest.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.konan.blackboxtest import org.jetbrains.kotlin.konan.blackboxtest.support.TestCaseId +import org.jetbrains.kotlin.konan.blackboxtest.support.group.PredefinedPaths.KOTLIN_NATIVE_DISTRIBUTION import org.jetbrains.kotlin.konan.blackboxtest.support.group.PredefinedTestCase as TC import org.jetbrains.kotlin.konan.blackboxtest.support.group.PredefinedTestCases import org.junit.jupiter.api.Tag @@ -44,4 +45,4 @@ class NativeStdlibBlackBoxTest : AbstractNativeBlackBoxTest() { fun nativeStdlib() = dynamicTestCase(TestCaseId.Named("nativeStdlib")) } -private const val KOTLIN_NATIVE_STDLIB_PATH = "kotlin-native/dist/klib/common/stdlib" +private const val KOTLIN_NATIVE_STDLIB_PATH = "$KOTLIN_NATIVE_DISTRIBUTION/klib/common/stdlib" diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt index d94d8bffde4..26e74578c19 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeBlackBoxTestSupport.kt @@ -8,11 +8,7 @@ package org.jetbrains.kotlin.konan.blackboxtest.support import org.jetbrains.kotlin.konan.blackboxtest.AbstractNativeBlackBoxTest import org.jetbrains.kotlin.konan.blackboxtest.support.group.TestCaseGroupProvider import org.jetbrains.kotlin.konan.blackboxtest.support.settings.* -import org.jetbrains.kotlin.konan.blackboxtest.support.settings.GeneratedSources import org.jetbrains.kotlin.konan.blackboxtest.support.settings.GlobalSettings -import org.jetbrains.kotlin.konan.blackboxtest.support.settings.Settings -import org.jetbrains.kotlin.konan.blackboxtest.support.settings.TestRoots -import org.jetbrains.kotlin.konan.blackboxtest.support.settings.TestSettings import org.jetbrains.kotlin.konan.blackboxtest.support.util.* import org.jetbrains.kotlin.test.TestMetadata import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals @@ -154,21 +150,23 @@ class NativeBlackBoxTestSupport : BeforeEachCallback { testSettings: TestSettings, testSettingsAnnotation: Annotation? ): TestCaseGroupProvider { - // Try to find a constructor that accepts the annotation. + // Try to find a constructor that accepts the setting and the annotation. if (testSettingsAnnotation != null) { val testSettingsAnnotationClass = testSettingsAnnotation.annotationClass testSettings.providerClass.constructors.asSequence() .forEach { c -> - val p = c.parameters.singleOrNull() ?: return@forEach - if (p.hasTypeOf(testSettingsAnnotationClass)) return c.call(testSettingsAnnotation).cast() + val (p1, p2) = c.parameters.takeIf { it.size == 2 } ?: return@forEach + if (p1.hasTypeOf() && p2.hasTypeOf(testSettingsAnnotationClass)) + return c.call(settings, testSettingsAnnotation).cast() } } - // ... or settings at least. + // ... or the settings at least. testSettings.providerClass.constructors.asSequence() .forEach { c -> val p = c.parameters.singleOrNull() ?: return@forEach - if (p.hasTypeOf()) return c.call(settings).cast() + if (p.hasTypeOf()) + return c.call(settings).cast() } fail { "No suitable constructor for ${testSettings.providerClass}" } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCaseGroupProvider.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCaseGroupProvider.kt index e00c1903cba..50f8460f03a 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCaseGroupProvider.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCaseGroupProvider.kt @@ -7,10 +7,8 @@ package org.jetbrains.kotlin.konan.blackboxtest.support.group import org.jetbrains.kotlin.konan.blackboxtest.support.* import org.jetbrains.kotlin.konan.blackboxtest.support.TestCase.WithTestRunnerExtras -import org.jetbrains.kotlin.konan.blackboxtest.support.TestCaseGroup -import org.jetbrains.kotlin.konan.blackboxtest.support.TestCaseGroupId -import org.jetbrains.kotlin.konan.blackboxtest.support.TestFile -import org.jetbrains.kotlin.konan.blackboxtest.support.TestModule +import org.jetbrains.kotlin.konan.blackboxtest.support.settings.GlobalSettings +import org.jetbrains.kotlin.konan.blackboxtest.support.settings.Settings import org.jetbrains.kotlin.konan.blackboxtest.support.util.ThreadSafeFactory import org.jetbrains.kotlin.konan.blackboxtest.support.util.expandGlobTo import org.jetbrains.kotlin.konan.blackboxtest.support.util.getAbsoluteFile @@ -19,7 +17,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.cast import org.junit.jupiter.api.fail import java.io.File -internal class PredefinedTestCaseGroupProvider(annotation: PredefinedTestCases) : TestCaseGroupProvider { +internal class PredefinedTestCaseGroupProvider(private val settings: Settings, annotation: PredefinedTestCases) : TestCaseGroupProvider { private val testCaseIdToPredefinedTestCase: Map = buildMap { annotation.testCases.forEach { predefinedTestCase -> val testCaseId = TestCaseId.Named(predefinedTestCase.name) @@ -76,31 +74,38 @@ internal class PredefinedTestCaseGroupProvider(annotation: PredefinedTestCases) return lazyTestCaseGroups[testCaseGroupId.cast()] } - companion object { - private fun Array.expandGlobs(noExpandedFilesErrorMessage: () -> String): Set { - val files = buildSet { - this@expandGlobs.forEach { pathPattern -> expandGlobTo(getAbsoluteFile(pathPattern), this) } + private fun Array.expandGlobs(noExpandedFilesErrorMessage: () -> String): Set { + val files = buildSet { + this@expandGlobs.forEach { pathPattern -> + expandGlobTo(getAbsoluteFile(substituteRealPaths(pathPattern)), this) } - assertTrue(files.isNotEmpty(), noExpandedFilesErrorMessage) - return files + } + assertTrue(files.isNotEmpty(), noExpandedFilesErrorMessage) + return files + } + + private fun Array.parseCompilerArgs(parsingErrorMessage: () -> String): TestCompilerArgs = + if (isEmpty()) + TestCompilerArgs.EMPTY + else { + val freeCompilerArgs = map { arg -> substituteRealPaths(arg) } + val forbiddenCompilerArgs = TestCompilerArgs.findForbiddenArgs(freeCompilerArgs) + assertTrue(forbiddenCompilerArgs.isEmpty()) { + """ + ${parsingErrorMessage()} + + Forbidden compiler arguments found: $forbiddenCompilerArgs + All arguments: $this + """.trimIndent() + } + + TestCompilerArgs(freeCompilerArgs) } - private fun Array.parseCompilerArgs(parsingErrorMessage: () -> String): TestCompilerArgs = - if (isEmpty()) - TestCompilerArgs.EMPTY - else { - val freeCompilerArgs = asList() - val forbiddenCompilerArgs = TestCompilerArgs.findForbiddenArgs(freeCompilerArgs) - assertTrue(forbiddenCompilerArgs.isEmpty()) { - """ - ${parsingErrorMessage()} - - Forbidden compiler arguments found: $forbiddenCompilerArgs - All arguments: $this - """.trimIndent() - } - - TestCompilerArgs(freeCompilerArgs) - } - } + private fun substituteRealPaths(value: String): String = + if ('$' in value) { + // N.B. Here, more substitutions can be supported in the future if it would be necessary. + value.replace(PredefinedPaths.KOTLIN_NATIVE_DISTRIBUTION, settings.get().kotlinNativeHome.path) + } else + value } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCases.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCases.kt index a753addf4de..ae1f049da79 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCases.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/group/PredefinedTestCases.kt @@ -9,7 +9,11 @@ import org.jetbrains.kotlin.konan.blackboxtest.support.settings.TestSettings @Target(AnnotationTarget.CLASS) @TestSettings(providerClass = PredefinedTestCaseGroupProvider::class) -annotation class PredefinedTestCases(vararg val testCases: PredefinedTestCase) +internal annotation class PredefinedTestCases(vararg val testCases: PredefinedTestCase) @Target() -annotation class PredefinedTestCase(val name: String, val freeCompilerArgs: Array, val sourceLocations: Array) +internal annotation class PredefinedTestCase(val name: String, val freeCompilerArgs: Array, val sourceLocations: Array) + +internal object PredefinedPaths { + const val KOTLIN_NATIVE_DISTRIBUTION = "\$KOTLIN_NATIVE_DISTRIBUTION\$" +}