warning 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 719af8cdb41da57d07edb0fa0eac81d2bb017d5d816cadf98e600cc75e63f4aa
4
- data.tar.gz: 169550daf070386f36072d01c9c36303c631b6f598cf358e0f16e786f1d4ac68
3
+ metadata.gz: c2628e7956e2f0c2a7f55ef1d4397a1f1ff8339d48904b49fc16692934930464
4
+ data.tar.gz: 72b7fa93465fe52a2907ef2172d3c90694308af52bb2cd5b9117c6e7084419d1
5
5
  SHA512:
6
- metadata.gz: 5ddf24399c73e0b2b747189d02972c3616482ac90c3c9e89b16b8594a1e2c6cf005b3b81bab905de6f30d3fd1882d806c8746fd317f59e6915408f80f054ee8f
7
- data.tar.gz: a5f1d714b59737dd6e67ab000687783acff87c3fe5cfd85bac2828c91556a7de5637d082e82d0a34198b53580a324c69639da93e07c0436e4f648e0031c7ff00
6
+ metadata.gz: e8f95072982d387477121b90c13790876743d9a40a0a32c06a1ea72720b85c6d831c4dceff28445dd8be2a3ba6e915670b874a908e6b438cce40c83229f95a15
7
+ data.tar.gz: 3cf24b069bc071cdf425aad86fae1c0b37b3cf723ee8c365a09a183da7e3f737f7eb454f8b1fbc29be1a9651d01fd286e13562ea442c1ae57ca5fc2c045a7570
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ === 1.2.0 (2021-02-16)
2
+
3
+ * Add support for :void_context as regexp argument to Warning.ignore (jeremyevans)
4
+
5
+ * Support :category keyword to Warning.warn on Ruby 3.0 (jeremyevans)
6
+
7
+ * Fix :taint warning handling on Ruby 3.0 (jeremyevans)
8
+
9
+ * Fix :ambiguous_slash warning handling on Ruby 3.0 (jeremyevans)
10
+
1
11
  === 1.1.0 (2020-06-12)
2
12
 
3
13
  * Allow Warning.process to be called with a hash of actions instead of a block (jeremyevans)
data/README.rdoc CHANGED
@@ -42,6 +42,7 @@ appropriate regexp. The supported symbols are:
42
42
  * :taint
43
43
  * :unused_var
44
44
  * :useless_operator
45
+ * :void_context
45
46
 
46
47
  <tt>Warning.process</tt> takes an optional path prefix and a block, and if the
47
48
  warning string starts with the path prefix, it calls the block with the warning
data/lib/warning.rb CHANGED
@@ -4,7 +4,7 @@ module Warning
4
4
  module Processor
5
5
  # Map of symbols to regexps for warning messages to ignore.
6
6
  IGNORE_MAP = {
7
- ambiguous_slash: /: warning: ambiguous first argument; put parentheses or a space even after `\/' operator\n\z/,
7
+ 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/,
8
8
  arg_prefix: /: warning: `[&\*]' interpreted as argument prefix\n\z/,
9
9
  bignum: /: warning: constant ::Bignum is deprecated\n\z/,
10
10
  fixnum: /: warning: constant ::Fixnum is deprecated\n\z/,
@@ -17,8 +17,9 @@ module Warning
17
17
  useless_operator: /: warning: possibly useless use of [><!=]+ in void context\n\z/,
18
18
  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/,
19
19
  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/,
20
- 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/,
20
+ 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/,
21
21
  mismatched_indentations: /: warning: mismatched indentations at '.+' with '.+' at \d+\n\z/,
22
+ void_context: /possibly useless use of :: in void context/,
22
23
  }
23
24
 
24
25
  # Map of action symbols to procs that return the symbol
@@ -75,6 +76,8 @@ module Warning
75
76
  # :unused_var :: Ignore warnings for unused variables.
76
77
  # :useless_operator :: Ignore warnings when using operators such as == and > when the
77
78
  # result is not used.
79
+ # :void_context :: Ignore warnings for :: to reference constants when the result is not
80
+ # used (often used to trigger autoload).
78
81
  #
79
82
  # Examples:
80
83
  #
