Disable parallel for small test coverage runs

`brew tests --coverage` can fail to produce coverage information when
run on a small number of tests (e.g., `--only utils/curl`). We use
`ParallelTests::last_process?` in `tests/spec_helper.rb` to handle
the SimpleCov output but due to the way the method is implemented, it
doesn't work as expected if the number of processes is greater than
one but lower than the number of cores. I tried to address this
through changes to  `spec_helper.rb` and/or changes to `ParallelTests`
but didn't meet with any success.

This works around the issue by disabling parallel test execution when
the `--coverage` option is used and the number of files to be tested
is lower than the number of CPU cores. I've been using this workaround
for months and it works as expected on my machine.
This commit is contained in:
Sam Ford 2025-01-09 15:42:27 -05:00
parent 5faa0e1dbf
commit cb514a1d04
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D

View File

@ -3,6 +3,7 @@
require "abstract_command"
require "fileutils"
require "hardware"
require "system_command"
module Homebrew
@ -75,7 +76,13 @@ module Homebrew
end
end
parallel = false if args.profile
# We use `ParallelTests.last_process?` in `test/spec_helper.rb` to
# handle SimpleCov output but, due to how the method is implemented,
# it doesn't work as expected if the number of processes is greater
# than one but lower than the number of CPU cores in the execution
# environment. Coverage information isn't saved in that scenario,
# so we disable parallel testing as a workaround in this case.
parallel = false if args.profile || (args.coverage? && files.length < Hardware::CPU.cores)
parallel_rspec_log_name = "parallel_runtime_rspec"
parallel_rspec_log_name = "#{parallel_rspec_log_name}.generic" if args.generic?