Skip to content

Commit

Permalink
Add a Dotenv::Railtie.overload method.
Browse files Browse the repository at this point in the history
It allows to overload environment variables in Rails context.
  • Loading branch information
walerian777 committed Oct 2, 2019
1 parent 284e4e6 commit 14af3b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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

0 comments on commit 14af3b8

Please sign in to comment.