@@ -126,7 +129,7 @@ module Warning
126
129
  # Instead of passing a block, you can pass a hash of actions to take for specific
127
130
  # warnings, using regexp as keys and a callable objects as values:
128
131
  #
129
- # Warning.ignore(__FILE__,
132
+ # Warning.process(__FILE__,
130
133
  # /instance variable @\w+ not initialized/ => proc do |warning|
131
134
  # LOGGER.warning(warning)
132
135
  # end,
@@ -166,57 +169,63 @@ module Warning
166
169
  nil
167
170
  end
168
171
 
169
- # Handle ignored warnings and warning processors. If the warning is
170
- # not ignored, is not a duplicate warning (if checking for duplicates)
171
- # and there is no warning processor setup for the warning
172
- # string, then use the default behavior of writing to $stderr.
173
- def warn(str)
174
- synchronize{@ignore.dup}.each do |path, regexp|
175
- if str.start_with?(path) && str =~ regexp
176
- return
177
- end
178
- end
179
172
 
180
- if @dedup
181
- if synchronize{@dedup[str]}
182
- return
173
+ if RUBY_VERSION >= '3.0'
174
+ method_args = ', category: nil'
175
+ super_ = "category ? super : super(str)"
176
+ else
177
+ super_ = "super"
178
+ end
179
+
180
+ class_eval(<<-END, __FILE__, __LINE__+1)
181
+ def warn(str#{method_args})
182
+ synchronize{@ignore.dup}.each do |path, regexp|
183
+ if str.start_with?(path) && str =~ regexp
184
+ return
185
+ end
183
186
  end
184
187
 
185
- synchronize{@dedup[str] = true}
186
- end
188
+ if @dedup
189
+ if synchronize{@dedup[str]}
190
+ return
191
+ end
192
+
193
+ synchronize{@dedup[str] = true}
194
+ end
187
195
 
188
- action = catch(:action) do
189
- synchronize{@process.dup}.each do |path, block|
190
- if str.start_with?(path)
191
- if block.is_a?(Hash)
192
- block.each do |regexp, blk|
193
- if str =~ regexp
194
- throw :action, blk.call(str)
196
+ action = catch(:action) do
197
+ synchronize{@process.dup}.each do |path, block|
198
+ if str.start_with?(path)
199
+ if block.is_a?(Hash)
200
+ block.each do |regexp, blk|
201
+ if str =~ regexp
202
+ throw :action, blk.call(str)
203
+ end
195
204
  end
205
+ else
206
+ throw :action, block.call(str)
196
207
  end
197
- else
198
- throw :action, block.call(str)
199
208
  end
200
209
  end
210
+
211
+ :default
201
212
  end
202
213
 
203
- :default
204
- end
214
+ case action
215
+ when :default
216
+ #{super_}
217
+ when :backtrace
218
+ #{super_}
219
+ $stderr.puts caller
220
+ when :raise
221
+ raise str
222
+ else
223
+ # nothing
224
+ end
205
225
 
206
- case action
207
- when :default
208
- super
209
- when :backtrace
210
- super
211
- $stderr.puts caller
212
- when :raise
213
- raise str
214
- else
215
- # nothing
226
+ nil
216
227
  end
217
-
218
- nil
219
- end
228
+ END
220
229
 
221
230
  private
222
231
 
@@ -40,31 +40,30 @@ class WarningFreezeTest < Minitest::Test
40
40
  end
41
41
 
42
42
  def test_warning_ignore
43
- obj = Object.new
44
43
  w = nil
45
44
 
46
- Warning.ignore(/instance variable @ivar not initialized/)
45
+ Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
47
46
  Warning.process do |warning|
48
47
  w = [4, warning]
49
48
  end
50
49
  Warning.freeze
51
50
 
52
51
  assert_raises RuntimeError do
53
- Warning.ignore(/instance variable @ivar not initialized/)
52
+ Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
54
53
  end
55
54
  assert_raises RuntimeError do
56
55
  Warning.process{|warning| w = [4, warning]}
57
56
  end
58
57
 
59
58
  assert_warning '' do
60
- assert_nil(obj.instance_variable_get(:@ivar))
59
+ $test_warning_ignore
61
60
  end
62
61
  assert_nil w
63
62
 
64
63
  assert_warning '' do
65
- assert_nil(obj.instance_variable_get(:@ivar6))
64
+ $test_warning_ignore2
66
65
  end
67
66
  assert_equal(4, w.first)
68
- assert_match(/instance variable @ivar6 not initialized/, w.last)
67
+ assert_match(/global variable `\$test_warning_ignore2' not initialized/, w.last)
69
68
  end
70
69
  end
data/test/test_warning.rb CHANGED
@@ -48,71 +48,65 @@ class WarningTest < Minitest::Test
48
48
  Warning.clear
49
49
  end
50
50
 
51
- def ivar
52
- Object.new.instance_variable_get(:@ivar)
53
- end
54
-
55
51
  def test_warning_dedup
56
- assert_warning(/instance variable @ivar not initialized/) do
57
- ivar
52
+ gvar = ->{$test_warning_dedup}
53
+
54
+ assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
55
+ gvar.call
58
56
  end
59
- assert_warning(/instance variable @ivar not initialized/) do
60
- ivar
57
+ assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
58
+ gvar.call
61
59
  end
62
60
 
63
61
  Warning.dedup
64
62
 
65
- assert_warning(/instance variable @ivar not initialized/) do
66
- ivar
63
+ assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
64
+ gvar.call
67
65
  end
68
66
  assert_warning('') do
69
- ivar
67
+ gvar.call
70
68
  end
71
69
  end
72
70
 
73
71
  def test_warning_ignore
74
- obj = Object.new
75
-
76
- assert_warning(/instance variable @ivar not initialized/) do
77
- assert_nil(obj.instance_variable_get(:@ivar))
78
- end
79
-
80
- assert_warning(/instance variable @ivar not initialized/) do
81
- assert_nil(obj.instance_variable_get(:@ivar))
72
+ assert_warning(/global variable `\$test_warning_ignore' not initialized/) do
73
+ assert_nil($test_warning_ignore)
82
74
  end
83
75
 
84
- Warning.ignore(/instance variable @ivar not initialized/)
76
+ Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
85
77
 
86
78
  assert_warning '' do
87
- assert_nil(obj.instance_variable_get(:@ivar))
79
+ assert_nil($test_warning_ignore)
88
80
  end
89
81
 
90
- assert_warning(/instance variable @ivar2 not initialized/) do
91
- assert_nil(obj.instance_variable_get(:@ivar2))
82
+ assert_warning(/global variable `\$test_warning_ignore2' not initialized/) do
83
+ assert_nil($test_warning_ignore2)
92
84
  end
93
85
 
94
- Warning.ignore(/instance variable @ivar2 not initialized/, __FILE__)
86
+ Warning.ignore(/global variable `\$test_warning_ignore2' not initialized/, __FILE__)
95
87
 
96
88
  assert_warning '' do
97
- assert_nil(obj.instance_variable_get(:@ivar2))
89
+ assert_nil($test_warning_ignore2)
98
90
  end
99
91
 
100
- assert_warning(/instance variable @ivar3 not initialized/) do
101
- assert_nil(obj.instance_variable_get(:@ivar3))
92
+ assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
93
+ assert_nil($test_warning_ignore3)
102
94
  end
103
95
 
104
- Warning.ignore(/instance variable @ivar3 not initialized/, __FILE__+'a')
96
+ Warning.ignore(/global variable `\$test_warning_ignore3' not initialized/, __FILE__ + 'a')
105
97
 
106
- assert_warning(/instance variable @ivar3 not initialized/) do
107
- assert_nil(obj.instance_variable_get(:@ivar3))
98
+ assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
99
+ assert_nil($test_warning_ignore3)
108
100
  end
109
101
  end
110
102
 
111
103
  def test_warning_ignore_missing_ivar
112
104
  Warning.clear
113
105
 
114
- assert_warning(/instance variable @ivar not initialized/) do
115
- assert_nil(instance_variable_get(:@ivar))
106
+ unless RUBY_VERSION >= '3.0'
107
+ assert_warning(/instance variable @ivar not initialized/) do
108
+ assert_nil(instance_variable_get(:@ivar))
109
+ end
116
110
  end
117
111
 
118
112
  Warning.ignore(:missing_ivar, __FILE__)
@@ -184,9 +178,21 @@ class WarningTest < Minitest::Test
184
178
  end
185
179
  end
186
180
 
181
+ def test_warning_ignore_void_context
182
+ assert_warning(/warning: possibly useless use of :: in void context/) do
183
+ instance_eval('::Object; nil', __FILE__, __LINE__)
184
+ end
185
+
186
+ Warning.ignore(:void_context, __FILE__)
187
+
188
+ assert_warning '' do
189
+ instance_eval('::Object; nil', __FILE__, __LINE__)
190
+ end
191
+ end
192
+
187
193
  def test_warning_ignore_ambiguous_slash
188
194
  def self.d(re); end
189
- assert_warning(/warning: ambiguous first argument; put parentheses or a space even after `\/' operator/) do
195
+ assert_warning(/warning: ambi/) do
190
196
  instance_eval('d /a/', __FILE__)
191
197
  end
192
198
 
@@ -368,15 +374,14 @@ class WarningTest < Minitest::Test
368
374
  end
369
375
 
370
376
  def test_warning_process
371
- obj = Object.new
372
377
  warn = nil
373
378
 
374
379
  Warning.process(__FILE__+'a') do |warning|
375
380
  warn = [0, warning]
376
381
  end
377
382
 
378
- assert_warning(/instance variable @ivar not initialized/) do
379
- assert_nil(obj.instance_variable_get(:@ivar))
383
+ assert_warning(/global variable `\$test_warning_process' not initialized/) do
384
+ $test_warning_process
380
385
  end
381
386
  assert_nil(warn)
382
387
 
@@ -385,10 +390,10 @@ class WarningTest < Minitest::Test
385
390
  end
386
391
 
387
392
  assert_warning '' do
388
- assert_nil(obj.instance_variable_get(:@ivar2))
393
+ $test_warning_process2
389
394
  end
390
395
  assert_equal(1, warn.first)
391
- assert_match(/instance variable @ivar2 not initialized/, warn.last)
396
+ assert_match(/global variable `\$test_warning_process2' not initialized/, warn.last)
392
397
  warn = nil
393
398
 
394
399
  Warning.process(File.dirname(__FILE__)) do |warning|
@@ -396,10 +401,10 @@ class WarningTest < Minitest::Test
396
401
  end
397
402
 
398
403
  assert_warning '' do
399
- assert_nil(obj.instance_variable_get(:@ivar3))
404
+ $test_warning_process3
400
405
  end
401
406
  assert_equal(1, warn.first)
402
- assert_match(/instance variable @ivar3 not initialized/, warn.last)
407
+ assert_match(/global variable `\$test_warning_process3' not initialized/, warn.last)
403
408
  warn = nil
404
409
 
405
410
  Warning.process(__FILE__+':') do |warning|
@@ -407,16 +412,16 @@ class WarningTest < Minitest::Test
407
412
  end
408
413
 
409
414
  assert_warning '' do
410
- assert_nil(obj.instance_variable_get(:@ivar4))
415
+ $test_warning_process4
411
416
  end
412
417
  assert_equal(3, warn.first)
413
- assert_match(/instance variable @ivar4 not initialized/, warn.last)
418
+ assert_match(/global variable `\$test_warning_process4' not initialized/, warn.last)
414
419
  warn = nil
415
420
 
416
421
  Warning.clear
417
422
 
418
- assert_warning(/instance variable @ivar5 not initialized/) do
419
- assert_nil(obj.instance_variable_get(:@ivar5))
423
+ assert_warning(/global variable `\$test_warning_process5' not initialized/) do
424
+ $test_warning_process5
420
425
  end
421
426
  assert_nil(warn)
422
427
 
@@ -425,10 +430,10 @@ class WarningTest < Minitest::Test
425
430
  end
426
431
 
427
432
  assert_warning '' do
428
- assert_nil(obj.instance_variable_get(:@ivar6))
433
+ $test_warning_process6
429
434
  end
430
435
  assert_equal(4, warn.first)
431
- assert_match(/instance variable @ivar6 not initialized/, warn.last)
436
+ assert_match(/global variable `\$test_warning_process6' not initialized/, warn.last)
432
437
  end
433
438
 
434
439
  def test_warning_process_block_return_default
@@ -438,10 +443,10 @@ class WarningTest < Minitest::Test
438
443
  :default
439
444
  end
440
445
 
441
- assert_warning(/instance variable @ivar not initialized/) do
442
- ivar
446
+ assert_warning(/global variable `\$test_warning_process_block_return_default' not initialized/) do
447
+ $test_warning_process_block_return_default
443
448
  end
444
- assert_match(/instance variable @ivar not initialized/, w)
449
+ assert_match(/global variable `\$test_warning_process_block_return_default' not initialized/, w)
445
450
  end
446
451
 
447
452
  def test_warning_process_block_return_backtrace
@@ -451,10 +456,10 @@ class WarningTest < Minitest::Test
451
456
  :backtrace
452
457
  end
453
458
 
454
- assert_warning(/instance variable @ivar not initialized.*#{__FILE__}/m) do
455
- ivar
459
+ assert_warning(/global variable `\$test_warning_process_block_return_backtrace' not initialized.*#{__FILE__}/m) do
460
+ $test_warning_process_block_return_backtrace
456
461
  end
457
- assert_match(/instance variable @ivar not initialized/, w)
462
+ assert_match(/global variable `\$test_warning_process_block_return_backtrace' not initialized/, w)
458
463
  end
459
464
 
460
465
  def test_warning_process_block_return_raise
@@ -464,26 +469,29 @@ class WarningTest < Minitest::Test
464
469
  :raise
465
470
  end
466
471
 
467
- assert_raises(RuntimeError, /instance variable @ivar not initialized/) do
468
- EnvUtil.verbose_warning{ivar}
472
+ assert_raises(RuntimeError) do
473
+ $test_warning_process_block_return_raise
469
474
  end
470
- assert_match(/instance variable @ivar not initialized/, w)
475
+ assert_match(/global variable `\$test_warning_process_block_return_raise' not initialized/, w)
471
476
  end
472
477
 
473
478
  def test_warning_process_action
474
479
  w = nil
475
- Warning.process(__FILE__, :missing_ivar=>:default, :missing_gvar=>:backtrace, :ambiguous_slash=>:raise)
480
+ Warning.process(__FILE__, :method_redefined=>:default, :missing_gvar=>:backtrace, :ambiguous_slash=>:raise)
476
481
  Warning.process(__FILE__, :not_reached=>proc do |warning|
477
482
  w = warning
478
483
  :raise
479
484
  end)
480
485
 
481
- assert_warning(/instance variable @ivar not initialized/) do
482
- ivar
486
+ assert_warning(/warning: method redefined/) do
487
+ Class.new do
488
+ def a; end
489
+ def a; end
490
+ end
483
491
  end
484
492
 
485
- assert_warning(/global variable `\$gvar' not initialized.*#{__FILE__}/m) do
486
- $gvar
493
+ assert_warning(/global variable `\$test_warning_process_action' not initialized.*#{__FILE__}/m) do
494
+ $test_warning_process_action
487
495
  end
488
496
 
489
497
  Warning.process(__FILE__) do |warning|
@@ -491,7 +499,7 @@ class WarningTest < Minitest::Test
491
499
  :raise
492
500
  end
493
501
 
494
- assert_raises(RuntimeError, /warning: ambiguous first argument; put parentheses or a space even after `\/' operator/) do
502
+ assert_raises(RuntimeError, /warning: ambi/) do
495
503
  EnvUtil.verbose_warning{instance_eval('d /a/', __FILE__)}
496
504
  end
497
505
 
@@ -512,4 +520,12 @@ class WarningTest < Minitest::Test
512
520
  Warning.process(__FILE__, :missing_ivar=>:default){}
513
521
  end
514
522
  end
523
+
524
+ if RUBY_VERSION >= '3.0'
525
+ def test_warning_warn_category_keyword
526
+ assert_warning('foo') do
527
+ Warning.warn("foo", category: :deprecated)
528
+ end
529
+ end
530
+ end
515
531
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warning
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-12 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest-global_expectations
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.1.2
79
+ rubygems_version: 3.2.3
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: Add custom processing for warnings