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

fix: always respect file importance order #460

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dotenv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load!(*filenames)

# same as `load`, but will override existing values in `ENV`
def overload(*filenames)
with(*filenames) do |f|
with(*filenames.reverse) do |f|
ignoring_nonexistent_files do
env = Environment.new(f, false)
instrument("dotenv.overload", env: env) { env.apply! }
Expand All @@ -39,7 +39,7 @@ def overload(*filenames)

# same as `overload`, but raises Errno::ENOENT if any files don't exist
def overload!(*filenames)
with(*filenames) do |f|
with(*filenames.reverse) do |f|
env = Environment.new(f, false)
instrument("dotenv.overload", env: env) { env.apply! }
end
Expand Down
6 changes: 3 additions & 3 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def add(*items)
)
end

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

context "when loading a file containing already set variables" do
Expand All @@ -125,7 +125,7 @@ def add(*items)

expect do
subject
end.to(change { ENV["DOTENV"] }.from("predefined").to("true"))
end.to(change { ENV["DOTENV"] }.from("predefined").to("test"))
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/dotenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@
end
end

shared_examples "overload" do
context "with multiple files" do
let(:env_files) { [fixture_path("important.env"), fixture_path("plain.env")] }

let(:expected) do
{
"OPTION_A" => "abc",
"OPTION_B" => "2",
"OPTION_C" => "3",
"OPTION_D" => "4",
"OPTION_E" => "5",
"PLAIN" => "false"
}
end

it "respects the file importance order" do
subject
expected.each do |key, value|
expect(ENV[key]).to eq(value)
end
end
end
end

describe "load" do
let(:env_files) { [] }
subject { Dotenv.load(*env_files) }
Expand Down Expand Up @@ -101,6 +125,7 @@
let(:env_files) { [fixture_path("plain.env")] }
subject { Dotenv.overload(*env_files) }
it_behaves_like "load"
it_behaves_like "overload"

it "initializes the Environment with a falsey is_load" do
expect(Dotenv::Environment).to receive(:new).with(anything, false)
Expand Down Expand Up @@ -134,6 +159,7 @@
let(:env_files) { [fixture_path("plain.env")] }
subject { Dotenv.overload!(*env_files) }
it_behaves_like "load"
it_behaves_like "overload"

it "initializes the Environment with a falsey is_load" do
expect(Dotenv::Environment).to receive(:new).with(anything, false)
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/important.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PLAIN=false
OPTION_A=abc
OPTION_B=2