Skip to content

Commit

Permalink
Create one testcase per error on JUnit format
Browse files Browse the repository at this point in the history
Modification of #28, fixes #27
  • Loading branch information
mpeterv committed Jun 20, 2015
1 parent 56e223a commit 56a8346
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
12 changes: 10 additions & 2 deletions spec/cli_spec.lua
Expand Up @@ -778,13 +778,21 @@ not ok 7 spec/samples/python_code.lua:1:6: expected '=' near '__future__'
it("has built-in JUnit formatter", function()
assert.equal([[
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Luacheck report" tests="3">
<testsuite name="Luacheck report" tests="7">
<testcase name="spec/samples/good_code.lua" classname="spec/samples/good_code.lua"/>
<testcase name="spec/samples/bad_code.lua" classname="spec/samples/bad_code.lua">
<testcase name="spec/samples/bad_code.lua:1" classname="spec/samples/bad_code.lua">
<failure type="W211" message="spec/samples/bad_code.lua:3:16: unused function 'helper'"/>
</testcase>
<testcase name="spec/samples/bad_code.lua:2" classname="spec/samples/bad_code.lua">
<failure type="W212" message="spec/samples/bad_code.lua:3:23: unused variable length argument"/>
</testcase>
<testcase name="spec/samples/bad_code.lua:3" classname="spec/samples/bad_code.lua">
<failure type="W111" message="spec/samples/bad_code.lua:7:10: setting non-standard global variable 'embrace'"/>
</testcase>
<testcase name="spec/samples/bad_code.lua:4" classname="spec/samples/bad_code.lua">
<failure type="W412" message="spec/samples/bad_code.lua:8:10: variable 'opt' was previously defined as an argument on line 7"/>
</testcase>
<testcase name="spec/samples/bad_code.lua:5" classname="spec/samples/bad_code.lua">
<failure type="W113" message="spec/samples/bad_code.lua:9:11: accessing undefined variable 'hepler'"/>
</testcase>
<testcase name="spec/samples/python_code.lua" classname="spec/samples/python_code.lua">
Expand Down
52 changes: 27 additions & 25 deletions src/luacheck/format.lua
Expand Up @@ -211,38 +211,40 @@ end

function formatters.JUnit(report, file_names)
local buf = {[[<?xml version="1.0" encoding="UTF-8"?>]]}
local num_testcases = 0

table.insert(buf, ([[<testsuite name="Luacheck report" tests="%d">]]):format(#report))
for _, file_report in ipairs(report) do
if file_report.error or #file_report == 0 then
num_testcases = num_testcases + 1
else
num_testcases = num_testcases + #file_report
end
end

for i, file_report in ipairs(report) do
if file_report.error or #file_report ~= 0 then
table.insert(buf, ([[ <testcase name="%s" classname="%s">]]):format(file_names[i], file_names[i]))

if file_report.error then
if file_report.msg then
table.insert(buf, ([[ <error type="%s" message=%q/>]]):format(
error_type(file_report), format_error_msg(file_names[i], file_report)))
else
table.insert(buf, ([[ <error type="%s"/>]]):format(error_type(file_report), file_report.msg))
end
table.insert(buf, ([[<testsuite name="Luacheck report" tests="%d">]]):format(num_testcases))

for file_i, file_report in ipairs(report) do
if file_report.error then
table.insert(buf, ([[ <testcase name="%s" classname="%s">]]):format(file_names[file_i], file_names[file_i]))

if file_report.msg then
table.insert(buf, ([[ <error type="%s" message="%s"/>]]):format(
error_type(file_report), format_error_msg(file_names[file_i], file_report)))
else
for _, warning in ipairs(file_report) do
local warning_type

if warning.code then
warning_type = "W" .. warning.code
else
warning_type = "Inline option"
end

table.insert(buf, ([[ <failure type="%s" message="%s"/>]]):format(
warning_type, format_warning(file_names[i], warning)))
end
table.insert(buf, ([[ <error type="%s"/>]]):format(error_type(file_report)))
end

table.insert(buf, [[ </testcase>]])
elseif #file_report == 0 then
table.insert(buf, ([[ <testcase name="%s" classname="%s"/>]]):format(file_names[file_i], file_names[file_i]))
else
table.insert(buf, ([[ <testcase name="%s" classname="%s"/>]]):format(file_names[i], file_names[i]))
for warning_i, warning in ipairs(file_report) do
local warning_type = warning.code and ("W" .. warning.code) or "Inline option"
table.insert(buf, ([[ <testcase name="%s:%d" classname="%s">]]):format(file_names[file_i], warning_i, file_names[file_i]))
table.insert(buf, ([[ <failure type="%s" message="%s"/>]]):format(
warning_type, format_warning(file_names[file_i], warning)))
table.insert(buf, [[ </testcase>]])
end
end
end

Expand Down

0 comments on commit 56a8346

Please sign in to comment.