From 2555a9e710fb6a9e0c58ee06087c5a8c7794a7fb Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 5 Aug 2025 12:35:44 +1000 Subject: [PATCH] Ignore breaking code coverage for coverage spec When we test our code coverage compilation, it breaks the code coverage report for the current rspec process. By running that code separately, we gain a correct coverage report for the rest of the code again. So unfortunately, we can't report on the code coverage of this particular task and have to ignore it. But at least CI depends on the correct function of this task and would fail if it didn't work. --- .simplecov | 3 --- lib/tasks/simplecov.rake | 4 ++++ spec/lib/tasks/simplecov_spec.rb | 12 +++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.simplecov b/.simplecov index dd47f33e21..8f18990a6a 100755 --- a/.simplecov +++ b/.simplecov @@ -7,8 +7,5 @@ SimpleCov.start 'rails' do add_filter '/script' add_filter '/db' - # We haven't managed to make simplecov recognise rake coverage accurately. - add_filter '/lib/tasks/' - formatter SimpleCov::Formatter::SimpleFormatter end diff --git a/lib/tasks/simplecov.rake b/lib/tasks/simplecov.rake index cc7f5c95e6..369929c195 100644 --- a/lib/tasks/simplecov.rake +++ b/lib/tasks/simplecov.rake @@ -4,6 +4,9 @@ namespace :simplecov do desc "Collates all result sets produced during parallel test runs" task :collate_results, # rubocop:disable Rails/RakeEnvironment doesn't need the full env [:path_to_results, :coverage_dir] do |_t, args| + # This code is covered by a spec but trying to measure the code coverage of + # the spec breaks the coverage report. We need to ignore it to avoid warnings. + # :nocov: require "simplecov" require "undercover/simplecov_formatter" @@ -19,5 +22,6 @@ namespace :simplecov do formatter(SimpleCov::Formatter::Undercover) coverage_dir(output_path) end + # :nocov: end end diff --git a/spec/lib/tasks/simplecov_spec.rb b/spec/lib/tasks/simplecov_spec.rb index f752ea1e84..e93b6324b7 100644 --- a/spec/lib/tasks/simplecov_spec.rb +++ b/spec/lib/tasks/simplecov_spec.rb @@ -13,10 +13,16 @@ RSpec.describe "simplecov.rake" do Dir.mktmpdir do |tmp_dir| output_dir = File.join(tmp_dir, "output") + task_name = "simplecov:collate_results[#{input_dir},#{output_dir}]" + expect { - invoke_task( - "simplecov:collate_results[#{input_dir},#{output_dir}]" - ) + if ENV["COVERAGE"] + # Start task in a new process to not mess with our coverage report. + `bundle exec rake #{task_name}` + else + # Use the quick standard invocation in dev. + invoke_task(task_name) + end }.to change { Dir.exist?(output_dir) }. from(false). to(true).