From f75ff89efbb6d311a8020c090b5082062223f98e Mon Sep 17 00:00:00 2001 From: alpaca-tc Date: Wed, 18 Sep 2024 12:00:45 +0900 Subject: [PATCH 1/2] follow-up https://github.com/bkeepers/dotenv/pull/511/ An exception occurs when invoking the rake task via ./bin/rails. ``` bin/rails aborted! NoMethodError: undefined method `watch' for Spring:Module (NoMethodError) Spring.watch event.payload[:env].filename if Rails.application ^^^^^^ .bundle/ruby/3.2.0/gems/dotenv-3.1.3/lib/dotenv/rails.rb:16:in `block in
' ``` When using `./bin/rails`, 'spring/client' is loaded, but 'spring/watcher', which is unnecessary for the client-side, is not required. This PR follows up on https://github.com/bkeepers/dotenv/pull/511/ and fixes exception. --- lib/dotenv/rails.rb | 2 +- spec/dotenv/rails_spec.rb | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index ab4613a..a94442f 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -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 diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index 1e4bd0a..ba95eba 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -82,6 +82,14 @@ 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) @@ -206,13 +214,19 @@ end end - def stub_spring - spring = Struct.new("Spring", :watcher) do - def watch(path) + def stub_spring(load_watcher: true) + spring = Module.new + + if load_watcher + def spring.watcher + @watcher ||= Set.new + end + + def spring.watch(path) watcher.add path end end - stub_const "Spring", spring.new(Set.new) + stub_const "Spring", spring end end From 691f7f992b6811b6a4458ae4cb4a73822461c4aa Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Tue, 17 Sep 2024 23:49:14 -0400 Subject: [PATCH 2/2] Make spring specs a little more explicit --- spec/dotenv/rails_spec.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index ba95eba..59fc678 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -75,7 +75,7 @@ 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) @@ -101,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 @@ -215,15 +215,15 @@ end def stub_spring(load_watcher: true) - spring = Module.new - - if load_watcher - def spring.watcher - @watcher ||= Set.new - end - - def spring.watch(path) - watcher.add path + spring = Module.new do + if load_watcher + def self.watcher + @watcher ||= Set.new + end + + def self.watch(path) + watcher.add path + end end end