Skip to content

Commit

Permalink
Restore ability to mutate Dotenv::Rails.files
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Feb 15, 2024
1 parent 09caa4d commit 069ec4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
11 changes: 3 additions & 8 deletions lib/dotenv/rails.rb
Expand Up @@ -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()
Expand All @@ -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
Expand Down
39 changes: 23 additions & 16 deletions spec/dotenv/rails_spec.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 }

Expand Down

0 comments on commit 069ec4f

Please sign in to comment.