Skip to content

Commit 22c6c63

Browse files
committed
Prevent Rails.env change from leaking to other tests
How to reproduce the flaky failure caused by the leak: ```sh-session bin/test --seed=1 -v test/generators/scaffold_generator_test.rb:517 test/commands/console_test.rb:51 Run options: --seed=1 -v Rails::ConsoleTest#test_console_with_environment = 0.00 s = . ScaffoldGeneratorTest#test_scaffold_generator_database_with_aliases = 0.13 s = F Failure: ScaffoldGeneratorTest#test_scaffold_generator_database_with_aliases [test/generators/scaffold_generator_test.rb:521]: Expected migration db/secondary_migrate/create_posts.rb to exist, but was not found ``` It looks like an instance of `Rails::Console#start` will set `Rails.env` if the console was initialized with an environment option: https://github.com/rails/rails/blob/fac6689b41a7086ac2dee06770b491bf8b2864c1/railties/lib/rails/commands/console/console_command.rb#L40-L50 `Rails.env` is not reset when the test finishes, which can sometimes influence (and break other tests). This fix adds a setup/teardown to the offending test file to mitigate the leak. I'm starting to wonder if we need a more nuclear approach... Should we have a global setup/teardown that nukes and restores the `Rails` constant completely? (eg. restore a duplicate)
1 parent f481602 commit 22c6c63

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

railties/test/commands/console_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def self.start
1818
end
1919
end
2020

21+
def setup
22+
@prev_rails_env = Rails.env
23+
end
24+
25+
def teardown
26+
Rails.env = @prev_rails_env
27+
end
28+
2129
def test_sandbox_option
2230
console = Rails::Console.new(app, parse_arguments(["--sandbox"]))
2331
assert_predicate console, :sandbox?

0 commit comments

Comments
 (0)