From b05575e2425f45bd0b854c850787661a5d43bb98 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 8 Nov 2023 08:04:17 -0500 Subject: [PATCH] Move RSpec config for streaming/search managers to be near classes (#27761) --- spec/rails_helper.rb | 31 --------------------- spec/support/search_data_manager.rb | 35 ++++++++++++++++++++++++ spec/support/streaming_server_manager.rb | 29 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c2ef3e315..79f98f2e2 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -11,10 +11,6 @@ if RUN_SYSTEM_SPECS ENV['STREAMING_API_BASE_URL'] = "http://localhost:#{STREAMING_PORT}" end -if RUN_SEARCH_SPECS - # Include any configuration or setups specific to search tests here -end - require File.expand_path('../config/environment', __dir__) abort('The Rails environment is running in production mode!') if Rails.env.production? @@ -35,8 +31,6 @@ Sidekiq.logger = nil # System tests config DatabaseCleaner.strategy = [:deletion] -streaming_server_manager = StreamingServerManager.new -search_data_manager = SearchDataManager.new Devise::Test::ControllerHelpers.module_eval do alias_method :original_sign_in, :sign_in @@ -100,26 +94,7 @@ RSpec.configure do |config| Capybara.current_driver = :rack_test end - config.before :suite do - if RUN_SYSTEM_SPECS - Webpacker.compile - streaming_server_manager.start(port: STREAMING_PORT) - end - - if RUN_SEARCH_SPECS - Chewy.strategy(:urgent) - search_data_manager.prepare_test_data - end - end - - config.after :suite do - streaming_server_manager.stop - - search_data_manager.cleanup_test_data if RUN_SEARCH_SPECS - end - config.around :each, type: :system do |example| - # driven_by :selenium, using: :chrome, screen_size: [1600, 1200] driven_by :selenium, using: :headless_chrome, screen_size: [1600, 1200] # The streaming server needs access to the database @@ -136,12 +111,6 @@ RSpec.configure do |config| self.use_transactional_tests = true end - config.around :each, type: :search do |example| - search_data_manager.populate_indexes - example.run - search_data_manager.remove_indexes - end - config.before do |example| unless example.metadata[:paperclip_processing] allow_any_instance_of(Paperclip::Attachment).to receive(:post_process).and_return(true) # rubocop:disable RSpec/AnyInstance diff --git a/spec/support/search_data_manager.rb b/spec/support/search_data_manager.rb index 24c95ae06..176a674ad 100644 --- a/spec/support/search_data_manager.rb +++ b/spec/support/search_data_manager.rb @@ -41,3 +41,38 @@ class SearchDataManager Tag.destroy_all end end + +RSpec.configure do |config| + config.before :suite do + if search_examples_present? + # Configure chewy to use `urgent` strategy to index documents + Chewy.strategy(:urgent) + + # Create search data + search_data_manager.prepare_test_data + end + end + + config.after :suite do + if search_examples_present? + # Clean up after search data + search_data_manager.cleanup_test_data + end + end + + config.around :each, type: :search do |example| + search_data_manager.populate_indexes + example.run + search_data_manager.remove_indexes + end + + private + + def search_data_manager + @search_data_manager ||= SearchDataManager.new + end + + def search_examples_present? + RUN_SEARCH_SPECS + end +end diff --git a/spec/support/streaming_server_manager.rb b/spec/support/streaming_server_manager.rb index 93e1a6d17..2c535767e 100644 --- a/spec/support/streaming_server_manager.rb +++ b/spec/support/streaming_server_manager.rb @@ -76,3 +76,32 @@ class StreamingServerManager @running_thread.join end end + +RSpec.configure do |config| + config.before :suite do + if streaming_examples_present? + # Compile assets + Webpacker.compile + + # Start the node streaming server + streaming_server_manager.start(port: STREAMING_PORT) + end + end + + config.after :suite do + if streaming_examples_present? + # Stop the node streaming server + streaming_server_manager.stop + end + end + + private + + def streaming_server_manager + @streaming_server_manager ||= StreamingServerManager.new + end + + def streaming_examples_present? + RUN_SYSTEM_SPECS + end +end