Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix at_exit hook to cooperate with Minitest #756

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
===================
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand Down Expand Up @@ -118,6 +119,7 @@ DEPENDENCIES
benchmark-ips
capybara (~> 3.29)
cucumber (~> 3.1)
minitest
rake (~> 13.0)
rspec (~> 3.2)
rubocop (= 0.53.0)
Expand Down
35 changes: 35 additions & 0 deletions features/minitest_basic.feature
@@ -0,0 +1,35 @@
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 | 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 % |
2 changes: 2 additions & 0 deletions features/step_definitions/simplecov_steps.rb
Expand Up @@ -12,6 +12,8 @@
"test"
when /Cucumber/i
"features/support"
when /Minitest/i
"minitest"
else
raise ArgumentError, "Could not identify test framework #{framework}!"
end
Expand Down
9 changes: 9 additions & 0 deletions 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
17 changes: 13 additions & 4 deletions lib/simplecov/defaults.rb
Expand Up @@ -21,12 +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(' ')}"

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

at_exit do
# If we are in a different process than called start, don't interfere.
next if SimpleCov.pid != Process.pid
# 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
Expand Down
1 change: 1 addition & 0 deletions spec/faked_project/Gemfile
Expand Up @@ -4,3 +4,4 @@ gem "simplecov", :path => "../../../"
gem "rake"
gem "rspec", ">= 2.6.0"
gem "cucumber"
gem "minitest"
6 changes: 6 additions & 0 deletions spec/faked_project/Rakefile
Expand Up @@ -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
24 changes: 24 additions & 0 deletions 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
16 changes: 16 additions & 0 deletions 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
11 changes: 11 additions & 0 deletions 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"