Skip to content

Commit

Permalink
follow-up #511
Browse files Browse the repository at this point in the history
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 <main>'
```

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 #511 and fixes exception.
  • Loading branch information
alpaca-tc committed Sep 18, 2024
1 parent 1e8dae2 commit f75ff89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
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
22 changes: 18 additions & 4 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit f75ff89

Please sign in to comment.