module FakeWebTestHelper
  module CodeCoverage
    def start_simplecov
      return unless coverage_supported_by_this_ruby?
      return if !ENV['AUTOTEST'].nil?

      require 'simplecov'
      require 'simplecov-console'

      SimpleCov.start do
        add_filter "/test/"

        minimum_coverage 100 if running_all_tests?
        command_name SIMPLECOV_COMMAND_NAME if child_test_process?
        formatter simplecov_formatter_class
      end
    end

    def simplecov_formatter_class
      include SimpleCov::Formatter
      if html_report_requested?
        MultiFormatter[HTMLFormatter, Console]
      else
        Console
      end
    end

    def running_all_tests?
      ENV['TEST'].nil?
    end

    def child_test_process?
      defined?(SIMPLECOV_COMMAND_NAME)
    end

    def html_report_requested?
      !ENV["COVERAGE_REPORT"].nil?
    end

    def coverage_supported_by_this_ruby?
      RUBY_VERSION >= "1.9.0"
    end

    extend self
  end
end

FakeWebTestHelper::CodeCoverage.start_simplecov

module FakeWebTestHelper
  module CodeCoverage
    def start_simplecov
      return unless coverage_supported_by_this_ruby?
      return if !ENV['AUTOTEST'].nil?

      require 'simplecov'
      require 'simplecov-console'

      SimpleCov.start do
        add_filter "/test/"

        minimum_coverage 100 if this_process_responsible_for_coverage_reporting?
        command_name SIMPLECOV_COMMAND_NAME if child_test_process?
        formatter simplecov_formatter_class
      end
    end

    def simplecov_formatter_class
      include SimpleCov::Formatter
      formatters = []
      if this_process_responsible_for_coverage_reporting?
        formatters << Console
        formatters << HTMLFormatter if html_report_requested?
      end
      MultiFormatter[*formatters]
    end

    def this_process_responsible_for_coverage_reporting?
      running_all_tests? && !child_test_process?
    end

    def running_all_tests?
      ARGV == Dir["test/test_*.rb"] - ["test/test_helper.rb"]
    end

    def child_test_process?
      defined?(SIMPLECOV_COMMAND_NAME)
    end

    def html_report_requested?
      !ENV["COVERAGE_REPORT"].nil?
    end

    def coverage_supported_by_this_ruby?
      RUBY_VERSION >= "1.9.0"
    end

    extend self
  end
end

FakeWebTestHelper::CodeCoverage.start_simplecov

Fix that tests run in forked processes were printing coverage reports
Fix that tests run in forked processes were printing coverage reports

For a few tests, we shell out to `ruby -e "require 'fakeweb'; ..."` to get a
clean environment in order to check what warnings appear when you combine
FakeWeb with other libs. Recently these processes starting printing coverage
reports, which messes with our assertions about the warnings output.

This sets the SimpleCov formatter to `MultiFormatter[]` for those processes,
which keeps them collecting/logging coverage data while suppressing any output.

p.s. this also sneaks in a more robust technique for checking whether the full
suite is being run (in #running_all_tests?).
