Skip to content

Commit

Permalink
issue #572: add support for LISTEN_GEM_ADAPTER_WARN_BEHAVIOR=warn|log…
Browse files Browse the repository at this point in the history
…|silent
  • Loading branch information
ColinDKelley committed Feb 24, 2024
1 parent 2a34596 commit 4d682d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ Listen.adapter_warn_behavior = ->(message) do
end
end
```
In cases where the `Listen` gem is embedded inside another service--such as `guard`--the above configuration
can be set in the environment variable `LISTEN_GEM_ADAPTER_WARN_BEHAVIOR=warn|log|silent`.

## Listen Adapters

Expand Down
2 changes: 1 addition & 1 deletion lib/listen/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def logger
end

def adapter_warn(message)
case adapter_warn_behavior_callback(message)
case ENV['LISTEN_GEM_ADAPTER_WARN_BEHAVIOR']&.to_sym || adapter_warn_behavior_callback(message)
when :log
logger.warn(message)
when :silent, nil, false
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/listen/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@
end
end

context "when LISTEN_GEM_ADAPTER_WARN_BEHAVIOR is set to 'log'" do
around do |spec|
orig_debugging_env_variable = ENV.fetch('LISTEN_GEM_ADAPTER_WARN_BEHAVIOR', :not_set)

ENV['LISTEN_GEM_ADAPTER_WARN_BEHAVIOR'] = 'log'

spec.run

if orig_debugging_env_variable == :not_set
ENV.delete('LISTEN_GEM_ADAPTER_WARN_BEHAVIOR')
else
ENV['ENV_VARIABLE_NAME'] = orig_debugging_env_variable
end
end

[:silent, nil, false, :warn].each do |behavior|
it "respects the environment variable over #{behavior.inspect}" do
Listen.adapter_warn_behavior = behavior

expect(Listen.logger).to receive(:warn).with(message)

subject
end
end

it "respects the environment variable over a callable config" do
Listen.adapter_warn_behavior = ->(_message) { :warn }

expect(Listen.logger).to receive(:warn).with(message)

subject
end
end

context 'when adapter_warn_behavior is set to a callable object like a proc' do
before do
Listen.adapter_warn_behavior = ->(message) do
Expand Down

0 comments on commit 4d682d3

Please sign in to comment.