Skip to content

Commit 945afed

Browse files
committed
Add simplecov plugin for Minitest
1 parent 8d9a314 commit 945afed

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

features/minitest_basic.feature

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,26 @@ Feature:
1010
When I open the coverage report generated with `bundle exec rake minitest`
1111
Then I should see the groups:
1212
| name | coverage | files |
13-
| All Files | 80.0% | 1 |
13+
| All Files | 85.71% | 3 |
1414

1515
And I should see the source files:
1616
| name | coverage |
1717
| lib/faked_project/some_class.rb | 80.0 % |
18+
| minitest/some_test.rb | 100.0 % |
19+
| minitest/test_helper.rb | 75.0 % |
20+
21+
Scenario:
22+
Given SimpleCov for Minitest is configured with:
23+
"""
24+
require 'simplecov'
25+
SimpleCov.start
26+
"""
1827

28+
When I open the coverage report generated with `bundle exec ruby -Ilib:minitest minitest/other_test.rb`
29+
Then I should see the groups:
30+
| name | coverage | files |
31+
| All Files | 80.0% | 1 |
32+
33+
And I should see the source files:
34+
| name | coverage |
35+
| lib/faked_project/some_class.rb | 80.0 % |

lib/minitest/simplecov_plugin.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Minitest
4+
def self.plugin_simplecov_init(_options)
5+
Minitest.after_run do
6+
SimpleCov.custom_at_exit if SimpleCov.respond_to?(:custom_at_exit)
7+
end
8+
end
9+
end

lib/simplecov/defaults.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,21 @@
2121
# Gotta stash this a-s-a-p, see the CommandGuesser class and i.e. #110 for further info
2222
SimpleCov::CommandGuesser.original_run_command = "#{$PROGRAM_NAME} #{ARGV.join(' ')}"
2323

24-
if defined?(Minitest)
25-
Minitest.after_run do
26-
simplecov_at_exit
27-
end
28-
else
29-
at_exit do
30-
simplecov_at_exit
24+
module SimpleCov
25+
def self.custom_at_exit
26+
# If we are in a different process than called start, don't interfere.
27+
return if SimpleCov.pid != Process.pid
28+
29+
SimpleCov.set_exit_exception
30+
SimpleCov.run_exit_tasks!
3131
end
3232
end
3333

34-
def simplecov_at_exit
35-
# If we are in a different process than called start, don't interfere.
36-
return if SimpleCov.pid != Process.pid
34+
at_exit do
35+
# Exit hook for Minitest defined in Minitest plugin
36+
next if defined?(Minitest)
3737

38-
SimpleCov.set_exit_exception
39-
SimpleCov.run_exit_tasks!
38+
SimpleCov.custom_at_exit
4039
end
4140

4241
# Autoload config from ~/.simplecov if present
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "bundler/setup"
2+
3+
begin
4+
require File.expand_path("simplecov_config", __dir__)
5+
rescue LoadError
6+
warn "No SimpleCov config file found!"
7+
end
8+
9+
require "minitest/autorun"
10+
require "faked_project/some_class"
11+
12+
class OtherTest < Minitest::Test
13+
def setup
14+
@instance = SomeClass.new("foo")
15+
end
16+
17+
def test_reverse
18+
assert_equal "oof", @instance.reverse
19+
end
20+
21+
def test_comparison
22+
assert @instance.compare_with("foo")
23+
end
24+
end

0 commit comments

Comments
 (0)