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

sax_document.rb « coverage « parsers « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ddd9c80f5ea224ff7217bcea2770e1c808eae688 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

module Gitlab
  module Ci
    module Parsers
      module Coverage
        class SaxDocument < Nokogiri::XML::SAX::Document
          GO_SOURCE_PATTERN = '/usr/local/go/src'
          MAX_SOURCES = 100

          def initialize(coverage_report, project_path, worktree_paths)
            @coverage_report = coverage_report
            @project_path = project_path
            @paths = worktree_paths&.to_set

            @matched_filenames = []
            @parsed_lines = []
            @sources = []
          end

          def error(error)
            raise Cobertura::InvalidXMLError, "XML parsing failed with error: #{error}"
          end

          def start_element(node_name, attrs = [])
            return unless node_name

            self.node_name = node_name
            node_attrs = Hash[attrs]

            if node_name == 'class' && node_attrs["filename"].present?
              self.filename = determine_filename(node_attrs["filename"])
              self.matched_filenames << filename if filename
            elsif node_name == 'line'
              self.parsed_lines << parse_line(node_attrs)
            end
          end

          def characters(node_content)
            if node_name == 'source'
              parse_source(node_content)
            end
          end

          def end_element(node_name)
            if node_name == "package"
              remove_matched_filenames
            elsif node_name == "class" && filename && parsed_lines.present?
              coverage_report.add_file(filename, Hash[parsed_lines])
              self.filename = nil
              self.parsed_lines = []
            end
          end

          private

          attr_accessor :coverage_report, :project_path, :paths, :sources, :node_name, :filename, :parsed_lines, :matched_filenames

          def parse_line(line)
            [Integer(line["number"]), Integer(line["hits"])]
          rescue StandardError
            raise Cobertura::InvalidLineInformationError, "Line information had invalid values"
          end

          def parse_source(node)
            return unless project_path && paths && !node.include?(GO_SOURCE_PATTERN)

            source = build_source_path(node)
            self.sources << source if source.present?
          end

          def build_source_path(node)
            # | raw source                  | extracted  |
            # |-----------------------------|------------|
            # | /builds/foo/test/SampleLib/ | SampleLib/ |
            # | /builds/foo/test/something  | something  |
            # | /builds/foo/test/           | nil        |
            # | /builds/foo/test            | nil        |
            # | D:\builds\foo\bar\app\      | app\       |
            unixify(node).split("#{project_path}/", 2)[1]
          end

          def unixify(path)
            path.tr('\\', '/')
          end

          def remove_matched_filenames
            return unless paths

            matched_filenames.each { |f| paths.delete(f) }
          end

          def determine_filename(filename)
            return filename unless sources.any?

            full_filename = nil

            sources.each_with_index do |source, index|
              break if index >= MAX_SOURCES
              break if full_filename = check_source(source, filename)
            end

            full_filename
          end

          def check_source(source, filename)
            full_path = File.join(source, filename)

            return full_path if paths.include?(full_path)
          end
        end
      end
    end
  end
end