diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bdf37b9..ff616c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,13 @@ jobs: name: Ruby ${{ matrix.ruby }} / ${{ matrix.gemfile }} strategy: matrix: - gemfile: [gemfiles/activesupport_5.2.gemfile, gemfiles/activesupport_6.0.gemfile, gemfiles/activesupport_6.1.gemfile, gemfiles/activesupport_7.0.gemfile, gemfiles/activesupport_edge.gemfile] + gemfile: + - Gemfile + - gemfiles/activesupport_5.2.gemfile + - gemfiles/activesupport_6.0.gemfile + - gemfiles/activesupport_6.1.gemfile + - gemfiles/activesupport_7.0.gemfile + - gemfiles/activesupport_edge.gemfile ruby: ["2.7", "3.0", "3.1", "3.2"] exclude: # Active Support requires Ruby >= 2.7 as of 7.0 diff --git a/gemfiles/activesupport_5.2.gemfile b/gemfiles/activesupport_5.2.gemfile index 43d25d2..b844ae7 100644 --- a/gemfiles/activesupport_5.2.gemfile +++ b/gemfiles/activesupport_5.2.gemfile @@ -1,5 +1,5 @@ # frozen_string_literal: true -@activesupport_gem_requirement = "~> 5.2" +@activesupport_gem_requirement = "~> 5.2.0" eval_gemfile "../Gemfile" diff --git a/gemfiles/activesupport_6.0.gemfile b/gemfiles/activesupport_6.0.gemfile index 968f823..540f830 100644 --- a/gemfiles/activesupport_6.0.gemfile +++ b/gemfiles/activesupport_6.0.gemfile @@ -1,5 +1,5 @@ # frozen_string_literal: true -@activesupport_gem_requirement = "~> 6.0" +@activesupport_gem_requirement = "~> 6.0.0" eval_gemfile "../Gemfile" diff --git a/gemfiles/activesupport_6.1.gemfile b/gemfiles/activesupport_6.1.gemfile index d022926..9152c74 100644 --- a/gemfiles/activesupport_6.1.gemfile +++ b/gemfiles/activesupport_6.1.gemfile @@ -1,5 +1,5 @@ # frozen_string_literal: true -@activesupport_gem_requirement = "~> 6.1" +@activesupport_gem_requirement = "~> 6.1.0" eval_gemfile "../Gemfile" diff --git a/gemfiles/activesupport_7.0.gemfile b/gemfiles/activesupport_7.0.gemfile index 0c22e7b..f222064 100644 --- a/gemfiles/activesupport_7.0.gemfile +++ b/gemfiles/activesupport_7.0.gemfile @@ -1,5 +1,5 @@ # frozen_string_literal: true -@activesupport_gem_requirement = "~> 7.0" +@activesupport_gem_requirement = "~> 7.0.0" eval_gemfile "../Gemfile" diff --git a/lib/deprecation_toolkit/rspec_plugin.rb b/lib/deprecation_toolkit/rspec_plugin.rb index 31a0ed1..d86f482 100644 --- a/lib/deprecation_toolkit/rspec_plugin.rb +++ b/lib/deprecation_toolkit/rspec_plugin.rb @@ -2,16 +2,22 @@ module DeprecationToolkit module RSpecPlugin + extend self + RSpec.configure do |config| config.before(:suite) do - case ENV["DEPRECATION_BEHAVIOR"] - when "r", "record", "record-deprecations" - DeprecationToolkit::Configuration.behavior = DeprecationToolkit::Behaviors::Record - end + RSpecPlugin.before_suite + end + end - DeprecationToolkit.add_notify_behavior - DeprecationToolkit.attach_subscriber + def before_suite + case ENV["DEPRECATION_BEHAVIOR"] + when "r", "record", "record-deprecations" + DeprecationToolkit::Configuration.behavior = DeprecationToolkit::Behaviors::Record end + + DeprecationToolkit.add_notify_behavior + DeprecationToolkit.attach_subscriber end end end diff --git a/spec/deprecation_toolkit/behaviors/disabled_spec.rb b/spec/deprecation_toolkit/behaviors/disabled_spec.rb index 702f33f..3f28443 100644 --- a/spec/deprecation_toolkit/behaviors/disabled_spec.rb +++ b/spec/deprecation_toolkit/behaviors/disabled_spec.rb @@ -3,6 +3,8 @@ require "spec_helper" RSpec.describe(DeprecationToolkit::Behaviors::Raise) do + include TestDeprecator + before do @previous_configuration = DeprecationToolkit::Configuration.behavior DeprecationToolkit::Configuration.behavior = DeprecationToolkit::Behaviors::Disabled @@ -14,8 +16,8 @@ it ".trigger noop any deprecations" do |example| expect do - ActiveSupport::Deprecation.warn("Foo") - ActiveSupport::Deprecation.warn("Bar") + deprecator.warn("Foo") + deprecator.warn("Bar") DeprecationToolkit::TestTriggerer.trigger_deprecation_toolkit_behavior(example) end.not_to(raise_error) diff --git a/spec/rspec/plugin_spec.rb b/spec/rspec/plugin_spec.rb index 98ab786..21c6b49 100644 --- a/spec/rspec/plugin_spec.rb +++ b/spec/rspec/plugin_spec.rb @@ -3,16 +3,46 @@ require "spec_helper" RSpec.describe(DeprecationToolkit::RSpecPlugin) do - it "should add `notify` behavior to the deprecations behavior list" do - behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify] + if ActiveSupport.gem_version < Gem::Version.new("7.1.0") + def with_rails_70_app + deprecators_before = Rails.application.method(:deprecators) + Rails.application.singleton_class.undef_method(:deprecators) + yield + ensure + Rails.application.singleton_class.define_method(:deprecators, &deprecators_before) + end + + it "should add `notify` behavior to the deprecations behavior list" do + with_rails_70_app do + behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify] + + DeprecationToolkit::RSpecPlugin.before_suite + expect(ActiveSupport::Deprecation.behavior).to(include(behavior)) + end + end + + it "doesn't remove previous deprecation behaviors" do + with_rails_70_app do + behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:silence] - expect(ActiveSupport::Deprecation.behavior).to(include(behavior)) + DeprecationToolkit::RSpecPlugin.before_suite + ActiveSupport::Deprecation.behavior = behavior + expect(ActiveSupport::Deprecation.behavior).to(include(behavior)) + end + end + end + + it "should add `notify` behavior to the deprecations behavior list with Rails.application.deprecators" do + behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify] + deprecator = Rails.application.deprecators.each.first + expect(deprecator.behavior).to(include(behavior)) end - it "doesn't remove previous deprecation behaviors" do + it "doesn't remove previous deprecation behaviors with Rails.application.deprecators" do behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:silence] - ActiveSupport::Deprecation.behavior = behavior + deprecator = Rails.application.deprecators.each.first - expect(ActiveSupport::Deprecation.behavior).to(include(behavior)) + deprecator.behavior = behavior + expect(deprecator.behavior).to(include(behavior)) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 54e7128..2f754c8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,10 +5,17 @@ require "deprecation_toolkit/rspec_plugin" require "active_support/all" require_relative "../test/support/test_deprecator" +require_relative "../test/support/fake_rails" DeprecationToolkit::Configuration.test_runner = :rspec DeprecationToolkit::Configuration.deprecation_path = "spec/deprecations" +if ActiveSupport.respond_to?(:deprecator) + ActiveSupport.deprecator.behavior = :raise +else + ActiveSupport::Deprecation.behavior = :raise +end + RSpec.configure do |config| # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! diff --git a/test/deprecation_toolkit/behaviors/disabled_test.rb b/test/deprecation_toolkit/behaviors/disabled_test.rb index facaa11..855f715 100644 --- a/test/deprecation_toolkit/behaviors/disabled_test.rb +++ b/test/deprecation_toolkit/behaviors/disabled_test.rb @@ -5,6 +5,8 @@ module DeprecationToolkit module Behaviors class DisabledTest < ActiveSupport::TestCase + include TestDeprecator + setup do @previous_configuration = Configuration.behavior Configuration.behavior = Disabled @@ -16,8 +18,8 @@ class DisabledTest < ActiveSupport::TestCase test ".trigger noop any deprecations" do assert_nothing_raised do - ActiveSupport::Deprecation.warn("Foo") - ActiveSupport::Deprecation.warn("Bar") + deprecator.warn("Foo") + deprecator.warn("Bar") trigger_deprecation_toolkit_behavior end diff --git a/test/minitest/deprecation_toolkit_plugin_test.rb b/test/minitest/deprecation_toolkit_plugin_test.rb index d1d5b81..b2dd635 100644 --- a/test/minitest/deprecation_toolkit_plugin_test.rb +++ b/test/minitest/deprecation_toolkit_plugin_test.rb @@ -119,17 +119,21 @@ def with_fake_application end test ".plugin_deprecation_toolkit_init doesn't init plugin when outside bundler context" do - notify_behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify] - old_bundle_gemfile = ENV["BUNDLE_GEMFILE"] - ENV.delete("BUNDLE_GEMFILE") + with_fake_application do + notify_behavior = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:notify] + old_bundle_gemfile = ENV["BUNDLE_GEMFILE"] + ENV.delete("BUNDLE_GEMFILE") - ActiveSupport::Deprecation.behavior.delete(notify_behavior) - Minitest.plugin_deprecation_toolkit_init({}) + deprecator = Rails.application.deprecators.first + deprecator.behavior.delete(notify_behavior) - refute_includes(ActiveSupport::Deprecation.behavior, notify_behavior) - ensure - ENV["BUNDLE_GEMFILE"] = old_bundle_gemfile - ActiveSupport::Deprecation.behavior << notify_behavior + Minitest.plugin_deprecation_toolkit_init({}) + + refute_includes(deprecator.behavior, notify_behavior) + ensure + ENV["BUNDLE_GEMFILE"] = old_bundle_gemfile + deprecator.behavior << notify_behavior + end end end end diff --git a/test/support/fake_rails.rb b/test/support/fake_rails.rb new file mode 100644 index 0000000..466d885 --- /dev/null +++ b/test/support/fake_rails.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# This is needed so that when we run the tests in this project, and the plugin is initialized by Minitest, we don't +# cause a deprecation warning by calling `ActiveSupport::Deprecation.behavior` and `.behavior=`. +module Rails + def self.application + Application + end + + module Application + def self.deprecators + @deprecators ||= DeprecatorSet.new + end + end + + class DeprecatorSet + def initialize + @deprecator = ActiveSupport::Deprecation.new + @deprecator.behavior = :raise + end + + def each + return to_enum unless block_given? + + yield @deprecator + end + + def behavior=(behavior) + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4100d40..29ee9af 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,28 +6,11 @@ require "minitest/autorun" require "active_support/all" require_relative "support/test_deprecator" +require_relative "support/fake_rails" -ActiveSupport::Deprecation.behavior = :silence -ActiveSupport::TestCase.test_order = :random - -# This is needed so that when we run the tests in this project, and the plugin is initialized by Minitest, we don't -# cause a deprecation warning by calling `ActiveSupport::Deprecation.behavior` and `.behavior=`. -module Rails - def self.application - Application - end - - module Application - def self.deprecators - DeprecatorSet - end - end - - module DeprecatorSet - def self.each - end - - def self.behavior=(behavior) - end - end +if ActiveSupport.respond_to?(:deprecator) + ActiveSupport.deprecator.behavior = :raise +else + ActiveSupport::Deprecation.behavior = :raise end +ActiveSupport::TestCase.test_order = :random