Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored Oct 2, 2023
2 parents 0366fc8 + d661506 commit 0655f79
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.ajalt.clikt.parsers.shlex
import com.github.ajalt.mordant.rendering.AnsiLevel
import com.github.ajalt.mordant.terminal.Terminal
import com.github.ajalt.mordant.terminal.TerminalRecorder
import kotlin.jvm.JvmName

data class CliktCommandTestResult(
/** Standard output captured from the command */
Expand Down Expand Up @@ -52,6 +53,33 @@ fun CliktCommand.test(
return test(argvArray, 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
*/
@JvmName("varargTest")
fun CliktCommand.test(
vararg argv: String,
stdin: String = "",
envvars: Map<String, String> = emptyMap(),
includeSystemEnvvars: Boolean = false,
ansiLevel: AnsiLevel = AnsiLevel.NONE,
width: Int = 79,
height: Int = 24,
): CliktCommandTestResult {
return test(argv.asList(), stdin, envvars, includeSystemEnvvars, ansiLevel, width, height)
}

/**
* Test this command, returning a result that captures the output and result status code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,7 @@ object PropertiesValueSource {
requireValid: Boolean = false,
getKey: (Context, Option) -> String = ValueSource.getKey(joinSubcommands = "."),
): ValueSource {
val properties = Properties()
if (file.isFile) {
try {
file.bufferedReader().use { properties.load(it) }
} catch (e: Throwable) {
if (requireValid) throw InvalidFileFormat(
file.name,
e.message ?: "could not read file"
)
}
}

return from(properties, getKey)
return from(file.toPath(), requireValid, getKey)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ main function directly from within gradle. You can pass command line arguments t
with the `--args` flag:

```shell
$ ./gradlew run --args="hello --count=3 --name=Clikt"
$ ./gradlew run --args="--count=3 Clikt"
```

A drawback to using the `run` gradle task is that it redirects stdout, so Clikt will not print
Expand Down
2 changes: 1 addition & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ You can set environment variables for your command by passing in a map of `envva
@Test
fun testHello() {
val command = Hello()
val result = command.test(""", envvars=mapOf("HELLO_NAME" to "Foo"))
val result = command.test("", envvars=mapOf("HELLO_NAME" to "Foo"))
assertEqual(result.stdout, "Hello, Foo!")
}
```
Expand Down
3 changes: 2 additions & 1 deletion samples/json/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# JSON Configuration files

This example shows how to read option values from JSON files using the kotlinx.serialization library.
This example shows how to read option values from JSON files using the `kotlinx.serialization`
library. It reads config files from the `config.json` file located in this directory.

```
./runsample json subcommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import com.github.ajalt.clikt.core.InvalidFileFormat
import com.github.ajalt.clikt.parameters.options.Option
import com.github.ajalt.clikt.sources.ValueSource
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.*
import java.io.File

/**
Expand All @@ -27,10 +24,17 @@ class JsonValueSource(
}
if (cursor == null) return emptyList()

// This implementation interprets a list as multiple invocations, but you could also
// implement it as a single invocation with multiple values.
if (cursor is JsonArray) return cursor.map { ValueSource.Invocation.value(it) }
return ValueSource.Invocation.just(cursor)
try {
// This implementation interprets a list as multiple invocations, but you could also
// implement it as a single invocation with multiple values.
if (cursor is JsonArray) return cursor.map {
ValueSource.Invocation.value(it.jsonPrimitive.content)
}
return ValueSource.Invocation.just(cursor.jsonPrimitive.content)
} catch (e: IllegalArgumentException) {
// This implementation skips invalid values, but you could handle them differently.
return emptyList()
}
}

companion object {
Expand All @@ -41,12 +45,16 @@ class JsonValueSource(
Json.parseToJsonElement(file.readText()) as? JsonObject
?: throw InvalidFileFormat(file.path, "object expected", 1)
} catch (e: SerializationException) {
if (requireValid) throw InvalidFileFormat(file.name, e.message ?: "could not read file")
if (requireValid) {
throw InvalidFileFormat(file.name, e.message ?: "could not read file")
}
JsonObject(emptyMap())
}
return JsonValueSource(json)
}

fun from(file: String, requireValid: Boolean = false): JsonValueSource = from(File(file), requireValid)
fun from(file: String, requireValid: Boolean = false): JsonValueSource {
return from(File(file), requireValid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Cli : CliktCommand(help = "An example using json files for configuration v
init {
context {
valueSources(
JsonValueSource.from(System.getProperty("user.dir") + "config.json"),
JsonValueSource.from(System.getProperty("user.dir") + "/config.json"),
JsonValueSource.from(System.getProperty("user.dir") + "/samples/json/config.json")
)
}
Expand Down

0 comments on commit 0655f79

Please sign in to comment.