forked from sksamuel/hoplite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support loading configs via prefix (sksamuel#412)
- Loading branch information
1 parent
c5ea754
commit ef0aa5b
Showing
5 changed files
with
201 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
hoplite-core/src/test/kotlin/com/sksamuel/hoplite/PrefixTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.sksamuel.hoplite | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.extensions.system.withEnvironment | ||
import io.kotest.matchers.shouldBe | ||
|
||
class PrefixTest : FunSpec() { | ||
init { | ||
|
||
test("reads config from string at a given prefix") { | ||
data class TestConfig(val a: String, val b: Int) | ||
|
||
val config = ConfigLoaderBuilder.default() | ||
.addPropertySource( | ||
PropertySource.string( | ||
""" | ||
foo.a = A value | ||
foo.b = 42 | ||
bar.a = A value bar | ||
bar.b = 45 | ||
""".trimIndent(), "props" | ||
) | ||
) | ||
.build() | ||
.loadConfigOrThrow<TestConfig>(prefix = "foo") | ||
|
||
config shouldBe TestConfig("A value", 42) | ||
} | ||
|
||
test("reads config from input stream at a given prefix") { | ||
data class TestConfig(val a: String, val b: Int) | ||
|
||
val stream = """ | ||
foo.a = A value | ||
foo.b = 42 | ||
bar.a = A value bar | ||
bar.b = 45 | ||
""".trimIndent().byteInputStream(Charsets.UTF_8) | ||
|
||
val config = ConfigLoaderBuilder.default() | ||
.addPropertySource(PropertySource.stream(stream, "props")) | ||
.build() | ||
.loadConfigOrThrow<TestConfig>(prefix = "foo") | ||
|
||
config shouldBe TestConfig("A value", 42) | ||
} | ||
|
||
test("reads config from map at a given prefix") { | ||
data class TestConfig(val a: String, val b: Int, val other: List<String>) | ||
|
||
val arguments = mapOf( | ||
"foo.a" to "A value", | ||
"foo.b" to "42", | ||
"bar.a" to "A value bar", | ||
"bar.b" to "45", | ||
"foo.other" to listOf("Value1", "Value2"), | ||
"bar.other" to listOf("Value1bar", "Value2bar") | ||
) | ||
|
||
val config = ConfigLoaderBuilder.default() | ||
.addPropertySource(PropertySource.map(arguments)) | ||
.build() | ||
.loadConfigOrThrow<TestConfig>(prefix = "foo") | ||
|
||
config shouldBe TestConfig("A value", 42, listOf("Value1", "Value2")) | ||
} | ||
|
||
test("reads config from command line at a given prefix") { | ||
data class TestConfig(val a: String, val b: Int, val other: List<String>) | ||
|
||
val arguments = arrayOf( | ||
"--foo.a=A value", | ||
"--foo.b=42", | ||
"--bar.a=A value bar", | ||
"--bar.b=45", | ||
"some other value", | ||
"--foo.other=Value1", | ||
"--foo.other=Value2", | ||
"--bar.other=Value1bar", | ||
"--bar.other=Value2bar", | ||
"--other=Value1o", | ||
"--other=Value2o" | ||
) | ||
|
||
val config = ConfigLoaderBuilder.default() | ||
.addPropertySource(PropertySource.commandLine(arguments)) | ||
.build() | ||
.loadConfigOrThrow<TestConfig>(prefix = "foo") | ||
|
||
config shouldBe TestConfig("A value", 42, listOf("Value1", "Value2")) | ||
} | ||
|
||
test("reads from added source before default sources at a given prefix") { | ||
data class TestConfig(val a: String, val b: Int, val other: List<String>) | ||
|
||
withEnvironment(mapOf("foo.b" to "91", "foo.other" to "Random13")) { | ||
|
||
val arguments = arrayOf( | ||
"--foo.a=A value", | ||
"--foo.b=42", | ||
"--bar.a=A value bar", | ||
"--bar.b=45", | ||
"some other value", | ||
"--foo.other=Value1", | ||
"--foo.other=Value2", | ||
"--bar.other=Value1bar", | ||
"--bar.other=Value2bar", | ||
"--other=Value1o", | ||
"--other=Value2o" | ||
) | ||
|
||
val config = ConfigLoaderBuilder.default() | ||
.addPropertySource(PropertySource.commandLine(arguments)) | ||
.addDefaultPropertySources() | ||
.addEnvironmentSource() | ||
.build() | ||
.loadConfigOrThrow<TestConfig>(prefix = "foo") | ||
|
||
config shouldBe TestConfig("A value", 42, listOf("Value1", "Value2")) | ||
} | ||
} | ||
|
||
test("reads from default source before specified at a given prefix") { | ||
data class TestConfig(val a: String, val b: Int, val other: List<String>) | ||
|
||
withEnvironment(mapOf("foo.b" to "91", "foo.other" to "Random13")) { | ||
val arguments = arrayOf( | ||
"--foo.a=A value", | ||
"--foo.b=42", | ||
"--bar.a=A value bar", | ||
"--bar.b=45", | ||
"some other value", | ||
"--foo.other=Value1", | ||
"--foo.other=Value2", | ||
"--bar.other=Value1bar", | ||
"--bar.other=Value2bar", | ||
"--other=Value1o", | ||
"--other=Value2o" | ||
) | ||
|
||
val config = ConfigLoaderBuilder.default() | ||
.addEnvironmentSource() | ||
.addDefaultPropertySources() | ||
.addPropertySource(PropertySource.commandLine(arguments)) | ||
.build() | ||
.loadConfigOrThrow<TestConfig>(prefix = "foo") | ||
|
||
config shouldBe TestConfig("A value", 91, listOf("Random13")) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters