Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions confutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ when defined(nimscript):
func scriptNameParamIdx: int =
for i in 1 ..< paramCount():
var param = paramStr(i)
if param.len > 0 and param[0] != '-':
if param.len > 0 and param[0] != '-' and param != "e":
return i

proc appInvocation: string =
let scriptNameIdx = scriptNameParamIdx()
"nim " & (if paramCount() > scriptNameIdx: paramStr(scriptNameIdx) else: "<nims-script>")
if paramCount() > scriptNameIdx:
paramStr(scriptNameIdx).splitFile.name
else:
""

type stderr = object

Expand Down
1 change: 0 additions & 1 deletion confutils.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ task test, "Run all tests":
foo = 1
bar = 2
baz = true
arg ./tests/cli_example.nim
arg 42"""
if actualOutput.strip() == expectedOutput:
echo " [OK] tests/cli_example.nim"
Expand Down
1 change: 1 addition & 0 deletions tests/help/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
!*/
!/*.nim
!/*.nims
!.gitignore
!README.md
10 changes: 10 additions & 0 deletions tests/help/snapshots/test_cli_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage:

test_cli_example [OPTIONS]... <args>

The following options are available:

--help Show this help message and exit.
--foo
--bar
--withBaz
10 changes: 10 additions & 0 deletions tests/help/snapshots/test_cli_example_nims.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage:

test_cli_example [OPTIONS]... <args>

The following options are available:

--help Show this help message and exit.
--foo
--bar
--withBaz
7 changes: 7 additions & 0 deletions tests/help/test_cli_example.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ../../confutils

cli do (foo: int, bar: string, withBaz: bool, args {.argument.}: seq[string]):
echo "foo = ", foo
echo "bar = ", bar
echo "baz = ", withBaz
for arg in args: echo "arg ", arg
1 change: 1 addition & 0 deletions tests/help/test_cli_example.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ./test_cli_example
10 changes: 10 additions & 0 deletions tests/test_envvar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ proc testEnvvar() =
let conf = TestConf.load(envVarsPrefix=EnvVarPrefix)
check conf.numList == @[123, 456, 789]

test "default envVarsPrefix is app file name":
const prefix =
when isMainModule:
"TEST_ENVVAR"
else:
"TEST_ALL"
putEnv(prefix & "_DATA_DIR", "ENV VAR DATADIR")
let conf = TestConf.load()
check conf.dataDir.string == "ENV VAR DATADIR"

testEnvvar()
81 changes: 46 additions & 35 deletions tests/test_help.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,87 @@ func cmdsToName(cmds: string): string =
func helpToName(help: string): string =
help.replace(":", "_")

proc execCmdTest(fname, cmdName: string, cmds = "", help = "") =
let res = execCmdEx(fname & " " & cmds & " --help" & help)
let output = res.output.normalizeHelp()
let snapshot = snapshotsPath / cmdName & cmds.cmdsToName() & help.helpToName() & ".txt"
if res.exitCode != 0:
checkpoint "Run output: " & res.output
fail()
elif not fileExists(snapshot):
writeFile(snapshot, output)
checkpoint "Snapshot created: " & snapshot
fail()
else:
let expected = readFile(snapshot).normalizeHelp()
checkpoint "Cmd output: " & $output.toBytes()
checkpoint "Snapshot: " & $expected.toBytes()
check output == expected

const cmdFlags = "--verbosity:0 --hints:off -d:confutilsNoColors"

proc cmdTest(cmdName: string, cmds = "", help = "") =
let fname = helpPath / cmdName
var build = "nim c --verbosity:0 --hints:off -d:confutilsNoColors"
var build = "nim c " & cmdFlags
if NimMajor < 2:
build.add " -d:nimOldCaseObjects"
let buildRes = execCmdEx(build & " " & fname & ".nim")
if buildRes.exitCode != 0:
checkpoint "Build output: " & buildRes.output
fail()
else:
let res = execCmdEx(fname & " " & cmds & " --help" & help)
let output = res.output.normalizeHelp()
let snapshot = snapshotsPath / cmdName & cmds.cmdsToName() & help.helpToName() & ".txt"
if res.exitCode != 0:
checkpoint "Run output: " & res.output
fail()
elif not fileExists(snapshot):
writeFile(snapshot, output)
checkpoint "Snapshot created: " & snapshot
fail()
else:
let expected = readFile(snapshot).normalizeHelp()
checkpoint "Cmd output: " & $output.toBytes()
checkpoint "Snapshot: " & $expected.toBytes()
check output == expected

suite "test --help":
test "test test_nested_cmd":
execCmdTest(fname, cmdName, cmds, help)

suite "help message":
test "test_nested_cmd":
cmdTest("test_nested_cmd", "")

test "test test_nested_cmd lvl1Cmd1":
test "test_nested_cmd lvl1Cmd1":
cmdTest("test_nested_cmd", "lvl1Cmd1")

test "test test_nested_cmd lvl1Cmd1 lvl2Cmd2":
test "test_nested_cmd lvl1Cmd1 lvl2Cmd2":
cmdTest("test_nested_cmd", "lvl1Cmd1 lvl2Cmd2")

test "test test_argument":
test "test_argument":
cmdTest("test_argument", "")

test "test test_argument_abbr":
test "test_argument_abbr":
cmdTest("test_argument_abbr", "")

test "test test_default_value_desc":
test "test_default_value_desc":
cmdTest("test_default_value_desc", "")

test "test test_separator":
test "test_separator":
cmdTest("test_separator", "")

test "test test_longdesc":
test "test_longdesc":
cmdTest("test_longdesc", "")

test "test test_longdesc lvl1Cmd1":
test "test_longdesc lvl1Cmd1":
cmdTest("test_longdesc", "lvl1Cmd1")

test "test test_case_opt":
test "test_case_opt":
cmdTest("test_case_opt", "")

test "test test_case_opt cmdBlockProcessing":
test "test_case_opt cmdBlockProcessing":
cmdTest("test_case_opt", "cmdBlockProcessing")

test "test test_builtins":
test "test_builtins":
cmdTest("test_builtins", "")

test "test test_builtins lvl1Cmd1":
test "test_builtins lvl1Cmd1":
cmdTest("test_builtins", "lvl1Cmd1")

test "test test_debug --help":
test "test_debug --help":
cmdTest("test_debug", "")

test "test test_debug --help:debug":
test "test_debug --help:debug":
cmdTest("test_debug", "", ":debug")

test "test test_debug lvl1Cmd1 --help:debug":
test "test_debug lvl1Cmd1 --help:debug":
cmdTest("test_debug", "lvl1Cmd1", ":debug")

test "test test_dispatch":
test "test_dispatch":
cmdTest("test_dispatch", "")

test "test test_default_cmd_desc":
Expand All @@ -119,3 +124,9 @@ suite "test --help":

test "test test_multi_case_values":
cmdTest("test_multi_case_values")

test "nims test_cli_example":
execCmdTest("nim " & cmdFlags & " " & helpPath / "test_cli_example.nims", "test_cli_example_nims")

test "nims test_cli_example":
execCmdTest("nim e " & cmdFlags & " " & helpPath / "test_cli_example.nim", "test_cli_example")