Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subprocess handling to simplecov (#881)
* Add subprocess handling to simplecov Using Process.fork (whether using the Parallel gem or directly) creates code that was invisible to SimpleCov. by starting SimpleCov within the subprocess with its own command name and etc we can see that code :) This also adds documentation for what to do when using Process.spawn or similar. fixes: #414 * Subprocesses is off by default Can be enabled with enable_for_subprocesses Also moved the testing to a feature test * Fix test to work for ruby 2.4 also * Have spawn doc match closer to spec * Remove unnecessary = Also Gem::Version.new comparisons please. Also don't run this test on jruby, Process.fork is NotImplementedError
- Loading branch information
Showing
14 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
@rspec @process_fork | ||
|
||
Feature: | ||
Coverage should include code run by subprocesses | ||
|
||
Background: | ||
Given I'm working on the project "subprocesses" | ||
|
||
Scenario: Coverage has seen the subprocess line | ||
When I open the coverage report generated with `bundle exec rspec spec/simple_spec.rb` | ||
Then I should see the groups: | ||
| name | coverage | files | | ||
| All Files | 100.0% | 1 | | ||
|
||
Scenario: The at_fork proc | ||
Given a file named ".simplecov" with: | ||
""" | ||
SimpleCov.enable_for_subprocesses true | ||
SimpleCov.command_name "parent process name" | ||
SimpleCov.at_fork do |_pid| | ||
SimpleCov.command_name "child process name" | ||
SimpleCov.start | ||
end | ||
""" | ||
When I open the coverage report generated with `bundle exec rspec spec/simple_spec.rb` | ||
Then I should see the groups: | ||
| name | coverage | files | | ||
| All Files | 100.0% | 1 | | ||
And the report should be based upon: | ||
| child process name | | ||
| parent process name | | ||
|
||
Scenario: The documentation on .simplecov_spawn | ||
When I open the coverage report generated with `bundle exec rspec spec/spawn_spec.rb` | ||
Then I should see the groups: | ||
| name | coverage | files | | ||
| All Files | 100.0% | 1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Process | ||
class << self | ||
def fork_with_simplecov(&block) | ||
if defined?(SimpleCov) && SimpleCov.running | ||
fork_without_simplecov do | ||
SimpleCov.at_fork.call(Process.pid) | ||
block.call if block_given? | ||
end | ||
else | ||
fork_without_simplecov(&block) | ||
end | ||
end | ||
|
||
alias fork_without_simplecov fork | ||
alias fork fork_with_simplecov | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
SimpleCov.enable_for_subprocesses true | ||
# different versions of ruby were tracking different numbers of files. idk why. | ||
# lets only worry about one file. | ||
SimpleCov.add_filter /command/ | ||
SimpleCov.add_filter /spawn/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'bundler/setup' | ||
require 'simplecov' | ||
SimpleCov.command_name 'spawn' | ||
SimpleCov.at_fork.call(Process.pid) | ||
SimpleCov.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require_relative './subprocesses' | ||
|
||
Subprocesses.new.run | ||
|
||
puts 'done' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Subprocesses | ||
def run | ||
method_called_in_parent_process | ||
|
||
pid = Process.fork do | ||
method_called_by_subprocess | ||
end | ||
|
||
Process.wait(pid) | ||
|
||
true | ||
end | ||
|
||
def method_called_in_parent_process | ||
true | ||
end | ||
|
||
def method_called_by_subprocess | ||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require_relative "spec_helper" | ||
|
||
describe Subprocesses do | ||
it "call things" do | ||
expect(subject.run).to be true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'spec_helper' | ||
require 'open3' | ||
describe 'spawn' do | ||
it 'calls things' do | ||
Dir.chdir(File.expand_path('..', __dir__)) do | ||
stdout, exitstatus = Open3.capture2("ruby -r./.simplecov_spawn lib/command") | ||
expect(stdout.chomp).to eq 'done' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require "simplecov" | ||
SimpleCov.start | ||
|
||
require_relative "../lib/subprocesses.rb" |