Skip to content

Commit 9554252

Browse files
committed
Fix 74; envVarsPrefix app file name when not set
1 parent aa7f92b commit 9554252

9 files changed

Lines changed: 90 additions & 38 deletions

confutils.nim

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

115115
proc appInvocation: string =
116116
let scriptNameIdx = scriptNameParamIdx()
117-
"nim " & (if paramCount() > scriptNameIdx: paramStr(scriptNameIdx) else: "<nims-script>")
117+
if paramCount() > scriptNameIdx:
118+
paramStr(scriptNameIdx).splitFile.name
119+
else:
120+
""
118121

119122
type stderr = object
120123

confutils.nimble

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ task test, "Run all tests":
6767
foo = 1
6868
bar = 2
6969
baz = true
70-
arg ./tests/cli_example.nim
7170
arg 42"""
7271
if actualOutput.strip() == expectedOutput:
7372
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,14 @@ proc testEnvvar() =
9494
let conf = TestConf.load(envVarsPrefix=EnvVarPrefix)
9595
check conf.numList == @[123, 456, 789]
9696

97+
test "default envVarsPrefix is app file name":
98+
const prefix =
99+
when isMainModule:
100+
"TEST_ENVVAR"
101+
else:
102+
"TEST_ALL"
103+
putEnv(prefix & "_DATA_DIR", "ENV VAR DATADIR")
104+
let conf = TestConf.load()
105+
check conf.dataDir.string == "ENV VAR DATADIR"
106+
97107
testEnvvar()

tests/test_help.nim

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,82 +30,87 @@ 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", "")
110115

111116
test "test test_default_cmd_desc":
@@ -119,3 +124,9 @@ suite "test --help":
119124

120125
test "test test_multi_case_values":
121126
cmdTest("test_multi_case_values")
127+
128+
test "nims test_cli_example":
129+
execCmdTest("nim " & cmdFlags & " " & helpPath / "test_cli_example.nims", "test_cli_example_nims")
130+
131+
test "nims test_cli_example":
132+
execCmdTest("nim e " & cmdFlags & " " & helpPath / "test_cli_example.nim", "test_cli_example")

0 commit comments

Comments
 (0)