Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.ajalt.clikt.testing

import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.parsers.CommandLineParser
import com.github.ajalt.mordant.input.InputEvent
import com.github.ajalt.mordant.rendering.AnsiLevel
import com.github.ajalt.mordant.terminal.Terminal
import com.github.ajalt.mordant.terminal.TerminalRecorder
Expand Down Expand Up @@ -140,6 +141,7 @@ fun CliktCommand.test(
*
* @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 inputEvents Input events to pass to an interactive command
* @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]
Expand All @@ -150,9 +152,11 @@ fun CliktCommand.test(
* @param outputInteractive Whether the output is interactive
* @param inputInteractive Whether the input is interactive
*/
@JvmOverloads
fun CliktCommand.test(
argv: List<String>,
stdin: String = "",
inputEvents: List<InputEvent> = listOf(),
envvars: Map<String, String> = emptyMap(),
includeSystemEnvvars: Boolean = false,
ansiLevel: AnsiLevel = AnsiLevel.NONE,
Expand All @@ -163,7 +167,7 @@ fun CliktCommand.test(
inputInteractive: Boolean = ansiLevel != AnsiLevel.NONE,
): CliktCommandTestResult {
return test(
argv, stdin, envvars, includeSystemEnvvars, ansiLevel, width,
argv, stdin, inputEvents, envvars, includeSystemEnvvars, ansiLevel, width,
height, hyperlinks, outputInteractive, inputInteractive
) { parse(it) }
}
Expand All @@ -176,6 +180,7 @@ fun CliktCommand.test(
*
* @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 inputEvents Input events to pass to an interactive command
* @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]
Expand All @@ -187,9 +192,11 @@ fun CliktCommand.test(
* @param inputInteractive Whether the input is interactive
* @param parse The function to call to parse the command line and run the command
*/
@JvmOverloads
inline fun <T : BaseCliktCommand<T>> BaseCliktCommand<T>.test(
argv: List<String>,
stdin: String = "",
inputEvents: List<InputEvent> = listOf(),
envvars: Map<String, String> = emptyMap(),
includeSystemEnvvars: Boolean = false,
ansiLevel: AnsiLevel = AnsiLevel.NONE,
Expand All @@ -205,6 +212,7 @@ inline fun <T : BaseCliktCommand<T>> BaseCliktCommand<T>.test(
ansiLevel, width, height, hyperlinks, outputInteractive, inputInteractive
)
recorder.inputLines = stdin.split("\n").toMutableList()
recorder.inputEvents = inputEvents.toMutableList()
configureContext {
val originalReader = readEnvvar
readEnvvar = { envvars[it] ?: (if (includeSystemEnvvars) originalReader(it) else null) }
Expand Down