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

Rebase: Fix at_exit hook to cooperate with Minitest #855

Merged
merged 2 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
unreleased
===========

## Bugfixes
* Use `Minitest.after_run` hook to trigger post-run hooks if `Minitest` is present. See [#756](https://github.com/colszowka/simplecov/pull/756) and [#855](https://github.com/colszowka/simplecov/pull/855) thanks ([@adam12](https://github.com/adam12))

0.18.2 (2020-02-12)
===================

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ group :development do
gem "aruba", github: "cucumber/aruba"
gem "capybara", "~> 3.31"
gem "cucumber", "~> 3.1"
gem "minitest"
gem "rake", "~> 13.0"
gem "rspec", "~> 3.2"
gem "pry"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ GEM
method_source (0.9.2)
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 @@ -135,6 +136,7 @@ DEPENDENCIES
benchmark-ips
capybara (~> 3.31)
cucumber (~> 3.1)
minitest
pry
rake (~> 13.0)
rspec (~> 3.2)
Expand Down
39 changes: 39 additions & 0 deletions features/minitest_basic.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Feature:

Background:
Given I'm working on the project "faked_project"

Scenario:
Given SimpleCov for Minitest is configured with:
"""
require 'simplecov'
SimpleCov.start do
add_filter "test_helper.rb"
end
"""

When I open the coverage report generated with `bundle exec rake minitest`
Then I should see the groups:
| name | coverage | files |
| All Files | 87.5% | 2 |

And I should see the source files:
| name | coverage |
| lib/faked_project/some_class.rb | 80.00 % |
| minitest/some_test.rb | 100.00 % |

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.00 % |
2 changes: 2 additions & 0 deletions features/step_definitions/simplecov_steps.rb
Original file line number Diff line number Diff line change
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
11 changes: 11 additions & 0 deletions lib/minitest/simplecov_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# How minitest plugins. See https://github.com/colszowka/simplecov/pull/756 for why we need this.
# https://github.com/seattlerb/minitest#writing-extensions
module Minitest
def self.plugin_simplecov_init(_options)
Minitest.after_run do
SimpleCov.at_exit_behvior if SimpleCov.respond_to?(:at_exit_behvior)
end
end
end
8 changes: 8 additions & 0 deletions lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ def exit_status_from_exception
end
end

def at_exit_behvior
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo at behvior? behavior?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thank you!

# If we are in a different process than called start, don't interfere.
return if SimpleCov.pid != Process.pid

# If SimpleCov is no longer running then don't run exit tasks
SimpleCov.run_exit_tasks! if SimpleCov.running
end

# @api private
#
# Called from at_exit block
Expand Down
9 changes: 3 additions & 6 deletions lib/simplecov/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
SimpleCov::CommandGuesser.original_run_command = "#{$PROGRAM_NAME} #{ARGV.join(' ')}"

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)

# If SimpleCov is no longer running then don't run exit tasks
next unless SimpleCov.running

SimpleCov.run_exit_tasks!
SimpleCov.at_exit_behvior
end

# Autoload config from ~/.simplecov if present
Expand Down
6 changes: 6 additions & 0 deletions test_projects/faked_project/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ task :collate do
require "simplecov"
SimpleCov.collate Dir["coverage/resultset*.json"]
end

Rake::TestTask.new(:minitest) do |test|
test.libs << "minitest"
test.test_files = FileList["minitest/**/*_test.rb"].sort
test.verbose = true
end
26 changes: 26 additions & 0 deletions test_projects/faked_project/minitest/other_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

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
18 changes: 18 additions & 0 deletions test_projects/faked_project/minitest/some_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

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
13 changes: 13 additions & 0 deletions test_projects/faked_project/minitest/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

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"