From cf9aa6a3600c4d1fa5e4fe06dddbc71358e30e4a Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Mon, 5 Jun 2023 11:20:44 +0200 Subject: [PATCH] [Native][tests] Clean-up in c-interop tests --- .../AbstractNativeCInteropBaseTest.kt | 40 +------------------ .../AbstractNativeCInteropKT39120Test.kt | 11 ++--- .../AbstractNativeCInteropTest.kt | 9 ++--- .../blackboxtest/LinkerOutputTestKT55578.kt | 4 +- .../blackboxtest/NativeSimpleTestUtils.kt | 21 ++++++---- .../blackboxtest/support/TestDirectives.kt | 5 +++ 6 files changed, 30 insertions(+), 60 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropBaseTest.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropBaseTest.kt index 836e0160fdf..dad1162224e 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropBaseTest.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropBaseTest.kt @@ -5,46 +5,8 @@ package org.jetbrains.kotlin.konan.blackboxtest -import org.jetbrains.kotlin.konan.blackboxtest.support.* -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.* -import org.jetbrains.kotlin.konan.blackboxtest.support.compilation.TestCompilationArtifact.* -import org.jetbrains.kotlin.konan.blackboxtest.support.runner.* import org.jetbrains.kotlin.konan.blackboxtest.support.settings.* -import java.io.File abstract class AbstractNativeCInteropBaseTest : AbstractNativeSimpleTest() { - internal val targets: KotlinNativeTargets get() = testRunSettings.get() - internal val kotlinNativeClassLoader: KotlinNativeClassLoader get() = testRunSettings.get() - - internal fun TestCase.cinteropToLibrary(): TestCompilationResult { - modules.singleOrNull() - val compilation = CInteropCompilation( - classLoader = testRunSettings.get(), - targets = targets, - freeCompilerArgs = freeCompilerArgs, - defFile = modules.singleOrNull()!!.files.singleOrNull()!!.location, - expectedArtifact = toLibraryArtifact() - ) - return compilation.result - } - - private fun TestCase.toLibraryArtifact() = KLIB(buildDir.resolve(modules.singleOrNull()!!.name + ".klib")) - - internal fun generateCInteropTestCaseWithSingleDef(defFile: File, extraArgs: List): TestCase { - val moduleName: String = defFile.name - val module = TestModule.Exclusive(moduleName, emptySet(), emptySet(), emptySet()) - module.files += TestFile.createCommitted(defFile, module) - - return TestCase( - id = TestCaseId.Named(moduleName), - kind = TestKind.STANDALONE, - modules = setOf(module), - freeCompilerArgs = TestCompilerArgs(extraArgs), - nominalPackageName = PackageName.EMPTY, - checks = TestRunChecks.Default(testRunSettings.get().executionTimeout), - extras = TestCase.WithTestRunnerExtras(TestRunnerType.DEFAULT) - ).apply { - initialize(null, null) - } - } + internal val kotlinNativeClassLoader: KotlinNativeClassLoader get() = testRunSettings.get() } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropKT39120Test.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropKT39120Test.kt index 537d44f73a9..a2b43515ea7 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropKT39120Test.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropKT39120Test.kt @@ -34,19 +34,16 @@ abstract class AbstractNativeCInteropKT39120Test : AbstractNativeCInteropBaseTes val golden1File = testPathFull.resolve("pod1.contents.gold.txt") val golden2File = testPathFull.resolve("pod2.contents.gold.txt") - val includeFrameworkArgs = listOf("-compiler-option", "-F${testDataDir.canonicalPath}") - - val test1Case: TestCase = generateCInteropTestCaseWithSingleDef(def1File, includeFrameworkArgs) - val klib1: KLIB = test1Case.cinteropToLibrary().assertSuccess().resultingArtifact + val includeFrameworkArgs = TestCompilerArgs("-compiler-option", "-F${testDataDir.canonicalPath}") + val klib1: KLIB = cinteropToLibrary(targets, def1File, buildDir, includeFrameworkArgs).assertSuccess().resultingArtifact val contents1 = klib1.getContents(kotlinNativeClassLoader.classLoader) val expectedFiltered1Output = golden1File.readText() val actualFiltered1Output = filterContentsOutput(contents1, " pod.Version|POD|class Pod") assertEquals(StringUtilRt.convertLineSeparators(expectedFiltered1Output), StringUtilRt.convertLineSeparators(actualFiltered1Output)) - val cinterop2ExtraArgs = listOf("-l", klib1.klibFile.canonicalPath, "-compiler-option", "-fmodules") - val test2Case: TestCase = generateCInteropTestCaseWithSingleDef(def2File, includeFrameworkArgs + cinterop2ExtraArgs) - val klib2: KLIB = test2Case.cinteropToLibrary().assertSuccess().resultingArtifact + val cinterop2ExtraArgs = TestCompilerArgs("-l", klib1.klibFile.canonicalPath, "-compiler-option", "-fmodules") + val klib2: KLIB = cinteropToLibrary(targets, def2File, buildDir, includeFrameworkArgs + cinterop2ExtraArgs).assertSuccess().resultingArtifact val contents2 = klib2.getContents(kotlinNativeClassLoader.classLoader) val expectedFiltered2Output = golden2File.readText() diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropTest.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropTest.kt index 62f60ea5819..43d0f45790d 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropTest.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/AbstractNativeCInteropTest.kt @@ -72,14 +72,13 @@ abstract class AbstractNativeCInteropTest : AbstractNativeCInteropBaseTest() { getBuiltinsGoldenFile(testPathFull) else getGoldenFile(testPathFull) - val fmodulesArgs = if (fmodules) listOf("-compiler-option", "-fmodules") else listOf() + val fmodulesArgs = if (fmodules) TestCompilerArgs("-compiler-option", "-fmodules") else TestCompilerArgs.EMPTY val includeArgs = if (testDataDir.name.startsWith("framework")) - listOf("-compiler-option", "-F${testDataDir.canonicalPath}") + TestCompilerArgs("-compiler-option", "-F${testDataDir.canonicalPath}") else - listOf("-compiler-option", "-I${includeFolder.canonicalPath}") + TestCompilerArgs("-compiler-option", "-I${includeFolder.canonicalPath}") - val testCase: TestCase = generateCInteropTestCaseWithSingleDef(defFile, includeArgs + fmodulesArgs) - val testCompilationResult = testCase.cinteropToLibrary() + val testCompilationResult = cinteropToLibrary(targets, defFile, buildDir, includeArgs + fmodulesArgs) // If we are running fmodules-specific test without -fmodules then we want to be sure that cinterop fails the way we want it to. if (!fmodules && testPath.endsWith("FModules/")) { val loggedData = (testCompilationResult as TestCompilationResult.CompilationToolFailure).loggedData diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/LinkerOutputTestKT55578.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/LinkerOutputTestKT55578.kt index a22f0f3792e..adab8f547bf 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/LinkerOutputTestKT55578.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/LinkerOutputTestKT55578.kt @@ -171,7 +171,7 @@ class LinkerOutputTestKT55578 : AbstractNativeLinkerOutputTest() { private fun compileKlib(defFile: File, sourceFile: File? = null, extraArgs: List = emptyList()): KLIB { val sourceArguments = sourceFile?.let { listOf("-Xcompile-source", sourceFile.absolutePath) } ?: emptyList() - val libraryTestCase: TestCase = generateCInteropTestCaseWithSingleDef(defFile, extraArgs + sourceArguments) - return libraryTestCase.cinteropToLibrary().assertSuccess().resultingArtifact + return cinteropToLibrary(targets, defFile, buildDir, TestCompilerArgs(extraArgs + sourceArguments)) + .assertSuccess().resultingArtifact } } diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeSimpleTestUtils.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeSimpleTestUtils.kt index 861587e3a5e..1255917fdc0 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeSimpleTestUtils.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/NativeSimpleTestUtils.kt @@ -106,6 +106,9 @@ internal fun TestCompilationArtifact.KLIB.asLibraryDependency() = internal fun TestCompilationArtifact.KLIB.asIncludedLibraryDependency() = ExistingDependency(this, TestCompilationDependencyType.IncludedLibrary) +internal fun TestCompilationArtifact.KLIBStaticCache.asStaticCacheDependency() = + ExistingDependency(this, TestCompilationDependencyType.LibraryStaticCache) + internal fun AbstractNativeSimpleTest.compileToLibrary(sourcesDir: File, vararg dependencies: TestCompilationArtifact.KLIB) = compileToLibrary(sourcesDir, buildDir, *dependencies) @@ -176,16 +179,20 @@ internal fun AbstractNativeSimpleTest.compileToExecutable(testCase: TestCase, va internal fun AbstractNativeSimpleTest.compileToStaticCache( klib: TestCompilationArtifact.KLIB, cacheDir: File, -): TestCompilationResult { + vararg dependencies: TestCompilationArtifact.KLIBStaticCache +): TestCompilationArtifact.KLIBStaticCache { val compilation = StaticCacheCompilation( settings = testRunSettings, freeCompilerArgs = TestCompilerArgs.EMPTY, StaticCacheCompilation.Options.Regular, pipelineType = testRunSettings.get(), - dependencies = listOf(klib.asLibraryDependency()), + dependencies = buildList { + this += klib.asLibraryDependency() + dependencies.mapTo(this) { it.asStaticCacheDependency() } + }, expectedArtifact = TestCompilationArtifact.KLIBStaticCache(cacheDir, klib) ) - return compilation.result + return compilation.result.assertSuccess().resultingArtifact } internal fun AbstractNativeSimpleTest.generateTestCaseWithSingleModule( @@ -258,7 +265,7 @@ internal fun AbstractNativeSimpleTest.generateCInteropTestCaseFromSingleDefFile( private fun AbstractNativeSimpleTest.compileToLibrary( testCase: TestCase, - dir: File, + outputDir: File, dependencies: List> ): TestCompilationResult.Success { val compilation = LibraryCompilation( @@ -266,7 +273,7 @@ private fun AbstractNativeSimpleTest.compileToLibrary( freeCompilerArgs = testCase.freeCompilerArgs, sourceModules = testCase.modules, dependencies = dependencies, - expectedArtifact = getLibraryArtifact(testCase, dir) + expectedArtifact = getLibraryArtifact(testCase, outputDir) ) return compilation.result.assertSuccess() } @@ -286,8 +293,8 @@ private fun AbstractNativeSimpleTest.compileToExecutable( return compilation.result } -internal fun getLibraryArtifact(testCase: TestCase, dir: File) = - TestCompilationArtifact.KLIB(dir.resolve(testCase.modules.first().name + ".klib")) +internal fun getLibraryArtifact(testCase: TestCase, outputDir: File) = + TestCompilationArtifact.KLIB(outputDir.resolve(testCase.modules.first().name + ".klib")) private fun AbstractNativeSimpleTest.getExecutableArtifact() = TestCompilationArtifact.Executable(buildDir.resolve("app." + testRunSettings.get().testTarget.family.exeSuffix)) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt index 60e092c77cb..0424e12d47f 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/TestDirectives.kt @@ -161,10 +161,15 @@ internal enum class MutedOption { } internal class TestCompilerArgs(val compilerArgs: List) { + constructor(vararg compilerArgs: String) : this(compilerArgs.asList()) + private val uniqueCompilerArgs = compilerArgs.toSet() override fun hashCode() = uniqueCompilerArgs.hashCode() override fun equals(other: Any?) = (other as? TestCompilerArgs)?.uniqueCompilerArgs == uniqueCompilerArgs + operator fun plus(otherCompilerArgs: TestCompilerArgs): TestCompilerArgs = this + otherCompilerArgs.compilerArgs + operator fun plus(otherCompilerArgs: List): TestCompilerArgs = TestCompilerArgs(compilerArgs + otherCompilerArgs) + companion object { val EMPTY = TestCompilerArgs(emptyList())