diff --git a/lib/deprecation_toolkit.rb b/lib/deprecation_toolkit.rb index 13b9317..407394a 100644 --- a/lib/deprecation_toolkit.rb +++ b/lib/deprecation_toolkit.rb @@ -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 diff --git a/lib/minitest/deprecation_toolkit_plugin.rb b/lib/minitest/deprecation_toolkit_plugin.rb index e888c90..d899ec3 100644 --- a/lib/minitest/deprecation_toolkit_plugin.rb +++ b/lib/minitest/deprecation_toolkit_plugin.rb @@ -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 diff --git a/test/minitest/deprecation_toolkit_plugin_test.rb b/test/minitest/deprecation_toolkit_plugin_test.rb index 73ef589..94d38d0 100644 --- a/test/minitest/deprecation_toolkit_plugin_test.rb +++ b/test/minitest/deprecation_toolkit_plugin_test.rb @@ -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 = {} @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 6b628e3..c97983a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,3 +8,6 @@ ActiveSupport::Deprecation.behavior = :silence ActiveSupport::TestCase.test_order = :random + +module Rails +end