From 86390d29019afb271e48cb64fd304c056e5846ba Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Tue, 13 Feb 2024 08:53:41 -0500 Subject: [PATCH] Disable autorestore if using climate_control or ice_age --- README.md | 2 +- lib/dotenv/rails.rb | 2 +- spec/dotenv/rails_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae99286f..1272c77e 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Dotenv.load('file1.env', 'file2.env') Since 3.0, dotenv in a Rails app will automatically restore `ENV` after each test. This means you can modify `ENV` in your tests without fear of leaking state to other tests. It works with both `ActiveSupport::TestCase` and `Rspec`. -To disable this behavior, set `config.dotenv.autorestore = false` in `config/application.rb` or `config/environments/test.rb`. +To disable this behavior, set `config.dotenv.autorestore = false` in `config/application.rb` or `config/environments/test.rb`. It is disabled by default if your app uses [climate_control](https://github.com/thoughtbot/climate_control) or [ice_age](https://github.com/dpep/ice_age_rb). To use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite. diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index f5b004dd..25816fc5 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -32,7 +32,7 @@ def initialize root.join(".env.#{env}"), root.join(".env") ].compact, - autorestore: env.test? + autorestore: env.test? && !defined?(ClimateControl) && !defined?(IceAge) ) end diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index febec319..d20b7233 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -150,5 +150,17 @@ expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/autorestore") application.initialize! end + + it "is not loaded if ClimateControl is defined" do + stub_const("ClimateControl", Module.new) + expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/autorestore") + application.initialize! + end + + it "is not loaded if IceAge is defined" do + stub_const("IceAge", Module.new) + expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/autorestore") + application.initialize! + end end end