debian-mirror-gitlab/lib/gitlab/ci/parsers/test/junit.rb

84 lines
2.5 KiB
Ruby
Raw Normal View History

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
def parse!(xml_data, test_suite)
root = Hash.from_xml(xml_data)
all_cases(root) do |test_case|
test_case = create_test_case(test_case)
test_suite.add_test_case(test_case)
end
2019-02-15 15:39:39 +05:30
rescue Nokogiri::XML::SyntaxError
2018-12-05 23:21:45 +05:30
raise JunitParserError, "XML parsing failed"
rescue
raise JunitParserError, "JUnit parsing failed"
end
private
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
each_case(node['testcase'], &blk) if parent
# we allow multiple nested 'testsuite' (eg. PHPUnit)
all_cases(node['testsuite'], root, &blk)
end
end
def each_case(testcase, &blk)
return unless testcase.present?
[testcase].flatten.compact.map(&blk)
end
def create_test_case(data)
if data['failure']
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'])
2019-12-21 20:55:43 +05:30
elsif data['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']
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(
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,
attachment: attachment
2018-12-05 23:21:45 +05:30
)
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