diff --git a/features/minitest_basic.feature b/features/minitest_basic.feature index 4df56009..a0fb2a4b 100644 --- a/features/minitest_basic.feature +++ b/features/minitest_basic.feature @@ -10,9 +10,26 @@ Feature: When I open the coverage report generated with `bundle exec rake minitest` Then I should see the groups: | name | coverage | files | - | All Files | 80.0% | 1 | + | All Files | 85.71% | 3 | And I should see the source files: | name | coverage | | lib/faked_project/some_class.rb | 80.0 % | + | minitest/some_test.rb | 100.0 % | + | minitest/test_helper.rb | 75.0 % | + + Scenario: + Given SimpleCov for Minitest is configured with: + """ + require 'simplecov' + SimpleCov.start + """ + When I open the coverage report generated with `bundle exec ruby -Ilib:minitest minitest/other_test.rb` + Then I should see the groups: + | name | coverage | files | + | All Files | 80.0% | 1 | + + And I should see the source files: + | name | coverage | + | lib/faked_project/some_class.rb | 80.0 % | diff --git a/lib/minitest/simplecov_plugin.rb b/lib/minitest/simplecov_plugin.rb new file mode 100644 index 00000000..96f1938d --- /dev/null +++ b/lib/minitest/simplecov_plugin.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Minitest + def self.plugin_simplecov_init(_options) + Minitest.after_run do + SimpleCov.custom_at_exit if SimpleCov.respond_to?(:custom_at_exit) + end + end +end diff --git a/lib/simplecov/defaults.rb b/lib/simplecov/defaults.rb index 2eb51b8a..3a4201fc 100644 --- a/lib/simplecov/defaults.rb +++ b/lib/simplecov/defaults.rb @@ -21,22 +21,21 @@ # 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(' ')}" -if defined?(Minitest) - Minitest.after_run do - simplecov_at_exit - end -else - at_exit do - simplecov_at_exit +module SimpleCov + def self.custom_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 end -def simplecov_at_exit - # If we are in a different process than called start, don't interfere. - return if SimpleCov.pid != Process.pid +at_exit do + # Exit hook for Minitest defined in Minitest plugin + next if defined?(Minitest) - SimpleCov.set_exit_exception - SimpleCov.run_exit_tasks! + SimpleCov.custom_at_exit end # Autoload config from ~/.simplecov if present diff --git a/spec/faked_project/minitest/other_test.rb b/spec/faked_project/minitest/other_test.rb new file mode 100644 index 00000000..84d74dd8 --- /dev/null +++ b/spec/faked_project/minitest/other_test.rb @@ -0,0 +1,24 @@ +require "bundler/setup" + +begin + require File.expand_path("simplecov_config", __dir__) +rescue LoadError + warn "No SimpleCov config file found!" +end + +require "minitest/autorun" +require "faked_project/some_class" + +class OtherTest < Minitest::Test + def setup + @instance = SomeClass.new("foo") + end + + def test_reverse + assert_equal "oof", @instance.reverse + end + + def test_comparison + assert @instance.compare_with("foo") + end +end