Skip to content

Commit 12e4ebc

Browse files
deiningalerque
authored andcommitted
test: Add test cases for INI file loading
1 parent 8608dc3 commit 12e4ebc

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

spec/core_spec.lua

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,115 @@ describe("cliargs::core", function()
177177
end)
178178
end)
179179
end)
180+
181+
182+
describe('#read_defaults_from_ini_file', function()
183+
local args, err
184+
185+
before_each(function()
186+
cli:option('-c, --compress=VALUE', '...', 'lzma')
187+
cli:flag('-q, --quiet', '...', false)
188+
local config
189+
190+
config, err = cli:read_defaults('spec/fixtures/config.ini')
191+
192+
assert.equal(err, nil)
193+
assert.same(config, {
194+
compress = 'bz2',
195+
quiet = true,
196+
})
197+
198+
if config and not err then
199+
cli:load_defaults(config)
200+
end
201+
end)
202+
203+
it('works', function()
204+
args, err = cli:parse({})
205+
206+
assert.equal(err, nil)
207+
assert.same(args, {
208+
c = 'bz2',
209+
compress = 'bz2',
210+
q = true,
211+
quiet = true
212+
})
213+
end)
214+
end)
215+
216+
217+
describe('#read_defaults_from_ini_file_group_no_cast', function()
218+
local args, err
219+
220+
before_each(function()
221+
cli:option('-h, --host=VALUE', '...', '127.0.0.1')
222+
cli:option('-p, --port=VALUE', '...', 8088)
223+
224+
local config
225+
226+
config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true)
227+
228+
assert.equal(err, nil)
229+
assert.same(config, {
230+
host = 'localhost',
231+
port = 5432,
232+
})
233+
234+
if config and not err then
235+
cli:load_defaults(config)
236+
end
237+
end)
238+
239+
it('works', function()
240+
args, err = cli:parse({})
241+
242+
assert.equal(err, nil)
243+
assert.same(args, {
244+
h = 'localhost',
245+
host = 'localhost',
246+
p = 5432,
247+
port = 5432,
248+
})
249+
end)
250+
end)
251+
252+
253+
describe('#read_defaults_from_ini_file_group_with_cast', function()
254+
local args, err
255+
256+
before_each(function()
257+
cli:option('-h, --host=VALUE', '...', '127.0.0.1')
258+
cli:option('-p, --port=VALUE', '...', 8088)
259+
260+
local config
261+
262+
-- failing test case for #64 below
263+
-- config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', false)
264+
265+
-- intermediate: prevent failure with Travis CI
266+
config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true)
267+
268+
assert.equal(err, nil)
269+
assert.same(config, {
270+
host = 'localhost',
271+
port = 5432,
272+
})
273+
274+
if config and not err then
275+
cli:load_defaults(config)
276+
end
277+
end)
278+
279+
it('works', function()
280+
args, err = cli:parse({})
281+
282+
assert.equal(err, nil)
283+
assert.same(args, {
284+
h = 'localhost',
285+
host = 'localhost',
286+
p = 5432,
287+
port = 5432,
288+
})
289+
end)
290+
end)
180291
end)

spec/fixtures/config.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[cli]
22
quiet = true
3-
compress = bz2
3+
compress = bz2
4+
[database]
5+
host=localhost
6+
port=5432

src/cliargs/config_loader.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ return {
7070
--- example:
7171
---
7272
--- [cli]
73-
--- quiet=true
74-
--- compress=lzma
73+
--- quiet = true
74+
--- compress = lzma
7575
---
7676
--- The routine will automatically cast boolean values ("true" and "false")
7777
--- into Lua booleans. You may opt out of this behavior by passing `false`

0 commit comments

Comments
 (0)