From 5b7c11ae393877eb3fa78d7a721551a785ee8623 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Fri, 16 Feb 2024 17:45:57 +0100 Subject: [PATCH] [KLIB tool] Introduce a '-test-mode' CLI option This is an option that allows running the commands that support it in a special "test mode". The "test mode" means (but not limited to) that a command may, for example, sort the output which is unsorted by default, and this way guarantee stable output. This is essentially helpful for tests, which rely on the command output. ^KT-62340 --- .../jetbrains/kotlin/cli/klib/KlibToolArguments.kt | 1 + .../kotlin/cli/klib/KlibToolArgumentsParser.kt | 13 ++++++++++++- .../kotlin/cli/klib/KotlinpBasedMetadataDumper.kt | 6 ++++++ .../kotlin/org/jetbrains/kotlin/cli/klib/main.kt | 12 +++--------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt index 577d073a03f..914278a3d27 100644 --- a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt +++ b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArguments.kt @@ -13,4 +13,5 @@ internal class KlibToolArguments( val repository: String?, val printSignatures: Boolean, val signatureVersion: KotlinIrSignatureVersion?, + val testMode: Boolean, ) diff --git a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt index 6da0521a7ff..42de575d13c 100644 --- a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt +++ b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibToolArgumentsParser.kt @@ -41,6 +41,7 @@ internal class KlibToolArgumentsParser { repository = extraArgs[ExtraOption.REPOSITORY]?.last(), printSignatures = extraArgs[ExtraOption.PRINT_SIGNATURES]?.last()?.toBoolean() == true, signatureVersion, + testMode = extraArgs[ExtraOption.INTERNAL_TEST_MODE]?.last()?.toBoolean() == true ) } @@ -105,7 +106,17 @@ internal class KlibToolArgumentsParser { private enum class ExtraOption(val option: String) { REPOSITORY("-repository"), PRINT_SIGNATURES("-print-signatures"), - SIGNATURE_VERSION("-signature-version"); + SIGNATURE_VERSION("-signature-version"), + + /** + * This is an option that allows running the commands that support it in a special "test mode". + * The "test mode" means (but not limited to) that a command may, for example, sort the output + * which is unsorted by default, and this way guarantee stable output. This is essentially helpful + * for tests, which rely on the command output. + * + * NOTE: This option is not supposed to be advertised in KLIB tool's "usage info". + */ + INTERNAL_TEST_MODE("-test-mode"); companion object { fun parseOrNull(option: String): ExtraOption? = entries.firstOrNull { it.option == option } diff --git a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KotlinpBasedMetadataDumper.kt b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KotlinpBasedMetadataDumper.kt index 48280f5bffc..e07474fd1ad 100644 --- a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KotlinpBasedMetadataDumper.kt +++ b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KotlinpBasedMetadataDumper.kt @@ -40,6 +40,12 @@ internal class KotlinpBasedMetadataDumper( private val printer = Printer(output) + /** + * @param testMode if `true` then a special pre-processing is performed towards the metadata before rendering: + * - empty package fragments are removed + * - package fragments with the same package FQN are merged + * - classes are sorted in alphabetical order + */ fun dumpLibrary(library: KotlinLibrary, testMode: Boolean) { val moduleMetadata = loadModuleMetadata(library) .let { originalModuleMetadata -> if (testMode) preprocessMetadataForTests(originalModuleMetadata) else originalModuleMetadata } diff --git a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/main.kt b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/main.kt index 35624b45eab..3d2f26fcc50 100644 --- a/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/main.kt +++ b/kotlin-native/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/main.kt @@ -198,14 +198,8 @@ internal class KlibToolCommand(private val args: KlibToolArguments) { printer.print(module) } - /** - * @param testMode if `true` then a special pre-processing is performed towards the metadata before rendering: - * - empty package fragments are removed - * - package fragments with the same package FQN are merged - * - classes are sorted in alphabetical order - */ - fun dumpMetadata(output: Appendable, testMode: Boolean) { - KotlinpBasedMetadataDumper(output, args.printSignatures, args.signatureVersion).dumpLibrary(libraryInCurrentDir(args.libraryNameOrPath), testMode) + fun dumpMetadata(output: Appendable) { + KotlinpBasedMetadataDumper(output, args.printSignatures, args.signatureVersion).dumpLibrary(libraryInCurrentDir(args.libraryNameOrPath), args.testMode) } fun signatures(output: Appendable) { @@ -258,7 +252,7 @@ fun main(rawArgs: Array) { "dump-abi" -> command.dumpAbi(System.out) "dump-ir" -> command.dumpIr(System.out) "dump-ir-signatures" -> command.dumpIrSignatures(System.out) - "dump-metadata" -> command.dumpMetadata(System.out, testMode = false) + "dump-metadata" -> command.dumpMetadata(System.out) "dump-metadata-signatures" -> command.dumpMetadataSignatures(System.out) "contents" -> command.contents(System.out) "signatures" -> command.signatures(System.out)