From 9ca8f6abc3af71e16b41fcf43d560425e459edfe Mon Sep 17 00:00:00 2001 From: Peter Leitzen Date: Wed, 6 Jul 2022 00:19:30 +0200 Subject: [PATCH] Disallow Warning.process(Regexp, &block) Previously, the following code worked by accident: Warning.process(/foo/) { :raise } Calling Warning.process a second time resulted in # => ArgumentError (comparison of Regexp with Regexp failed) This commit checks the type of passed path to be a String. --- lib/warning.rb | 4 ++++ test/test_warning.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/warning.rb b/lib/warning.rb index 4211807..a279a5d 100644 --- a/lib/warning.rb +++ b/lib/warning.rb @@ -144,6 +144,10 @@ def ignore(regexp, path='') # # Warning.process(__FILE__, :missing_ivar=>:backtrace, :keyword_separation=>:raise) def process(path='', actions=nil, &block) + unless path.is_a?(String) + raise ArgumentError, "path must be a String not a #{path.class}" + end + if block if actions raise ArgumentError, "cannot pass both actions and block to Warning.process" diff --git a/test/test_warning.rb b/test/test_warning.rb index 1842340..c6642a3 100644 --- a/test/test_warning.rb +++ b/test/test_warning.rb @@ -543,6 +543,13 @@ def test_warning_process_no_action_and_no_block end end + def test_warning_process_path_no_string + e = assert_raises(ArgumentError) do + Warning.process(/foo/) { :raise } + end + assert_includes(e.message, "path must be a String not a Regexp") + end + if RUBY_VERSION >= '3.0' def test_warning_warn_category_keyword assert_warning('foo') do