-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #472 from bkeepers/save-and-restore
Autorestore ENV between tests
- Loading branch information
Showing
9 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Automatically restore `ENV` to its original state after | ||
|
||
if defined?(RSpec.configure) | ||
RSpec.configure do |config| | ||
# Save ENV before the suite starts | ||
config.before(:suite) { Dotenv.save } | ||
|
||
# Restore ENV after each example | ||
config.after { Dotenv.restore } | ||
end | ||
end | ||
|
||
if defined?(ActiveSupport) | ||
ActiveSupport.on_load(:active_support_test_case) do | ||
ActiveSupport::TestCase.class_eval do | ||
# Save ENV before each test | ||
setup { Dotenv.save } | ||
|
||
# Restore ENV after each test | ||
teardown { Dotenv.restore } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
require "dotenv" | ||
require "dotenv/autorestore" | ||
|
||
RSpec.configure do |config| | ||
# Restore the state of ENV after each spec | ||
config.before { @env_keys = ENV.keys } | ||
config.after { ENV.delete_if { |k, _v| !@env_keys.include?(k) } } | ||
end | ||
|
||
def fixture_path(name) | ||
Pathname.new(__dir__).join("./fixtures", name) | ||
def fixture_path(*parts) | ||
Pathname.new(__dir__).join("./fixtures", *parts) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "active_support" # Rails 6.1 fails if this is not loaded | ||
require "active_support/test_case" | ||
require "minitest/autorun" | ||
|
||
require "dotenv" | ||
require "dotenv/autorestore" | ||
|
||
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" | ||
end | ||
|
||
test "restores ENV between tests, part 2" do | ||
assert_nil ENV["DOTENV"], "ENV was not restored between tests" | ||
ENV["DOTENV"] = "2" | ||
end | ||
end |