Add post-suite hooks for SimpleCov integration#28
Conversation
Introduces a class-level hook registry that enables callbacks to run in the parent process after all workers complete but before printing summary and exiting. This allows libraries like SimpleCov to aggregate results from forked workers without requiring manual invocation outside of spec_helper configuration. New API: - RSpec::Conductor.register_post_suite_hook(&block) Hooks are called after suite_run.suite_complete, enabling access to merged worker results before final exit code determination. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
If SimpleCov is loaded, automatically register the result merging hook in the parent process. No configuration needed in applications. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
|
@markiz, what are your thoughts on the simplecov issue? |
|
@cb341 But my question is gonna be: what's so bad about running a one-line bash-script in the CI? I'm a bit reluctant to add a feature that's specific to simplecov unless there's something that's not achievable in a straightforward way with other tools. |
|
Thanks for your quick response @markiz. Sorry for finding time only now to respond. Because rspec-conductor can in some ways be interpreted as the natural successor to parallel_tests, inlining features such as coverage report concatenation for SimpleCov reduces the friction of migrating to rspec-conductor.
I believe that not having to update existing project configuration to accommodate rspec-conductor will improve the developer experience. SimpleCov already has built-in support for parallel_tests, and if rspec-conductor is supposed to serve as a drop-in replacement, I'd expect coverage to work out of the box. It seems reasonable for me to assume people running tests with parallel_tests also have code coverage checks with simplecov. Alternatively, we could mention the changes needed for SimpleCov to work properly directly in the README. Figuring out how to configure SimpleCov to concatenate reports by yourself takes time. Reading the gem's development philosophy:
it'd probably make the most sense to stick with the README changes to adhere to the gem's philosophy. |
|
@cb341
Let me think about this a little. Maybe we need a per-worker post-suite-require (in line with the other config options), so that you could write the simplecov result there |
|
Can you try adding this file to your app, let's say require 'bundler/setup'
require 'simplecov'
require 'rspec/core'
SimpleCov.start do
formatter SimpleCov::Formatter::MultiFormatter.new(
[
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::SimpleFormatter,
]
)
end
RSpec.configure do |config|
# This will trigger per worker on suite start
config.before(:suite) do
if ENV['TEST_ENV_NUMBER']
SimpleCov.command_name "rspec #{ENV['TEST_ENV_NUMBER']}"
else
SimpleCov.command_name "rspec"
end
end
# This will trigger per worker on suite finish
config.after(:suite) do
SimpleCov.result.format!
end
end
# This will trigger once all workers finish
SimpleCov.at_exit do
result = SimpleCov::ResultMerger.merged_result
if result
result.format!
SimpleCov.process_result(result)
end
end
# This will load the rails app
require_relative '../config/application'then run with This should about cover it, I think? Piggybacking off the rspec before/after suite hooks since those are run per worker already. |
🤖 This PR was authored by Claude Haiku and Opus (Anthropic).
Problem
SimpleCov doesn't support rspec-conductor. When tests run across forked workers, each process writes its own coverage data, but there's no mechanism to merge results in the parent process after workers exit.
SimpleCov handles this for
parallel_teststhrough explicit integration:CommandGuesser.from_envdetectsPARALLEL_TEST_GROUPS/TEST_ENV_NUMBERto assign unique command names per workerfinal_result_process?gates reporting to run only in the lastParallelTestsprocesswait_for_other_processescallsParallelTests.wait_for_other_processes_to_finishbefore mergingNone of this works for rspec-conductor. Users are forced to manually invoke
ResultMerger.merged_resultandprocess_resultafter rspec-conductor exits, e.g. in CI:Related SimpleCov issues:
minimum_coveragevalidates too early, before all processes finishcommand_name/merge_timeoutsetupSolution
Add a post-suite hook mechanism: callbacks that run in the parent process after all workers complete, before printing the summary.
When
SimpleCovis defined, rspec-conductor automatically registers a hook that callsmerged_resultandprocess_result. No application-side code needed. This mirrors what SimpleCov does internally forparallel_testsviafinal_result_process?+wait_for_other_processes, but driven from the runner side.Changes
Server.post_suite_hooks/Server.register_post_suite_hook: class-level hook registryServer#run: call hooks aftersuite_run.suite_complete, beforeprint_summaryRSpec::Conductor.register_post_suite_hook: public API (delegates toServer)SimpleCovconstant is defined (parent process only)