From e892efbc4081129fbe807d3b6d8dec024e2175ed Mon Sep 17 00:00:00 2001
From: Matt Jankowski <matt@jankowski.online>
Date: Fri, 17 Nov 2023 06:52:20 -0500
Subject: [PATCH] Configure elastic search integration with rspec tag (#27882)

---
 .github/workflows/test-ruby.yml     |  4 ++--
 lib/tasks/spec.rake                 | 10 ----------
 spec/rails_helper.rb                | 13 ++++++++++---
 spec/support/search_data_manager.rb |  4 ++--
 4 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/.github/workflows/test-ruby.yml b/.github/workflows/test-ruby.yml
index 101de66ac..ae25648a0 100644
--- a/.github/workflows/test-ruby.yml
+++ b/.github/workflows/test-ruby.yml
@@ -227,7 +227,7 @@ jobs:
           path: tmp/screenshots/
 
   test-search:
-    name: Testing search
+    name: Elastic Search integration testing
     runs-on: ubuntu-latest
 
     needs:
@@ -314,7 +314,7 @@ jobs:
       - name: Load database schema
         run: './bin/rails db:create db:schema:load db:seed'
 
-      - run: bundle exec rake spec:search
+      - run: bin/rspec --tag search
 
       - name: Archive logs
         uses: actions/upload-artifact@v3
diff --git a/lib/tasks/spec.rake b/lib/tasks/spec.rake
index ec4cd39bf..8f2cbeea3 100644
--- a/lib/tasks/spec.rake
+++ b/lib/tasks/spec.rake
@@ -9,13 +9,3 @@ if Rake::Task.task_defined?('spec:system')
 
   Rake::Task['spec:system'].enhance ['spec:enable_system_specs']
 end
-
-if Rake::Task.task_defined?('spec:search')
-  namespace :spec do
-    task :enable_search_specs do # rubocop:disable Rails/RakeEnvironment
-      ENV['RUN_SEARCH_SPECS'] = 'true'
-    end
-  end
-
-  Rake::Task['spec:search'].enhance ['spec:enable_search_specs']
-end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 68023b70d..7deab6c7f 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -4,7 +4,6 @@ ENV['RAILS_ENV'] ||= 'test'
 
 # This needs to be defined before Rails is initialized
 RUN_SYSTEM_SPECS = ENV.fetch('RUN_SYSTEM_SPECS', false)
-RUN_SEARCH_SPECS = ENV.fetch('RUN_SEARCH_SPECS', false)
 
 if RUN_SYSTEM_SPECS
   STREAMING_PORT = ENV.fetch('TEST_STREAMING_PORT', '4020')
@@ -55,20 +54,28 @@ RSpec.configure do |config|
     case type
     when :system
       !RUN_SYSTEM_SPECS
-    when :search
-      !RUN_SEARCH_SPECS
     end
   }
+
+  # By default, skip the elastic search integration specs
+  config.filter_run_excluding search: true
+
   config.fixture_path = Rails.root.join('spec', 'fixtures')
   config.use_transactional_fixtures = true
   config.order = 'random'
   config.infer_spec_type_from_file_location!
   config.filter_rails_from_backtrace!
 
+  # Set type to `cli` for all CLI specs
   config.define_derived_metadata(file_path: Regexp.new('spec/lib/mastodon/cli')) do |metadata|
     metadata[:type] = :cli
   end
 
+  # Set `search` metadata true for all specs in spec/search/
+  config.define_derived_metadata(file_path: Regexp.new('spec/search/*')) do |metadata|
+    metadata[:search] = true
+  end
+
   config.include Devise::Test::ControllerHelpers, type: :controller
   config.include Devise::Test::ControllerHelpers, type: :helper
   config.include Devise::Test::ControllerHelpers, type: :view
diff --git a/spec/support/search_data_manager.rb b/spec/support/search_data_manager.rb
index 176a674ad..3c7140b48 100644
--- a/spec/support/search_data_manager.rb
+++ b/spec/support/search_data_manager.rb
@@ -60,7 +60,7 @@ RSpec.configure do |config|
     end
   end
 
-  config.around :each, type: :search do |example|
+  config.around :each, :search do |example|
     search_data_manager.populate_indexes
     example.run
     search_data_manager.remove_indexes
@@ -73,6 +73,6 @@ RSpec.configure do |config|
   end
 
   def search_examples_present?
-    RUN_SEARCH_SPECS
+    RSpec.world.filtered_examples.values.flatten.any? { |example| example.metadata[:search] == true }
   end
 end