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

Restore ability to mutate Dotenv::Rails.files #486

Merged
merged 1 commit into from
Feb 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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