Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly error out on bad config files #600

Merged
merged 1 commit into from
Jun 28, 2019
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
37 changes: 27 additions & 10 deletions busted/modules/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,34 @@ return function(options)
end

-- Load busted config file if available
local bustedConfigFilePath = cliArgs.f or path.normpath(path.join(cliArgs.directory, '.busted'))
local bustedConfigFile = loadfile(bustedConfigFilePath)
if bustedConfigFile then
local ok, config = pcall(function()
local conf, err = configLoader(bustedConfigFile(), cliArgsParsed, cliArgs)
return conf or error(err, 0)
end)
if not ok then
return nil, appName .. ': error: ' .. config
local bustedConfigFilePath
if cliArgs.f then
-- if the file is given, then we require it to exist
if not path.isfile(cliArgs.f) then
return nil, ("specified config file '%s' not found"):format(cliArgs.f)
end
bustedConfigFilePath = cliArgs.f
else
-- try default file
bustedConfigFilePath = path.normpath(path.join(cliArgs.directory, '.busted'))
if not path.isfile(bustedConfigFilePath) then
bustedConfigFilePath = nil -- clear default file, since it doesn't exist
end
end
if bustedConfigFilePath then
local bustedConfigFile, err = loadfile(bustedConfigFilePath)
if not bustedConfigFile then
return nil, ("failed loading config file `%s`: %s"):format(bustedConfigFilePath, err)
else
cliArgs = config
local ok, config = pcall(function()
local conf, err = configLoader(bustedConfigFile(), cliArgsParsed, cliArgs)
return conf or error(err, 0)
end)
if not ok then
return nil, appName .. ': error: ' .. config
else
cliArgs = config
end
end
else
cliArgs = tablex.merge(cliArgs, cliArgsParsed, true)
Expand Down
2 changes: 1 addition & 1 deletion spec/.hidden/.busted_bad
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ return {
default = {
['ROOT'] = {'specs'},
['pattern'] = '_spec%.lua$',
['loaders'] = doesnotexist.loaders,
['loaders'] = doesnotexist.loaders, -- errors when run
['verbose'] = true,
},
test = {
Expand Down
14 changes: 14 additions & 0 deletions spec/.hidden/.busted_bad_syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
return {
_all = {
['ROOT'] = {'tests'},
},
default = {
['ROOT'] = {'specs'},
['pattern'] = '_spec%.lua$',
['loaders'] = doesnotexist.loaders,
['verbose'] = true,
},
test = {
['pattern'] = '_test%.lua$',
}
-- } -- missing bracket, errors when compiled
18 changes: 18 additions & 0 deletions spec/cl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ describe('Tests the busted command-line options', function()
assert.is_equal(1, errcnt)
end)

it('tests running a non-existing configfile', function()
local success, errcnt = executeBusted('--config-file=spec/.hidden/.this_one_does_not_exist --pattern=cl_execute_fail.lua$')
assert.is_false(success)
assert.is_equal(0, errcnt)
end)

it('tests running a non-compiling configfile', function()
local success, errcnt = executeBusted('--config-file=spec/.hidden/.busted_bad_syntax --pattern=cl_execute_fail.lua$')
assert.is_false(success)
assert.is_equal(0, errcnt)
end)

it('tests running a configfile throwing errors when being run', function()
local success, errcnt, out, err = executeBusted('--config-file=spec/.hidden/.busted_bad --pattern=cl_execute_fail.lua$')
assert.is_false(success)
assert.is_equal(0, errcnt)
end)

it('tests running with --output specified', function()
local success, errcnt = executeBusted('--pattern=cl_success.lua$ --output=TAP')
assert.is_true(success)
Expand Down