[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
This commit is contained in:
Dmitriy Dolovov
2024-02-16 17:45:57 +01:00
committed by Space Team
parent 82115c1f26
commit 5b7c11ae39
4 changed files with 22 additions and 10 deletions
@@ -13,4 +13,5 @@ internal class KlibToolArguments(
val repository: String?,
val printSignatures: Boolean,
val signatureVersion: KotlinIrSignatureVersion?,
val testMode: Boolean,
)
@@ -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 }
@@ -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 }
@@ -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<String>) {
"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)