From 6beead38678d6a25adc94ee82ed07974e3e20147 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 15 May 2024 05:33:36 -0400 Subject: [PATCH] Move `simplecov` config into `rails_helper` (#30302) --- .simplecov | 22 ---------------- config/initializers/simple_cov_source_file.rb | 19 ++++++++++++++ spec/rails_helper.rb | 25 +++++++++++++++++++ spec/spec_helper.rb | 4 --- 4 files changed, 44 insertions(+), 26 deletions(-) delete mode 100644 .simplecov create mode 100644 config/initializers/simple_cov_source_file.rb diff --git a/.simplecov b/.simplecov deleted file mode 100644 index fbd0207bec..0000000000 --- a/.simplecov +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -if ENV['CI'] - require 'simplecov-lcov' - SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true - SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter -else - SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter -end - -SimpleCov.start 'rails' do - enable_coverage :branch - - add_filter 'lib/linter' - - add_group 'Libraries', 'lib' - add_group 'Policies', 'app/policies' - add_group 'Presenters', 'app/presenters' - add_group 'Serializers', 'app/serializers' - add_group 'Services', 'app/services' - add_group 'Validators', 'app/validators' -end diff --git a/config/initializers/simple_cov_source_file.rb b/config/initializers/simple_cov_source_file.rb new file mode 100644 index 0000000000..c6b3586c82 --- /dev/null +++ b/config/initializers/simple_cov_source_file.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# TODO: https://github.com/simplecov-ruby/simplecov/pull/1084 +# Patches this missing condition, monitor for upstream fix + +module SimpleCov + module SourceFileExtensions + def build_branches + coverage_branch_data = coverage_data.fetch('branches', {}) || {} # Add the final empty hash in case where 'branches' is present, but returns nil + branches = coverage_branch_data.flat_map do |condition, coverage_branches| + build_branches_from(condition, coverage_branches) + end + + process_skipped_branches(branches) + end + end +end + +SimpleCov::SourceFile.prepend(SimpleCov::SourceFileExtensions) if defined?(SimpleCov::SourceFile) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index d8eb561d42..38aa711089 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -2,6 +2,31 @@ ENV['RAILS_ENV'] ||= 'test' +unless ENV['DISABLE_SIMPLECOV'] == 'true' + require 'simplecov' + + SimpleCov.start 'rails' do + if ENV['CI'] + require 'simplecov-lcov' + formatter SimpleCov::Formatter::LcovFormatter + formatter.config.report_with_single_file = true + else + formatter SimpleCov::Formatter::HTMLFormatter + end + + enable_coverage :branch + + add_filter 'lib/linter' + + add_group 'Libraries', 'lib' + add_group 'Policies', 'app/policies' + add_group 'Presenters', 'app/presenters' + add_group 'Serializers', 'app/serializers' + add_group 'Services', 'app/services' + add_group 'Validators', 'app/validators' + end +end + # This needs to be defined before Rails is initialized STREAMING_PORT = ENV.fetch('TEST_STREAMING_PORT', '4020') ENV['STREAMING_API_BASE_URL'] = "http://localhost:#{STREAMING_PORT}" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8a01792a19..1f9cc40f12 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -unless ENV['DISABLE_SIMPLECOV'] == 'true' - require 'simplecov' # Configuration details loaded from .simplecov -end - RSpec.configure do |config| config.example_status_persistence_file_path = 'tmp/rspec/examples.txt' config.expect_with :rspec do |expectations|