Skip to content

Commit

Permalink
Add simplecov plugin for Minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
adam12 committed Dec 6, 2019
1 parent 8d9a314 commit 945afed
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
19 changes: 18 additions & 1 deletion features/minitest_basic.feature
Expand Up @@ -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 % |
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
23 changes: 11 additions & 12 deletions lib/simplecov/defaults.rb
Expand Up @@ -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
Expand Down
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

0 comments on commit 945afed

Please sign in to comment.