Skip to content

Commit 77bef51

Browse files
authored
Fix addConfigFileContent regression (#137)
1 parent 9f3e3bd commit 77bef51

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

confutils.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ when hasSerialization:
10261026
): untyped =
10271027
try:
10281028
secondarySources.data.add decode(
1029-
Format, content, type(secondarySources.data[0], params)
1029+
Format, content, typeof(secondarySources.data[0]), params
10301030
)
10311031
except SerializationError as err:
10321032
raise newException(ConfigurationError, err.formatMsg("<content>"), err)
@@ -1254,7 +1254,6 @@ proc loadImpl[C, SecondarySources](
12541254

12551255
template processHelpAndVersionOptions(optKey, optVal: string) =
12561256
let key = optKey
1257-
let val = optVal
12581257
if cmpIgnoreStyle(key, "help") == 0:
12591258
help.showHelp(lazyHelpAppInfo(optVal.toHelpFlags), activeCmds)
12601259
elif version.len > 0 and cmpIgnoreStyle(key, "version") == 0:

tests/test_config_file.nim

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ proc testConfigFile() =
185185

186186
test "basic config file":
187187
let conf = TestConf.load(
188-
envVarsPrefix="prefix",
189-
secondarySources = proc (
190-
config: TestConf, sources: ref SecondarySources
191-
) {.raises: [ConfigurationError].} =
192-
if config.configFile.isSome:
193-
sources.addConfigFile(Toml, config.configFile.get)
194-
else:
195-
sources.addConfigFile(Toml, InputFile(confPathCurrUser / "testVendor" / "testApp.toml"))
196-
sources.addConfigFile(Toml, InputFile(confPathSystemWide / "testVendor" / "testApp.toml"))
188+
envVarsPrefix="prefix",
189+
secondarySources = proc (
190+
config: TestConf, sources: ref SecondarySources
191+
) {.raises: [ConfigurationError].} =
192+
if config.configFile.isSome:
193+
sources.addConfigFile(Toml, config.configFile.get)
194+
else:
195+
sources.addConfigFile(Toml, InputFile(confPathCurrUser / "testVendor" / "testApp.toml"))
196+
sources.addConfigFile(Toml, InputFile(confPathSystemWide / "testVendor" / "testApp.toml"))
197197
)
198198

199199
# dataDir is in env var
@@ -207,4 +207,21 @@ proc testConfigFile() =
207207
check conf.logLevel == "TOML SW DEBUG"
208208
check conf.rpcPort.int == 1235
209209

210+
test "basic config file content":
211+
let currUserCont = readFile(confPathCurrUser / "testVendor" / "testApp.toml")
212+
let sysWideCont = readFile(confPathSystemWide / "testVendor" / "testApp.toml")
213+
let conf = TestConf.load(
214+
envVarsPrefix="prefix",
215+
secondarySources = proc (
216+
config: TestConf, sources: ref SecondarySources
217+
) {.raises: [ConfigurationError].} =
218+
sources.addConfigFileContent(Toml, currUserCont)
219+
sources.addConfigFileContent(Toml, sysWideCont)
220+
)
221+
check conf.dataDir.string == "ENV VAR DATADIR"
222+
check conf.logFile.isSome()
223+
check conf.logFile.get().string == "TOML CU LOGFILE"
224+
check conf.logLevel == "TOML SW DEBUG"
225+
check conf.rpcPort.int == 1235
226+
210227
testConfigFile()

tests/test_nested_cmd.nim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ template loadFile(T, file): untyped =
1919
Toml, InputFile(configFilePath / file), flags = {TomlInlineTableNewline}
2020
)
2121

22+
template loadContent(T, cont): untyped =
23+
proc (
24+
config: T, sources: ref SecondarySources
25+
) {.raises: [ConfigurationError].} =
26+
sources.addConfigFileContentWithParams(
27+
Toml, cont, flags = {TomlInlineTableNewline}
28+
)
29+
2230
type
2331
OuterCmd = enum
2432
noCommand
@@ -148,3 +156,18 @@ suite "test nested cmd toml":
148156
conf.innerCmd == InnerCmd.innerCmd2
149157
conf.outerArg1 == "toml outer-arg1"
150158
conf.innerArg2 == "toml inner-arg2"
159+
160+
test "subcommand outerCmd1 innerCmd2":
161+
let tomlCont = readFile(configFilePath / "nested_cmd.toml")
162+
let conf = TestConf.load(
163+
secondarySources = loadContent(TestConf, tomlCont),
164+
cmdLine = @[
165+
"outerCmd1",
166+
"innerCmd2"
167+
]
168+
)
169+
check:
170+
conf.cmd == OuterCmd.outerCmd1
171+
conf.innerCmd == InnerCmd.innerCmd2
172+
conf.outerArg1 == "toml outer-arg1"
173+
conf.innerArg2 == "toml inner-arg2"

0 commit comments

Comments
 (0)