Welcome to mirror list, hosted at ThFree Co, Russian Federation.

junit.rb « parsers « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c4668ec13b6ddb3fa7d686a284c465e58be0b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Gitlab
  module Ci
    module Parsers
      class Junit
        attr_reader :data

        JunitParserError = Class.new(StandardError)

        def parse!(xml_data, test_suite)
          @data = Hash.from_xml(xml_data)

          each_suite do |testcases|
            testcases.each do |testcase|
              test_case = create_test_case(testcase)
              test_suite.add_test_case(test_case)
            end
          end
        rescue REXML::ParseException => e
          raise JunitParserError, "XML parsing failed: #{e.message}"
        rescue => e
          raise JunitParserError, "JUnit parsing failed: #{e.message}"
        end

        private

        def each_suite
          testsuites.each do |testsuite|
            yield testcases(testsuite)
          end
        end

        def testsuites
          if data['testsuites']
            data['testsuites']['testsuite']
          else
            [data['testsuite']]
          end
        end

        def testcases(testsuite)
          if testsuite['testcase'].is_a?(Array)
            testsuite['testcase']
          else
            [testsuite['testcase']]
          end
        end

        def create_test_case(data)
          if data['failure']
            status = ::Gitlab::Ci::Reports::TestCase::STATUS_FAILED
            system_output = data['failure']
          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,
            system_output: system_output
          )
        end
      end
    end
  end
end