diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8f574..d4317b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - [BUGFIX: example](https://github.com/fastruby/next_rails/pull/) - [CHORE: Create an entry point for the BundleReport command](https://github.com/fastruby/next_rails/pull/154) - [CHORE: Bring back support of Ruby 2.3, 2.4 and 2.5](https://github.com/fastruby/next_rails/pull/155) +- [BUGFIX: deprecation_tracker breaking with unknown keywords](https://github.com/fastruby/next_rails/pull/158) * Your changes/patches go here. diff --git a/lib/deprecation_tracker.rb b/lib/deprecation_tracker.rb index c7ade8d..47700c8 100644 --- a/lib/deprecation_tracker.rb +++ b/lib/deprecation_tracker.rb @@ -18,17 +18,22 @@ 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) } + messages.each { |message| callback.call(message) } end - if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.5.0") - super(*messages) - elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0") - super(*messages, uplevel: nil) + ruby_version = Gem::Version.new(RUBY_VERSION) + + if ruby_version >= Gem::Version.new("3.2.0") + # Kernel#warn supports uplevel, category + super(*messages, uplevel: uplevel, category: category) + elsif ruby_version >= Gem::Version.new("2.5.0") + # Kernel#warn supports only uplevel + super(*messages, uplevel: uplevel) else - super + # No keyword args supported + super(*messages) end end end @@ -43,7 +48,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 diff --git a/lib/next_rails/version.rb b/lib/next_rails/version.rb index e81698c..d2c1d0c 100644 --- a/lib/next_rails/version.rb +++ b/lib/next_rails/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module NextRails - VERSION = "1.4.6" + VERSION = "1.4.7" end diff --git a/spec/deprecation_tracker_spec.rb b/spec/deprecation_tracker_spec.rb index 21d7dfe..330723e 100644 --- a/spec/deprecation_tracker_spec.rb +++ b/spec/deprecation_tracker_spec.rb @@ -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 } @@ -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