warning 0.10.0 → 1.0.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
- SHA1:
3
- metadata.gz: c0a00f58401bfb0c3b182b5e472de7d417e5e9c7
4
- data.tar.gz: 6e4a3377be9ee85d5de9848e4eaaa20b367035a4
2
+ SHA256:
3
+ metadata.gz: 0fd71447749bdaad9176ecb3eb6302104bcaf5401516719beed80594d2fa234a
4
+ data.tar.gz: 18eee8db50ef702428bcfd5690c3f92d013b4b8013dd6cd28e554ff808cccbd6
5
5
  SHA512:
6
- metadata.gz: 1e3c7971f5ec2a5b28ec263aa6847778f45790b3554cdfee4fc8b4c08af21e3459dcf148fd8618b4d632cbd285e6afd6518ededc69a01469ea676666df564a5c
7
- data.tar.gz: 674820530875fa42a1920f06673add77b66210fee9b50bd150cbafdce603e275c30c422cf985838e6f5500096aeb815c40d2a5d7cb5766b430a7912bf7f2238b
6
+ metadata.gz: 8038062c8d96ec8eb800be734041654d3dd24a1a5ddda523c5c44f465f7cbdf3fdbaa3537e4d360ad9673effb4a54e27de544325235b0fe343ec13f6ee3ec948
7
+ data.tar.gz: 26324dfc9591fda0a4627d515c3b18738e55f3213c0ff76b15f7493616980c54cf3cf3b945985d460d230db69b57ccd687f2b4ac5f367feec1b1efeae72319f0
data/CHANGELOG CHANGED
@@ -1,3 +1,17 @@
1
+ === 1.0.0 (2020-01-21)
2
+
3
+ * Add support for :taint as regexp argument to Warning.ignore (jeremyevans)
4
+
5
+ * Add support for :safe as regexp argument to Warning.ignore (jeremyevans)
6
+
7
+ * Add Warning.dedup for deduplicating warnings (jeremyevans)
8
+
9
+ * Add support for :keyword_separation as regexp argument to Warning.ignore (jeremyevans)
10
+
11
+ === 0.10.1 (2017-11-16)
12
+
13
+ * Correctly handle Warning.freeze (jeremyevans)
14
+
1
15
  === 0.10.0 (2016-11-16)
2
16
 
3
17
  * Add support for :arg_prefix and :shadow as regexp argument to Warning.ignore (jeremyevans)
data/README.rdoc CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  ruby-warning adds custom processing for warnings, including the
4
4
  ability to ignore specific warning messages, ignore warnings
5
- in specific files/directories, and add custom handling for all
6
- warnings in specific files/directories.
5
+ in specific files/directories, deduplicate warnings, and add
6
+ custom handling for all warnings in specific files/directories.
7
7
 
8
8
  ruby-warning requires ruby 2.4+, as previous versions of ruby do
9
9
  not support custom processing of warnings.
@@ -30,11 +30,14 @@ appropriate regexp. The supported symbols are:
30
30
  * :ambiguous_slash
31
31
  * :bignum
32
32
  * :fixnum
33
+ * :keyword_separation
33
34
  * :method_redefined
34
35
  * :missing_gvar
35
36
  * :missing_ivar
36
37
  * :not_reached
38
+ * :safe
37
39
  * :shadow
40
+ * :taint
38
41
  * :unused_var
39
42
  * :useless_operator
40
43
 
@@ -44,15 +47,27 @@ string instead of performing the default behavior. You can call
44
47
  <tt>Warning.process</tt> multiple times and it will operate intelligently,
45
48
  choosing the longest path prefix that the string starts with.
46
49
 
47
- <tt>Warning.clear</tt> just clears the current ignored warnings and warning
48
- processors.
50
+ <tt>Warning.dedup</tt> deduplicates warnings, so that if a warning is received
51
+ that is the same as a warning that has already been processed, the warning is
52
+ ignored. Note that this should be used with care, since if the application
53
+ generates an arbitrary number of unique warnings, that can lead to unbounded
54
+ memory growth.
55
+
56
+ <tt>Warning.clear</tt> resets the library to its initial state, clearing the
57
+ current ignored warnings and warning processors, and turning off deduplication.
49
58
 
50
59
  By using path prefixes, it's fairly easy for a gem to set that specific warnings
51
60
  should be ignored inside the gem's directory.
52
61
 
