Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Dotenv::Railtie.overload method. #403

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def load
Dotenv.load(*dotenv_files)
end

# Public: Reload dotenv
#
# Same as `load`, but will override existing values in `ENV`
def overload
Dotenv.overload(*dotenv_files)
end

# Internal: `Rails.root` is nil in Rails 4.1 before the application is
# initialized, so this falls back to the `RAILS_ROOT` environment variable,
# or the current working directory.
Expand Down
42 changes: 42 additions & 0 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,46 @@ def add(*items)
end
end
end

context "overload" do
before { Dotenv::Railtie.overload }

it "does not load .env.local in test rails environment" do
expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql(
[
Rails.root.join(".env.test.local"),
Rails.root.join(".env.test"),
Rails.root.join(".env")
]
)
end

it "does load .env.local in development environment" do
Rails.env = "development"
expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql(
[
Rails.root.join(".env.development.local"),
Rails.root.join(".env.local"),
Rails.root.join(".env.development"),
Rails.root.join(".env")
]
)
end

it "overloads .env.test with .env" do
expect(ENV["DOTENV"]).to eql("true")
end

context "when loading a file containing already set variables" do
subject { Dotenv::Railtie.overload }

it "overrides any existing ENV variables" do
ENV["DOTENV"] = "predefined"

expect do
subject
end.to(change { ENV["DOTENV"] }.from("predefined").to("true"))
end
end
end
end