From 069ec4f8e41a9860ceba35b0fc297652db6552fa Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 15 Feb 2024 13:39:31 -0500 Subject: [PATCH] Restore ability to mutate Dotenv::Rails.files --- lib/dotenv/rails.rb | 11 +++-------- spec/dotenv/rails_spec.rb | 39 +++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index b29096b..719b473 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -24,7 +24,7 @@ module Dotenv # Rails integration for using Dotenv to load ENV variables from a file class Rails < ::Rails::Railtie - delegate :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, :logger=, to: "config.dotenv" + delegate :files, :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, :logger=, to: "config.dotenv" def initialize super() @@ -42,22 +42,17 @@ def initialize ) end - # The list of files to load, joined with Rails.root - def files - config.dotenv.files.map { |file| root.join(file) } - end - # Public: Load dotenv # # This will get called during the `before_configuration` callback, but you # can manually call `Dotenv::Rails.load` if you needed it sooner. def load - Dotenv.load(*files, overwrite: overwrite) + Dotenv.load(*files.map { |file| root.join(file).to_s }, overwrite: overwrite) end def overload deprecator.warn("Dotenv::Rails.overload is deprecated. Set `Dotenv::Rails.overwrite = true` and call Dotenv::Rails.load instead.") - Dotenv.load(*files, overwrite: true) + Dotenv.load(*files.map { |file| root.join(file).to_s }, overwrite: true) end # Internal: `Rails.root` is nil in Rails 4.1 before the application is diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index 040d53f..eba6b14 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -47,10 +47,10 @@ expect(Dotenv::Rails.files).to eql( [ - application.root.join(".env.development.local"), - application.root.join(".env.local"), - application.root.join(".env.development"), - application.root.join(".env") + ".env.development.local", + ".env.local", + ".env.development", + ".env" ] ) end @@ -59,22 +59,16 @@ Rails.env = "test" expect(Dotenv::Rails.files).to eql( [ - application.root.join(".env.test.local"), - application.root.join(".env.test"), - application.root.join(".env") + ".env.test.local", + ".env.test", + ".env" ] ) end - it "returns the relatives paths to Rails.root" do - expect(Dotenv::Rails.files.last).to eql(fixture_path(".env")) - allow(Rails).to receive(:root).and_return(Pathname.new("/tmp")) - expect(Dotenv::Rails.files.last.to_s).to eql("/tmp/.env") - end - - it "returns absolute paths unchanged" do - Dotenv::Rails.files = ["/tmp/.env"] - expect(Dotenv::Rails.files).to eql([Pathname.new("/tmp/.env")]) + it "can be modified in place" do + Dotenv::Rails.files << ".env.shared" + expect(Dotenv::Rails.files.last).to eq(".env.shared") end end @@ -110,6 +104,19 @@ expect { subject }.to change { ENV["PLAIN"] }.from(nil).to("true") end + it "loads file relative to Rails.root" do + allow(Rails).to receive(:root).and_return(Pathname.new("/tmp")) + Dotenv::Rails.files = [".env"] + expect(Dotenv).to receive(:load).with("/tmp/.env", {overwrite: false}) + subject + end + + it "returns absolute paths unchanged" do + Dotenv::Rails.files = ["/tmp/.env"] + expect(Dotenv).to receive(:load).with("/tmp/.env", {overwrite: false}) + subject + end + context "with overwrite = true" do before { Dotenv::Rails.overwrite = true }