diff --git a/lib/dotenv.rb b/lib/dotenv.rb index 18ddeeb0..8d543401 100644 --- a/lib/dotenv.rb +++ b/lib/dotenv.rb @@ -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! } @@ -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 diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index 2cb4ecb1..1baabb0a 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -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 @@ -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 diff --git a/spec/dotenv_spec.rb b/spec/dotenv_spec.rb index ea80e2fe..2676e6d9 100644 --- a/spec/dotenv_spec.rb +++ b/spec/dotenv_spec.rb @@ -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) } @@ -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) @@ -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) diff --git a/spec/fixtures/important.env b/spec/fixtures/important.env new file mode 100644 index 00000000..6b042934 --- /dev/null +++ b/spec/fixtures/important.env @@ -0,0 +1,3 @@ +PLAIN=false +OPTION_A=abc +OPTION_B=2 \ No newline at end of file