Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## main (unreleased)

* [#140](https://github.com/Shopify/deprecation_toolkit/pull/140) Fixed a bug where `warnings_treated_as_deprecation` would suppress warnings even if they were explicitly allowed.

## 2.2.4 (2025-08-08)

* [#136](https://github.com/Shopify/deprecation_toolkit/pull/136) Stable write logic.
Expand Down
1 change: 1 addition & 0 deletions lib/deprecation_toolkit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module DeprecationToolkit
autoload :Collector, "deprecation_toolkit/collector"
autoload :ReadWriteHelper, "deprecation_toolkit/read_write_helper"
autoload :TestTriggerer, "deprecation_toolkit/test_triggerer"
autoload :DeprecationAllowed, "deprecation_toolkit/deprecation_allowed"

module Behaviors
autoload :Disabled, "deprecation_toolkit/behaviors/disabled"
Expand Down
26 changes: 26 additions & 0 deletions lib/deprecation_toolkit/deprecation_allowed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module DeprecationToolkit
class DeprecationAllowed
class << self
# Checks if a deprecation is allowed by the configured rules.
#
# A rule can be a `Regexp` to match the deprecation message, or a callable object that will receive the
# message and the callstack to perform a more advanced check.
#
# @param payload [Hash] The payload from the deprecation event.
# @option payload [String] :message The deprecation message.
# @option payload [Array<String>] :callstack The callstack for the deprecation.
# @return [Boolean] `true` if the deprecation is allowed, `false` otherwise.
def call(payload)
Configuration.allowed_deprecations.any? do |rule|
if rule.is_a?(Regexp)
rule.match?(payload[:message])
else
rule.call(payload[:message], payload[:callstack])
end
end
end
end
end
end
14 changes: 1 addition & 13 deletions lib/deprecation_toolkit/deprecation_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,7 @@ def attached_subscriber?(subscriber, gem_name)
def deprecation(event)
message = event.payload[:message]

Collector.collect(message) unless deprecation_allowed?(event.payload)
end

private

def deprecation_allowed?(payload)
Configuration.allowed_deprecations.any? do |rule|
if rule.is_a?(Regexp)
rule.match?(payload[:message])
else
rule.call(payload[:message], payload[:callstack])
end
end
Collector.collect(message) unless DeprecationToolkit::DeprecationAllowed.call(event.payload)
end
end
end
8 changes: 5 additions & 3 deletions lib/deprecation_toolkit/warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def handle_multipart(str)
str
end

def deprecation_triggered?(str)
DeprecationToolkit::Configuration.warnings_treated_as_deprecation.any? { |warning| warning === str }
def deprecation_triggered?(str, callstack: [])
DeprecationToolkit::Configuration.warnings_treated_as_deprecation.any? { |warning| warning === str } &&
!DeprecationToolkit::DeprecationAllowed.call({ message: str, callstack: callstack })
end

def deprecator
Expand All @@ -51,14 +52,15 @@ def deprecator

module WarningPatch
def warn(str, *)
callstack = caller[0,20]
if Configuration.warnings_treated_as_deprecation.empty?
return super
end

str = DeprecationToolkit::Warning.handle_multipart(str)
return unless str

if DeprecationToolkit::Warning.deprecation_triggered?(str)
if DeprecationToolkit::Warning.deprecation_triggered?(str, callstack: callstack)
DeprecationToolkit::Warning.deprecator.warn(str)
else
super
Expand Down
16 changes: 16 additions & 0 deletions test/deprecation_toolkit/warning_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ module DeprecationToolkit
class WarningTest < ActiveSupport::TestCase
setup do
@previous_warnings_treated_as_deprecation = Configuration.warnings_treated_as_deprecation
@previous_allowed_deprecations = Configuration.allowed_deprecations
end

teardown do
Configuration.warnings_treated_as_deprecation = @previous_warnings_treated_as_deprecation
Configuration.allowed_deprecations = @previous_allowed_deprecations
end

test "treats warnings as deprecations" do
Expand Down Expand Up @@ -80,5 +82,19 @@ class WarningTest < ActiveSupport::TestCase
assert_match(/Using the last argument as keyword parameters/, error.message)
assert_match(/The called method/, error.message)
end

test "using warnings_treated_as_deprecation do not supress warn messages if the deprecation is allowed" do
Configuration.allowed_deprecations = [/#example is deprecated/]
Configuration.warnings_treated_as_deprecation = [//]

captured_io = capture_io do
assert_nothing_raised do
warn("#example is deprecated")
trigger_deprecation_toolkit_behavior
end
end
line_msg = captured_io.find { |line| line.include?("#example is deprecated") }
assert_equal("#example is deprecated\n", line_msg)
end
end
end