Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into log-everything
Browse files Browse the repository at this point in the history
* origin/master:
  Use setup+teardown in AS::TestCase
  Update README
  Configure files for `rake test`
  Rename test_help to autorestore
  • Loading branch information
bkeepers committed Jan 24, 2024
2 parents 2d080db + 68dfc56 commit 9b3534f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
```

## Autorestore in tests

Since 3.0, dotenv in a Rails app will automatically restore `ENV` to its original state before 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 use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite.

### Rake

To ensure `.env` is loaded in rake, load the tasks:
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
end

Rake::TestTask.new
Rake::TestTask.new do |t|
t.test_files = Dir["test/**/*_test.rb"]
end

task default: [:spec, :test, :standard]
10 changes: 6 additions & 4 deletions lib/dotenv/test_help.rb → lib/dotenv/autorestore.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Automatically restore `ENV` to its original state after

if defined?(RSpec.configure)
RSpec.configure do |config|
# Save ENV before the suite starts
Expand All @@ -10,12 +12,12 @@

if defined?(ActiveSupport)
ActiveSupport.on_load(:active_support_test_case) do
# Save ENV when the test suite loads
Dotenv.save

ActiveSupport::TestCase.class_eval do
# Save ENV before each test
setup { Dotenv.save }

# Restore ENV after each test
setup { Dotenv.restore }
teardown { Dotenv.restore }
end
end
end
8 changes: 4 additions & 4 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
module Dotenv
# Rails integration for using Dotenv to load ENV variables from a file
class Rails < ::Rails::Railtie
delegate :files, :files=, :overwrite, :overwrite=, :test_help, :test_help=, :logger, :logger=, to: "config.dotenv"
delegate :files, :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, :logger=, to: "config.dotenv"

def initialize
super()
Expand All @@ -32,7 +32,7 @@ def initialize
root.join(".env.#{env}"),
root.join(".env")
].compact,
test_help: env.test?
autorestore: env.test?
)
end

Expand Down Expand Up @@ -93,8 +93,8 @@ def self.load
app.deprecators[:dotenv] = deprecator if app.respond_to?(:deprecators)
end

initializer "dotenv.test_help" do |app|
require "dotenv/test_help" if test_help
initializer "dotenv.autorestore" do |app|
require "dotenv/autorestore" if autorestore
end

config.before_configuration { load }
Expand Down
16 changes: 8 additions & 8 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,23 @@
end
end

describe "test_help" do
describe "autorestore" do
it "is loaded if RAILS_ENV=test" do
expect(Dotenv::Rails.test_help).to eq(true)
expect(Dotenv::Rails.instance).to receive(:require).with("dotenv/test_help")
expect(Dotenv::Rails.autorestore).to eq(true)
expect(Dotenv::Rails.instance).to receive(:require).with("dotenv/autorestore")
application.initialize!
end

it "is not loaded if RAILS_ENV=development" do
Rails.env = "development"
expect(Dotenv::Rails.test_help).to eq(false)
expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/test_help")
expect(Dotenv::Rails.autorestore).to eq(false)
expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/autorestore")
application.initialize!
end

it "is not loaded if test_help set to false" do
Dotenv::Rails.test_help = false
expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/test_help")
it "is not loaded if autorestore set to false" do
Dotenv::Rails.autorestore = false
expect(Dotenv::Rails.instance).not_to receive(:require).with("dotenv/autorestore")
application.initialize!
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "dotenv"
require "dotenv/test_help"
require "dotenv/autorestore"

def fixture_path(*parts)
Pathname.new(__dir__).join("./fixtures", *parts)
Expand Down
10 changes: 2 additions & 8 deletions test/test_help_test.rb → test/autorestore_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
begin
require "active_support/deprecator"
rescue LoadError
# Rails 7.1 fails if this is not loaded
end

require "active_support" # Rails 6.1 fails if this is not loaded
require "active_support/test_case"
require "minitest/autorun"

require "dotenv"
require "dotenv/test_help"
require "dotenv/autorestore"

class TestHelpTest < ActiveSupport::TestCase
class AutorestoreTest < ActiveSupport::TestCase
test "restores ENV between tests, part 1" do
assert_nil ENV["DOTENV"], "ENV was not restored between tests"
ENV["DOTENV"] = "1"
Expand Down

0 comments on commit 9b3534f

Please sign in to comment.