Skip to content

Commit

Permalink
Add output handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ajacksified committed Jun 25, 2014
1 parent 2bdcac3 commit eb1d967
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/busted
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ local outputHandlerOptions = {
deferPrint = cliArgs['defer-print']
}

outputHandler = outputHandlerLoader(cliArgs.output, cliArgs.o, outputHandlerOptions)
outputHandler = outputHandlerLoader(cliArgs.output, cliArgs.o, outputHandlerOptions, busted)

busted.subscribe({ 'test', 'start' }, outputHandler.testStart)
busted.subscribe({ 'test', 'end' }, outputHandler.testEnd)
Expand Down
5 changes: 5 additions & 0 deletions busted-2.0.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ build = {
['busted.modules.files.terra'] = 'busted/modules/files/terra.lua',

['busted.outputHandlers.utfTerminal'] = 'busted/outputHandlers/utfTerminal.lua',
['busted.outputHandlers.plainTerminal'] = 'busted/outputHandlers/plainTerminal.lua',
['busted.outputHandlers.TAP'] = 'busted/outputHandlers/TAP.lua',
['busted.outputHandlers.json'] = 'busted/outputHandlers/json.lua',
['busted.outputHandlers.junit'] = 'busted/outputHandlers/junit.lua',
['busted.outputHandlers.sound'] = 'busted/outputHandlers/sound.lua',

['busted.languages.en'] = 'busted/languages/en.lua',
['busted.languages.ar'] = 'busted/languages/ar.lua',
Expand Down
91 changes: 91 additions & 0 deletions busted/outputHandlers/TAP.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
local pretty = require 'pl.pretty'

return function(options, busted)
local language = require('busted.languages.' .. options.language)

-- options.language, options.deferPrint, options.suppressPending, options.verbose
local handler = {}
local tests = {}

local getFullName = function(context)
local parent = context.parent
local names = { (context.name or context.descriptor) }

while parent and (parent.name or parent.descriptor) and
parent.descriptor ~= 'file' do

current_context = context.parent
table.insert(names, 1, parent.name or parent.descriptor)
parent = busted.context.parent(parent)
end

return table.concat(names, ' ')
end

handler.testStart = function(name, parent)
return nil, true
end

handler.testEnd = function(element, parent, status)
if status == 'success' then
table.insert(tests, {
name = getFullName(element),
success = true
})
end

return nil, true
end

handler.fileStart = function(name, parent)
return nil, true
end

handler.fileEnd = function(name, parent)
return nil, true
end

handler.suiteStart = function(name, parent)
return nil, true
end

handler.suiteEnd = function(name, parent)
print('1..' .. #tests)

local success = 'ok %u - %s'
local failure = 'not ' .. success

for i,t in pairs(tests) do
if t.success then
print(success:format(i, t.name))
else
local message = t.message

if message == nil then
message = 'Nil error'
elseif type(message) ~= 'string' then
message = pretty.write(message)
end

print(failure:format(i, t.name))
print('# ' .. t.elementTrace.short_src .. ' @ ' .. t.elementTrace.currentline)
print('# ' .. message:gsub('\n', '\n# ' ))
end
end

return nil, true
end

handler.error = function(element, parent, message, debug)
table.insert(tests, {
elementTrace = element.trace,
name = getFullName(element),
message = message,
success = false
})

return nil, true
end

return handler
end
68 changes: 68 additions & 0 deletions busted/outputHandlers/json.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local pretty = require 'pl.pretty'
local json = require 'dkjson'

return function(options, busted)
-- options.language, options.deferPrint, options.suppressPending, options.verbose
local handler = {}
local tests = {}

local getFullName = function(context)
local parent = context.parent
local names = { (context.name or context.descriptor) }

while parent and (parent.name or parent.descriptor) and
parent.descriptor ~= 'file' do

current_context = context.parent
table.insert(names, 1, parent.name or parent.descriptor)
parent = busted.context.parent(parent)
end

return table.concat(names, ' ')
end

handler.testStart = function(name, parent)
return nil, true
end

handler.testEnd = function(element, parent, status)
table.insert(tests, {
name = getFullName(element),
status = status,
trace = element.trace
})

print(json.encode(tests[#tests]))

return nil, true
end

handler.fileStart = function(name, parent)
return nil, true
end

handler.fileEnd = function(name, parent)
return nil, true
end

handler.suiteStart = function(name, parent)
return nil, true
end

handler.suiteEnd = function(name, parent)
return nil, true
end

handler.error = function(element, parent, message, debug)
table.insert(tests, {
elementTrace = element.trace,
name = getFullName(element),
message = message,
success = false
})

return nil, true
end

return handler
end
92 changes: 92 additions & 0 deletions busted/outputHandlers/junit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local xml = require 'pl.xml'
local hostname = assert(io.popen('uname -n')):read('*l')

return function(options, busted)
-- options.language, options.deferPrint, options.suppressPending, options.verbose
local node
local startTime, endTime
local handler = {}

local getFullName = function(context)
local parent = context.parent
local names = { (context.name or context.descriptor) }

while parent and (parent.name or parent.descriptor) and
parent.descriptor ~= 'file' do

current_context = context.parent
table.insert(names, 1, parent.name or parent.descriptor)
parent = busted.context.parent(parent)
end

return table.concat(names, ' ')
end

handler.testStart = function(name, parent)
return nil, true
end

handler.testEnd = function(element, parent, status)
node.attr.tests = node.attr.tests + 1

node:addtag('testcase', {
classname = element.trace.short_src .. ':' .. element.trace.currentline,
name = element.name
})

if status == 'failure' then
node.attr.failures = node.attr.failures + 1
end

return nil, true
end

handler.fileStart = function(name, parent)
return nil, true
end

handler.fileEnd = function(name, parent)
return nil, true
end

handler.suiteStart = function(name, parent)
startTime = os.clock()

node = xml.new('testsuite', {
tests = 0,
errors = 0,
failures = 0,
skip = 0,
header = 'Busted Suite',
hostname = hostname,
timestamp = os.time()
})

return nil, true
end

handler.suiteEnd = function(name, parent)
endTime = os.clock()

local ms = (endTime - startTime) * 1000
node.attr.time = ms

print(xml.tostring(node, '', '\t'))

return nil, true
end

handler.error = function(element, parent, message, trace)
if status == 'failure' then
node.attr.errors = node.attr.errors + 1
end

node:addtag('failure', {
message = message
}):text(trace.traceback):up()

return nil, true
end

return handler
end
Loading

0 comments on commit eb1d967

Please sign in to comment.