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

88 lines
2.8 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
2020-04-22 19:07:51 +05:30
def parse!(xml_data, test_suite, **args)
2018-12-05 23:21:45 +05:30
root = Hash.from_xml(xml_data)
all_cases(root) do |test_case|
2020-04-22 19:07:51 +05:30
test_case = create_test_case(test_case, args)
2018-12-05 23:21:45 +05:30
test_suite.add_test_case(test_case)
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
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
2020-04-22 19:07:51 +05:30
def create_test_case(data, args)
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(
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,
job: args.fetch(:job)
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