warning 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aed045b84ec748f7558ea24268aef892d0efcd7d0f0f51cca7b5cf5f95d1aae7
4
- data.tar.gz: ad651f6e4f47d4b6064f7863d145d2747fab8477a27a5234e2e3336d85d1ae17
3
+ metadata.gz: 2af0fa1becb10a11e209630c73fdb0f1184352cdb174edbf2ce8eabeff2d1763
4
+ data.tar.gz: 51b04db9d1bcdfd9432c7501d3cfe15043bad6556940aea4214154791cee3964
5
5
  SHA512:
6
- metadata.gz: ead742a4b97033e1d1b6ec6460039d7f2138ba8586709a2819303ad0d6791daa26821ee7fd35a9701b2bf7d4ae01f47ef485781331ca77f116ef92d1ec06dada
7
- data.tar.gz: 935a429f2a967a57e67bf44739e0583abf186fe3baeaee0ce61532e9083844971ba956ca55831f18092d80652f7090d30f0d1da06758186fccebb98b0f78b18a
6
+ metadata.gz: bc4fe66b7efc5066225ad74b947bc7d32113651112a88ff12a9d8888dea60ed7b22ba54bc6cc156eb19f85f8cfe0c3199cbb7e041af44c54db08a9fda4fb4738
7
+ data.tar.gz: dc243ded2132d05c9e926a59577c978a10169253419ba6e1aa1cfbc9c96a2628b1360991dac9dce7ab4ac11e29683e129a52eb9859b9a61eb07f9492f8ae089a
data/CHANGELOG CHANGED
@@ -1,3 +1,15 @@
1
+ === 1.4.0 (2024-05-24)
2
+
3
+ * Add support for :ignored_block as regexp argument to Warning.ignore (jeremyevans)
4
+
5
+ * Support new warning format in Ruby 3.4 (jeremyevans)
6
+
7
+ === 1.3.0 (2022-07-14)
8
+
9
+ * Allow Warning.clear to take a block, and restore current state after block (splattael) (#18, #20)
10
+
11
+ * Raise ArgumentError if Warning.process is passed non-String as first argument (splattael) (#17, #19)
12
+
1
13
  === 1.2.1 (2021-10-04)
2
14
 
3
15
  * Recognize additional void context warnings (kachick) (#13)
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016-2020 Jeremy Evans
1
+ Copyright (c) 2016-2022 Jeremy Evans
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to
data/README.rdoc CHANGED
@@ -31,6 +31,7 @@ appropriate regexp. The supported symbols are:
31
31
  * :ambiguous_slash
32
32
  * :bignum
33
33
  * :fixnum
34
+ * :ignored_block
34
35
  * :keyword_separation
35
36
  * :method_redefined
36
37
  * :mismatched_indentations
data/lib/warning.rb CHANGED
@@ -4,18 +4,19 @@ 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|: warning: ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `\/' operator\n\z/,
8
- arg_prefix: /: warning: `[&\*]' interpreted as argument prefix\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
+ 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/,
11
+ ignored_block: /: warning: the block passed to '.+' defined at .+:\d+ may be ignored\n\z/,
11
12
  method_redefined: /: warning: method redefined; discarding old .+\n\z|: warning: previous definition of .+ was here\n\z/,
12
- missing_gvar: /: warning: global variable `\$.+' not initialized\n\z/,
13
+ missing_gvar: /: warning: global variable [`']\$.+' not initialized\n\z/,
13
14
  missing_ivar: /: warning: instance variable @.+ not initialized\n\z/,
14
15
  not_reached: /: warning: statement not reached\n\z/,
15
16
  shadow: /: warning: shadowing outer local variable - \w+\n\z/,
16
17
  unused_var: /: warning: assigned but unused variable - \w+\n\z/,
17
18
  useless_operator: /: warning: possibly useless use of [><!=]+ in void context\n\z/,
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
+ 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
20
  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
21
  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
22
  mismatched_indentations: /: warning: mismatched indentations at '.+' with '.+' at \d+\n\z/,
@@ -24,19 +25,52 @@ module Warning
24
25
 
25
26
  # Map of action symbols to procs that return the symbol
26
27
  ACTION_PROC_MAP = {
28
+ raise: proc{|_| :raise},
27
29
  default: proc{|_| :default},
28
30
  backtrace: proc{|_| :backtrace},
29
- raise: proc{|_| :raise},
30
31
  }
31
32
  private_constant :ACTION_PROC_MAP
32
33
 
33
34
  # Clear all current ignored warnings, warning processors, and duplicate check cache.
34
35
  # Also disables deduplicating warnings if that is currently enabled.
36
+ #
37
+ # If a block is passed, the previous values are restored after the block exits.
38
+ #
39
+ # Examples:
40
+ #
41
+ # # Clear warning state
42
+ # Warning.clear
43
+ #
44
+ # Warning.clear do
45
+ # # Clear warning state inside the block
46
+ # ...
47
+ # end
48
+ # # Previous warning state restored when block exists
35
49
  def clear
36
- synchronize do
37
- @ignore.clear
38
- @process.clear
39
- @dedup = false
50
+ if block_given?
51
+ ignore = process = dedup = nil
52
+ synchronize do
53
+ ignore = @ignore.dup
54
+ process = @process.dup
55
+ dedup = @dedup.dup
56
+ end
57
+
58
+ begin
59
+ clear
60
+ yield
61
+ ensure
62
+ synchronize do
63
+ @ignore = ignore
64
+ @process = process
65
+ @dedup = dedup
66
+ end
67
+ end
68
+ else
69
+ synchronize do
70
+ @ignore.clear
71
+ @process.clear
72
+ @dedup = false
73
+ end
40
74
  end
41
75
  end
42
76
 
@@ -133,7 +167,7 @@ module Warning
133
167
  # /instance variable @\w+ not initialized/ => proc do |warning|
134
168
  # LOGGER.warning(warning)
135
169
  # end,
136
- # /global variable `\$\w+' not initialized/ => proc do |warning|
170
+ # /global variable [`']\$\w+' not initialized/ => proc do |warning|
137
171
  # LOGGER.error(warning)
138
172
  # end
139
173
  # )
@@ -144,6 +178,10 @@ module Warning
144
178
  #
145
179
  # Warning.process(__FILE__, :missing_ivar=>:backtrace, :keyword_separation=>:raise)
146
180
  def process(path='', actions=nil, &block)
181
+ unless path.is_a?(String)
182
+ raise ArgumentError, "path must be a String (given an instance of #{path.class})"
183
+ end
184
+
147
185
  if block
148
186
  if actions
149
187
  raise ArgumentError, "cannot pass both actions and block to Warning.process"
@@ -173,14 +211,16 @@ module Warning
173
211
  if RUBY_VERSION >= '3.0'
174
212
  method_args = ', category: nil'
175
213
  super_ = "category ? super : super(str)"
214
+ # :nocov:
176
215
  else
177
216
  super_ = "super"
217
+ # :nocov:
178
218
  end
179
219
 
180
220
  class_eval(<<-END, __FILE__, __LINE__+1)
181
221
  def warn(str#{method_args})
182
222
  synchronize{@ignore.dup}.each do |path, regexp|
183
- if str.start_with?(path) && str =~ regexp
223
+ if str.start_with?(path) && regexp.match?(str)
184
224
  return
185
225
  end
186
226
  end
@@ -198,7 +238,7 @@ module Warning
198
238
  if str.start_with?(path)
199
239
  if block.is_a?(Hash)
200
240
  block.each do |regexp, blk|
201
- if str =~ regexp
241
+ if regexp.match?(str)
202
242
  throw :action, blk.call(str)
203
243
  end
204
244
  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.2.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-04 00:00:00.000000000 Z
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest-global_expectations
@@ -41,11 +41,7 @@ files:
41
41
  - CHANGELOG
42
42
  - MIT-LICENSE
43
43
  - README.rdoc
44
- - Rakefile
45
44
  - lib/warning.rb
46
- - test/fixtures/mismatched_indentations.rb
47
- - test/test_freeze_warning.rb
48
- - test/test_warning.rb
49
45
  homepage: https://github.com/jeremyevans/ruby-warning
50
46
  licenses:
51
47
  - MIT
@@ -54,7 +50,7 @@ metadata:
54
50
  changelog_uri: https://github.com/jeremyevans/ruby-warning/blob/master/CHANGELOG
55
51
  documentation_uri: https://github.com/jeremyevans/ruby-warning/blob/master/README.rdoc
56
52
  source_code_uri: https://github.com/jeremyevans/ruby-warning
57
- post_install_message:
53
+ post_install_message:
58
54
  rdoc_options:
59
55
  - "--quiet"
60
56
  - "--line-numbers"
@@ -76,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
72
  - !ruby/object:Gem::Version
77
73
  version: '0'
78
74
  requirements: []
79
- rubygems_version: 3.2.22
80
- signing_key:
75
+ rubygems_version: 3.5.9
76
+ signing_key:
81
77
  specification_version: 4
82
78
  summary: Add custom processing for warnings
83
79
  test_files: []
data/Rakefile DELETED
@@ -1,47 +0,0 @@
1
- require "rake"
2
- require "rake/clean"
3
- require 'rake/testtask'
4
- require "rdoc/task"
5
-
6
- CLEAN.include ["warning-*.gem", "rdoc"]
7
-
8
- desc "Build warning gem"
9
- task :package=>[:clean] do |p|
10
- sh %{#{FileUtils::RUBY} -S gem build warning.gemspec}
11
- end
12
-
13
- ### Specs
14
-
15
- desc "Run test"
16
- Rake::TestTask.new do |t|
17
- t.libs.push "lib"
18
- t.test_files = FileList['test/test_warning.rb']
19
- t.verbose = true
20
- end
21
-
22
- desc "Run test"
23
- Rake::TestTask.new(:test_freeze) do |t|
24
- t.libs.push "lib"
25
- t.test_files = FileList['test/test_freeze_warning.rb']
26
- t.verbose = true
27
- end
28
-
29
- desc "Run all tests"
30
- task :default=>[:test, :test_freeze]
31
-
32
- ### RDoc
33
-
34
- RDOC_OPTS = ['--main', 'README.rdoc', "--quiet", "--line-numbers", "--inline-source", '--title', 'ruby-warning: Add custom processing for warnings']
35
-
36
- begin
37
- gem 'hanna-nouveau'
38
- RDOC_OPTS.concat(['-f', 'hanna'])
39
- rescue Gem::LoadError
40
- end
41
-
42
-
43
- RDoc::Task.new do |rdoc|
44
- rdoc.rdoc_dir = "rdoc"
45
- rdoc.options += RDOC_OPTS
46
- rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
47
- end
@@ -1,4 +0,0 @@
1
- # Example that will trigger the "mismatched indentations" warning from Ruby
2
- if 2 > 1
3
- true
4
- end
@@ -1,69 +0,0 @@
1
- ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
2
- require 'minitest/global_expectations/autorun'
3
- require 'warning'
4
-
5
- class WarningFreezeTest < Minitest::Test
6
- module EnvUtil
7
- def verbose_warning
8
- class << (stderr = "")
9
- alias write <<
10
- end
11
- stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
12
- yield stderr
13
- return $stderr
14
- ensure
15
- stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
16
- end
17
- module_function :verbose_warning
18
-
19
- def with_default_internal(enc)
20
- verbose, $VERBOSE = $VERBOSE, nil
21
- origenc, Encoding.default_internal = Encoding.default_internal, enc
22
- $VERBOSE = verbose
23
- yield
24
- ensure
25
- verbose, $VERBOSE = $VERBOSE, nil
26
- Encoding.default_internal = origenc
27
- $VERBOSE = verbose
28
- end
29
- module_function :with_default_internal
30
- end
31
-
32
- def assert_warning(pat, msg = nil)
33
- stderr = EnvUtil.verbose_warning {
34
- EnvUtil.with_default_internal(pat.encoding) {
35
- yield
36
- }
37
- }
38
- msg = message(msg) {diff pat, stderr}
39
- assert(pat === stderr, msg)
40
- end
41
-
42
- def test_warning_ignore
43
- w = nil
44
-
45
- Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
46
- Warning.process do |warning|
47
- w = [4, warning]
48
- end
49
- Warning.freeze
50
-
51
- assert_raises RuntimeError do
52
- Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
53
- end
54
- assert_raises RuntimeError do
55
- Warning.process{|warning| w = [4, warning]}
56
- end
57
-
58
- assert_warning '' do
59
- $test_warning_ignore
60
- end
61
- assert_nil w
62
-
63
- assert_warning '' do
64
- $test_warning_ignore2
65
- end
66
- assert_equal(4, w.first)
67
- assert_match(/global variable `\$test_warning_ignore2' not initialized/, w.last)
68
- end
69
- end
data/test/test_warning.rb DELETED
@@ -1,553 +0,0 @@
1
- ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
2
- require 'minitest/global_expectations/autorun'
3
- require 'warning'
4
- require 'pathname'
5
-
6
- class WarningTest < Minitest::Test
7
- module EnvUtil
8
- def verbose_warning
9
- stderr = ""
10
- class << (stderr = "")
11
- alias write <<
12
- def puts(*a)
13
- self << a.join("\n")
14
- end
15
- end
16
- stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
17
- yield stderr
18
- return $stderr
19
- ensure
20
- stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
21
- end
22
- module_function :verbose_warning
23
-
24
- def with_default_internal(enc)
25
- verbose, $VERBOSE = $VERBOSE, nil
26
- origenc, Encoding.default_internal = Encoding.default_internal, enc
27
- $VERBOSE = verbose
28
- yield
29
- ensure
30
- verbose, $VERBOSE = $VERBOSE, nil
31
- Encoding.default_internal = origenc
32
- $VERBOSE = verbose
33
- end
34
- module_function :with_default_internal
35
- end
36
-
37
- def assert_warning(pat, msg = nil)
38
- stderr = EnvUtil.verbose_warning {
39
- EnvUtil.with_default_internal(pat.encoding) {
40
- yield
41
- }
42
- }
43
- msg = message(msg) {diff pat, stderr}
44
- assert(pat === stderr, msg)
45
- end
46
-
47
- def teardown
48
- Warning.clear
49
- end
50
-
51
- def test_warning_dedup
52
- gvar = ->{$test_warning_dedup}
53
-
54
- assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
55
- gvar.call
56
- end
57
- assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
58
- gvar.call
59
- end
60
-
61
- Warning.dedup
62
-
63
- assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
64
- gvar.call
65
- end
66
- assert_warning('') do
67
- gvar.call
68
- end
69
- end
70
-
71
- def test_warning_ignore
72
- assert_warning(/global variable `\$test_warning_ignore' not initialized/) do
73
- assert_nil($test_warning_ignore)
74
- end
75
-
76
- Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
77
-
78
- assert_warning '' do
79
- assert_nil($test_warning_ignore)
80
- end
81
-
82
- assert_warning(/global variable `\$test_warning_ignore2' not initialized/) do
83
- assert_nil($test_warning_ignore2)
84
- end
85
-
86
- Warning.ignore(/global variable `\$test_warning_ignore2' not initialized/, __FILE__)
87
-
88
- assert_warning '' do
89
- assert_nil($test_warning_ignore2)
90
- end
91
-
92
- assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
93
- assert_nil($test_warning_ignore3)
94
- end
95
-
96
- Warning.ignore(/global variable `\$test_warning_ignore3' not initialized/, __FILE__ + 'a')
97
-
98
- assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
99
- assert_nil($test_warning_ignore3)
100
- end
101
- end
102
-
103
- def test_warning_ignore_missing_ivar
104
- Warning.clear
105
-
106
- unless RUBY_VERSION >= '3.0'
107
- assert_warning(/instance variable @ivar not initialized/) do
108
- assert_nil(instance_variable_get(:@ivar))
109
- end
110
- end
111
-
112
- Warning.ignore(:missing_ivar, __FILE__)
113
-
114
- assert_warning '' do
115
- assert_nil(instance_variable_get(:@ivar))
116
- end
117
- end
118
-
119
- def test_warning_ignore_missing_gvar
120
- assert_warning(/global variable `\$gvar' not initialized/) do
121
- $gvar
122
- end
123
-
124
- Warning.ignore(:missing_gvar, __FILE__)
125
-
126
- assert_warning '' do
127
- $gvar
128
- end
129
- end
130
-
131
- def test_warning_ignore_method_redefined
132
- def self.a; end
133
-
134
- assert_warning(/method redefined; discarding old a.+previous definition of a was here/m) do
135
- def self.a; end
136
- end
137
-
138
- Warning.ignore(:method_redefined, __FILE__)
139
-
140
- assert_warning '' do
141
- def self.a; end
142
- end
143
- end
144
-
145
- def test_warning_ignore_not_reached
146
- assert_warning(/: warning: statement not reached/) do
147
- instance_eval('def self.b; return; 1 end', __FILE__)
148
- end
149
-
150
- Warning.ignore(:not_reached, __FILE__)
151
-
152
- assert_warning '' do
153
- instance_eval('def self.c; return; 1 end', __FILE__)
154
- end
155
- end
156
-
157
- def test_warning_ignore_fixnum
158
- assert_warning(/warning: constant ::Fixnum is deprecated/) do
159
- ::Fixnum
160
- end
161
-
162
- Warning.ignore(:fixnum, __FILE__)
163
-
164
- assert_warning '' do
165
- ::Fixnum
166
- end
167
- end
168
-
169
- def test_warning_ignore_bignum
170
- assert_warning(/warning: constant ::Bignum is deprecated/) do
171
- ::Bignum
172
- end
173
-
174
- Warning.ignore(:bignum, __FILE__)
175
-
176
- assert_warning '' do
177
- ::Bignum
178
- end
179
- end
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
-
192
- assert_warning '' do
193
- instance_eval('Object; nil', __FILE__, __LINE__)
194
- end
195
-
196
- assert_warning '' do
197
- instance_eval('v = 0; v; nil', __FILE__, __LINE__)
198
- end
199
-
200
- assert_warning '' do
201
- instance_eval('1 > 1; nil', __FILE__, __LINE__)
202
- end
203
-
204
- assert_warning '' do
205
- instance_eval('defined? C; nil', __FILE__, __LINE__)
206
- end
207
-
208
- if RUBY_VERSION >= '2.6'
209
- assert_warning '' do
210
- instance_eval('1..; nil', __FILE__, __LINE__)
211
- end
212
- end
213
- end
214
-
215
- def test_warning_ignore_ambiguous_slash
216
- def self.d(re); end
217
- assert_warning(/warning: ambi/) do
218
- instance_eval('d /a/', __FILE__)
219
- end
220
-
221
- Warning.ignore(:ambiguous_slash, __FILE__)
222
-
223
- assert_warning '' do
224
- instance_eval('d /a/', __FILE__)
225
- end
226
- end
227
-
228
- def test_warning_ignore_unused_var
229
- assert_warning(/warning: assigned but unused variable - \w+/) do
230
- instance_eval('def self.e; b = 1; 2 end', __FILE__)
231
- end
232
-
233
- Warning.ignore(:unused_var, __FILE__)
234
-
235
- assert_warning '' do
236
- instance_eval('def self.f; b = 1; 2 end', __FILE__)
237
- end
238
- end
239
-
240
- def test_warning_ignore_useless_operator
241
- assert_warning(/warning: possibly useless use of == in void context/) do
242
- instance_eval('1 == 2; true', __FILE__)
243
- end
244
-
245
- Warning.ignore(:useless_operator, __FILE__)
246
-
247
- assert_warning '' do
248
- instance_eval('1 == 2; true', __FILE__)
249
- end
250
- end
251
-
252
- def test_warning_ignore_arg_prefix
253
- assert_warning(/: warning: `\*' interpreted as argument prefix/) do
254
- instance_eval('Array *[nil]', __FILE__)
255
- end
256
-
257
- assert_warning(/: warning: `&' interpreted as argument prefix/) do
258
- instance_eval('tap &proc{}', __FILE__)
259
- end
260
- Warning.ignore(:arg_prefix, __FILE__)
261
-
262
- assert_warning '' do
263
- instance_eval('Array *[nil]', __FILE__)
264
- instance_eval('tap &proc{}', __FILE__)
265
- end
266
- end
267
-
268
- def test_warning_ignore_shadow
269
- assert_warning(/warning: shadowing outer local variable - a/) do
270
- instance_eval('lambda{|a| lambda{|a|}}', __FILE__)
271
- end
272
-
273
- Warning.ignore(:shadow, __FILE__)
274
-
275
- assert_warning '' do
276
- instance_eval('lambda{|a| lambda{|a|}}', __FILE__)
277
- end
278
- end if RUBY_VERSION < '2.6'
279
-
280
- if RUBY_VERSION > '2.7' && RUBY_VERSION < '2.8'
281
- def h2kw(**kw)
282
- end
283
- def kw2h(h, **kw)
284
- end
285
- def skw(h=1, a: 1)
286
- end
287
-
288
- def test_warning_ignore_keyword
289
- 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
290
- h2kw({})
291
- end
292
- assert_warning(/warning: Passing the keyword argument as the last hash parameter is deprecated.*The called method `kw2h' is defined here/m) do
293
- kw2h(a: 1)
294
- end
295
- assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
296
- skw("b" => 1, a: 2)
297
- end
298
- assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
299
- skw({"b" => 1, a: 2})
300
- end
301
-
302
- Warning.ignore(:keyword_separation, __FILE__)
303
-
304
- assert_warning '' do
305
- h2kw({})
306
- kw2h(a: 1)
307
- skw("b" => 1, a: 2)
308
- skw({"b" => 1, a: 2})
309
- end
310
- end
311
-
312
- def test_warning_ignore_safe
313
- assert_warning(/\$SAFE will become a normal global variable in Ruby 3\.0/) do
314
- $SAFE = 0
315
- end
316
-
317
- Warning.ignore(:safe, __FILE__)
318
-
319
- assert_warning("") do
320
- $SAFE = 0
321
- end
322
- end
323
- end
324
-
325
- if RUBY_VERSION > '2.7' && RUBY_VERSION < '3.2'
326
-
327
- def test_warning_ignore_taint
328
- o = Object.new
329
-
330
- assert_warning(/Object#taint is deprecated and will be removed in Ruby 3\.2/) do
331
- o.taint
332
- end
333
- assert_warning(/Object#untaint is deprecated and will be removed in Ruby 3\.2/) do
334
- o.untaint
335
- end
336
- assert_warning(/Object#tainted\? is deprecated and will be removed in Ruby 3\.2/) do
337
- o.tainted?
338
- end
339
- assert_warning(/Object#trust is deprecated and will be removed in Ruby 3\.2/) do
340
- o.trust
341
- end
342
- assert_warning(/Object#untrust is deprecated and will be removed in Ruby 3\.2/) do
343
- o.untrust
344
- end
345
- assert_warning(/Object#untrusted\? is deprecated and will be removed in Ruby 3\.2/) do
346
- o.untrusted?
347
- end
348
-
349
- path = Pathname.new(__FILE__)
350
- assert_warning(/Pathname#taint is deprecated and will be removed in Ruby 3\.2/) do
351
- path.taint
352
- end
353
- assert_warning(/Pathname#untaint is deprecated and will be removed in Ruby 3\.2/) do
354
- path.untaint
355
- end
356
-
357
- Warning.ignore(:taint, __FILE__)
358
-
359
- assert_warning("") do
360
- o.taint
361
- o.untaint
362
- o.tainted?
363
- o.trust
364
- o.untrust
365
- o.untrusted?
366
- p.taint
367
- p.untaint
368
- end
369
- end
370
- end
371
-
372
- def test_warning_ignore_symbol_array
373
- def self.c; end
374
-
375
- assert_warning(/statement not reached.+method redefined; discarding old c.+previous definition of c was here/m) do
376
- instance_eval('def self.c; return; 1 end', __FILE__)
377
- end
378
-
379
- Warning.ignore([:method_redefined, :not_reached], __FILE__)
380
-
381
- assert_warning '' do
382
- instance_eval('def self.c; return; 1 end', __FILE__)
383
- end
384
- end
385
-
386
- def test_warning_ignore_mismatched_indentation
387
- assert_warning(/warning: mismatched indentations/) do
388
- load 'test/fixtures/mismatched_indentations.rb'
389
- end
390
-
391
- Warning.ignore(:mismatched_indentations, 'test/fixtures/mismatched_indentations.rb')
392
-
393
- assert_warning '' do
394
- load 'test/fixtures/mismatched_indentations.rb'
395
- end
396
- end
397
-
398
- def test_warning_process
399
- warn = nil
400
-
401
- Warning.process(__FILE__+'a') do |warning|
402
- warn = [0, warning]
403
- end
404
-
405
- assert_warning(/global variable `\$test_warning_process' not initialized/) do
406
- $test_warning_process
407
- end
408
- assert_nil(warn)
409
-
410
- Warning.process(__FILE__) do |warning|
411
- warn = [1, warning]
412
- end
413
-
414
- assert_warning '' do
415
- $test_warning_process2
416
- end
417
- assert_equal(1, warn.first)
418
- assert_match(/global variable `\$test_warning_process2' not initialized/, warn.last)
419
- warn = nil
420
-
421
- Warning.process(File.dirname(__FILE__)) do |warning|
422
- warn = [2, warning]
423
- end
424
-
425
- assert_warning '' do
426
- $test_warning_process3
427
- end
428
- assert_equal(1, warn.first)
429
- assert_match(/global variable `\$test_warning_process3' not initialized/, warn.last)
430
- warn = nil
431
-
432
- Warning.process(__FILE__+':') do |warning|
433
- warn = [3, warning]
434
- end
435
-
436
- assert_warning '' do
437
- $test_warning_process4
438
- end
439
- assert_equal(3, warn.first)
440
- assert_match(/global variable `\$test_warning_process4' not initialized/, warn.last)
441
- warn = nil
442
-
443
- Warning.clear
444
-
445
- assert_warning(/global variable `\$test_warning_process5' not initialized/) do
446
- $test_warning_process5
447
- end
448
- assert_nil(warn)
449
-
450
- Warning.process do |warning|
451
- warn = [4, warning]
452
- end
453
-
454
- assert_warning '' do
455
- $test_warning_process6
456
- end
457
- assert_equal(4, warn.first)
458
- assert_match(/global variable `\$test_warning_process6' not initialized/, warn.last)
459
- end
460
-
461
- def test_warning_process_block_return_default
462
- w = nil
463
- Warning.process(__FILE__) do |warning|
464
- w = warning
465
- :default
466
- end
467
-
468
- assert_warning(/global variable `\$test_warning_process_block_return_default' not initialized/) do
469
- $test_warning_process_block_return_default
470
- end
471
- assert_match(/global variable `\$test_warning_process_block_return_default' not initialized/, w)
472
- end
473
-
474
- def test_warning_process_block_return_backtrace
475
- w = nil
476
- Warning.process(__FILE__) do |warning|
477
- w = warning
478
- :backtrace
479
- end
480
-
481
- assert_warning(/global variable `\$test_warning_process_block_return_backtrace' not initialized.*#{__FILE__}/m) do
482
- $test_warning_process_block_return_backtrace
483
- end
484
- assert_match(/global variable `\$test_warning_process_block_return_backtrace' not initialized/, w)
485
- end
486
-
487
- def test_warning_process_block_return_raise
488
- w = nil
489
- Warning.process(__FILE__) do |warning|
490
- w = warning
491
- :raise
492
- end
493
-
494
- assert_raises(RuntimeError) do
495
- $test_warning_process_block_return_raise
496
- end
497
- assert_match(/global variable `\$test_warning_process_block_return_raise' not initialized/, w)
498
- end
499
-
500
- def test_warning_process_action
501
- w = nil
502
- Warning.process(__FILE__, :method_redefined=>:default, :missing_gvar=>:backtrace, :ambiguous_slash=>:raise)
503
- Warning.process(__FILE__, :not_reached=>proc do |warning|
504
- w = warning
505
- :raise
506
- end)
507
-
508
- assert_warning(/warning: method redefined/) do
509
- Class.new do
510
- def a; end
511
- def a; end
512
- end
513
- end
514
-
515
- assert_warning(/global variable `\$test_warning_process_action' not initialized.*#{__FILE__}/m) do
516
- $test_warning_process_action
517
- end
518
-
519
- Warning.process(__FILE__) do |warning|
520
- w = warning
521
- :raise
522
- end
523
-
524
- assert_raises(RuntimeError, /warning: ambi/) do
525
- EnvUtil.verbose_warning{instance_eval('d /a/', __FILE__)}
526
- end
527
-
528
- assert_raises(RuntimeError, /warning: statement not reached/) do
529
- EnvUtil.verbose_warning{instance_eval('def self.b; return; 1 end', __FILE__)}
530
- end
531
- assert_match(/warning: statement not reached/, w)
532
- end
533
-
534
- def test_warning_process_action_and_block
535
- assert_raises(ArgumentError) do
536
- Warning.process(__FILE__)
537
- end
538
- end
539
-
540
- def test_warning_process_no_action_and_no_block
541
- assert_raises(ArgumentError) do
542
- Warning.process(__FILE__, :missing_ivar=>:default){}
543
- end
544
- end
545
-
546
- if RUBY_VERSION >= '3.0'
547
- def test_warning_warn_category_keyword
548
- assert_warning('foo') do
549
- Warning.warn("foo", category: :deprecated)
550
- end
551
- end
552
- end
553
- end