diff --git a/confutils.nim b/confutils.nim index 2c8b6f9..dd57d5c 100644 --- a/confutils.nim +++ b/confutils.nim @@ -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: "") + if paramCount() > scriptNameIdx: + paramStr(scriptNameIdx).splitFile.name + else: + "" type stderr = object diff --git a/confutils.nimble b/confutils.nimble index 73728c3..72d8c0b 100644 --- a/confutils.nimble +++ b/confutils.nimble @@ -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" diff --git a/tests/help/.gitignore b/tests/help/.gitignore index 1cdce7c..a37f45c 100644 --- a/tests/help/.gitignore +++ b/tests/help/.gitignore @@ -1,5 +1,6 @@ /* !*/ !/*.nim +!/*.nims !.gitignore !README.md diff --git a/tests/help/snapshots/test_cli_example.txt b/tests/help/snapshots/test_cli_example.txt new file mode 100644 index 0000000..924e0fe --- /dev/null +++ b/tests/help/snapshots/test_cli_example.txt @@ -0,0 +1,10 @@ +Usage: + +test_cli_example [OPTIONS]... + +The following options are available: + + --help Show this help message and exit. + --foo + --bar + --withBaz \ No newline at end of file diff --git a/tests/help/snapshots/test_cli_example_nims.txt b/tests/help/snapshots/test_cli_example_nims.txt new file mode 100644 index 0000000..924e0fe --- /dev/null +++ b/tests/help/snapshots/test_cli_example_nims.txt @@ -0,0 +1,10 @@ +Usage: + +test_cli_example [OPTIONS]... + +The following options are available: + + --help Show this help message and exit. + --foo + --bar + --withBaz \ No newline at end of file diff --git a/tests/help/test_cli_example.nim b/tests/help/test_cli_example.nim new file mode 100644 index 0000000..55e81e3 --- /dev/null +++ b/tests/help/test_cli_example.nim @@ -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 diff --git a/tests/help/test_cli_example.nims b/tests/help/test_cli_example.nims new file mode 100644 index 0000000..d0af8d7 --- /dev/null +++ b/tests/help/test_cli_example.nims @@ -0,0 +1 @@ +import ./test_cli_example diff --git a/tests/test_envvar.nim b/tests/test_envvar.nim index 1fb4ac8..fb85552 100644 --- a/tests/test_envvar.nim +++ b/tests/test_envvar.nim @@ -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() diff --git a/tests/test_help.nim b/tests/test_help.nim index e9875eb..869a0ba 100644 --- a/tests/test_help.nim +++ b/tests/test_help.nim @@ -30,9 +30,28 @@ 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") @@ -40,72 +59,58 @@ proc cmdTest(cmdName: string, cmds = "", help = "") = 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": @@ -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")