Skip to content

Commit

Permalink
Support new warning format in Ruby 3.4
Browse files Browse the repository at this point in the history
Ruby 3.4 uses ' instead of ` when quoting in warnings, so handle
both quoting types.
  • Loading branch information
jeremyevans committed May 22, 2024
1 parent c97469d commit 8ee6e8b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== master

* Support new warning format in Ruby 3.4 (jeremyevans)

=== 1.3.0 (2022-07-14)

* Allow Warning.clear to take a block, and restore current state after block (splattael) (#18, #20)
Expand Down
10 changes: 5 additions & 5 deletions lib/warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ module Warning
module Processor
# Map of symbols to regexps for warning messages to ignore.
IGNORE_MAP = {
ambiguous_slash: /: warning: ambiguous first argument; put parentheses or a space even after `\/' operator\n\z|: warning: ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `\/' operator\n\z/,
arg_prefix: /: warning: `[&\*]' interpreted as argument prefix\n\z/,
ambiguous_slash: /: warning: ambiguous first argument; put parentheses or a space even after [`']\/' operator\n\z|: warning: ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after [`']\/' operator\n\z/,
arg_prefix: /: warning: [`'][&\*]' interpreted as argument prefix\n\z/,
bignum: /: warning: constant ::Bignum is deprecated\n\z/,
fixnum: /: warning: constant ::Fixnum is deprecated\n\z/,
method_redefined: /: warning: method redefined; discarding old .+\n\z|: warning: previous definition of .+ was here\n\z/,
missing_gvar: /: warning: global variable `\$.+' not initialized\n\z/,
missing_gvar: /: warning: global variable [`']\$.+' not initialized\n\z/,
missing_ivar: /: warning: instance variable @.+ not initialized\n\z/,
not_reached: /: warning: statement not reached\n\z/,
shadow: /: warning: shadowing outer local variable - \w+\n\z/,
unused_var: /: warning: assigned but unused variable - \w+\n\z/,
useless_operator: /: warning: possibly useless use of [><!=]+ in void context\n\z/,
keyword_separation: /: warning: (?:Using the last argument (?:for `.+' )?as keyword parameters is deprecated; maybe \*\* should be added to the call|Passing the keyword argument (?:for `.+' )?as the last hash parameter is deprecated|Splitting the last argument (?:for `.+' )?into positional and keyword parameters is deprecated|The called method (?:`.+' )?is defined here)\n\z/,
keyword_separation: /: warning: (?:Using the last argument (?:for [`'].+' )?as keyword parameters is deprecated; maybe \*\* should be added to the call|Passing the keyword argument (?:for [`'].+' )?as the last hash parameter is deprecated|Splitting the last argument (?:for [`'].+' )?into positional and keyword parameters is deprecated|The called method (?:[`'].+' )?is defined here)\n\z/,
safe: /: warning: (?:rb_safe_level_2_warning|rb_safe_level|rb_set_safe_level_force|rb_set_safe_level|rb_secure|rb_insecure_operation|rb_check_safe_obj|\$SAFE) will (?:be removed|become a normal global variable) in Ruby 3\.0\n\z/,
taint: /: warning: (?:rb_error_untrusted|rb_check_trusted|Pathname#taint|Pathname#untaint|rb_env_path_tainted|Object#tainted\?|Object#taint|Object#untaint|Object#untrusted\?|Object#untrust|Object#trust|rb_obj_infect|rb_tainted_str_new|rb_tainted_str_new_cstr) is deprecated and will be removed in Ruby 3\.2\.?\n\z/,
mismatched_indentations: /: warning: mismatched indentations at '.+' with '.+' at \d+\n\z/,
Expand Down Expand Up @@ -166,7 +166,7 @@ def ignore(regexp, path='')
# /instance variable @\w+ not initialized/ => proc do |warning|
# LOGGER.warning(warning)
# end,
# /global variable `\$\w+' not initialized/ => proc do |warning|
# /global variable [`']\$\w+' not initialized/ => proc do |warning|
# LOGGER.error(warning)
# end
# )
Expand Down
9 changes: 5 additions & 4 deletions test/test_freeze_warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class WarningFreezeTest < Minitest::Test
module EnvUtil
def verbose_warning
class << (stderr = "")
stderr = String.new
class << stderr
alias write <<
end
stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
Expand Down Expand Up @@ -40,14 +41,14 @@ def assert_warning(pat, msg = nil)
def test_warning_ignore
w = nil

Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
Warning.ignore(/global variable [`']\$test_warning_ignore' not initialized/)
Warning.process do |warning|
w = [4, warning]
end
Warning.freeze

assert_raises RuntimeError do
Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
Warning.ignore(/global variable [`']\$test_warning_ignore' not initialized/)
end
assert_raises RuntimeError do
Warning.process{|warning| w = [4, warning]}
Expand All @@ -62,6 +63,6 @@ def test_warning_ignore
$test_warning_ignore2
end
assert_equal(4, w.first)
assert_match(/global variable `\$test_warning_ignore2' not initialized/, w.last)
assert_match(/global variable [`']\$test_warning_ignore2' not initialized/, w.last)
end
end
62 changes: 31 additions & 31 deletions test/test_warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class WarningTest < Minitest::Test
module EnvUtil
def verbose_warning
stderr = ""
class << (stderr = "")
stderr = String.new
class << stderr
alias write <<
def puts(*a)
self << a.join("\n")
Expand Down Expand Up @@ -135,16 +135,16 @@ def test_warning_clear_dedup
def test_warning_dedup
gvar = ->{$test_warning_dedup}

assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
assert_warning(/global variable [`']\$test_warning_dedup' not initialized/) do
gvar.call
end
assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
assert_warning(/global variable [`']\$test_warning_dedup' not initialized/) do
gvar.call
end

Warning.dedup

assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
assert_warning(/global variable [`']\$test_warning_dedup' not initialized/) do
gvar.call
end
assert_warning('') do
Expand All @@ -153,33 +153,33 @@ def test_warning_dedup
end

def test_warning_ignore
assert_warning(/global variable `\$test_warning_ignore' not initialized/) do
assert_warning(/global variable [`']\$test_warning_ignore' not initialized/) do
assert_nil($test_warning_ignore)
end

Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
Warning.ignore(/global variable [`']\$test_warning_ignore' not initialized/)

assert_warning '' do
assert_nil($test_warning_ignore)
end

assert_warning(/global variable `\$test_warning_ignore2' not initialized/) do
assert_warning(/global variable [`']\$test_warning_ignore2' not initialized/) do
assert_nil($test_warning_ignore2)
end

Warning.ignore(/global variable `\$test_warning_ignore2' not initialized/, __FILE__)
Warning.ignore(/global variable [`']\$test_warning_ignore2' not initialized/, __FILE__)

assert_warning '' do
assert_nil($test_warning_ignore2)
end

assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
assert_warning(/global variable [`']\$test_warning_ignore3' not initialized/) do
assert_nil($test_warning_ignore3)
end

Warning.ignore(/global variable `\$test_warning_ignore3' not initialized/, __FILE__ + 'a')
Warning.ignore(/global variable [`']\$test_warning_ignore3' not initialized/, __FILE__ + 'a')

assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
assert_warning(/global variable [`']\$test_warning_ignore3' not initialized/) do
assert_nil($test_warning_ignore3)
end

Expand All @@ -205,7 +205,7 @@ def test_warning_ignore_missing_ivar
end

def test_warning_ignore_missing_gvar
assert_warning(/global variable `\$gvar' not initialized/) do
assert_warning(/global variable [`']\$gvar' not initialized/) do
$gvar
end

Expand Down Expand Up @@ -338,11 +338,11 @@ def test_warning_ignore_useless_operator
end

def test_warning_ignore_arg_prefix
assert_warning(/: warning: `\*' interpreted as argument prefix/) do
assert_warning(/: warning: [`']\*' interpreted as argument prefix/) do
instance_eval('Array *[nil]', __FILE__)
end

assert_warning(/: warning: `&' interpreted as argument prefix/) do
assert_warning(/: warning: [`']&' interpreted as argument prefix/) do
instance_eval('tap &proc{}', __FILE__)
end
Warning.ignore(:arg_prefix, __FILE__)
Expand Down Expand Up @@ -374,16 +374,16 @@ def skw(h=1, a: 1)
end

def test_warning_ignore_keyword
assert_warning(/warning: Using the last argument as keyword parameters is deprecated; maybe \*\* should be added to the call.*The called method `h2kw' is defined here/m) do
assert_warning(/warning: Using the last argument as keyword parameters is deprecated; maybe \*\* should be added to the call.*The called method [`']h2kw' is defined here/m) do
h2kw({})
end
assert_warning(/warning: Passing the keyword argument as the last hash parameter is deprecated.*The called method `kw2h' is defined here/m) do
assert_warning(/warning: Passing the keyword argument as the last hash parameter is deprecated.*The called method [`']kw2h' is defined here/m) do
kw2h(a: 1)
end
assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method [`']skw' is defined here/m) do
skw("b" => 1, a: 2)
end
assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method [`']skw' is defined here/m) do
skw({"b" => 1, a: 2})
end

Expand Down Expand Up @@ -490,7 +490,7 @@ def test_warning_process
warn = [0, warning]
end

assert_warning(/global variable `\$test_warning_process' not initialized/) do
assert_warning(/global variable [`']\$test_warning_process' not initialized/) do
$test_warning_process
end
assert_nil(warn)
Expand All @@ -503,7 +503,7 @@ def test_warning_process
$test_warning_process2
end
assert_equal(1, warn.first)
assert_match(/global variable `\$test_warning_process2' not initialized/, warn.last)
assert_match(/global variable [`']\$test_warning_process2' not initialized/, warn.last)
warn = nil

Warning.process(File.dirname(__FILE__)) do |warning|
Expand All @@ -514,7 +514,7 @@ def test_warning_process
$test_warning_process3
end
assert_equal(1, warn.first)
assert_match(/global variable `\$test_warning_process3' not initialized/, warn.last)
assert_match(/global variable [`']\$test_warning_process3' not initialized/, warn.last)
warn = nil

Warning.process(__FILE__+':') do |warning|
Expand All @@ -525,12 +525,12 @@ def test_warning_process
$test_warning_process4
end
assert_equal(3, warn.first)
assert_match(/global variable `\$test_warning_process4' not initialized/, warn.last)
assert_match(/global variable [`']\$test_warning_process4' not initialized/, warn.last)
warn = nil

Warning.clear

assert_warning(/global variable `\$test_warning_process5' not initialized/) do
assert_warning(/global variable [`']\$test_warning_process5' not initialized/) do
$test_warning_process5
end
assert_nil(warn)
Expand All @@ -543,7 +543,7 @@ def test_warning_process
$test_warning_process6
end
assert_equal(4, warn.first)
assert_match(/global variable `\$test_warning_process6' not initialized/, warn.last)
assert_match(/global variable [`']\$test_warning_process6' not initialized/, warn.last)

assert_raises(TypeError) do
Warning.process('', Object.new=>:raise)
Expand All @@ -557,10 +557,10 @@ def test_warning_process_block_return_default
:default
end

assert_warning(/global variable `\$test_warning_process_block_return_default' not initialized/) do
assert_warning(/global variable [`']\$test_warning_process_block_return_default' not initialized/) do
$test_warning_process_block_return_default
end
assert_match(/global variable `\$test_warning_process_block_return_default' not initialized/, w)
assert_match(/global variable [`']\$test_warning_process_block_return_default' not initialized/, w)
end

def test_warning_process_block_return_backtrace
Expand All @@ -570,10 +570,10 @@ def test_warning_process_block_return_backtrace
:backtrace
end

assert_warning(/global variable `\$test_warning_process_block_return_backtrace' not initialized.*#{__FILE__}/m) do
assert_warning(/global variable [`']\$test_warning_process_block_return_backtrace' not initialized.*#{__FILE__}/m) do
$test_warning_process_block_return_backtrace
end
assert_match(/global variable `\$test_warning_process_block_return_backtrace' not initialized/, w)
assert_match(/global variable [`']\$test_warning_process_block_return_backtrace' not initialized/, w)
end

def test_warning_process_block_return_raise
Expand All @@ -586,7 +586,7 @@ def test_warning_process_block_return_raise
assert_raises(RuntimeError) do
$test_warning_process_block_return_raise
end
assert_match(/global variable `\$test_warning_process_block_return_raise' not initialized/, w)
assert_match(/global variable [`']\$test_warning_process_block_return_raise' not initialized/, w)
end

def test_warning_process_action
Expand All @@ -602,7 +602,7 @@ def a; end
end
end

assert_warning(/global variable `\$test_warning_process_action' not initialized.*#{__FILE__}/m) do
assert_warning(/global variable [`']\$test_warning_process_action' not initialized.*#{__FILE__}/m) do
$test_warning_process_action
end

Expand Down

0 comments on commit 8ee6e8b

Please sign in to comment.