53
62
  Note that path prefixes will not correctly handle warnings raised by
54
63
  <tt>Kernel#warn</tt>, unless the warning message given to <tt>Kernel#warn</tt>
55
- starts with the filename where the warning is used.
64
+ starts with the filename where the warning is used. The <tt>Kernel#warn</tt>
65
+ +:uplevel+ option will make sure the warning starts with the filename.
66
+
67
+ Note that many of the warnings this library can ignore are warnings caused
68
+ during compilation (i.e. when files are loaded via require). You should
69
+ require this library and setup the appropriate warning handling before
70
+ loading any code that could cause warnings.
56
71
 
57
72
  = Examples
58
73
 
@@ -78,6 +93,14 @@ starts with the filename where the warning is used.
78
93
  LOGGER.error(warning)
79
94
  end
80
95
 
96
+ # Deduplicate warnings
97
+ Warning.dedup
98
+
99
+ # Ignore all warnings in Gem dependencies
100
+ Gem.path.each do |path|
101
+ Warning.ignore(//, path)
102
+ end
103
+
81
104
  = License
82
105
 
83
106
  MIT
data/Rakefile CHANGED
@@ -15,11 +15,19 @@ end
15
15
  desc "Run test"
16
16
  Rake::TestTask.new do |t|
17
17
  t.libs.push "lib"
18
- t.test_files = FileList['test/test_*.rb']
18
+ t.test_files = FileList['test/test_warning.rb']
19
19
  t.verbose = true
20
20
  end
21
21
 
22
- task :default=>:test
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]
23
31
 
24
32
  ### RDoc
25
33
 
data/lib/warning.rb CHANGED
@@ -15,15 +15,33 @@ module Warning
15
15
  shadow: /: warning: shadowing outer local variable - \w+\n\z/,
16
16
  unused_var: /: warning: assigned but unused variable - \w+\n\z/,
17
17
  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
+ 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/,
18
21
  }
19
22
 
20
- # Clear all current ignored warnings and warning processors.
23
+ # Clear all current ignored warnings, warning processors, and duplicate check cache.
24
+ # Also disables deduplicating warnings if that is currently enabled.
21
25
  def clear
22
26
  synchronize do
23
27
  @ignore.clear
24
28
  @process.clear
29
+ @dedup = false
25
30
  end
26
31
  end
32
+
33
+ # Deduplicate warnings, supress warning messages if the same warning message
34
+ # has already occurred. Note that this can lead to unbounded memory use
35
+ # if unique warnings are generated.
36
+ def dedup
37
+ @dedup = {}
38
+ end
39
+
40
+ def freeze
41
+ @ignore.freeze
42
+ @process.freeze
43
+ super
44
+ end
27
45
 
28
46
  # Ignore any warning messages matching the given regexp, if they
29
47
  # start with the given path.
@@ -34,6 +52,7 @@ module Warning
34
52
  # :ambiguous_slash :: Ignore warnings for things like <tt>method /regexp/</tt>
35
53
  # :bignum :: Ignore warnings when referencing the ::Bignum constant.
36
54
  # :fixnum :: Ignore warnings when referencing the ::Fixnum constant.
55
+ # :keyword_separation :: Ignore warnings related to keyword argument separation.
37
56
  # :method_redefined :: Ignore warnings when defining a method in a class/module where a
38
57
  # method of the same name was already defined in that class/module.
39
58
  # :missing_gvar :: Ignore warnings for accesses to global variables
@@ -41,7 +60,9 @@ module Warning
41
60
  # :missing_ivar :: Ignore warnings for accesses to instance variables
42
61
  # that have not yet been initialized
43
62
  # :not_reached :: Ignore statement not reached warnings.
63
+ # :safe :: Ignore warnings related to $SAFE and related C-API functions.
44
64
  # :shadow :: Ignore warnings related to shadowing outer local variables.
65
+ # :taint :: Ignore warnings related to taint and related methods and C-API functions.
45
66
  # :unused_var :: Ignore warnings for unused variables.
46
67
  # :useless_operator :: Ignore warnings when using operators such as == and > when the
47
68
  # result is not used.
@@ -99,7 +120,8 @@ module Warning
99
120
  end
100
121
 
101
122
  # Handle ignored warnings and warning processors. If the warning is
