-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module RailsBand | ||
# DeprecationSubscriber is responsible for logging deprecation warnings. | ||
class DeprecationSubscriber < ::ActiveSupport::LogSubscriber | ||
# DeprecationEvent is a wrapper around a deprecation notification event. | ||
class DeprecationEvent < BaseEvent | ||
def message | ||
@message ||= @event.payload.fetch(:message) | ||
end | ||
|
||
def callstack | ||
@callstack ||= @event.payload.fetch(:callstack) | ||
end | ||
|
||
def gem_name | ||
@gem_name ||= @event.payload.fetch(:gem_name) | ||
end | ||
|
||
def deprecation_horizon | ||
@deprecation_horizon ||= @event.payload.fetch(:deprecation_horizon) | ||
end | ||
end | ||
|
||
mattr_accessor :consumers | ||
|
||
def deprecation(event) | ||
consumer&.call(DeprecationEvent.new(event)) | ||
end | ||
|
||
private | ||
|
||
def consumer | ||
# HACK: ActiveSupport::Subscriber has the instance variable @namespace, but it's not documented. | ||
# This hack might possibly break in the future. | ||
namespace = self.class.instance_variable_get(:@namespace) | ||
consumers[:"deprecation.#{namespace}"] || consumers[:deprecation] || consumers[:default] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
get :cache | ||
get :cache2 | ||
get :cache3 | ||
get :deprecation | ||
end | ||
|
||
resources :yay, only: %i[index show] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class DeprecationSubscriberTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@event = nil | ||
end | ||
|
||
test 'use the consumer with the exact event name' do # rubocop:disable Minitest/MultipleAssertions | ||
RailsBand::DeprecationSubscriber.consumers = { | ||
'deprecation.rails': ->(event) { @event = event } | ||
} | ||
get '/users/1/deprecation' | ||
assert_match(/DEPRECATION WARNING: deprecated!!!/, @event.message) | ||
assert_equal 'Rails', @event.gem_name | ||
assert_instance_of String, @event.deprecation_horizon | ||
@event.callstack.each do |location| | ||
assert_instance_of Thread::Backtrace::Location, location | ||
end | ||
end | ||
|
||
test 'use the consumer with namespace' do # rubocop:disable Minitest/MultipleAssertions | ||
RailsBand::DeprecationSubscriber.consumers = { | ||
deprecation: ->(event) { @event = event } | ||
} | ||
get '/users/1/deprecation' | ||
assert_match(/DEPRECATION WARNING: deprecated!!!/, @event.message) | ||
assert_equal 'Rails', @event.gem_name | ||
assert_instance_of String, @event.deprecation_horizon | ||
@event.callstack.each do |location| | ||
assert_instance_of Thread::Backtrace::Location, location | ||
end | ||
end | ||
|
||
test 'use the consumer with default' do # rubocop:disable Minitest/MultipleAssertions | ||
RailsBand::DeprecationSubscriber.consumers = { | ||
default: ->(event) { @event = event } | ||
} | ||
get '/users/1/deprecation' | ||
assert_match(/DEPRECATION WARNING: deprecated!!!/, @event.message) | ||
assert_equal 'Rails', @event.gem_name | ||
assert_instance_of String, @event.deprecation_horizon | ||
@event.callstack.each do |location| | ||
assert_instance_of Thread::Backtrace::Location, location | ||
end | ||
end | ||
|
||
test 'do not use the consumer because the event is not for the target' do | ||
RailsBand::DeprecationSubscriber.consumers = { | ||
'unknown.rails': ->(event) { @event = event } | ||
} | ||
get '/users/1/deprecation' | ||
assert_nil @event | ||
end | ||
end |