Skip to content

Commit 804b51e

Browse files
committed
Fix test leak due to using class instance variables and inheritance
Here's a simple script to illustrate the problem: ```rb class Foo def initialize @@options ||= "Foo @@options initialized!" end def get_options @@options end end class Bar < Foo def reset_options @@options = "@@options reset in Bar!" end end Bar.new.reset_options puts Foo.new.get_options #=> "@@options reset in Bar!" ``` Here, the `Configuration` test class defined in `dynamic_options_test.rb` by inheritance from `Rails::Railtie::Configuration` ends up overriding the latter's `@@options` class instance variable. I'm not sure I understand the general use of `@@` class instance variables in `Rails::Railstie::Configuration` (I problably wouldn't), but this fix allows me to remove the leak without changing the implementation. To reproduce and confirm the fix, run this test: ```sh-session $ bin/test test/rails_health_controller_test.rb test/configuration/dynamic_options_test.rb --seed 15527 ```
1 parent 8ceb7b9 commit 804b51e

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

railties/test/configuration/dynamic_options_test.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@
77

88
module RailtiesTest
99
class DynamicOptionsTest < ActiveSupport::TestCase
10-
class Configuration < Rails::Railtie::Configuration
11-
def reset_options
12-
@@options = {}
13-
end
14-
end
15-
1610
setup do
17-
@config = Configuration.new
18-
@config.reset_options
11+
@config = Rails::Railtie::Configuration.dup.new
12+
@config.class.class_variable_set(:@@options, {})
1913
end
2014

2115
test "arbitrary keys can be set, reset, and read" do

0 commit comments

Comments
 (0)