From 36683664ad8a26b7f5841285f93b67dd7137a5e5 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Mon, 11 Nov 2019 16:17:14 +0000 Subject: [PATCH 1/4] Fix at_exit hook to cooperate with Minitest --- CHANGELOG.md | 1 + lib/simplecov/defaults.rb | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bae0e9c2..f7eee17d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Unrealeased ## Bugfixes * Add new instance of `Minitest` constant. The `MiniTest` constant (with the capital T) will be removed in the next major release of Minitest. See [#757](https://github.com/colszowka/simplecov/pull/757) (thanks [@adam12](https://github.com/adam12)) +* Use `Minitest.after_run` hook to trigger post-run hooks if `Minitest` is present. 0.17.1 (2019-09-16) =================== diff --git a/lib/simplecov/defaults.rb b/lib/simplecov/defaults.rb index 468568ab..2eb51b8a 100644 --- a/lib/simplecov/defaults.rb +++ b/lib/simplecov/defaults.rb @@ -21,9 +21,19 @@ # 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) + Minitest.after_run do + simplecov_at_exit + end +else + at_exit do + simplecov_at_exit + end +end + +def simplecov_at_exit # If we are in a different process than called start, don't interfere. - next if SimpleCov.pid != Process.pid + return if SimpleCov.pid != Process.pid SimpleCov.set_exit_exception SimpleCov.run_exit_tasks! From 4fd1fa2490ecfe4bb4e7413c90abc5f1d63e657e Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Sun, 1 Dec 2019 03:35:07 +0000 Subject: [PATCH 2/4] Add feature for testing minitest --- Gemfile | 1 + Gemfile.lock | 2 ++ features/minitest_basic.feature | 18 ++++++++++++++++++ features/step_definitions/simplecov_steps.rb | 2 ++ spec/faked_project/Rakefile | 6 ++++++ spec/faked_project/minitest/some_test.rb | 16 ++++++++++++++++ spec/faked_project/minitest/test_helper.rb | 11 +++++++++++ 7 files changed, 56 insertions(+) create mode 100644 features/minitest_basic.feature create mode 100644 spec/faked_project/minitest/some_test.rb create mode 100644 spec/faked_project/minitest/test_helper.rb diff --git a/Gemfile b/Gemfile index 221505f6..fe0add87 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ group :development do gem "aruba", "~> 0.14" gem "capybara", "~> 3.29" gem "cucumber", "~> 3.1" + gem "minitest" gem "rake", "~> 13.0" gem "rspec", "~> 3.2" gem "rubocop", "0.53.0" diff --git a/Gemfile.lock b/Gemfile.lock index 7d6259ad..a9b720f6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,6 +57,7 @@ GEM gherkin (5.1.0) mini_mime (1.0.2) mini_portile2 (2.4.0) + minitest (5.13.0) multi_json (1.14.1) multi_test (0.1.2) nokogiri (1.10.7) @@ -118,6 +119,7 @@ DEPENDENCIES benchmark-ips capybara (~> 3.29) cucumber (~> 3.1) + minitest rake (~> 13.0) rspec (~> 3.2) rubocop (= 0.53.0) diff --git a/features/minitest_basic.feature b/features/minitest_basic.feature new file mode 100644 index 00000000..4df56009 --- /dev/null +++ b/features/minitest_basic.feature @@ -0,0 +1,18 @@ +Feature: + + Scenario: + Given SimpleCov for Minitest is configured with: + """ + require 'simplecov' + SimpleCov.start + """ + + 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 | + + And I should see the source files: + | name | coverage | + | lib/faked_project/some_class.rb | 80.0 % | + diff --git a/features/step_definitions/simplecov_steps.rb b/features/step_definitions/simplecov_steps.rb index 56c6844a..2a63582a 100644 --- a/features/step_definitions/simplecov_steps.rb +++ b/features/step_definitions/simplecov_steps.rb @@ -12,6 +12,8 @@ "test" when /Cucumber/i "features/support" + when /Minitest/i + "minitest" else raise ArgumentError, "Could not identify test framework #{framework}!" end diff --git a/spec/faked_project/Rakefile b/spec/faked_project/Rakefile index 05f1c57c..0519dc19 100644 --- a/spec/faked_project/Rakefile +++ b/spec/faked_project/Rakefile @@ -6,3 +6,9 @@ Rake::TestTask.new(:test) do |test| test.test_files = FileList["test/**/*_test.rb"].sort test.verbose = true end + +Rake::TestTask.new(:minitest) do |test| + test.libs << "minitest" + test.test_files = FileList["minitest/**/*_test.rb"].sort + test.verbose = true +end diff --git a/spec/faked_project/minitest/some_test.rb b/spec/faked_project/minitest/some_test.rb new file mode 100644 index 00000000..24e2f1a4 --- /dev/null +++ b/spec/faked_project/minitest/some_test.rb @@ -0,0 +1,16 @@ +require "test_helper" +require "faked_project/some_class" + +class SomeTest < 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 diff --git a/spec/faked_project/minitest/test_helper.rb b/spec/faked_project/minitest/test_helper.rb new file mode 100644 index 00000000..72da5775 --- /dev/null +++ b/spec/faked_project/minitest/test_helper.rb @@ -0,0 +1,11 @@ +require "bundler/setup" + +# We're injecting simplecov_config via aruba in cucumber here +# depending on what the test case is... +begin + require File.join(File.dirname(__FILE__), "simplecov_config") +rescue LoadError + warn "No SimpleCov config file found!" +end + +require "minitest/autorun" From 8d9a314c21db81fb6e7c679aaa8436c28d5c0acd Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Fri, 6 Dec 2019 20:49:47 +0000 Subject: [PATCH 3/4] Add minitest as dependency of faked project --- spec/faked_project/Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/faked_project/Gemfile b/spec/faked_project/Gemfile index d04236de..48d9864a 100644 --- a/spec/faked_project/Gemfile +++ b/spec/faked_project/Gemfile @@ -4,3 +4,4 @@ gem "simplecov", :path => "../../../" gem "rake" gem "rspec", ">= 2.6.0" gem "cucumber" +gem "minitest" From 945afed32b6e5dd53de79e7df2f9ef52d9c0b4a8 Mon Sep 17 00:00:00 2001 From: Adam Daniels Date: Fri, 6 Dec 2019 20:51:01 +0000 Subject: [PATCH 4/4] Add simplecov plugin for Minitest --- features/minitest_basic.feature | 19 +++++++++++++++++- lib/minitest/simplecov_plugin.rb | 9 +++++++++ lib/simplecov/defaults.rb | 23 +++++++++++----------- spec/faked_project/minitest/other_test.rb | 24 +++++++++++++++++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 lib/minitest/simplecov_plugin.rb create mode 100644 spec/faked_project/minitest/other_test.rb 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