2023-04-19 09:54:48 +00:00
|
|
|
# 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
|
2023-04-19 09:54:48 +00:00
|
|
|
std/[os, strutils],
|
2023-02-08 01:19:55 +00:00
|
|
|
unittest2,
|
2023-04-19 09:54:48 +00:00
|
|
|
../confutils
|
|
|
|
|
|
|
|
const EnvVarPrefix = "Nimbus"
|
2020-10-22 07:18:08 +00:00
|
|
|
|
2020-10-28 09:17:22 +00:00
|
|
|
type
|
2023-04-19 09:54:48 +00:00
|
|
|
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
|
|
|
|
2023-04-19 09:54:48 +00:00
|
|
|
proc testEnvvar() =
|
|
|
|
suite "env var support suite":
|
2020-10-28 09:17:22 +00:00
|
|
|
|
2023-04-19 09:54:48 +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
|
|
|
|
2023-04-19 09:54:48 +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
|
|
|
|
2023-04-19 09:54:48 +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
|
|
|
|
2023-04-19 09:54:48 +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
|
2023-02-08 01:19:55 +00:00
|
|
|
|
2023-04-19 09:54:48 +00:00
|
|
|
testEnvvar()
|