2022-11-03 16:58:48 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[os, options],
|
|
|
|
stew/results,
|
|
|
|
stew/shims/net as stewNet,
|
|
|
|
testutils/unittests,
|
|
|
|
confutils,
|
|
|
|
confutils/defs,
|
|
|
|
confutils/std/net
|
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/common/confutils/envvar/defs as confEnvvarDefs,
|
|
|
|
waku/common/confutils/envvar/std/net as confEnvvarNet
|
2022-11-03 16:58:48 +00:00
|
|
|
|
|
|
|
type ConfResult[T] = Result[T, string]
|
|
|
|
|
|
|
|
type TestConf = object
|
2024-03-15 23:08:47 +00:00
|
|
|
configFile* {.desc: "Configuration file path", name: "config-file".}:
|
|
|
|
Option[InputFile]
|
|
|
|
|
|
|
|
testFile* {.desc: "Configuration test file path", name: "test-file".}:
|
|
|
|
Option[InputFile]
|
|
|
|
|
|
|
|
listenAddress* {.
|
|
|
|
defaultValue: parseIpAddress("127.0.0.1"),
|
|
|
|
desc: "Listening address",
|
|
|
|
name: "listen-address"
|
|
|
|
.}: IpAddress
|
|
|
|
|
|
|
|
tcpPort* {.desc: "TCP listening port", defaultValue: 60000, name: "tcp-port".}: Port
|
2022-11-03 16:58:48 +00:00
|
|
|
|
|
|
|
{.push warning[ProveInit]: off.}
|
|
|
|
|
|
|
|
proc load*(T: type TestConf, prefix: string): ConfResult[T] =
|
|
|
|
try:
|
|
|
|
let conf = TestConf.load(
|
2024-03-15 23:08:47 +00:00
|
|
|
secondarySources = proc(
|
|
|
|
conf: TestConf, sources: auto
|
|
|
|
) {.gcsafe, raises: [ConfigurationError].} =
|
2022-11-03 16:58:48 +00:00
|
|
|
sources.addConfigFile(Envvar, InputFile(prefix))
|
|
|
|
)
|
|
|
|
ok(conf)
|
|
|
|
except CatchableError:
|
|
|
|
err(getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
|
|
|
suite "nim-confutils - envvar":
|
|
|
|
test "load configuration from environment variables":
|
|
|
|
## Given
|
|
|
|
let prefix = "test-prefix"
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2022-11-03 16:58:48 +00:00
|
|
|
let
|
|
|
|
listenAddress = "1.1.1.1"
|
|
|
|
tcpPort = "8080"
|
|
|
|
configFile = "/tmp/test.conf"
|
|
|
|
|
|
|
|
## When
|
|
|
|
os.putEnv("TEST_PREFIX_CONFIG_FILE", configFile)
|
|
|
|
os.putEnv("TEST_PREFIX_LISTEN_ADDRESS", listenAddress)
|
|
|
|
os.putEnv("TEST_PREFIX_TCP_PORT", tcpPort)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2022-11-03 16:58:48 +00:00
|
|
|
let confLoadRes = TestConf.load(prefix)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check confLoadRes.isOk()
|
|
|
|
|
|
|
|
let conf = confLoadRes.get()
|
|
|
|
check:
|
2023-12-14 06:16:39 +00:00
|
|
|
conf.listenAddress == parseIpAddress(listenAddress)
|
2022-11-03 16:58:48 +00:00
|
|
|
conf.tcpPort == Port(8080)
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2022-11-03 16:58:48 +00:00
|
|
|
conf.configFile.isSome()
|
|
|
|
conf.configFile.get().string == configFile
|
|
|
|
|
2023-09-21 11:12:14 +00:00
|
|
|
conf.testFile.isNone()
|