Skip to content

Commit daeba3a

Browse files
committed
fix: warns suppressed even when allowed
Using `warnings_treated_as_deprecation` caused `warn` messages to be suppressed even if the message was explicitly allowed.
1 parent 7743ba4 commit daeba3a

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## main (unreleased)
44

5+
* [#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.
6+
57
## 2.2.4 (2025-08-08)
68

79
* [#136](https://github.com/Shopify/deprecation_toolkit/pull/136) Stable write logic.

lib/deprecation_toolkit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module DeprecationToolkit
1010
autoload :Collector, "deprecation_toolkit/collector"
1111
autoload :ReadWriteHelper, "deprecation_toolkit/read_write_helper"
1212
autoload :TestTriggerer, "deprecation_toolkit/test_triggerer"
13+
autoload :DeprecationAllowed, "deprecation_toolkit/deprecation_allowed"
1314

1415
module Behaviors
1516
autoload :Disabled, "deprecation_toolkit/behaviors/disabled"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module DeprecationToolkit
4+
class DeprecationAllowed
5+
class << self
6+
# Checks if a deprecation is allowed by the configured rules.
7+
#
8+
# A rule can be a `Regexp` to match the deprecation message, or a callable object that will receive the
9+
# message and the callstack to perform a more advanced check.
10+
#
11+
# @param payload [Hash] The payload from the deprecation event.
12+
# @option payload [String] :message The deprecation message.
13+
# @option payload [Array<String>] :callstack The callstack for the deprecation.
14+
# @return [Boolean] `true` if the deprecation is allowed, `false` otherwise.
15+
def call(payload)
16+
require "pry-byebug"
17+
Configuration.allowed_deprecations.any? do |rule|
18+
if rule.is_a?(Regexp)
19+
rule.match?(payload[:message])
20+
else
21+
rule.call(payload[:message], payload[:callstack])
22+
end
23+
end
24+
end
25+
end
26+
end
27+
end

lib/deprecation_toolkit/deprecation_subscriber.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,7 @@ def attached_subscriber?(subscriber, gem_name)
5959
def deprecation(event)
6060
message = event.payload[:message]
6161

62-
Collector.collect(message) unless deprecation_allowed?(event.payload)
63-
end
64-
65-
private
66-
67-
def deprecation_allowed?(payload)
68-
Configuration.allowed_deprecations.any? do |rule|
69-
if rule.is_a?(Regexp)
70-
rule.match?(payload[:message])
71-
else
72-
rule.call(payload[:message], payload[:callstack])
73-
end
74-
end
62+
Collector.collect(message) unless DeprecationToolkit::DeprecationAllowed.call(event.payload)
7563
end
7664
end
7765
end

lib/deprecation_toolkit/warning.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def handle_multipart(str)
3737
str
3838
end
3939

40-
def deprecation_triggered?(str)
41-
DeprecationToolkit::Configuration.warnings_treated_as_deprecation.any? { |warning| warning === str }
40+
def deprecation_triggered?(str, callstack: [])
41+
DeprecationToolkit::Configuration.warnings_treated_as_deprecation.any? { |warning| warning === str } &&
42+
!DeprecationToolkit::DeprecationAllowed.call({ message: str, callstack: callstack })
4243
end
4344

4445
def deprecator
@@ -51,14 +52,15 @@ def deprecator
5152

5253
module WarningPatch
5354
def warn(str, *)
55+
callstack = caller[0,20]
5456
if Configuration.warnings_treated_as_deprecation.empty?
5557
return super
5658
end
5759

5860
str = DeprecationToolkit::Warning.handle_multipart(str)
5961
return unless str
6062

61-
if DeprecationToolkit::Warning.deprecation_triggered?(str)
63+
if DeprecationToolkit::Warning.deprecation_triggered?(str, callstack: callstack)
6264
DeprecationToolkit::Warning.deprecator.warn(str)
6365
else
6466
super

test/deprecation_toolkit/warning_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ module DeprecationToolkit
66
class WarningTest < ActiveSupport::TestCase
77
setup do
88
@previous_warnings_treated_as_deprecation = Configuration.warnings_treated_as_deprecation
9+
@previous_allowed_deprecations = Configuration.allowed_deprecations
910
end
1011

1112
teardown do
1213
Configuration.warnings_treated_as_deprecation = @previous_warnings_treated_as_deprecation
14+
Configuration.allowed_deprecations = @previous_allowed_deprecations
1315
end
1416

1517
test "treats warnings as deprecations" do
@@ -80,5 +82,19 @@ class WarningTest < ActiveSupport::TestCase
8082
assert_match(/Using the last argument as keyword parameters/, error.message)
8183
assert_match(/The called method/, error.message)
8284
end
85+
86+
test "using warnings_treated_as_deprecation do not supress warn if the deprecation is allowed" do
87+
Configuration.allowed_deprecations = [/#example is deprecated/]
88+
Configuration.warnings_treated_as_deprecation = [//]
89+
90+
captured_io = capture_io do
91+
assert_nothing_raised do
92+
warn("#example is deprecated")
93+
trigger_deprecation_toolkit_behavior
94+
end
95+
end
96+
line_msg = captured_io.find { |line| line.include?("#example is deprecated") }
97+
assert_equal("#example is deprecated\n", line_msg)
98+
end
8399
end
84100
end

0 commit comments

Comments
 (0)