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

follow-up https://github.com/bkeepers/dotenv/pull/511/ #512

Merged
merged 2 commits into from
Sep 18, 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
2 changes: 1 addition & 1 deletion lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Watch all loaded env files with Spring
ActiveSupport::Notifications.subscribe("load.dotenv") do |*args|
if defined?(Spring)
if defined?(Spring) && Spring.respond_to?(:watch)
event = ActiveSupport::Notifications::Event.new(*args)
Spring.watch event.payload[:env].filename if Rails.application
end
Expand Down
28 changes: 21 additions & 7 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,21 @@
end

it "watches other loaded files with Spring" do
stub_spring
stub_spring(load_watcher: true)
application.initialize!
path = fixture_path("plain.env")
Dotenv.load(path)
expect(Spring.watcher).to include(path.to_s)
end

it "doesn't raise an error if Spring.watch is not defined" do
stub_spring(load_watcher: false)

expect {
application.initialize!
}.to_not raise_error
end

context "before_configuration" do
it "calls #load" do
expect(Dotenv::Rails.instance).to receive(:load)
Expand All @@ -93,7 +101,7 @@
subject { application.initialize! }

it "watches .env with Spring" do
stub_spring
stub_spring(load_watcher: true)
subject
expect(Spring.watcher).to include(fixture_path(".env").to_s)
end
Expand Down Expand Up @@ -206,13 +214,19 @@
end
end

def stub_spring
spring = Struct.new("Spring", :watcher) do
def watch(path)
watcher.add path
def stub_spring(load_watcher: true)
spring = Module.new do
if load_watcher
def self.watcher
@watcher ||= Set.new
end

def self.watch(path)
watcher.add path
end
end
end

stub_const "Spring", spring.new(Set.new)
stub_const "Spring", spring
end
end