2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Parsers
|
|
|
|
module Test
|
|
|
|
class Junit
|
2019-02-15 15:39:39 +05:30
|
|
|
JunitParserError = Class.new(Gitlab::Ci::Parsers::ParserError)
|
2020-04-08 14:13:33 +05:30
|
|
|
ATTACHMENT_TAG_REGEX = /\[\[ATTACHMENT\|(?<path>.+?)\]\]/.freeze
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def parse!(xml_data, test_suite, job:)
|
2018-12-05 23:21:45 +05:30
|
|
|
root = Hash.from_xml(xml_data)
|
2021-01-03 14:25:43 +05:30
|
|
|
total_parsed = 0
|
|
|
|
max_test_cases = job.max_test_cases_per_report
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
all_cases(root) do |test_case|
|
2021-01-03 14:25:43 +05:30
|
|
|
test_case = create_test_case(test_case, test_suite, job)
|
2018-12-05 23:21:45 +05:30
|
|
|
test_suite.add_test_case(test_case)
|
2021-01-03 14:25:43 +05:30
|
|
|
total_parsed += 1
|
|
|
|
|
|
|
|
ensure_test_cases_limited!(total_parsed, max_test_cases)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
rescue Nokogiri::XML::SyntaxError => e
|
|
|
|
test_suite.set_suite_error("JUnit XML parsing failed: #{e}")
|
|
|
|
rescue StandardError => e
|
|
|
|
test_suite.set_suite_error("JUnit data parsing failed: #{e}")
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def ensure_test_cases_limited!(total_parsed, limit)
|
|
|
|
return unless limit > 0 && total_parsed > limit
|
|
|
|
|
|
|
|
raise JunitParserError.new("number of test cases exceeded the limit of #{limit}")
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def all_cases(root, parent = nil, &blk)
|
|
|
|
return unless root.present?
|
|
|
|
|
|
|
|
[root].flatten.compact.map do |node|
|
|
|
|
next unless node.is_a?(Hash)
|
|
|
|
|
|
|
|
# we allow only one top-level 'testsuites'
|
|
|
|
all_cases(node['testsuites'], root, &blk) unless parent
|
|
|
|
|
|
|
|
# we require at least one level of testsuites or testsuite
|
2021-01-03 14:25:43 +05:30
|
|
|
each_case(node['testcase'], node['name'], &blk) if parent
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
# we allow multiple nested 'testsuite' (eg. PHPUnit)
|
|
|
|
all_cases(node['testsuite'], root, &blk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def each_case(testcase, testsuite_name, &blk)
|
2018-12-05 23:21:45 +05:30
|
|
|
return unless testcase.present?
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
[testcase].flatten.compact.each do |tc|
|
|
|
|
tc['suite_name'] = testsuite_name
|
|
|
|
|
|
|
|
yield(tc)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def create_test_case(data, test_suite, job)
|
2020-04-22 19:07:51 +05:30
|
|
|
if data.key?('failure')
|
2018-12-05 23:21:45 +05:30
|
|
|
status = ::Gitlab::Ci::Reports::TestCase::STATUS_FAILED
|
|
|
|
system_output = data['failure']
|
2020-04-08 14:13:33 +05:30
|
|
|
attachment = attachment_path(data['system_out'])
|
2020-04-22 19:07:51 +05:30
|
|
|
elsif data.key?('error')
|
2020-03-13 15:44:24 +05:30
|
|
|
status = ::Gitlab::Ci::Reports::TestCase::STATUS_ERROR
|
2019-12-21 20:55:43 +05:30
|
|
|
system_output = data['error']
|
2020-04-22 19:07:51 +05:30
|
|
|
elsif data.key?('skipped')
|
|
|
|
status = ::Gitlab::Ci::Reports::TestCase::STATUS_SKIPPED
|
|
|
|
system_output = data['skipped']
|
2018-12-05 23:21:45 +05:30
|
|
|
else
|
|
|
|
status = ::Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS
|
|
|
|
system_output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
::Gitlab::Ci::Reports::TestCase.new(
|
2021-01-03 14:25:43 +05:30
|
|
|
suite_name: data['suite_name'] || test_suite.name,
|
2018-12-05 23:21:45 +05:30
|
|
|
classname: data['classname'],
|
|
|
|
name: data['name'],
|
|
|
|
file: data['file'],
|
|
|
|
execution_time: data['time'],
|
|
|
|
status: status,
|
2020-04-08 14:13:33 +05:30
|
|
|
system_output: system_output,
|
2020-04-22 19:07:51 +05:30
|
|
|
attachment: attachment,
|
2021-01-03 14:25:43 +05:30
|
|
|
job: job
|
2018-12-05 23:21:45 +05:30
|
|
|
)
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def suite_name(parent, test_suite)
|
|
|
|
parent.dig('testsuite', 'name') || test_suite.name
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def attachment_path(data)
|
|
|
|
return unless data
|
|
|
|
|
|
|
|
matches = data.match(ATTACHMENT_TAG_REGEX)
|
|
|
|
matches[:path] if matches
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|