Skip to content

Commit

Permalink
Support Rails.deprecators
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcgibbon committed Nov 16, 2022
1 parent 96bdab1 commit 6053a4e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lib/deprecation_toolkit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,35 @@ module Behaviors
autoload :CIRecordHelper, "deprecation_toolkit/behaviors/ci_record_helper"
end

def self.add_notify_behavior
notify = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify]
behaviors = ActiveSupport::Deprecation.behavior
class << self
def add_notify_behavior
notify = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify]

unless behaviors.find { |behavior| behavior == notify }
ActiveSupport::Deprecation.behavior = behaviors << notify
each_deprecator do |deprecator|
behaviors = deprecator.behavior

unless behaviors.find { |behavior| behavior == notify }
deprecator.behavior = (behaviors << notify)
end
end
end

def attach_subscriber
return if DeprecationSubscriber.already_attached?

Configuration.attach_to.each do |gem_name|
DeprecationSubscriber.attach_to(gem_name)
end
end
end

def self.attach_subscriber
return if DeprecationSubscriber.already_attached?
private

Configuration.attach_to.each do |gem_name|
DeprecationSubscriber.attach_to(gem_name)
def each_deprecator(&block)
if defined?(Rails.application) && Rails.application.respond_to?(:deprecators)
Rails.application.deprecators.each(&block)
else
block.call(ActiveSupport::Deprecation)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/minitest/deprecation_toolkit_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def plugin_deprecation_toolkit_init(options)
return unless using_bundler?

require "deprecation_toolkit"

if options[:record_deprecations]
DeprecationToolkit::Configuration.behavior = DeprecationToolkit::Behaviors::Record
end
Expand Down
40 changes: 40 additions & 0 deletions test/minitest/deprecation_toolkit_plugin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@

module Minitest
class DeprecationToolkitPluginTest < ActiveSupport::TestCase
class FakeApplication
class Deprecator
def behavior
@behavior ||= []
end

attr_writer :behavior
end

class Deprecatiors < Array
def initialize(number)
super()
number.times { self << Deprecator.new }
end

def behaviour=(value)
each { |deprecator| deprecator.behaviour = value }
end
end

def deprecators
@deprecators ||= Deprecatiors.new(3)
end
end

test ".plugin_deprecation_toolkit_options when running test with the `-r` flag" do
option_parser = OptionParser.new
options = {}
Expand All @@ -30,6 +55,21 @@ class DeprecationToolkitPluginTest < ActiveSupport::TestCase
assert_includes ActiveSupport::Deprecation.behavior, behavior
end

test ".plugin_deprecation_toolkit_init add `notify` behavior to the deprecations behavior list with Rails.application.deprecators" do
behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify]

Rails.singleton_class.define_method(:application) { @application ||= FakeApplication.new }

Minitest.plugin_deprecation_toolkit_init({})

Rails.application.deprecators.each do |deprecator|
assert_includes(deprecator.behavior, behavior)
end

ensure
Rails.singleton_class.undef_method(:application)
end

test ".plugin_deprecation_toolkit_init doesn't remove previous deprecation behaviors" do
behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:silence]
ActiveSupport::Deprecation.behavior = behavior
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

ActiveSupport::Deprecation.behavior = :silence
ActiveSupport::TestCase.test_order = :random

module Rails
end

0 comments on commit 6053a4e

Please sign in to comment.