nim-confutils/tests/test_envvar.nim

68 lines
2.0 KiB
Nim
Raw Normal View History

# nim-confutils
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
# * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
2020-10-22 07:18:08 +00:00
import
std/[os, strutils],
unittest2,
../confutils
const EnvVarPrefix = "Nimbus"
2020-10-22 07:18:08 +00:00
2020-10-28 09:17:22 +00:00
type
SomeObject = object
name: string
isNice: bool
TestConf* = object
logLevel* {.
defaultValue: "DEBUG"
desc: "Sets the log level."
name: "log-level" }: string
somObject* {.
desc: "..."
defaultValue: SomeObject()
name: "object" }: SomeObject
dataDir* {.
defaultValue: ""
desc: "The directory where nimbus will store all blockchain data"
abbr: "d"
name: "data-dir" }: OutDir
func completeCmdArg(T: type SomeObject, val: string): seq[string] =
@[]
func parseCmdArg(T: type SomeObject, p: string): T =
let parsedString = p.split('-')
SomeObject(name:parsedString[0], isNice: parseBool(parsedString[1]))
2020-10-28 09:17:22 +00:00
proc testEnvvar() =
suite "env var support suite":
2020-10-28 09:17:22 +00:00
test "env vars are loaded":
putEnv("NIMBUS_DATA_DIR", "ENV VAR DATADIR")
let conf = TestConf.load(envVarsPrefix=EnvVarPrefix)
check conf.dataDir.string == "ENV VAR DATADIR"
2020-10-28 09:17:22 +00:00
test "env vars do not have priority over cli parameters":
putEnv("NIMBUS_DATA_DIR", "ENV VAR DATADIR")
putEnv("NIMBUS_LOG_LEVEL", "ERROR")
2020-10-28 09:17:22 +00:00
let conf = TestConf.load(@["--log-level=INFO"], envVarsPrefix=EnvVarPrefix)
check conf.dataDir.string == "ENV VAR DATADIR"
check conf.logLevel.string == "INFO"
2020-10-28 09:17:22 +00:00
test "env vars use parseCmdArg":
putEnv("NIMBUS_OBJECT", "helloObject-true")
let conf = TestConf.load(envVarsPrefix=EnvVarPrefix)
check conf.somObject.name.string == "helloObject"
check conf.somObject.isNice.bool == true
testEnvvar()