Skip to content

Commit 265307c

Browse files
committed
add location to MapValueSource invocations.
1 parent 9f60a19 commit 265307c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

clikt/src/commonMain/kotlin/com/github/ajalt/clikt/sources/MapValueSource.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class MapValueSource(
1919
private val getKey: (Context, Option) -> String = ValueSource.getKey(joinSubcommands = "."),
2020
) : ValueSource {
2121
override fun getValues(context: Context, option: Option): List<ValueSource.Invocation> {
22-
return values[option.valueSourceKey ?: getKey(context, option)]
23-
?.let { ValueSource.Invocation.just(it) }.orEmpty()
22+
val key = option.valueSourceKey ?: getKey(context, option)
23+
return values[key]
24+
?.let { ValueSource.Invocation.just(value = it, location = key) }.orEmpty()
2425
}
2526
}

test/src/commonTest/kotlin/com/github/ajalt/clikt/sources/MapValueSourceTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import com.github.ajalt.clikt.core.subcommands
77
import com.github.ajalt.clikt.parameters.options.Option
88
import com.github.ajalt.clikt.parameters.options.flag
99
import com.github.ajalt.clikt.parameters.options.option
10+
import com.github.ajalt.clikt.parameters.types.int
1011
import com.github.ajalt.clikt.sources.ValueSource.Invocation
1112
import com.github.ajalt.clikt.testing.TestCommand
1213
import com.github.ajalt.clikt.testing.defaultLocalization
14+
import com.github.ajalt.clikt.testing.formattedMessage
1315
import com.github.ajalt.clikt.testing.parse
1416
import io.kotest.assertions.throwables.shouldThrow
1517
import io.kotest.data.blocking.forAll
@@ -102,4 +104,34 @@ class MapValueSourceTest {
102104
}.message shouldBe defaultLocalization.invalidFlagValueInFile("")
103105
}
104106

107+
@Test
108+
fun errorMessage() {
109+
class C : TestCommand() {
110+
@Suppress("unused")
111+
val theInteger by option().int()
112+
113+
@Suppress("unused")
114+
val theFlag by option(valueSourceKey = "that flag").flag()
115+
}
116+
117+
shouldThrow<BadParameterValue> {
118+
val valueSource = MapValueSource(mapOf("the-integer" to "foo"))
119+
C().apply { configureContext { this.valueSource = valueSource } }.parse("")
120+
}.formattedMessage shouldBe "invalid value for the-integer: foo is not a valid integer"
121+
122+
shouldThrow<BadParameterValue> {
123+
val valueSource = MapValueSource(mapOf("that flag" to "foo"))
124+
C().apply { configureContext { this.valueSource = valueSource } }.parse("")
125+
}.formattedMessage shouldBe "invalid value for that flag: foo is not a valid boolean"
126+
127+
shouldThrow<BadParameterValue> {
128+
val valueSource = MapValueSource(mapOf("A_THE_INTEGER" to "foo"), getKey = ValueSource.envvarKey())
129+
C().apply {
130+
configureContext {
131+
this.valueSource = valueSource
132+
this.autoEnvvarPrefix = "A"
133+
}
134+
}.parse("")
135+
}.formattedMessage shouldBe "invalid value for A_THE_INTEGER: foo is not a valid integer"
136+
}
105137
}

test/src/jvmTest/kotlin/com/github/ajalt/clikt/sources/PropertiesValueSourceTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.ajalt.clikt.sources
22

3+
import com.github.ajalt.clikt.core.BadParameterValue
34
import com.github.ajalt.clikt.core.InvalidFileFormat
45
import com.github.ajalt.clikt.core.context
56
import com.github.ajalt.clikt.core.subcommands
@@ -10,6 +11,7 @@ import com.github.ajalt.clikt.parameters.types.double
1011
import com.github.ajalt.clikt.parameters.types.float
1112
import com.github.ajalt.clikt.parameters.types.int
1213
import com.github.ajalt.clikt.testing.TestCommand
14+
import com.github.ajalt.clikt.testing.formattedMessage
1315
import com.github.ajalt.clikt.testing.parse
1416
import io.kotest.assertions.throwables.shouldThrow
1517
import io.kotest.data.blocking.forAll
@@ -19,6 +21,7 @@ import org.junit.Rule
1921
import org.junit.Test
2022
import org.junit.rules.TemporaryFolder
2123
import java.io.File
24+
import java.util.Properties
2225

2326
class PropertiesValueSourceTest {
2427
@get:Rule
@@ -174,4 +177,36 @@ class PropertiesValueSourceTest {
174177

175178
C().parse("")
176179
}
180+
181+
@Test
182+
fun errorMessage() {
183+
class C : TestCommand() {
184+
@Suppress("unused")
185+
val theInteger by option().int()
186+
187+
@Suppress("unused")
188+
val theFlag by option(valueSourceKey = "that flag").flag()
189+
}
190+
191+
shouldThrow<BadParameterValue> {
192+
val valueSource = PropertiesValueSource.from(Properties().apply { setProperty("the-integer", "foo") })
193+
C().apply { configureContext { this.valueSource = valueSource } }.parse("")
194+
}.formattedMessage shouldBe "invalid value for the-integer: foo is not a valid integer"
195+
196+
shouldThrow<BadParameterValue> {
197+
val valueSource = PropertiesValueSource.from(Properties().apply { setProperty("that flag", "foo") })
198+
C().apply { configureContext { this.valueSource = valueSource } }.parse("")
199+
}.formattedMessage shouldBe "invalid value for that flag: foo is not a valid boolean"
200+
201+
shouldThrow<BadParameterValue> {
202+
val properties = Properties().apply { setProperty("A_THE_INTEGER", "foo") }
203+
val valueSource = PropertiesValueSource.from(properties, getKey = ValueSource.envvarKey())
204+
C().apply {
205+
configureContext {
206+
this.valueSource = valueSource
207+
this.autoEnvvarPrefix = "A"
208+
}
209+
}.parse("")
210+
}.formattedMessage shouldBe "invalid value for A_THE_INTEGER: foo is not a valid integer"
211+
}
177212
}

0 commit comments

Comments
 (0)