simplecov-ruby / simplecov

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites
MIT License
4.76k stars 552 forks source link

README says SimpleCov.start should be called after SimpleCov.at_fork, is it true? #1085

Closed manicmaniac closed 5 months ago

manicmaniac commented 5 months ago

See https://github.com/simplecov-ruby/simplecov/blob/0260b1f309e76c8ed87731ebbc348b05d753b8a2/README.md#L650-L654

The document says SimpleCov.start should be called after SimpleCov.at_fork when we need to collect coverage of subprocesses.

However, the current implementation of SimpleCov.at_fork internally calls SimpleCov.start, so I'm wondering that manually invoking SimpleCov.start is needed or not.

https://github.com/simplecov-ruby/simplecov/blob/0260b1f309e76c8ed87731ebbc348b05d753b8a2/lib/simplecov/configuration.rb#L233-L245

As far as I tested, that manual SimpleCov.start seems not only unnecessary but misleading under some conditions.

Let's say that you need to collect branch coverage of subprocess, you will read the document and write some code like the following:

# .simplecov_spawn.rb
require 'simplecov'

SimpleCov.command_name 'spawn'
SimpleCov.at_fork.call(Process.pid)
SimpleCov.start do
  enable_coverage :branch
end

It does not work because SimpleCov.start is already called without enabling branch coverage, and the latter invocation does not seem to overwrite the former configurations.

PragTob commented 5 months ago

:wave:

Hi there,

yes it's still necessary. The at_fork block will only be called if the process is forked. You still need to start SimpleCov for the main process under test.

In your example script you're simply calling the block stored in at_fork although no process was forked. Also be mindful that at_fork is a block you can set yourself so it doesn't need to have the default values.

Documentation for the feature could probably be better/more in depth :)