Skip to content

Commit

Permalink
Update multiple formatter interface
Browse files Browse the repository at this point in the history
Closes #295.
  • Loading branch information
sferik committed Jul 14, 2014
1 parent ad0e092 commit 73dd88e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 31 deletions.
25 changes: 25 additions & 0 deletions features/config_formatters.feature
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,28 @@ Feature:
When I successfully run `bundle exec rake test`
Then the output should contain "lib/faked_project/meta_magic.rb (coverage: 100.0%)"
And the output should match /Formatter [^\s]* failed with RuntimeError: Unable to format/

Scenario: With multiple formatters
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
SimpleCov.formatters = [
SimpleCov::Formatter::SimpleFormatter,
Class.new do
def format(result)
raise "Unable to format"
end
end
]
SimpleCov.at_exit do
puts SimpleCov.result.format!.join
end
SimpleCov.start do
add_group 'Libs', 'lib/faked_project/'
end
"""

When I successfully run `bundle exec rake test`
Then the output should contain "lib/faked_project/meta_magic.rb (coverage: 100.0%)"
And the output should match /Formatter [^\s]* failed with RuntimeError: Unable to format/
19 changes: 19 additions & 0 deletions lib/simplecov/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'fileutils'
require 'docile'
require 'simplecov/formatter/multi_formatter'
#
# Bundles the configuration options used for SimpleCov. All methods
# defined here are usable from SimpleCov directly. Please check out
Expand Down Expand Up @@ -74,6 +75,24 @@ def formatter(formatter=nil)
@formatter
end

#
# Sets the configured formatters.
#
def formatters=(formatters)
@formatter = SimpleCov::Formatter::MultiFormatter[*formatters]
end

#
# Gets the configured formatters.
#
def formatters
if @formatter.is_a?(SimpleCov::Formatter::MultiFormatter)
@formatter.formatters
else
Array(formatter)
end
end

#
# Certain code blocks (i.e. Ruby-implementation specific code) can be excluded from
# the coverage metrics by wrapping it inside # :nocov: comment blocks. The nocov token
Expand Down
41 changes: 22 additions & 19 deletions lib/simplecov/formatter/multi_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
class SimpleCov::Formatter::MultiFormatter
def self.[](*args)
Class.new(self) do
define_method :formatters do
@formatters ||= args
module SimpleCov
module Formatter
class MultiFormatter
def self.[](*args)
Class.new(self) do
define_method :formatters do
@formatters ||= args
end
end
end
end
end

def format(result)
formatters.map do |formatter|
begin
formatter.new.format(result)
rescue => e
STDERR.puts("Formatter #{formatter} failed with #{e.class}: #{e.message} (#{e.backtrace.first})")
nil
def format(result)
formatters.map do |formatter|
begin
formatter.new.format(result)
rescue => e
STDERR.puts("Formatter #{formatter} failed with #{e.class}: #{e.message} (#{e.backtrace.first})")
nil
end
end
end
end
end

def formatters
@formatters ||= []
def formatters
@formatters ||= []
end
end
end

end
28 changes: 16 additions & 12 deletions lib/simplecov/formatter/simple_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#
# A ridiculously simple formatter for SimpleCov results.
#
class SimpleCov::Formatter::SimpleFormatter
# Takes a SimpleCov::Result and generates a string out of it
def format(result)
output = ""
result.groups.each do |name, files|
output << "Group: #{name}\n"
output << "="*40
output << "\n"
files.each do |file|
output << "#{file.filename} (coverage: #{file.covered_percent.round(2)}%)\n"
module SimpleCov
module Formatter
class SimpleFormatter
# Takes a SimpleCov::Result and generates a string out of it
def format(result)
output = ""
result.groups.each do |name, files|
output << "Group: #{name}\n"
output << "="*40
output << "\n"
files.each do |file|
output << "#{file.filename} (coverage: #{file.covered_percent.round(2)}%)\n"
end
output << "\n"
end
output
end
output << "\n"
end
output
end
end

0 comments on commit 73dd88e

Please sign in to comment.