[KLIB] Native K1 & K2 tests for dumping KLIB ABI
^KT-54402
This commit is contained in:
committed by
Space Team
parent
ab9b91180f
commit
ac3c000ab1
+21
@@ -266,6 +266,27 @@ fun main() {
|
||||
model("diagnostics/nativeTests", excludedPattern = CUSTOM_TEST_DATA_EXTENSION_PATTERN)
|
||||
}
|
||||
}
|
||||
|
||||
generateTestGroupSuiteWithJUnit5 {
|
||||
testGroup("native/native.tests/tests-gen", "compiler/util-klib-abi/testData") {
|
||||
testClass<AbstractNativeLibraryAbiReaderTest>(
|
||||
suiteTestClassName = "NativeLibraryAbiReaderTest"
|
||||
) {
|
||||
model("content", targetBackend = TargetBackend.NATIVE)
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("native/native.tests/tests-gen", "compiler/util-klib-abi/testData") {
|
||||
testClass<AbstractNativeLibraryAbiReaderTest>(
|
||||
suiteTestClassName = "FirNativeLibraryAbiReaderTest",
|
||||
annotations = listOf(
|
||||
*frontendFir()
|
||||
)
|
||||
) {
|
||||
model("content", targetBackend = TargetBackend.NATIVE)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.konan.blackboxtest
|
||||
|
||||
import org.jetbrains.kotlin.konan.blackboxtest.support.Location
|
||||
import org.jetbrains.kotlin.konan.blackboxtest.support.TestCompilerArgs
|
||||
import org.jetbrains.kotlin.konan.blackboxtest.support.TestDirectives
|
||||
import org.jetbrains.kotlin.konan.blackboxtest.support.TestDirectives.MODULE
|
||||
import org.jetbrains.kotlin.konan.blackboxtest.support.parseModule
|
||||
import org.jetbrains.kotlin.konan.blackboxtest.support.util.getAbsoluteFile
|
||||
import org.jetbrains.kotlin.library.abi.*
|
||||
import org.jetbrains.kotlin.library.abi.directives.LibraryAbiDumpDirectives
|
||||
import org.jetbrains.kotlin.library.abi.directives.LibraryAbiDumpDirectives.EXCLUDED_CLASSES
|
||||
import org.jetbrains.kotlin.library.abi.directives.LibraryAbiDumpDirectives.EXCLUDED_PACKAGES
|
||||
import org.jetbrains.kotlin.library.abi.directives.LibraryAbiDumpDirectives.NON_PUBLIC_MARKERS
|
||||
import org.jetbrains.kotlin.test.directives.model.ComposedDirectivesContainer
|
||||
import org.jetbrains.kotlin.test.services.JUnit5Assertions
|
||||
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEqualsToFile
|
||||
import org.jetbrains.kotlin.test.services.impl.RegisteredDirectivesParser
|
||||
import org.jetbrains.kotlin.test.services.impl.RegisteredDirectivesParser.ParsedDirective
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
|
||||
import org.jetbrains.kotlin.utils.fileUtils.withReplacedExtensionOrNull
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.Tag
|
||||
import java.io.File
|
||||
|
||||
@Tag("klib")
|
||||
@OptIn(ExperimentalLibraryAbiReader::class)
|
||||
abstract class AbstractNativeLibraryAbiReaderTest : AbstractNativeSimpleTest() {
|
||||
fun runTest(localPath: String) {
|
||||
val (sourceFile, dumpFiles) = computeTestFiles(localPath)
|
||||
val (moduleName, filters) = parseDirectives(sourceFile)
|
||||
|
||||
val library = compileToLibrary(
|
||||
testCase = generateTestCaseWithSingleFile(
|
||||
sourceFile = sourceFile,
|
||||
moduleName = moduleName,
|
||||
freeCompilerArgs = TestCompilerArgs("-Xcontext-receivers")
|
||||
)
|
||||
).resultingArtifact.klibFile
|
||||
|
||||
val libraryAbi = LibraryAbiReader.readAbiInfo(library, filters)
|
||||
|
||||
dumpFiles.entries.forEach { (signatureVersion, dumpFile) ->
|
||||
val abiDump = LibraryAbiRenderer.render(
|
||||
libraryAbi,
|
||||
AbiRenderingSettings(signatureVersion)
|
||||
)
|
||||
|
||||
assertEqualsToFile(dumpFile, abiDump)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private fun computeTestFiles(localPath: String): Pair<File, Map<AbiSignatureVersion, File>> {
|
||||
val sourceFile = getAbsoluteFile(localPath)
|
||||
assertEquals("kt", sourceFile.extension) { "Invalid source file: $sourceFile" }
|
||||
assertTrue(sourceFile.isFile) { "Source file does not exist: $sourceFile" }
|
||||
|
||||
return sourceFile to AbiSignatureVersion.allSupportedByAbiReader.associateWith { signatureVersion ->
|
||||
val dumpFile = sourceFile.withReplacedExtensionOrNull("kt", "v${signatureVersion.versionNumber}.txt")!!
|
||||
assertTrue(dumpFile.isFile) { "Dump file does not exist: $dumpFile" }
|
||||
dumpFile
|
||||
}
|
||||
}
|
||||
|
||||
internal data class FromDirectives(val moduleName: String, val filters: List<AbiReadingFilter>)
|
||||
|
||||
internal fun parseDirectives(sourceFile: File): FromDirectives {
|
||||
val directivesParser = RegisteredDirectivesParser(
|
||||
ComposedDirectivesContainer(LibraryAbiDumpDirectives, TestDirectives),
|
||||
JUnit5Assertions
|
||||
)
|
||||
sourceFile.forEachLine(action = directivesParser::parse)
|
||||
|
||||
val registeredDirectives = directivesParser.build()
|
||||
|
||||
val moduleName = parseModule(
|
||||
ParsedDirective(MODULE, registeredDirectives[MODULE]),
|
||||
Location(sourceFile)
|
||||
).name
|
||||
|
||||
val excludedPackages = registeredDirectives[EXCLUDED_PACKAGES]
|
||||
val excludedClasses = registeredDirectives[EXCLUDED_CLASSES]
|
||||
val nonPublicMarkers = registeredDirectives[NON_PUBLIC_MARKERS]
|
||||
|
||||
return FromDirectives(
|
||||
moduleName = moduleName,
|
||||
filters = listOfNotNull(
|
||||
excludedPackages.ifNotEmpty(AbiReadingFilter::ExcludedPackages),
|
||||
excludedClasses.ifNotEmpty(AbiReadingFilter::ExcludedClasses),
|
||||
nonPublicMarkers.ifNotEmpty(AbiReadingFilter::NonPublicMarkerAnnotations)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -221,11 +221,11 @@ internal fun AbstractNativeSimpleTest.generateTestCaseWithSingleModule(
|
||||
|
||||
internal fun AbstractNativeSimpleTest.generateTestCaseWithSingleFile(
|
||||
sourceFile: File,
|
||||
moduleName: String = sourceFile.name,
|
||||
freeCompilerArgs: TestCompilerArgs = TestCompilerArgs.EMPTY,
|
||||
testKind: TestKind = TestKind.STANDALONE,
|
||||
extras: TestCase.Extras = TestCase.WithTestRunnerExtras(TestRunnerType.DEFAULT)
|
||||
): TestCase {
|
||||
val moduleName: String = sourceFile.name ?: LAUNCHER_MODULE_NAME
|
||||
val module = TestModule.Exclusive(moduleName, emptySet(), emptySet(), emptySet())
|
||||
module.files += TestFile.createCommitted(sourceFile, module)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user