Skip to content

Commit

Permalink
Merge pull request lunarmodules#521 from o-lim/stdin-compatibility
Browse files Browse the repository at this point in the history
Support running standalone tests from stdin
  • Loading branch information
DorianGray committed Jun 7, 2016
2 parents eb6367e + 2734e08 commit 1a7bfe6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 37 deletions.
1 change: 1 addition & 0 deletions busted-2.0.rc12-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ build = {

['busted.modules.configuration_loader'] = 'busted/modules/configuration_loader.lua',
['busted.modules.luacov'] = 'busted/modules/luacov.lua',
['busted.modules.standalone_loader'] = 'busted/modules/standalone_loader.lua',
['busted.modules.test_file_loader'] = 'busted/modules/test_file_loader.lua',
['busted.modules.output_handler_loader'] = 'busted/modules/output_handler_loader.lua',
['busted.modules.helper_loader'] = 'busted/modules/helper_loader.lua',
Expand Down
1 change: 1 addition & 0 deletions busted-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ build = {

['busted.modules.configuration_loader'] = 'busted/modules/configuration_loader.lua',
['busted.modules.luacov'] = 'busted/modules/luacov.lua',
['busted.modules.standalone_loader'] = 'busted/modules/standalone_loader.lua',
['busted.modules.test_file_loader'] = 'busted/modules/test_file_loader.lua',
['busted.modules.output_handler_loader'] = 'busted/modules/output_handler_loader.lua',
['busted.modules.helper_loader'] = 'busted/modules/helper_loader.lua',
Expand Down
4 changes: 2 additions & 2 deletions busted/compatibility.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ return {
loadstring = loadstring or load,
unpack = table.unpack or unpack,

exit = function(code)
if code ~= 0 and _VERSION:match('^Lua 5%.[12]$') then
exit = function(code, force)
if not force and code ~= 0 and _VERSION:match('^Lua 5%.[12]$') then
error()
elseif code ~= 0 then
code = 1
Expand Down
2 changes: 1 addition & 1 deletion busted/modules/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ return function(options)
local configLoader = require 'busted.modules.configuration_loader'()

-- Default cli arg values
local defaultOutput = options.defaultOutput
local defaultOutput = options.output or 'utfTerminal'
local defaultLoaders = 'lua,moonscript'
local defaultPattern = '_spec'
local defaultSeed = '/dev/urandom or os.time()'
Expand Down
28 changes: 28 additions & 0 deletions busted/modules/standalone_loader.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local getTrace = function(filename, info)
local index = info.traceback:find('\n%s*%[C]')
info.traceback = info.traceback:sub(1, index)
return info
end

return function(busted)
local loadCurrentFile = function(info, options)
local filename = 'string'
if info.source:sub(1,1) == '@' or info.source:sub(1,1) == '=' then
filename = info.source:sub(2)
end

-- Setup test file to be compatible with live coding
if info.func then
local file = setmetatable({
getTrace = getTrace,
rewriteMessage = nil
}, {
__call = info.func
})

busted.executors.file(filename, file)
end
end

return loadCurrentFile
end
2 changes: 1 addition & 1 deletion busted/options.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
return {
standalone = true,
defaultOutput = 'utfTerminal',
output = nil,
}
49 changes: 24 additions & 25 deletions busted/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ local loadstring = require 'busted.compatibility'.loadstring
local loaded = false

return function(options)
if loaded then return else loaded = true end
if loaded then return function() end else loaded = true end

local isatty = io.type(io.stdout) == 'file' and term.isatty(io.stdout)
options = tablex.update(require 'busted.options', options or {})
options.defaultOutput = isatty and 'utfTerminal' or 'plainTerminal'
options.output = options.output or (isatty and 'utfTerminal' or 'plainTerminal')

local busted = require 'busted.core'()

Expand All @@ -29,28 +29,29 @@ return function(options)
local level = 2
local info = debug.getinfo(level, 'Sf')
local source = info.source
local fileName = source:sub(1,1) == '@' and source:sub(2) or source
local fileName = source:sub(1,1) == '@' and source:sub(2) or nil
local forceExit = fileName == nil

-- Parse the cli arguments
local appName = path.basename(fileName)
local appName = path.basename(fileName or 'busted')
cli:set_name(appName)
local cliArgs, err = cli:parse(arg)
if not cliArgs then
io.stderr:write(err .. '\n')
exit(1)
exit(1, forceExit)
end

if cliArgs.version then
-- Return early if asked for the version
print(busted.version)
exit(0)
exit(0, forceExit)
end

-- Load current working directory
local _, err = path.chdir(path.normpath(cliArgs.directory))
if err then
io.stderr:write(appName .. ': error: ' .. err .. '\n')
exit(1)
exit(1, forceExit)
end

-- If coverage arg is passed in, load LuaCovsupport
Expand Down Expand Up @@ -123,7 +124,7 @@ return function(options)

-- Set up output handler to listen to events
outputHandlerLoader(busted, cliArgs.output, {
defaultOutput = options.defaultOutput,
defaultOutput = options.output,
enableSound = cliArgs['enable-sound'],
verbose = cliArgs.verbose,
suppressPending = cliArgs['suppress-pending'],
Expand Down Expand Up @@ -152,22 +153,20 @@ return function(options)
suppressPending = cliArgs['suppress-pending'],
})

-- Load test directory
local rootFiles = cliArgs.ROOT or { fileName }
local patterns = cliArgs.pattern
local testFileLoader = require 'busted.modules.test_file_loader'(busted, cliArgs.loaders)
testFileLoader(rootFiles, patterns, {
excludes = cliArgs['exclude-pattern'],
verbose = cliArgs.verbose,
recursive = cliArgs['recursive'],
})

-- If running standalone, setup test file to be compatible with live coding
if options.standalone then
local ctx = busted.context.get()
local children = busted.context.children(ctx)
local file = children[#children]
debug.getmetatable(file.run).__call = info.func
if cliArgs.ROOT then
-- Load test directories/files
local rootFiles = cliArgs.ROOT
local patterns = cliArgs.pattern
local testFileLoader = require 'busted.modules.test_file_loader'(busted, cliArgs.loaders)
testFileLoader(rootFiles, patterns, {
excludes = cliArgs['exclude-pattern'],
verbose = cliArgs.verbose,
recursive = cliArgs['recursive'],
})
else
-- Running standalone, use standalone loader
local testFileLoader = require 'busted.modules.standalone_loader'(busted)
testFileLoader(info, { verbose = cliArgs.verbose })
end

local runs = cliArgs['repeat']
Expand All @@ -181,6 +180,6 @@ return function(options)
busted.publish({ 'exit' })

if options.standalone or failures > 0 or errors > 0 then
exit(failures + errors)
exit(failures + errors, forceExit)
end
end
6 changes: 6 additions & 0 deletions spec/cl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ describe('Test busted running standalone', function()
local success, errcnt = executeLua('spec/cl_standalone.lua --help')
assert.is_false(success)
end)

it('tests running with via stdin', function()
local success, errcnt = executeLua('< spec/cl_standalone.lua')
assert.is_false(success)
assert.is_equal(3, errcnt)
end)
end)

describe('Test busted command-line runner', function()
Expand Down
16 changes: 8 additions & 8 deletions spec/modules/cli_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Tests command-line interface', function()
local defaultOutput = 'default_output_handler'
local lpath = './src/?.lua;./src/?/?.lua;./src/?/init.lua'
local cpath = path.is_windows and './csrc/?.dll;./csrc/?/?.dll;' or './csrc/?.so;./csrc/?/?.so;'
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
local args = cli:parse({})
assert.is_equal(defaultOutput, args.o)
assert.is_equal(defaultOutput, args.output)
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('Tests using .busted tasks', function()
local defaultOutput = 'default_output_handler'
local lpath = './src/?.lua;./src/?/?.lua;./src/?/init.lua'
local cpath = path.is_windows and './csrc/?.dll;./csrc/?/?.dll;' or './csrc/?.so;./csrc/?/?.so;'
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
local args = cli:parse({ '--directory=spec/.hidden' })
assert.is_equal(defaultOutput, args.o)
assert.is_equal(defaultOutput, args.output)
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('Tests using .busted tasks', function()
local defaultOutput = 'default_output_handler'
local lpath = './src/?.lua;./src/?/?.lua;./src/?/init.lua'
local cpath = path.is_windows and './csrc/?.dll;./csrc/?/?.dll;' or './csrc/?.so;./csrc/?/?.so;'
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
local args = cli:parse({ '--config-file', 'spec/.hidden/.busted' })
assert.is_equal(defaultOutput, args.o)
assert.is_equal(defaultOutput, args.output)
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('Tests using .busted tasks', function()
end)

it('load configuration options', function()
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
local args = cli:parse({ '--directory=spec/.hidden', '--run=test' })
assert.is_same({'_test1%.lua$', '_test2%.lua$'}, args.pattern)
assert.is_same({'_exclude1', '_exclude2'}, args['exclude-pattern'])
Expand All @@ -459,7 +459,7 @@ describe('Tests using .busted tasks', function()
end)

it('load configuration options and override with command-line', function()
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
local args = cli:parse({ '--directory=spec/.hidden', '--run=test', '-t', 'tag1', '-p', 'patt', '--filter=fin', '--filter-out=fout', '--exclude-pattern', '', '--loaders=moonscript' })
assert.is_same({'patt'}, args.pattern)
assert.is_same({''}, args['exclude-pattern'])
Expand All @@ -473,7 +473,7 @@ describe('Tests using .busted tasks', function()
end)

it('detects error in configuration file', function()
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
cli:set_name('app')
local args, err = cli:parse({ '--config-file=spec/.hidden/.busted_bad', '--run=test' })
assert.is_nil(args)
Expand All @@ -483,15 +483,15 @@ describe('Tests using .busted tasks', function()
end)

it('detects invalid configuration file', function()
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
cli:set_name('myapp')
local args, err = cli:parse({ '--config-file=spec/.hidden/.busted_empty' })
assert.is_nil(args)
assert.is_equal('myapp: error: .busted file does not return a table.', err)
end)

it('detects unknown/invalid task', function()
local cli = require 'busted.modules.cli'({ standalone = false, defaultOutput = defaultOutput })
local cli = require 'busted.modules.cli'({ standalone = false, output = defaultOutput })
cli:set_name('appname')
local args, err = cli:parse({ '--config-file=spec/.hidden/.busted', '--run=invalid' })
assert.is_nil(args)
Expand Down

0 comments on commit 1a7bfe6

Please sign in to comment.