Skip to content

Commit 9b59552

Browse files
committed
Fix 74; envVarsPrefix app file name when not set
1 parent 0dc585a commit 9b59552

9 files changed

Lines changed: 85 additions & 38 deletions

confutils.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,15 @@ when defined(nimscript):
105105
func scriptNameParamIdx: int =
106106
for i in 1 ..< paramCount():
107107
var param = paramStr(i)
108-
if param.len > 0 and param[0] != '-':
108+
if param.len > 0 and param[0] != '-' and param != "e":
109109
return i
110110

111111
proc appInvocation: string =
112112
let scriptNameIdx = scriptNameParamIdx()
113-
"nim " & (if paramCount() > scriptNameIdx: paramStr(scriptNameIdx) else: "<nims-script>")
113+
if paramCount() > scriptNameIdx:
114+
paramStr(scriptNameIdx).splitFile.name
115+
else:
116+
""
114117

115118
type stderr = object
116119

confutils.nimble

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ task test, "Run all tests":
6565
foo = 1
6666
bar = 2
6767
baz = true
68-
arg ./tests/cli_example.nim
6968
arg 42"""
7069
if actualOutput.strip() == expectedOutput:
7170
echo " [OK] tests/cli_example.nim"

tests/help/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
!*/
33
!/*.nim
4+
!/*.nims
45
!.gitignore
56
!README.md
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Usage:
2+
3+
test_cli_example [OPTIONS]... <args>
4+
5+
The following options are available:
6+
7+
--help Show this help message and exit.
8+
--foo
9+
--bar
10+
--withBaz
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Usage:
2+
3+
test_cli_example [OPTIONS]... <args>
4+
5+
The following options are available:
6+
7+
--help Show this help message and exit.
8+
--foo
9+
--bar
10+
--withBaz

tests/help/test_cli_example.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ../../confutils
2+
3+
cli do (foo: int, bar: string, withBaz: bool, args {.argument.}: seq[string]):
4+
echo "foo = ", foo
5+
echo "bar = ", bar
6+
echo "baz = ", withBaz
7+
for arg in args: echo "arg ", arg

tests/help/test_cli_example.nims

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import ./test_cli_example

tests/test_envvar.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ proc testEnvvar() =
6464
check conf.somObject.name.string == "helloObject"
6565
check conf.somObject.isNice.bool == true
6666

67+
test "default envVarsPrefix is app file name":
68+
putEnv("TEST_ENVVAR_DATA_DIR", "ENV VAR DATADIR")
69+
let conf = TestConf.load()
70+
check conf.dataDir.string == "ENV VAR DATADIR"
71+
6772
testEnvvar()

tests/test_help.nim

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,80 +30,91 @@ func cmdsToName(cmds: string): string =
3030
func helpToName(help: string): string =
3131
help.replace(":", "_")
3232

33+
proc execCmdTest(fname, cmdName: string, cmds = "", help = "") =
34+
let res = execCmdEx(fname & " " & cmds & " --help" & help)
35+
let output = res.output.normalizeHelp()
36+
let snapshot = snapshotsPath / cmdName & cmds.cmdsToName() & help.helpToName() & ".txt"
37+
if res.exitCode != 0:
38+
checkpoint "Run output: " & res.output
39+
fail()
40+
elif not fileExists(snapshot):
41+
writeFile(snapshot, output)
42+
checkpoint "Snapshot created: " & snapshot
43+
fail()
44+
else:
45+
let expected = readFile(snapshot).normalizeHelp()
46+
checkpoint "Cmd output: " & $output.toBytes()
47+
checkpoint "Snapshot: " & $expected.toBytes()
48+
check output == expected
49+
50+
const cmdFlags = "--verbosity:0 --hints:off -d:confutilsNoColors"
51+
3352
proc cmdTest(cmdName: string, cmds = "", help = "") =
3453
let fname = helpPath / cmdName
35-
var build = "nim c --verbosity:0 --hints:off -d:confutilsNoColors"
54+
var build = "nim c " & cmdFlags
3655
if NimMajor < 2:
3756
build.add " -d:nimOldCaseObjects"
3857
let buildRes = execCmdEx(build & " " & fname & ".nim")
3958
if buildRes.exitCode != 0:
4059
checkpoint "Build output: " & buildRes.output
4160
fail()
4261
else:
43-
let res = execCmdEx(fname & " " & cmds & " --help" & help)
44-
let output = res.output.normalizeHelp()
45-
let snapshot = snapshotsPath / cmdName & cmds.cmdsToName() & help.helpToName() & ".txt"
46-
if res.exitCode != 0:
47-
checkpoint "Run output: " & res.output
48-
fail()
49-
elif not fileExists(snapshot):
50-
writeFile(snapshot, output)
51-
checkpoint "Snapshot created: " & snapshot
52-
fail()
53-
else:
54-
let expected = readFile(snapshot).normalizeHelp()
55-
checkpoint "Cmd output: " & $output.toBytes()
56-
checkpoint "Snapshot: " & $expected.toBytes()
57-
check output == expected
58-
59-
suite "test --help":
60-
test "test test_nested_cmd":
62+
execCmdTest(fname, cmdName, cmds, help)
63+
64+
suite "help message":
65+
test "test_nested_cmd":
6166
cmdTest("test_nested_cmd", "")
6267

63-
test "test test_nested_cmd lvl1Cmd1":
68+
test "test_nested_cmd lvl1Cmd1":
6469
cmdTest("test_nested_cmd", "lvl1Cmd1")
6570

66-
test "test test_nested_cmd lvl1Cmd1 lvl2Cmd2":
71+
test "test_nested_cmd lvl1Cmd1 lvl2Cmd2":
6772
cmdTest("test_nested_cmd", "lvl1Cmd1 lvl2Cmd2")
6873

69-
test "test test_argument":
74+
test "test_argument":
7075
cmdTest("test_argument", "")
7176

72-
test "test test_argument_abbr":
77+
test "test_argument_abbr":
7378
cmdTest("test_argument_abbr", "")
7479

75-
test "test test_default_value_desc":
80+
test "test_default_value_desc":
7681
cmdTest("test_default_value_desc", "")
7782

78-
test "test test_separator":
83+
test "test_separator":
7984
cmdTest("test_separator", "")
8085

81-
test "test test_longdesc":
86+
test "test_longdesc":
8287
cmdTest("test_longdesc", "")
8388

84-
test "test test_longdesc lvl1Cmd1":
89+
test "test_longdesc lvl1Cmd1":
8590
cmdTest("test_longdesc", "lvl1Cmd1")
8691

87-
test "test test_case_opt":
92+
test "test_case_opt":
8893
cmdTest("test_case_opt", "")
8994

90-
test "test test_case_opt cmdBlockProcessing":
95+
test "test_case_opt cmdBlockProcessing":
9196
cmdTest("test_case_opt", "cmdBlockProcessing")
9297

93-
test "test test_builtins":
98+
test "test_builtins":
9499
cmdTest("test_builtins", "")
95100

96-
test "test test_builtins lvl1Cmd1":
101+
test "test_builtins lvl1Cmd1":
97102
cmdTest("test_builtins", "lvl1Cmd1")
98103

99-
test "test test_debug --help":
104+
test "test_debug --help":
100105
cmdTest("test_debug", "")
101106

102-
test "test test_debug --help:debug":
107+
test "test_debug --help:debug":
103108
cmdTest("test_debug", "", ":debug")
104109

105-
test "test test_debug lvl1Cmd1 --help:debug":
110+
test "test_debug lvl1Cmd1 --help:debug":
106111
cmdTest("test_debug", "lvl1Cmd1", ":debug")
107112

108-
test "test test_dispatch":
113+
test "test_dispatch":
109114
cmdTest("test_dispatch", "")
115+
116+
test "nims test_cli_example":
117+
execCmdTest("nim " & cmdFlags & " " & helpPath / "test_cli_example.nims", "test_cli_example_nims")
118+
119+
test "nims test_cli_example":
120+
execCmdTest("nim e " & cmdFlags & " " & helpPath / "test_cli_example.nim", "test_cli_example")

0 commit comments

Comments
 (0)