mirror of
https://github.com/apple/pkl.git
synced 2026-05-25 16:19:20 +02:00
Fix stream double-consumption in CommandSpecParser (#1448)
The `choices` stream was consumed eagerly for metavar construction, then captured in a lambda for later validation—which promptly fell over with `IllegalStateException`. Materialise to a `List` straightaway.
This commit is contained in:
@@ -787,4 +787,31 @@ class CommandSpecParserTest {
|
||||
.contains("Option `foo` with annotation `@CountedFlag` has invalid type `String`.")
|
||||
assertThat(exc.message).contains("Expected type: `Int`")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `union typed option validates invalid choice without stream error`() {
|
||||
val moduleUri =
|
||||
writePklFile(
|
||||
"cmd.pkl",
|
||||
renderOptions +
|
||||
"""
|
||||
class Options {
|
||||
format: "json" | "yaml" | "toml"
|
||||
}
|
||||
"""
|
||||
.trimIndent(),
|
||||
)
|
||||
|
||||
val spec = parse(moduleUri)
|
||||
val flag = spec.options.first() as CommandSpec.Flag
|
||||
|
||||
assertThat(flag.metavar()).isEqualTo("[json, toml, yaml]")
|
||||
|
||||
val apply =
|
||||
assertThrows<CommandSpec.Option.BadValue> {
|
||||
flag.transformEach().apply("xml", URI("file:///tmp"))
|
||||
}
|
||||
assertThat(apply.message).contains("invalid choice")
|
||||
assertThat(apply.message).contains("xml")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user