102
- # not ignored and there is no warning processor setup for the warning
123
+ # not ignored, is not a duplicate warning (if checking for duplicates)
124
+ # and there is no warning processor setup for the warning
103
125
  # string, then use the default behavior of writing to $stderr.
104
126
  def warn(str)
105
127
  synchronize{@ignore.dup}.each do |path, regexp|
@@ -108,6 +130,14 @@ module Warning
108
130
  end
109
131
  end
110
132
 
133
+ if @dedup
134
+ if synchronize{@dedup[str]}
135
+ return
136
+ end
137
+
138
+ synchronize{@dedup[str] = true}
139
+ end
140
+
111
141
  synchronize{@process.dup}.each do |path, block|
112
142
  if str.start_with?(path)
113
143
  block.call(str)
@@ -117,11 +147,18 @@ module Warning
117
147
 
118
148
  super
119
149
  end
150
+
151
+ private
152
+
153
+ def synchronize(&block)
154
+ @monitor.synchronize(&block)
155
+ end
120
156
  end
121
157
 
122
158
  @ignore = []
123
159
  @process = []
160
+ @dedup = false
161
+ @monitor = Monitor.new
124
162
 
125
- extend MonitorMixin
126
163
  extend Processor
127
164
  end
@@ -0,0 +1,70 @@
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
+ obj = Object.new
44
+ w = nil
45
+
46
+ Warning.ignore(/instance variable @ivar not initialized/)
47
+ Warning.process do |warning|
48
+ w = [4, warning]
49
+ end
50
+ Warning.freeze
51
+
52
+ assert_raises RuntimeError do
53
+ Warning.ignore(/instance variable @ivar not initialized/)
54
+ end
55
+ assert_raises RuntimeError do
56
+ Warning.process{|warning| w = [4, warning]}
57
+ end
58
+
59
+ assert_warning '' do
60
+ assert_nil(obj.instance_variable_get(:@ivar))
61
+ end
62
+ assert_nil w
63
+
64
+ assert_warning '' do
65
+ assert_nil(obj.instance_variable_get(:@ivar6))
66
+ end
67
+ assert_equal(4, w.first)
68
+ assert_match(/instance variable @ivar6 not initialized/, w.last)
69
+ end
70
+ end
data/test/test_warning.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'minitest/autorun'
1
+ ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
2
+ require 'minitest/global_expectations/autorun'
2
3
  require 'warning'
4
+ require 'pathname'
3
5
 
4
6
  class WarningTest < Minitest::Test
5
7
  module EnvUtil
@@ -42,6 +44,28 @@ class WarningTest < Minitest::Test
42
44
  Warning.clear
43
45
  end
44
46
 
47
+ def ivar
48
+ Object.new.instance_variable_get(:@ivar)
49
+ end
50
+
51
+ def test_warning_dedup
52
+ assert_warning(/instance variable @ivar not initialized/) do
53
+ ivar
54
+ end
55
+ assert_warning(/instance variable @ivar not initialized/) do
56
+ ivar
57
+ end
58
+
59
+ Warning.dedup
60
+
61
+ assert_warning(/instance variable @ivar not initialized/) do
62
+ ivar
63
+ end
64
+ assert_warning('') do
65
+ ivar
66
+ end
67
+ end
68
+
45
69
  def test_warning_ignore
46
70
  obj = Object.new
47
71
 
@@ -219,6 +243,98 @@ class WarningTest < Minitest::Test
219
243
  assert_warning '' do
220
244
  instance_eval('lambda{|a| lambda{|a|}}', __FILE__)
221
245
  end
