Module: Slather::CoverageService::CoberturaXmlOutput

Defined in:
lib/slather/coverage_service/cobertura_xml_output.rb

Instance Method Summary collapse

Instance Method Details

#create_class_node(coverage_file) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 120

def create_class_node(coverage_file)
  filename = coverage_file.source_file_basename
  filepath = coverage_file.source_file_pathname_relative_to_repo_root.to_s

  class_node = Nokogiri::XML::Node.new "class", @doc
  class_node['name'] = filename
  class_node['filename'] = filepath
  class_node['line-rate'] = '%.16f' %  [(coverage_file.num_lines_testable > 0) ? coverage_file.rate_lines_tested : 1.0]
  class_node['branch-rate'] = '%.16f' % [(coverage_file.num_branches_testable > 0) ? coverage_file.rate_branches_tested : 1.0]
  class_node['complexity'] = '0.0'

  methods_node = Nokogiri::XML::Node.new "methods", @doc
  methods_node.parent = class_node
  lines_node = Nokogiri::XML::Node.new "lines", @doc
  lines_node.parent = class_node
  
  coverage_file.all_lines.each do |line|
    if coverage_file.coverage_for_line(line)
      line_node = create_line_node(line, coverage_file)
      line_node.parent = lines_node
    end
  end
  class_node
end

#create_empty_xml_reportObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 169

def create_empty_xml_report
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.doc.create_internal_subset(
      'coverage',
      nil,
      "https://raw.githubusercontent.com/cobertura/cobertura/master/cobertura/src/site/htdocs/xml/coverage-04.dtd"
    )
    xml.coverage do
      xml.sources do
        xml.source
      end
      xml.packages
    end
  end
  @doc = builder.doc
end

#create_line_node(line, coverage_file) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 145

def create_line_node(line, coverage_file)
  line_number = coverage_file.line_number_in_line(line)
  line_node = Nokogiri::XML::Node.new "line", @doc
  line_node['number'] = line_number
  line_node['branch'] = "false"
  line_node['hits'] = coverage_file.coverage_for_line(line)

  unless coverage_file.branch_coverage_data_for_statement_on_line(line_number).empty?
    line_node['branch'] = "true"  
    conditions_node = Nokogiri::XML::Node.new "conditions", @doc
    conditions_node.parent = line_node
    condition_node = Nokogiri::XML::Node.new "condition", @doc
    condition_node.parent = conditions_node
    condition_node['number'] = "0"
    condition_node['type'] = "jump"
    branches_testable = coverage_file.num_branches_for_statement_on_line(line_number)
    branch_hits = coverage_file.num_branch_hits_for_statement_on_line(line_number)
    condition_coverage = coverage_file.percentage_branch_coverage_for_statement_on_line(line_number)
    condition_node['coverage'] = "#{condition_coverage.to_i}%"
    line_node['condition-coverage'] = "#{condition_coverage.to_i}% (#{branch_hits}/#{branches_testable})"
  end
  line_node
end

#create_xml_report(coverage_files) ⇒ Object



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
116
117
118
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 44

def create_xml_report(coverage_files)
  total_project_lines = 0
  total_project_lines_tested = 0
  total_project_line_rate = '%.16f' % 1.0
  total_project_branches = 0
  total_project_branches_tested = 0
  total_project_branch_rate = '%.16f' % 1.0

  create_empty_xml_report
  coverage_node = @doc.root
  source_node = @doc.at_css "source" 
  source_node.content = Pathname.pwd.to_s
  packages_node = @doc.at_css "packages"

  # group files by path
    grouped_coverage_files(coverage_files).each do |path , package_coverage_files|
    package_node = Nokogiri::XML::Node.new "package", @doc
    package_node.parent = packages_node
    classes_node = Nokogiri::XML::Node.new "classes", @doc
    classes_node.parent = package_node
    package_node['name'] = path.gsub(/\//, '.')

    total_package_lines = 0
    total_package_lines_tested = 0
    total_package_lines_rate = '%.16f' % 1.0
    total_package_branches = 0
    total_package_branches_tested = 0
    total_package_branch_rate = '%.16f' % 1.0

    package_coverage_files.each do |package_coverage_file|
      class_node = create_class_node(package_coverage_file)
      class_node.parent = classes_node
      total_package_lines += package_coverage_file.num_lines_testable
      total_package_lines_tested += package_coverage_file.num_lines_tested
      total_package_branches += package_coverage_file.num_branches_testable
      total_package_branches_tested += package_coverage_file.num_branches_tested
    end

    if (total_package_lines > 0)
      total_package_line_rate = '%.16f' % (total_package_lines_tested / total_package_lines.to_f)
    end

    if (total_package_branches > 0)
      total_package_branch_rate = '%.16f' % (total_package_branches_tested / total_package_branches.to_f)
    end

    package_node['line-rate'] = total_package_line_rate
    package_node['branch-rate'] = total_package_branch_rate
    package_node['complexity'] = '0.0'

    total_project_lines += total_package_lines
    total_project_lines_tested += total_package_lines_tested
    total_project_branches += total_package_branches
    total_project_branches_tested += total_package_branches_tested
  end

  if (total_project_lines > 0)
    total_project_line_rate = '%.16f' % (total_project_lines_tested / total_project_lines.to_f)
  end

  if (total_project_branches > 0)
    total_project_branch_rate = '%.16f' % (total_project_branches_tested / total_project_branches.to_f)
  end

  coverage_node['line-rate'] = total_project_line_rate
  coverage_node['branch-rate'] = total_project_branch_rate
  coverage_node['lines-covered'] = total_project_lines_tested
  coverage_node['lines-valid'] = total_project_lines
  coverage_node['branches-covered'] = total_project_branches_tested
  coverage_node['branches-valid'] = total_project_branches
  coverage_node['complexity'] = "0.0"
  coverage_node['timestamp'] = DateTime.now.strftime('%s')
  coverage_node['version'] = "Slather #{Slather::VERSION}"
  @doc.to_xml
end

#grouped_coverage_files(coverage_files) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 31

def grouped_coverage_files(coverage_files)
  groups = Hash.new
  coverage_files.each do |coverage_file|
    next if coverage_file == nil
    path = File.dirname(coverage_file.source_file_pathname_relative_to_repo_root)
    if groups[path] == nil
      groups[path] = Array.new
    end
    groups[path].push(coverage_file)
  end
  groups
end

#postObject



17
18
19
20
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 17

def post
  cobertura_xml_report = create_xml_report(coverage_files)
  store_report(cobertura_xml_report)
end

#store_report(report) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/slather/coverage_service/cobertura_xml_output.rb', line 22

def store_report(report)
  output_file = 'cobertura.xml'
  if output_directory
    FileUtils.mkdir_p(output_directory)
    output_file = File.join(output_directory, output_file)
  end
  File.write(output_file, report.to_s)
end