Skip to content

Commit

Permalink
Merge pull request #460 from eriklovmo/respect-file-priority-policy-w…
Browse files Browse the repository at this point in the history
…hen-overloading

fix: always respect file importance order
  • Loading branch information
bkeepers committed Jan 20, 2024
2 parents e455753 + 29c9232 commit 32c521b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
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

0 comments on commit 32c521b

Please sign in to comment.