Skip to content

STJ-23: BUGFIX: deprecation_tracker breaking with unknown keywords #157 #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
19 changes: 13 additions & 6 deletions lib/deprecation_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ def self.callbacks
@callbacks ||= []
end

def warn(*messages, uplevel: nil, category: nil)
def warn(*messages, uplevel: nil, category: nil, **kwargs)
KernelWarnTracker.callbacks.each do |callback|
messages.each { |message| callback.(message) }
end

if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.5.0")
# Build keyword args supported by this Ruby version
keyword_args = {}
keyword_args[:uplevel] = uplevel if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.5.0")
keyword_args[:category] = category if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.2.0")

if keyword_args.empty?
super(*messages)
elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
super(*messages, uplevel: nil)
else
super
begin
super(*messages, **keyword_args, **kwargs)
rescue ArgumentError
super(*messages, **keyword_args)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julioalucero if you know when the signature of the method changed, it would be best to use if/else vs. begin/rescue

end
end
end
Expand All @@ -43,7 +50,7 @@ def before_setup
@@deprecation_tracker.bucket = test_file_name.gsub(Rails.root.to_s, ".")
super
end

def after_teardown
super
@@deprecation_tracker.bucket = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/next_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module NextRails
VERSION = "1.4.6"
VERSION = "1.4.7"
end
31 changes: 31 additions & 0 deletions spec/deprecation_tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ def self.behavior
end

describe DeprecationTracker::KernelWarnTracker do
before { DeprecationTracker::KernelWarnTracker.callbacks.clear }

it "captures Kernel#warn" do
warn_messages = []
DeprecationTracker::KernelWarnTracker.callbacks << -> (message) { warn_messages << message }
Expand Down Expand Up @@ -330,5 +332,34 @@ def self.behavior
end
end
end

describe "bug when warning uses unexpected keyword arguments" do
it "does not raise an error with unknown keyword args like :deprecation, :span, :stack" do
DeprecationTracker::KernelWarnTracker.callbacks << -> (message) { message.to_s }

expect {
warn("Unknown deprecation warning", deprecation: true, span: 1.2, stack: ["line1", "line2"])
}.to not_raise_error.and output.to_stderr
end
end

it "handles known and unknown keyword arguments without raising" do
warnings = []
DeprecationTracker::KernelWarnTracker.callbacks << ->(msg) { warnings << msg }

expect {
warn(
"This is a test warning",
uplevel: 1,
category: :deprecated,
deprecation: true,
span: 1.2,
stack: ["line"]
)
}.to not_raise_error

expect(warnings).to include("This is a test warning")
end

end
end