From 92a8dd09df50dde58f12b45d6434a242b8ab5289 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 7 Sep 2023 08:47:24 +0200 Subject: [PATCH] Allow `CliktCommand.test` to take a `vararg` For convenience and to better visualize separate options, allow to write test("--foo", "--bar") in addition to test("--foo --bar") This requires to remove the `test(argv: Array, ...)` overload as that would have the same JVM function signature. --- .../ajalt/clikt/testing/CliktTesting.kt | 30 ++----------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt index a2cabac9..4d438d42 100644 --- a/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt +++ b/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/testing/CliktTesting.kt @@ -40,7 +40,7 @@ data class CliktCommandTestResult( * @param height The height of the terminal */ fun CliktCommand.test( - argv: String, + vararg argv: String, stdin: String = "", envvars: Map = emptyMap(), includeSystemEnvvars: Boolean = false, @@ -48,7 +48,7 @@ fun CliktCommand.test( width: Int = 79, height: Int = 24, ): CliktCommandTestResult { - val argvArray = shlex("test", argv, null) + val argvArray = argv.flatMap { shlex("test", it, null) } return test(argvArray, stdin, envvars, includeSystemEnvvars, ansiLevel, width, height) } @@ -76,32 +76,6 @@ fun CliktCommand.test( ansiLevel: AnsiLevel = AnsiLevel.NONE, width: Int = 79, height: Int = 24, -): CliktCommandTestResult = - test(argv.toTypedArray(), stdin, envvars, includeSystemEnvvars, ansiLevel, width, height) - -/** - * Test this command, returning a result that captures the output and result status code. - * - * Note that only output printed with [echo][CliktCommand.echo] will be captured. Anything printed with [print] or - * [println] is not. - * - * @param argv The command line to send to the command - * @param stdin Content of stdin that will be read by prompt options. Multiple inputs should be separated by `\n`. - * @param envvars A map of environment variable name to value for envvars that can be read by the command - * @param includeSystemEnvvars Set to true to include the environment variables from the system in addition to those - * defined in [envvars] - * @param ansiLevel Defaults to no colored output; set to [AnsiLevel.TRUECOLOR] to include ANSI codes in the output. - * @param width The width of the terminal, used to wrap text - * @param height The height of the terminal - */ -fun CliktCommand.test( - argv: Array, - stdin: String = "", - envvars: Map = emptyMap(), - includeSystemEnvvars: Boolean = false, - ansiLevel: AnsiLevel = AnsiLevel.NONE, - width: Int = 79, - height: Int = 24, ): CliktCommandTestResult { var exitCode = 0 val recorder = TerminalRecorder(ansiLevel, width, height)