-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to save/restore ENV, automatically load in Rails
- Loading branch information
Showing
8 changed files
with
137 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,21 @@ | ||
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 | ||
# Save ENV when the test suite loads | ||
Dotenv.save | ||
|
||
ActiveSupport::TestCase.class_eval do | ||
# Restore ENV after each test | ||
setup { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
require "dotenv" | ||
require "dotenv/test_help" | ||
|
||
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,17 @@ | ||
require "dotenv" | ||
require "active_support/deprecator" | ||
require "active_support/test_case" | ||
require "dotenv/test_help" | ||
require "minitest/autorun" | ||
|
||
class TestHelpTest < ActiveSupport::TestCase | ||
test "restores ENV between tests, part 1" do | ||
assert_nil ENV["PART_2"], "ENV was not restored between tests" | ||
ENV["PART_1"] = "1" | ||
end | ||
|
||
test "restores ENV between tests, part 2" do | ||
assert_nil ENV["PART_1"], "ENV was not restored between tests" | ||
ENV["PART_2"] = "2" | ||
end | ||
end |