From 7080ba3766c679473dea003162f7803b2b082b55 Mon Sep 17 00:00:00 2001 From: Nikita Afanasenko Date: Thu, 30 Aug 2012 20:55:39 +0400 Subject: [PATCH 1/4] Implement MultiFormatter. --- lib/simplecov/formatter.rb | 1 + lib/simplecov/formatter/multi_formatter.rb | 20 ++++++++++++++++++++ test/test_result.rb | 15 +++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 lib/simplecov/formatter/multi_formatter.rb diff --git a/lib/simplecov/formatter.rb b/lib/simplecov/formatter.rb index 21bf0416..40040354 100644 --- a/lib/simplecov/formatter.rb +++ b/lib/simplecov/formatter.rb @@ -5,3 +5,4 @@ module Formatter end require 'simplecov/formatter/simple_formatter' +require 'simplecov/formatter/multi_formatter' diff --git a/lib/simplecov/formatter/multi_formatter.rb b/lib/simplecov/formatter/multi_formatter.rb new file mode 100644 index 00000000..fd3e039b --- /dev/null +++ b/lib/simplecov/formatter/multi_formatter.rb @@ -0,0 +1,20 @@ +class SimpleCov::Formatter::MultiFormatter + def self.[](*args) + Class.new(self) do + define_method :formatters do + @formatters ||= args + end + end + end + + def format(result) + formatters.map do |formatter| + formatter.new.format(result) + end + end + + def formatters + @formatters ||= [] + end + +end diff --git a/test/test_result.rb b/test/test_result.rb index 4cb2da21..2ce78754 100644 --- a/test/test_result.rb +++ b/test/test_result.rb @@ -111,6 +111,21 @@ class TestResult < Test::Unit::TestCase assert_equal String, @result.format!.class end end + + context "and multi formatter being used" do + setup do + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + SimpleCov::Formatter::SimpleFormatter, + SimpleCov::Formatter::SimpleFormatter, + ] + end + + should "return an array containing formatted string with result.format!" do + formated = @result.format! + assert_equal 2, formated.count + assert_equal String, formated.first.class + end + end end context "with groups set up that do not match all files" do From 3e104bf0cbd2a83ca134d2537eb7ae86354a3fc9 Mon Sep 17 00:00:00 2001 From: Nikita Afanasenko Date: Thu, 30 Aug 2012 20:56:08 +0400 Subject: [PATCH 2/4] Update docs about using multiple formatters. --- README.md | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3bb6abad..d27ee351 100644 --- a/README.md +++ b/README.md @@ -414,18 +414,12 @@ being an instance of SimpleCov::Result. Do whatever your wish with that! ## Using multiple formatters -There is currently no built-in support for this, but you could help yourself with a wrapper class: +Configure the formatter to use built-in MultiFormatter: - class SimpleCov::Formatter::MergedFormatter - def format(result) - SimpleCov::Formatter::HTMLFormatter.new.format(result) - SimpleCov::Formatter::CSVFormatter.new.format(result) - end - end - -Then configure the formatter to use the new merger: - - SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::CSVFormatter, + ] ## Available formatters From e51ba3b6dd9af9149a13be35d7341b2720f24a97 Mon Sep 17 00:00:00 2001 From: Nikita Afanasenko Date: Fri, 31 Aug 2012 21:48:19 +0400 Subject: [PATCH 3/4] Implement MultiFormatter exceptions handling. Handle exceptions in underlying formatters and print it in STDERR. --- lib/simplecov/formatter/multi_formatter.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/simplecov/formatter/multi_formatter.rb b/lib/simplecov/formatter/multi_formatter.rb index fd3e039b..0e979d89 100644 --- a/lib/simplecov/formatter/multi_formatter.rb +++ b/lib/simplecov/formatter/multi_formatter.rb @@ -9,7 +9,12 @@ def self.[](*args) def format(result) formatters.map do |formatter| - formatter.new.format(result) + begin + formatter.new.format(result) + rescue => e + STDERR.puts("Formatter #{formatter} failed with #{e.class}: #{e.message} (#{e.backtrace.first})") + nil + end end end From 6decd51797770e79177c9839dfcfd1fbfb2472bb Mon Sep 17 00:00:00 2001 From: Nikita Afanasenko Date: Fri, 31 Aug 2012 21:48:58 +0400 Subject: [PATCH 4/4] Implement Cucumber feature showing built-in formatters configuration. --- features/config_formatters.feature | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 features/config_formatters.feature diff --git a/features/config_formatters.feature b/features/config_formatters.feature new file mode 100644 index 00000000..09f5ea6d --- /dev/null +++ b/features/config_formatters.feature @@ -0,0 +1,52 @@ +@test_unit @config +Feature: + + The formatter for test coverage can be customized + with the SimpleCov.formatter setting. There are two + built-in formatters: + SimpleCov::Formatter::SimpleFormatter is a simple + formatter returning a string of all files with + theirs coverages. + SimpleCov::Formatter::MultiFormatter is a formatter + used to call multiple formatters at once. + + Scenario: With SimpleFormatter + Given SimpleCov for Test/Unit is configured with: + """ + require 'simplecov' + SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter + SimpleCov.at_exit do + puts SimpleCov.result.format! + 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%)" + + Scenario: With MultiFormatter + Given SimpleCov for Test/Unit is configured with: + """ + require 'simplecov' + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + 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/