Module: Slather::CoverageService::SonarqubeXmlOutput

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

Instance Method Summary collapse

Instance Method Details

#create_empty_xml_reportObject



52
53
54
55
56
57
# File 'lib/slather/coverage_service/sonarqube_xml_output.rb', line 52

def create_empty_xml_report
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.coverage
  end
  @doc = builder.doc
end

#create_xml_report(coverage_files) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/slather/coverage_service/sonarqube_xml_output.rb', line 31

def create_xml_report(coverage_files)
  create_empty_xml_report
  coverage_node = @doc.root
  coverage_node['version'] = "1"

  coverage_files.each do |coverage_file|
    file_node = Nokogiri::XML::Node.new "file", @doc
    file_node.parent = coverage_node
    file_node['path'] = coverage_file.source_file_pathname_relative_to_repo_root.to_s
    coverage_file.all_lines.each do |line|
      if coverage_file.coverage_for_line(line)
        line_node = Nokogiri::XML::Node.new "lineToCover", @doc
        line_node['lineNumber'] = coverage_file.line_number_in_line(line)
        line_node['covered'] = coverage_file.coverage_for_line(line) == 0 ? "false" : "true"
        line_node.parent = file_node
      end
    end
  end
  @doc.to_xml
end

#postObject



17
18
19
20
# File 'lib/slather/coverage_service/sonarqube_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/sonarqube_xml_output.rb', line 22

def store_report(report)
  output_file = 'sonarqube-generic-coverage.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