diff --git a/README.md b/README.md index e2411b73..cc59e02f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/listen/logger.rb b/lib/listen/logger.rb index 605b7e90..9689252e 100644 --- a/lib/listen/logger.rb +++ b/lib/listen/logger.rb @@ -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 diff --git a/spec/lib/listen/logger_spec.rb b/spec/lib/listen/logger_spec.rb index 5fbe848c..3dfd472b 100644 --- a/spec/lib/listen/logger_spec.rb +++ b/spec/lib/listen/logger_spec.rb @@ -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