Commit c41492dd authored by Shinya Maeda's avatar Shinya Maeda Committed by Kamil Trzciński

Fix bugs/edge cases of JUnitParser

parent f7c1a5e1
---
title: Fix edge cases of JUnitParser
merge_request: 21469
author:
type: fixed
...@@ -139,3 +139,11 @@ java: ...@@ -139,3 +139,11 @@ java:
- target/surefire-reports/TEST-*.xml - target/surefire-reports/TEST-*.xml
- target/failsafe-reports/TEST-*.xml - target/failsafe-reports/TEST-*.xml
``` ```
## Limitations
Currently, the following tools might not work because their XML formats are unsupported in GitLab.
|Case|Tool|Issue|
|---|---|---|
|`<testcase>` does not have `classname` attribute|ESlint, sass-lint|https://gitlab.com/gitlab-org/gitlab-ce/issues/50964|
...@@ -2,18 +2,14 @@ module Gitlab ...@@ -2,18 +2,14 @@ module Gitlab
module Ci module Ci
module Parsers module Parsers
class Junit class Junit
attr_reader :data
JunitParserError = Class.new(StandardError) JunitParserError = Class.new(StandardError)
def parse!(xml_data, test_suite) def parse!(xml_data, test_suite)
@data = Hash.from_xml(xml_data) root = Hash.from_xml(xml_data)
each_suite do |testcases| all_cases(root) do |test_case|
testcases.each do |testcase| test_case = create_test_case(test_case)
test_case = create_test_case(testcase) test_suite.add_test_case(test_case)
test_suite.add_test_case(test_case)
end
end end
rescue REXML::ParseException => e rescue REXML::ParseException => e
raise JunitParserError, "XML parsing failed: #{e.message}" raise JunitParserError, "XML parsing failed: #{e.message}"
...@@ -23,26 +19,27 @@ module Gitlab ...@@ -23,26 +19,27 @@ module Gitlab
private private
def each_suite def all_cases(root, parent = nil, &blk)
testsuites.each do |testsuite| return unless root.present?
yield testcases(testsuite)
end
end
def testsuites [root].flatten.compact.map do |node|
if data['testsuites'] next unless node.is_a?(Hash)
data['testsuites']['testsuite']
else # we allow only one top-level 'testsuites'
[data['testsuite']] all_cases(node['testsuites'], root, &blk) unless parent
# we require at least one level of testsuites or testsuite
each_case(node['testcase'], &blk) if parent
# we allow multiple nested 'testsuite' (eg. PHPUnit)
all_cases(node['testsuite'], root, &blk)
end end
end end
def testcases(testsuite) def each_case(testcase, &blk)
if testsuite['testcase'].is_a?(Array) return unless testcase.present?
testsuite['testcase']
else [testcase].flatten.compact.map(&blk)
[testsuite['testcase']]
end
end end
def create_test_case(data) def create_test_case(data)
......
require 'spec_helper' require 'fast_spec_helper'
describe Gitlab::Ci::Parsers::Junit do describe Gitlab::Ci::Parsers::Junit do
describe '#parse!' do describe '#parse!' do
...@@ -8,21 +8,35 @@ describe Gitlab::Ci::Parsers::Junit do ...@@ -8,21 +8,35 @@ describe Gitlab::Ci::Parsers::Junit do
let(:test_cases) { flattened_test_cases(test_suite) } let(:test_cases) { flattened_test_cases(test_suite) }
context 'when data is JUnit style XML' do context 'when data is JUnit style XML' do
context 'when there are no test cases' do context 'when there are no <testcases> in <testsuite>' do
let(:junit) do let(:junit) do
<<-EOF.strip_heredoc <<-EOF.strip_heredoc
<testsuite></testsuite> <testsuite></testsuite>
EOF EOF
end end
it 'raises an error and does not add any test cases' do it 'ignores the case' do
expect { subject }.to raise_error(described_class::JunitParserError) expect { subject }.not_to raise_error
expect(test_cases.count).to eq(0)
end
end
context 'when there are no <testcases> in <testsuites>' do
let(:junit) do
<<-EOF.strip_heredoc
<testsuites><testsuite /></testsuites>
EOF
end
it 'ignores the case' do
expect { subject }.not_to raise_error
expect(test_cases.count).to eq(0) expect(test_cases.count).to eq(0)
end end
end end
context 'when there is a test case' do context 'when there is only one <testcase> in <testsuite>' do
let(:junit) do let(:junit) do
<<-EOF.strip_heredoc <<-EOF.strip_heredoc
<testsuite> <testsuite>
...@@ -40,6 +54,46 @@ describe Gitlab::Ci::Parsers::Junit do ...@@ -40,6 +54,46 @@ describe Gitlab::Ci::Parsers::Junit do
end end
end end
context 'when there is only one <testsuite> in <testsuites>' do
let(:junit) do
<<-EOF.strip_heredoc
<testsuites>
<testsuite>
<testcase classname='Calculator' name='sumTest1' time='0.01'></testcase>
</testsuite>
</testsuites>
EOF
end
it 'parses XML and adds a test case to a suite' do
expect { subject }.not_to raise_error
expect(test_cases[0].classname).to eq('Calculator')
expect(test_cases[0].name).to eq('sumTest1')
expect(test_cases[0].execution_time).to eq(0.01)
end
end
context 'PHPUnit' do
let(:junit) do
<<-EOF.strip_heredoc
<testsuites>
<testsuite name="Project Test Suite" tests="1" assertions="1" failures="0" errors="0" time="1.376748">
<testsuite name="XXX\\FrontEnd\\WebBundle\\Tests\\Controller\\LogControllerTest" file="/Users/mcfedr/projects/xxx/server/tests/XXX/FrontEnd/WebBundle/Tests/Controller/LogControllerTest.php" tests="1" assertions="1" failures="0" errors="0" time="1.376748">
<testcase name="testIndexAction" class="XXX\\FrontEnd\\WebBundle\\Tests\\Controller\\LogControllerTest" file="/Users/mcfedr/projects/xxx/server/tests/XXX/FrontEnd/WebBundle/Tests/Controller/LogControllerTest.php" line="9" assertions="1" time="1.376748"/>
</testsuite>
</testsuite>
</testsuites>
EOF
end
it 'parses XML and adds a test case to a suite' do
expect { subject }.not_to raise_error
expect(test_cases.count).to eq(1)
end
end
context 'when there are two test cases' do context 'when there are two test cases' do
let(:junit) do let(:junit) do
<<-EOF.strip_heredoc <<-EOF.strip_heredoc
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment