Skip to content

Fallback to empty array if branches is missing#1140

Merged
sferik merged 2 commits intosimplecov-ruby:mainfrom
exoego:branch-fix
Mar 28, 2026
Merged

Fallback to empty array if branches is missing#1140
sferik merged 2 commits intosimplecov-ruby:mainfrom
exoego:branch-fix

Conversation

@exoego
Copy link
Copy Markdown
Contributor

@exoego exoego commented Sep 3, 2025

When I enabled branch coverage in exoego/rspec-openapi#125, I found this line raises error because of coverage_branch_data is nil.

Sorry, I was not able to make a minimum repro nor make a test case, but managed to add failing tests.
And the change is working in rspec-openapi repo.
Just an error log:

       Formatter SimpleCov::Formatter::CoberturaFormatter failed with NoMethodError: undefined method 'flat_map' for nil (/usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/source_file.rb:275:in 'SimpleCov::SourceFile#build_branches')
       /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/source_file.rb:275:in 'SimpleCov::SourceFile#build_branches': undefined method 'flat_map' for nil (NoMethodError)

             branches = coverage_branch_data.flat_map do |condition, coverage_branches|
                                            ^^^^^^^^^
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/source_file.rb:99:in 'SimpleCov::SourceFile#branches'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/source_file.rb:130:in 'SimpleCov::SourceFile#covered_branches'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/source_file.rb:349:in 'SimpleCov::SourceFile#branch_coverage_statistics'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/source_file.rb:36:in 'SimpleCov::SourceFile#coverage_statistics'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:109:in 'block in SimpleCov::FileList#compute_coverage_statistics_by_file'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:108:in 'Array#each'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:108:in 'Enumerable#each_with_object'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:108:in 'SimpleCov::FileList#compute_coverage_statistics_by_file'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:31:in 'SimpleCov::FileList#coverage_statistics_by_file'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:115:in 'SimpleCov::FileList#compute_coverage_statistics'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/file_list.rb:27:in 'SimpleCov::FileList#coverage_statistics'
       	from /usr/local/lib/ruby/3.4.0/forwardable.rb:240:in 'SimpleCov::Result#coverage_statistics'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb:43:in 'block in SimpleCov::ExitCodes::MinimumOverallCoverageCheck#calculate_minimum_violations'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb:39:in 'Hash#each'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb:39:in 'Enumerable#map'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb:39:in 'SimpleCov::ExitCodes::MinimumOverallCoverageCheck#calculate_minimum_violations'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb:35:in 'SimpleCov::ExitCodes::MinimumOverallCoverageCheck#minimum_violations'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb:12:in 'SimpleCov::ExitCodes::MinimumOverallCoverageCheck#failing?'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/exit_code_handling.rb:11:in 'Array#each'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/exit_code_handling.rb:11:in 'Enumerable#find'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/exit_codes/exit_code_handling.rb:11:in 'SimpleCov::ExitCodes::ExitCodeHandling.call'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov.rb:262:in 'SimpleCov.result_exit_status'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov.rb:249:in 'SimpleCov.process_result'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov.rb:234:in 'SimpleCov.process_results_and_report_error'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov.rb:192:in 'SimpleCov.run_exit_tasks!'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov.rb:179:in 'SimpleCov.at_exit_behavior'
       	from /usr/local/bundle/gems/simplecov-0.22.0/lib/simplecov/defaults.rb:30:in 'block in <top (required)>'
     # ./spec/spec_helper.rb:13:in 'SpecHelper#assert_run'
     # ./spec/spec_helper.rb:23:in 'block (2 levels) in SpecHelper#run_tests'
     # ./spec/spec_helper.rb:22:in 'Dir.chdir'
     # ./spec/spec_helper.rb:22:in 'block in SpecHelper#run_tests'
     # ./spec/spec_helper.rb:21:in 'Kernel#public_send'
     # ./spec/spec_helper.rb:21:in 'SpecHelper#run_tests'
     # ./spec/spec_helper.rb:29:in 'SpecHelper#rspec'
     # ./spec/rspec/rack_test_spec.rb:30:in 'block (3 levels) in <top (required)>'

Comment thread lib/simplecov/source_file.rb Outdated
Co-authored-by: Erik Berlin <sferik@gmail.com>
@sferik sferik merged commit 0a67d3b into simplecov-ruby:main Mar 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants