[Native][tests] Clean-up in c-interop tests

This commit is contained in:
Dmitriy Dolovov
2023-06-05 11:20:44 +02:00
committed by Space Team
parent 502cbe8b17
commit cf9aa6a360
6 changed files with 30 additions and 60 deletions
@@ -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<KotlinNativeTargets>()
internal val kotlinNativeClassLoader: KotlinNativeClassLoader get() = testRunSettings.get<KotlinNativeClassLoader>()
internal fun TestCase.cinteropToLibrary(): TestCompilationResult<out KLIB> {
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<String>): 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<Timeouts>().executionTimeout),
extras = TestCase.WithTestRunnerExtras(TestRunnerType.DEFAULT)
).apply {
initialize(null, null)
}
}
internal val kotlinNativeClassLoader: KotlinNativeClassLoader get() = testRunSettings.get()
}
@@ -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()
@@ -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
@@ -171,7 +171,7 @@ class LinkerOutputTestKT55578 : AbstractNativeLinkerOutputTest() {
private fun compileKlib(defFile: File, sourceFile: File? = null, extraArgs: List<String> = 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
}
}
@@ -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<out TestCompilationArtifact.KLIBStaticCache> {
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<TestCompilationDependency<*>>
): TestCompilationResult.Success<out TestCompilationArtifact.KLIB> {
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<KotlinNativeTargets>().testTarget.family.exeSuffix))
@@ -161,10 +161,15 @@ internal enum class MutedOption {
}
internal class TestCompilerArgs(val compilerArgs: List<String>) {
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<String>): TestCompilerArgs = TestCompilerArgs(compilerArgs + otherCompilerArgs)
companion object {
val EMPTY = TestCompilerArgs(emptyList())