From 73dd88edb721515195b6760228f9416487d18232 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Mon, 14 Jul 2014 10:03:07 +0200 Subject: [PATCH] Update multiple formatter interface Closes https://github.com/colszowka/simplecov/issues/295. --- features/config_formatters.feature | 25 +++++++++++++ lib/simplecov/configuration.rb | 19 ++++++++++ lib/simplecov/formatter/multi_formatter.rb | 41 +++++++++++---------- lib/simplecov/formatter/simple_formatter.rb | 28 ++++++++------ 4 files changed, 82 insertions(+), 31 deletions(-) diff --git a/features/config_formatters.feature b/features/config_formatters.feature index 09f5ea6d..81cf5a06 100644 --- a/features/config_formatters.feature +++ b/features/config_formatters.feature @@ -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/ diff --git a/lib/simplecov/configuration.rb b/lib/simplecov/configuration.rb index 7b5a1401..2b64fa70 100644 --- a/lib/simplecov/configuration.rb +++ b/lib/simplecov/configuration.rb @@ -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 @@ -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 diff --git a/lib/simplecov/formatter/multi_formatter.rb b/lib/simplecov/formatter/multi_formatter.rb index 0e979d89..017cde6c 100644 --- a/lib/simplecov/formatter/multi_formatter.rb +++ b/lib/simplecov/formatter/multi_formatter.rb @@ -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 diff --git a/lib/simplecov/formatter/simple_formatter.rb b/lib/simplecov/formatter/simple_formatter.rb index 2e6b01d6..c3a0f8c1 100644 --- a/lib/simplecov/formatter/simple_formatter.rb +++ b/lib/simplecov/formatter/simple_formatter.rb @@ -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