Skip to content

Commit 02a28a0

Browse files
authored
Merge pull request #158 from fastruby/fix/152-deprecation_tracker-breaking-unknown-keywords
STJ-23: BUGFIX: deprecation_tracker breaking with unknown keywords #157
2 parents 00b4832 + e918dfe commit 02a28a0

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [BUGFIX: example](https://github.com/fastruby/next_rails/pull/<number>)
44
- [CHORE: Create an entry point for the BundleReport command](https://github.com/fastruby/next_rails/pull/154)
55
- [CHORE: Bring back support of Ruby 2.3, 2.4 and 2.5](https://github.com/fastruby/next_rails/pull/155)
6+
- [BUGFIX: deprecation_tracker breaking with unknown keywords](https://github.com/fastruby/next_rails/pull/158)
67

78
* Your changes/patches go here.
89

lib/deprecation_tracker.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@ def self.callbacks
1818
@callbacks ||= []
1919
end
2020

21-
def warn(*messages, uplevel: nil, category: nil)
21+
def warn(*messages, uplevel: nil, category: nil, **kwargs)
2222
KernelWarnTracker.callbacks.each do |callback|
23-
messages.each { |message| callback.(message) }
23+
messages.each { |message| callback.call(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)
26+
ruby_version = Gem::Version.new(RUBY_VERSION)
27+
28+
if ruby_version >= Gem::Version.new("3.2.0")
29+
# Kernel#warn supports uplevel, category
30+
super(*messages, uplevel: uplevel, category: category)
31+
elsif ruby_version >= Gem::Version.new("2.5.0")
32+
# Kernel#warn supports only uplevel
33+
super(*messages, uplevel: uplevel)
3034
else
31-
super
35+
# No keyword args supported
36+
super(*messages)
3237
end
3338
end
3439
end
@@ -43,7 +48,7 @@ def before_setup
4348
@@deprecation_tracker.bucket = test_file_name.gsub(Rails.root.to_s, ".")
4449
super
4550
end
46-
51+
4752
def after_teardown
4853
super
4954
@@deprecation_tracker.bucket = nil

lib/next_rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module NextRails
4-
VERSION = "1.4.6"
4+
VERSION = "1.4.7"
55
end

spec/deprecation_tracker_spec.rb

Lines changed: 31 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 }
@@ -330,5 +332,34 @@ def self.behavior
330332
end
331333
end
332334
end
335+
336+
describe "bug when warning uses unexpected keyword arguments" do
337+
it "does not raise an error with unknown keyword args like :deprecation, :span, :stack" do
338+
DeprecationTracker::KernelWarnTracker.callbacks << -> (message) { message.to_s }
339+
340+
expect {
341+
warn("Unknown deprecation warning", deprecation: true, span: 1.2, stack: ["line1", "line2"])
342+
}.to not_raise_error.and output.to_stderr
343+
end
344+
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+
333364
end
334365
end

0 commit comments

Comments
 (0)