Skip to content

Commit 9d3294e

Browse files
committed
Use keyword_args
1 parent aff7ca0 commit 9d3294e

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/deprecation_tracker.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ def warn(*messages, uplevel: nil, category: nil, **kwargs)
2323
messages.each { |message| callback.(message) }
2424
end
2525

26-
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.5.0")
27-
super(*messages)
28-
elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
29-
super(*messages, uplevel: nil)
30-
else
31-
super(*messages)
26+
# Build keyword args supported by this Ruby version
27+
keyword_args = {}
28+
keyword_args[:uplevel] = uplevel if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.5.0")
29+
keyword_args[:category] = category if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.2.0")
30+
31+
begin
32+
# Combine known safe keywords with any extras
33+
super(*messages, **keyword_args, **kwargs)
34+
rescue ArgumentError
35+
# Fallback: only pass known safe keywords
36+
super(*messages, **keyword_args)
3237
end
3338
end
3439
end

spec/deprecation_tracker_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ def self.behavior
294294
end
295295

296296
describe DeprecationTracker::KernelWarnTracker do
297+
before { DeprecationTracker::KernelWarnTracker.callbacks.clear }
298+
297299
it "captures Kernel#warn" do
298300
warn_messages = []
299301
DeprecationTracker::KernelWarnTracker.callbacks << -> (message) { warn_messages << message }
@@ -340,5 +342,24 @@ def self.behavior
340342
}.to not_raise_error.and output.to_stderr
341343
end
342344
end
345+
346+
it "handles known and unknown keyword arguments without raising" do
347+
warnings = []
348+
DeprecationTracker::KernelWarnTracker.callbacks << ->(msg) { warnings << msg }
349+
350+
expect {
351+
warn(
352+
"This is a test warning",
353+
uplevel: 1,
354+
category: :deprecated,
355+
deprecation: true,
356+
span: 1.2,
357+
stack: ["line"]
358+
)
359+
}.to not_raise_error
360+
361+
expect(warnings).to include("This is a test warning")
362+
end
363+
343364
end
344365
end

0 commit comments

Comments
 (0)