Permalink
Browse files

Merge branch '10-emma-reports' of http://github.com/andreacampi/cover_me

 into andreacampi-10-emma-reports
  • Loading branch information...
2 parents 9921c0e + 77051d7 commit 81fe2c8845ba206f79f5df215ab35932e2c14de5 @markbates markbates committed Oct 21, 2010
View
@@ -33,7 +33,7 @@ def complete!
CoverMe.set_defaults
-%w{index report formatter processor html_formatter results}.each do |file|
+%w{index report formatter processor html_formatter emma_formatter results directory_report global_report}.each do |file|
require File.expand_path(File.join(path, file))
end
View
@@ -41,6 +41,7 @@ def set_defaults # :nodoc:
c.html_formatter.set_default(:output_path, Configatron::Delayed.new {File.join(CoverMe.config.project.root, 'coverage')})
c.html_formatter.set_default(:finalizer_files, {'report.css' => 'report.css', 'index.css' => 'index.css',
'jquery.js' => 'jquery.js', 'jquery.tablesorter.js' => 'jquery.tablesorter.js'})
+ c.emma_formatter.set_default(:output_path, Configatron::Delayed.new {File.join(CoverMe.config.project.root, 'coverage')})
end
end
@@ -0,0 +1,35 @@
+# Used to represent the directory summary.
+class CoverMe::DirectoryReport
+ attr_accessor :name
+ attr_accessor :reports
+ attr_accessor :lines_of_code
+ attr_accessor :lines_executed
+
+ def initialize(name, reports) # :nodoc:
+ self.name = name
+ self.reports = reports
+ self.lines_of_code = 0
+ self.lines_executed = 0
+ reports.each do |report|
+ self.lines_of_code += report.lines_of_code
+ self.lines_executed += report.lines_executed
+ end
+ end
+
+ class << self
+ def summarize(reports)
+ reports_by_dir = reports.group_by do |report|
+ dir_for_file(report.filename)
+ end
+
+ reports_by_dir.map do |dir,reports|
+ self.new(dir, reports)
+ end
+ end
+
+ private
+ def dir_for_file(filename)
+ File.dirname(filename).split(File::SEPARATOR).join('.')
+ end
+ end
+end
@@ -0,0 +1,28 @@
+# Generates Emma-style xml coverage files.
+class CoverMe::EmmaFormatter < CoverMe::Formatter
+
+ def initialize(options = {}) # :nodoc:
+ super
+ self.options = ({:output_path => CoverMe.config.emma_formatter.output_path}.merge(self.options)).to_mash
+ end
+
+ def format_report(report)
+ # nothing to do
+ end
+
+ # Formats a CoverMe::Index object using the _emma.xml.erb_ template
+ def format_index(index)
+ reports_by_dir = CoverMe::DirectoryReport.summarize(index.reports)
+ global = CoverMe::GlobalReport.new(reports_by_dir)
+ write_file('coverage') do |file|
+ file.write(self.template('emma.xml.erb').result(binding))
+ end
+ end
+
+ protected
+ def write_file(filename, extension = '.xml', &block) # :nodoc:
+ path = File.join(self.options.output_path, filename)
+ FileUtils.mkdir_p(File.dirname(path))
+ File.open(path + extension, 'w', &block)
+ end
+end
@@ -0,0 +1,14 @@
+# Used to represent the module summary.
+class CoverMe::GlobalReport
+ attr_accessor :lines_of_code
+ attr_accessor :lines_executed
+
+ def initialize(reports) # :nodoc:
+ self.lines_of_code = 0
+ self.lines_executed = 0
+ reports.each do |report|
+ self.lines_of_code += report.lines_of_code
+ self.lines_executed += report.lines_executed
+ end
+ end
+end
View
@@ -5,6 +5,7 @@ class CoverMe::Report
attr_accessor :coverage
attr_accessor :lines
attr_accessor :lines_of_code
+ attr_accessor :lines_executed
attr_accessor :executed_percent
attr_accessor :unexecuted_percent
@@ -14,7 +15,8 @@ def initialize(filename, coverage = []) # :nodoc:
self.coverage = coverage
self.lines = self.coverage.size
self.lines_of_code = self.coverage.reject{|x| x.nil?}.size
- self.executed_percent = ((self.coverage.reject{|x| x.nil? || x < 1}.size.to_f / self.lines_of_code.to_f) * 100).round(1)
+ self.lines_executed = self.coverage.reject{|x| x.nil? || x < 1}.size
+ self.executed_percent = ((self.lines_executed.to_f / self.lines_of_code.to_f) * 100).round(1)
self.unexecuted_percent = (100 - self.executed_percent).round(1)
end
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?><!-- cover_me report -->
+
+<report>
+ <stats>
+ <packages value="42"/>
+ <classes value="42"/>
+ <methods value="42"/>
+ <srcfiles value="42"/>
+ <srclines value="42"/>
+ </stats>
+ <data>
+ <all name="all classes">
+ <coverage type="class, %" value="0% (0/0)"/>
+ <coverage type="method, %" value="0% (0/0)"/>
+ <coverage type="block, %" value="0% (0/0)"/>
+ <coverage type="line, %" value="0% (<%= global.lines_executed %>/<%= global.lines_of_code %>)"/>
+
+ <% reports_by_dir.sort_by(&:name).each do |dir| %>
+ <package name="<%= dir.name %>">
+ <coverage type="class, %" value="0% (0/0)"/>
+ <coverage type="method, %" value="0% (0/0)"/>
+ <coverage type="block, %" value="0% (0/0)"/>
+ <coverage type="line, %" value="0% (<%= dir.lines_executed %>/<%= dir.lines_of_code %>)"/>
+
+ <% dir.reports.sort.each do |report| %>
+ <srcfile name="<%= File.basename(report.filename) %>">
+ <coverage type="class, %" value="0% (0/0)"/>
+ <coverage type="method, %" value="0% (0/0)"/>
+ <coverage type="block, %" value="0% (0/0)"/>
+ <coverage type="line, %" value="<%= report.executed_percent.to_i %>% (<%= report.lines_executed %>/<%= report.lines_of_code %>)"/>
+ </srcfile>
+ <% end %>
+ </package>
+ <% end %>
+ </all>
+ </data>
+</report>

0 comments on commit 81fe2c8

Please sign in to comment.