-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46049 from jonathanhefner/add-rails_application_d…
…eprecators Add `Rails.application.deprecators`
- Loading branch information
Showing
8 changed files
with
313 additions
and
8 deletions.
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
96 changes: 96 additions & 0 deletions
96
activesupport/lib/active_support/deprecation/deprecators.rb
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,96 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveSupport | ||
class Deprecation | ||
# A managed collection of deprecators. Configuration methods, such as | ||
# #behavior=, affect all deprecators in the collection. Additionally, the | ||
# #silence method silences all deprecators in the collection for the | ||
# duration of a given block. | ||
class Deprecators | ||
def initialize | ||
@options = {} | ||
@deprecators = {} | ||
end | ||
|
||
# Returns a deprecator added to this collection via #[]=. | ||
def [](name) | ||
@deprecators[name] | ||
end | ||
|
||
# Adds a given +deprecator+ to this collection. The deprecator will be | ||
# immediately configured with any options previously set on this | ||
# collection. | ||
# | ||
# deprecators = ActiveSupport::Deprecation::Deprecators.new | ||
# deprecators.debug = true | ||
# | ||
# foo_deprecator = ActiveSupport::Deprecation.new("2.0", "Foo") | ||
# foo_deprecator.debug # => false | ||
# | ||
# deprecators[:foo] = foo_deprecator | ||
# deprecators[:foo].debug # => true | ||
# foo_deprecator.debug # => true | ||
# | ||
def []=(name, deprecator) | ||
apply_options(deprecator) | ||
@deprecators[name] = deprecator | ||
end | ||
|
||
# Iterates over all deprecators in this collection. If no block is given, | ||
# returns an +Enumerator+. | ||
def each(&block) | ||
return to_enum(__method__) unless block | ||
@deprecators.each_value(&block) | ||
end | ||
|
||
# Sets the silenced flag for all deprecators in this collection. | ||
def silenced=(silenced) | ||
set_option(:silenced, silenced) | ||
end | ||
|
||
# Sets the debug flag for all deprecators in this collection. | ||
def debug=(debug) | ||
set_option(:debug, debug) | ||
end | ||
|
||
# Sets the deprecation warning behavior for all deprecators in this | ||
# collection. | ||
# | ||
# See ActiveSupport::Deprecation::Behavior#behavior=. | ||
def behavior=(behavior) | ||
set_option(:behavior, behavior) | ||
end | ||
|
||
# Sets the disallowed deprecation warning behavior for all deprecators in | ||
# this collection. | ||
# | ||
# See ActiveSupport::Deprecation::Behavior#disallowed_behavior=. | ||
def disallowed_behavior=(disallowed_behavior) | ||
set_option(:disallowed_behavior, disallowed_behavior) | ||
end | ||
|
||
# Silences all deprecators in this collection for the duration of the | ||
# given block. | ||
# | ||
# See ActiveSupport::Deprecation::Reporting#silence. | ||
def silence(&block) | ||
each { |deprecator| deprecator.begin_silence } | ||
block.call | ||
ensure | ||
each { |deprecator| deprecator.end_silence } | ||
end | ||
|
||
private | ||
def set_option(name, value) | ||
@options[name] = value | ||
each { |deprecator| deprecator.public_send("#{name}=", value) } | ||
end | ||
|
||
def apply_options(deprecator) | ||
@options.each do |name, value| | ||
deprecator.public_send("#{name}=", value) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../abstract_unit" | ||
|
||
class DeprecationTest < ActiveSupport::TestCase | ||
setup do | ||
@deprecator_names = [:fubar, :foo, :bar] | ||
@deprecators = ActiveSupport::Deprecation::Deprecators.new | ||
@deprecator_names.each do |name| | ||
@deprecators[name] = ActiveSupport::Deprecation.new("2.0", name.to_s) | ||
end | ||
end | ||
|
||
test "#[] gets an individual deprecator" do | ||
@deprecator_names.each do |name| | ||
assert_equal name.to_s, @deprecators[name].gem_name | ||
end | ||
end | ||
|
||
test "#each iterates over each deprecator" do | ||
gem_names = [] | ||
@deprecators.each { |deprecator| gem_names << deprecator.gem_name } | ||
|
||
assert_equal @deprecator_names.map(&:to_s).sort, gem_names.sort | ||
end | ||
|
||
test "#each without block returns an Enumerator" do | ||
assert_kind_of Enumerator, @deprecators.each | ||
assert_equal @deprecator_names.map(&:to_s).sort, @deprecators.each.map(&:gem_name).sort | ||
end | ||
|
||
test "#silenced= applies to each deprecator" do | ||
@deprecators.each { |deprecator| assert_not_predicate deprecator, :silenced } | ||
|
||
@deprecators.silenced = true | ||
@deprecators.each { |deprecator| assert_predicate deprecator, :silenced } | ||
|
||
@deprecators.silenced = false | ||
@deprecators.each { |deprecator| assert_not_predicate deprecator, :silenced } | ||
end | ||
|
||
test "#debug= applies to each deprecator" do | ||
@deprecators.each { |deprecator| assert_not_predicate deprecator, :debug } | ||
|
||
@deprecators.debug = true | ||
@deprecators.each { |deprecator| assert_predicate deprecator, :debug } | ||
|
||
@deprecators.debug = false | ||
@deprecators.each { |deprecator| assert_not_predicate deprecator, :debug } | ||
end | ||
|
||
test "#behavior= applies to each deprecator" do | ||
callback = proc { } | ||
|
||
@deprecators.behavior = callback | ||
@deprecators.each { |deprecator| assert_equal [callback], deprecator.behavior } | ||
end | ||
|
||
test "#disallowed_behavior= applies to each deprecator" do | ||
callback = proc { } | ||
|
||
@deprecators.disallowed_behavior = callback | ||
@deprecators.each { |deprecator| assert_equal [callback], deprecator.disallowed_behavior } | ||
end | ||
|
||
test "#silence silences each deprecator" do | ||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
|
||
@deprecators.silence do | ||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
end | ||
|
||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
end | ||
|
||
test "#silence returns the result of the block" do | ||
assert_equal 123, @deprecators.silence { 123 } | ||
end | ||
|
||
test "#silence ensures silencing is reverted after an error is raised" do | ||
assert_raises do | ||
@deprecators.silence { raise } | ||
end | ||
|
||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
end | ||
|
||
test "#silence blocks can be nested" do | ||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
|
||
@deprecators.silence do | ||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
|
||
@deprecators.silence do | ||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
end | ||
|
||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
end | ||
|
||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
end | ||
|
||
test "#silence only affects the current thread" do | ||
@deprecators.silence do | ||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
|
||
Thread.new do | ||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
|
||
@deprecators.silence do | ||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
end | ||
|
||
@deprecators.each { |deprecator| assert_not_silencing(deprecator) } | ||
end.join | ||
|
||
@deprecators.each { |deprecator| assert_silencing(deprecator) } | ||
end | ||
end | ||
|
||
private | ||
def assert_silencing(deprecator) | ||
assert_not_deprecated(deprecator) { deprecator.warn } | ||
end | ||
|
||
def assert_not_silencing(deprecator) | ||
assert_deprecated(/./, deprecator) { deprecator.warn } | ||
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
Oops, something went wrong.