246
+ end if RUBY_VERSION < '2.6'
247
+
248
+ if RUBY_VERSION > '2.7' && RUBY_VERSION < '2.8'
249
+ def h2kw(**kw)
250
+ end
251
+ def kw2h(h, **kw)
252
+ end
253
+ def skw(h=1, a: 1)
254
+ end
255
+
256
+ def test_warning_ignore_keyword
257
+ 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
258
+ h2kw({})
259
+ end
260
+ assert_warning(/warning: Passing the keyword argument as the last hash parameter is deprecated.*The called method `kw2h' is defined here/m) do
261
+ kw2h(a: 1)
262
+ end
263
+ assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
264
+ skw("b" => 1, a: 2)
265
+ end
266
+ assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
267
+ skw({"b" => 1, a: 2})
268
+ end
269
+
270
+ Warning.ignore(:keyword_separation, __FILE__)
271
+
272
+ assert_warning '' do
273
+ h2kw({})
274
+ kw2h(a: 1)
275
+ skw("b" => 1, a: 2)
276
+ skw({"b" => 1, a: 2})
277
+ end
278
+ end
279
+
280
+ def test_warning_ignore_safe
281
+ assert_warning(/\$SAFE will become a normal global variable in Ruby 3\.0/) do
282
+ $SAFE = 0
283
+ end
284
+
285
+ Warning.ignore(:safe, __FILE__)
286
+
287
+ assert_warning("") do
288
+ $SAFE = 0
289
+ end
290
+ end
291
+ end
292
+
293
+ if RUBY_VERSION > '2.7' && RUBY_VERSION < '3.2'
294
+
295
+ def test_warning_ignore_taint
296
+ o = Object.new
297
+
298
+ assert_warning(/Object#taint is deprecated and will be removed in Ruby 3\.2/) do
299
+ o.taint
300
+ end
301
+ assert_warning(/Object#untaint is deprecated and will be removed in Ruby 3\.2/) do
302
+ o.untaint
303
+ end
304
+ assert_warning(/Object#tainted\? is deprecated and will be removed in Ruby 3\.2/) do
305
+ o.tainted?
306
+ end
307
+ assert_warning(/Object#trust is deprecated and will be removed in Ruby 3\.2/) do
308
+ o.trust
309
+ end
310
+ assert_warning(/Object#untrust is deprecated and will be removed in Ruby 3\.2/) do
311
+ o.untrust
312
+ end
313
+ assert_warning(/Object#untrusted\? is deprecated and will be removed in Ruby 3\.2/) do
314
+ o.untrusted?
315
+ end
316
+
317
+ path = Pathname.new(__FILE__)
318
+ assert_warning(/Pathname#taint is deprecated and will be removed in Ruby 3\.2/) do
319
+ path.taint
320
+ end
321
+ assert_warning(/Pathname#untaint is deprecated and will be removed in Ruby 3\.2/) do
322
+ path.untaint
323
+ end
324
+
325
+ Warning.ignore(:taint, __FILE__)
326
+
327
+ assert_warning("") do
328
+ o.taint
329
+ o.untaint
330
+ o.tainted?
331
+ o.trust
332
+ o.untrust
333
+ o.untrusted?
334
+ p.taint
335
+ p.untaint
336
+ end
337
+ end
222
338
  end
223
339
 
224
340
  def test_warning_ignore_symbol_array
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warning
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 1.0.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: 2016-11-16 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest-global_expectations
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: |
14
28
  ruby-warning adds custom processing for warnings, including the
15
29
  ability to ignore specific warning messages, ignore warnings
@@ -28,11 +42,16 @@ files:
28
42
  - README.rdoc
29
43
  - Rakefile
30
44
  - lib/warning.rb
45
+ - test/test_freeze_warning.rb
31
46
  - test/test_warning.rb
32
47
  homepage: https://github.com/jeremyevans/ruby-warning
33
48
  licenses:
34
49
  - MIT
35
- metadata: {}
50
+ metadata:
51
+ bug_tracker_uri: https://github.com/jeremyevans/ruby-warning/issues
52
+ changelog_uri: https://github.com/jeremyevans/ruby-warning/blob/master/CHANGELOG
53
+ documentation_uri: https://github.com/jeremyevans/ruby-warning/blob/master/README.rdoc
54
+ source_code_uri: https://github.com/jeremyevans/ruby-warning
36
55
  post_install_message:
37
56
  rdoc_options:
38
57
  - "--quiet"
@@ -46,17 +65,16 @@ require_paths:
46
65
  - lib
47
66
  required_ruby_version: !ruby/object:Gem::Requirement
48
67
  requirements:
49
- - - ">"
68
+ - - ">="
50
69
  - !ruby/object:Gem::Version
51
- version: 2.3.99
70
+ version: 2.4.0
52
71
  required_rubygems_version: !ruby/object:Gem::Requirement
53
72
  requirements:
54
73
  - - ">="
55
74
  - !ruby/object:Gem::Version
56
75
  version: '0'
57
76
  requirements: []
58
- rubyforge_project:
59
- rubygems_version: 2.6.8
77
+ rubygems_version: 3.1.2
60
78
  signing_key:
61
79
  specification_version: 4
62
80
  summary: Add custom processing for warnings