From 68e2f73a88572c55cefed688bc68a9256a86109f Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 18 Jan 2024 10:20:27 -0500 Subject: [PATCH 1/2] TestHelp: Reconfigure once, remove all features before each test --- lib/flipper/test_help.rb | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/flipper/test_help.rb b/lib/flipper/test_help.rb index 14a7e7251..88f8e3fe3 100644 --- a/lib/flipper/test_help.rb +++ b/lib/flipper/test_help.rb @@ -1,17 +1,24 @@ module Flipper module TestHelp + extend self + def flipper_configure - # Create a single shared memory adapter instance for each test - @flipper_adapter = Flipper::Adapters::Memory.new + # Use a shared Memory adapter for all tests. This is instantiated outside of the + # `configure` block so the same instance is returned in new threads. + adapter = Flipper::Adapters::Memory.new Flipper.configure do |config| - config.adapter { @flipper_adapter } + config.adapter { adapter } config.default { Flipper.new(config.adapter) } end end def flipper_reset - Flipper.instance = nil # Reset previous flipper instance + # Remove all features + Flipper.features.each(&:remove) rescue nil + + # Reset previous DSL instance + Flipper.instance = nil end end end @@ -19,19 +26,17 @@ def flipper_reset if defined?(RSpec) && RSpec.respond_to?(:configure) RSpec.configure do |config| config.include Flipper::TestHelp - config.before(:each) do - flipper_reset - flipper_configure - end + config.before(:suite) { Flipper::TestHelp.flipper_configure } + config.before(:each) { flipper_reset } end end - if defined?(ActiveSupport) ActiveSupport.on_load(:active_support_test_case) do + Flipper::TestHelp.flipper_configure + ActiveSupport::TestCase.class_eval do include Flipper::TestHelp - setup :flipper_configure setup :flipper_reset end end From bcafbc47e7e2dfc456ea5c9e3a8329f4a3d548c2 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 18 Jan 2024 10:52:39 -0500 Subject: [PATCH 2/2] Reconfigure in integration test --- test_rails/system/test_help_test.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test_rails/system/test_help_test.rb b/test_rails/system/test_help_test.rb index 6c5e495ef..dbcd99b82 100644 --- a/test_rails/system/test_help_test.rb +++ b/test_rails/system/test_help_test.rb @@ -31,8 +31,13 @@ class TestHelpTest < ActionDispatch::SystemTestCase # Any driver that runs the app in a separate thread will test what we want here. driven_by :cuprite, options: { process_timeout: 30 } - # Ensure this test uses this app instance - setup { Rails.application = TestApp.instance } + setup do + # Reconfigure Flipper since other tests change the adapter. + flipper_configure + + # Ensure this test uses this app instance + Rails.application = TestApp.instance + end test "configures a shared adapter between tests and app" do Flipper.disable(:test)