inspec/test/helpers/simplecov_minitest.rb
Ryan Davis 6bed51d69a Fixed up interactions between minitest & simplecov.
Sometime during the 2.x's, `at_exit` changed its ordering. As a result,
a lot of things that were stacking `at_exit`'s broke. This is one of
those since both simplecov and minitest do their thing via `at_exit`.
This switches to simplecov w/ no defaults on, then replicates their
simplecov/defaults.rb with our own.

I'm going to try to get that entire file back upstream but it can live
here for now.

Signed-off-by: Ryan Davis <zenspider@chef.io>
2019-06-05 15:16:42 -07:00

60 lines
1.5 KiB
Ruby

# frozen_string_literal: true
# Load default formatter gem
require "simplecov-html"
require "pathname"
require "simplecov/profiles/root_filter"
require "simplecov/profiles/test_frameworks"
require "simplecov/profiles/bundler_filter"
require "simplecov/profiles/rails"
# Default configuration
SimpleCov.configure do
formatter SimpleCov::Formatter::HTMLFormatter
load_profile "bundler_filter"
# Exclude files outside of SimpleCov.root
load_profile "root_filter"
end
# Gotta stash this a-s-a-p, see the CommandGuesser class and i.e. #110 for further info
SimpleCov::CommandGuesser.original_run_command = "#{$PROGRAM_NAME} #{ARGV.join(' ')}"
at_exit do
if defined? Minitest then
Minitest.after_run do
simplecov_at_exit
end
else
simplecov_at_exit
end
end
def simplecov_at_exit
# If we are in a different process than called start, don't interfere.
return if SimpleCov.pid != Process.pid
SimpleCov.set_exit_exception
SimpleCov.run_exit_tasks!
end
# Autoload config from ~/.simplecov if present
require "simplecov/load_global_config"
# Autoload config from .simplecov if present
# Recurse upwards until we find .simplecov or reach the root directory
config_path = Pathname.new(SimpleCov.root)
loop do
filename = config_path.join(".simplecov")
if filename.exist?
begin
load filename
rescue LoadError, StandardError
$stderr.puts "Warning: Error occurred while trying to load #{filename}. " \
"Error message: #{$!.message}"
end
break
end
config_path, = config_path.split
break if config_path.root?
end