Skip to content

Commit

Permalink
Fix at_exit hook to cooperate with Minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
adam12 authored and PragTob committed Feb 6, 2020
1 parent e56dc46 commit a3ec24d
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -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.1 (2020-01-31)
===================

Expand Down
1 change: 1 addition & 0 deletions Gemfile
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
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
36 changes: 36 additions & 0 deletions features/minitest_basic.feature
@@ -0,0 +1,36 @@
Feature:

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.0 % |
| minitest/some_test.rb | 100.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
11 changes: 11 additions & 0 deletions lib/minitest/simplecov_plugin.rb
@@ -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
Expand Up @@ -189,6 +189,14 @@ def exit_status_from_exception
end
end

def at_exit_behvior
# 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
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
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
@@ -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
@@ -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
@@ -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"

0 comments on commit a3ec24d

Please sign in to comment.