warning 0.9.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 +7 -0
- data/CHANGELOG +3 -0
- data/MIT-LICENSE +18 -0
- data/README.rdoc +68 -0
- data/Rakefile +39 -0
- data/lib/warning.rb +75 -0
- data/test/test_warning.rb +156 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 70b1655bff238b444c9373002b6d1010812a9a71
|
4
|
+
data.tar.gz: 2276292182d974e7fdb71802295ed1dc45771f82
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a512057f73e7f275e806a9e9442919e128c0edcf312e730b4787f9b9630c8f088bdfce0af7a7b7f9d6e3811aa94ae95211a4b13d205353e1853c20f2f5a0a4d
|
7
|
+
data.tar.gz: 16e8d3da29e75c5c6f1dd70fede11564006ad30201923d8750aa0d383e77820fa4355c6fd6f6b44600bd276d9afd38706581c123c34898bbbf2534bb64cab5b3
|
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2016 Jeremy Evans
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
= ruby-warning
|
2
|
+
|
3
|
+
ruby-warning adds custom processing for warnings, including the
|
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.
|
7
|
+
|
8
|
+
ruby-warning requires ruby 2.4+, as previous versions of ruby do
|
9
|
+
not support custom processing of warnings.
|
10
|
+
|
11
|
+
= Installation
|
12
|
+
|
13
|
+
gem install warning
|
14
|
+
|
15
|
+
= Source Code
|
16
|
+
|
17
|
+
Source code is available on GitHub at https://github.com/jeremyevans/ruby-warning
|
18
|
+
|
19
|
+
= Usage
|
20
|
+
|
21
|
+
By default, requiring the library does not make changes to how ruby processes
|
22
|
+
warnings, it just adds methods that allow you to customize the processing.
|
23
|
+
|
24
|
+
<tt>Warning.ignore</tt> takes a regexp and optionally a path prefix, and ignores
|
25
|
+
any warning that matches the regular expression if it starts with the path
|
26
|
+
prefix.
|
27
|
+
|
28
|
+
<tt>Warning.process</tt> takes an optional path prefix and a block, and if the
|
29
|
+
warning string starts with the path prefix, it calls the block with the warning
|
30
|
+
string instead of performing the default behavior. You can call
|
31
|
+
<tt>Warning.process</tt> multiple times and it will operate intelligently,
|
32
|
+
choosing the longest path prefix that the string starts with.
|
33
|
+
|
34
|
+
<tt>Warning.clear</tt> just clears the current ignored warnings and warning
|
35
|
+
processors.
|
36
|
+
|
37
|
+
By using path prefixes, it's fairly easy for a gem to set that specific warnings
|
38
|
+
should be ignored inside the gem's directory.
|
39
|
+
|
40
|
+
Note that path prefixes will not correctly handle warnings raised by
|
41
|
+
<tt>Kernel#warn</tt>, unless the warning message given to <tt>Kernel#warn</tt>
|
42
|
+
starts with the filename where the warning is used.
|
43
|
+
|
44
|
+
= Examples
|
45
|
+
|
46
|
+
# Ignore all uninitialized instance variable warnings
|
47
|
+
Warning.ignore(/instance variable @\w+ not initialized/)
|
48
|
+
|
49
|
+
# Ignore all uninitialized instance variable warnings in current file
|
50
|
+
Warning.ignore(/instance variable @\w+ not initialized/, __FILE__)
|
51
|
+
|
52
|
+
# Write warning to LOGGER at level warning
|
53
|
+
Warning.process do |warning|
|
54
|
+
LOGGER.warning(warning)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Write warnings in the current file to LOGGER at level error
|
58
|
+
Warning.process(__FILE__) do |warning|
|
59
|
+
LOGGER.error(warning)
|
60
|
+
end
|
61
|
+
|
62
|
+
= License
|
63
|
+
|
64
|
+
MIT
|
65
|
+
|
66
|
+
= Author
|
67
|
+
|
68
|
+
Jeremy Evans <code@jeremyevans.net>
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
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_*.rb']
|
19
|
+
t.verbose = true
|
20
|
+
end
|
21
|
+
|
22
|
+
task :default=>:test
|
23
|
+
|
24
|
+
### RDoc
|
25
|
+
|
26
|
+
RDOC_OPTS = ['--main', 'README.rdoc', "--quiet", "--line-numbers", "--inline-source", '--title', 'ruby-warning: Add custom processing for warnings']
|
27
|
+
|
28
|
+
begin
|
29
|
+
gem 'hanna-nouveau'
|
30
|
+
RDOC_OPTS.concat(['-f', 'hanna'])
|
31
|
+
rescue Gem::LoadError
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
RDoc::Task.new do |rdoc|
|
36
|
+
rdoc.rdoc_dir = "rdoc"
|
37
|
+
rdoc.options += RDOC_OPTS
|
38
|
+
rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
|
39
|
+
end
|
data/lib/warning.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
|
3
|
+
module Warning
|
4
|
+
module Processor
|
5
|
+
# Clear all current ignored warnings and warning processors.
|
6
|
+
def clear
|
7
|
+
synchronize do
|
8
|
+
@ignore.clear
|
9
|
+
@process.clear
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Ignore any warning messages matching the given regexp, if they
|
14
|
+
# start with the given path. Examples:
|
15
|
+
#
|
16
|
+
# # Ignore all uninitialized instance variable warnings
|
17
|
+
# Warning.ignore(/instance variable @\w+ not initialized/)
|
18
|
+
#
|
19
|
+
# # Ignore all uninitialized instance variable warnings in current file
|
20
|
+
# Warning.ignore(/instance variable @\w+ not initialized/, __FILE__)
|
21
|
+
def ignore(regexp, path='')
|
22
|
+
synchronize do
|
23
|
+
@ignore << [path, regexp]
|
24
|
+
end
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
# Handle all warnings starting with the given path, instead of
|
29
|
+
# the default behavior of printing them to $stderr. Examples:
|
30
|
+
#
|
31
|
+
# # Write warning to LOGGER at level warning
|
32
|
+
# Warning.process do |warning|
|
33
|
+
# LOGGER.warning(warning)
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# # Write warnings in the current file to LOGGER at level error level
|
37
|
+
# Warning.process(__FILE__) do |warning|
|
38
|
+
# LOGGER.error(warning)
|
39
|
+
# end
|
40
|
+
def process(path='', &block)
|
41
|
+
synchronize do
|
42
|
+
@process << [path, block]
|
43
|
+
@process.sort_by!(&:first)
|
44
|
+
@process.reverse!
|
45
|
+
end
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
|
49
|
+
# Handle ignored warnings and warning processors. If the warning is
|
50
|
+
# not ignored and there is no warning processor setup for the warning
|
51
|
+
# string, then use the default behavior of writing to $stderr.
|
52
|
+
def warn(str)
|
53
|
+
synchronize{@ignore.dup}.each do |path, regexp|
|
54
|
+
if str.start_with?(path) && str =~ regexp
|
55
|
+
return
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
synchronize{@process.dup}.each do |path, block|
|
60
|
+
if str.start_with?(path)
|
61
|
+
block.call(str)
|
62
|
+
return
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
super
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
@ignore = []
|
71
|
+
@process = []
|
72
|
+
|
73
|
+
extend MonitorMixin
|
74
|
+
extend Processor
|
75
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
require 'minitest/unit'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class WarningTest < Minitest::Test
|
5
|
+
module EnvUtil
|
6
|
+
def verbose_warning
|
7
|
+
class << (stderr = "")
|
8
|
+
alias write <<
|
9
|
+
end
|
10
|
+
stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
|
11
|
+
yield stderr
|
12
|
+
return $stderr
|
13
|
+
ensure
|
14
|
+
stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
|
15
|
+
end
|
16
|
+
module_function :verbose_warning
|
17
|
+
|
18
|
+
def with_default_internal(enc)
|
19
|
+
verbose, $VERBOSE = $VERBOSE, nil
|
20
|
+
origenc, Encoding.default_internal = Encoding.default_internal, enc
|
21
|
+
$VERBOSE = verbose
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
verbose, $VERBOSE = $VERBOSE, nil
|
25
|
+
Encoding.default_internal = origenc
|
26
|
+
$VERBOSE = verbose
|
27
|
+
end
|
28
|
+
module_function :with_default_internal
|
29
|
+
end
|
30
|
+
|
31
|
+
def assert_warning(pat, msg = nil)
|
32
|
+
stderr = EnvUtil.verbose_warning {
|
33
|
+
EnvUtil.with_default_internal(pat.encoding) {
|
34
|
+
yield
|
35
|
+
}
|
36
|
+
}
|
37
|
+
msg = message(msg) {diff pat, stderr}
|
38
|
+
assert(pat === stderr, msg)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_warning_ignore
|
42
|
+
obj = Object.new
|
43
|
+
|
44
|
+
assert_warning /instance variable @ivar not initialized/ do
|
45
|
+
assert_nil(obj.instance_variable_get(:@ivar))
|
46
|
+
end
|
47
|
+
|
48
|
+
require 'warning'
|
49
|
+
|
50
|
+
assert_warning /instance variable @ivar not initialized/ do
|
51
|
+
assert_nil(obj.instance_variable_get(:@ivar))
|
52
|
+
end
|
53
|
+
|
54
|
+
Warning.ignore(/instance variable @ivar not initialized/)
|
55
|
+
|
56
|
+
assert_warning '' do
|
57
|
+
assert_nil(obj.instance_variable_get(:@ivar))
|
58
|
+
end
|
59
|
+
|
60
|
+
assert_warning /instance variable @ivar2 not initialized/ do
|
61
|
+
assert_nil(obj.instance_variable_get(:@ivar2))
|
62
|
+
end
|
63
|
+
|
64
|
+
Warning.ignore(/instance variable @ivar2 not initialized/, __FILE__)
|
65
|
+
|
66
|
+
assert_warning '' do
|
67
|
+
assert_nil(obj.instance_variable_get(:@ivar2))
|
68
|
+
end
|
69
|
+
|
70
|
+
assert_warning /instance variable @ivar3 not initialized/ do
|
71
|
+
assert_nil(obj.instance_variable_get(:@ivar3))
|
72
|
+
end
|
73
|
+
|
74
|
+
Warning.ignore(/instance variable @ivar3 not initialized/, __FILE__+'a')
|
75
|
+
|
76
|
+
assert_warning /instance variable @ivar3 not initialized/ do
|
77
|
+
assert_nil(obj.instance_variable_get(:@ivar3))
|
78
|
+
end
|
79
|
+
|
80
|
+
Warning.clear
|
81
|
+
|
82
|
+
assert_warning /instance variable @ivar not initialized/ do
|
83
|
+
assert_nil(obj.instance_variable_get(:@ivar))
|
84
|
+
end
|
85
|
+
ensure
|
86
|
+
Warning.clear
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_warning_process
|
90
|
+
obj = Object.new
|
91
|
+
warn = nil
|
92
|
+
|
93
|
+
require 'warning'
|
94
|
+
|
95
|
+
Warning.process(__FILE__+'a') do |warning|
|
96
|
+
warn = [0, warning]
|
97
|
+
end
|
98
|
+
|
99
|
+
assert_warning /instance variable @ivar not initialized/ do
|
100
|
+
assert_nil(obj.instance_variable_get(:@ivar))
|
101
|
+
end
|
102
|
+
assert_nil(warn)
|
103
|
+
|
104
|
+
Warning.process(__FILE__) do |warning|
|
105
|
+
warn = [1, warning]
|
106
|
+
end
|
107
|
+
|
108
|
+
assert_warning '' do
|
109
|
+
assert_nil(obj.instance_variable_get(:@ivar2))
|
110
|
+
end
|
111
|
+
assert_equal(1, warn.first)
|
112
|
+
assert_match(/instance variable @ivar2 not initialized/, warn.last)
|
113
|
+
warn = nil
|
114
|
+
|
115
|
+
Warning.process(File.dirname(__FILE__)) do |warning|
|
116
|
+
warn = [2, warning]
|
117
|
+
end
|
118
|
+
|
119
|
+
assert_warning '' do
|
120
|
+
assert_nil(obj.instance_variable_get(:@ivar3))
|
121
|
+
end
|
122
|
+
assert_equal(1, warn.first)
|
123
|
+
assert_match(/instance variable @ivar3 not initialized/, warn.last)
|
124
|
+
warn = nil
|
125
|
+
|
126
|
+
Warning.process(__FILE__+':') do |warning|
|
127
|
+
warn = [3, warning]
|
128
|
+
end
|
129
|
+
|
130
|
+
assert_warning '' do
|
131
|
+
assert_nil(obj.instance_variable_get(:@ivar4))
|
132
|
+
end
|
133
|
+
assert_equal(3, warn.first)
|
134
|
+
assert_match(/instance variable @ivar4 not initialized/, warn.last)
|
135
|
+
warn = nil
|
136
|
+
|
137
|
+
Warning.clear
|
138
|
+
|
139
|
+
assert_warning /instance variable @ivar5 not initialized/ do
|
140
|
+
assert_nil(obj.instance_variable_get(:@ivar5))
|
141
|
+
end
|
142
|
+
assert_nil(warn)
|
143
|
+
|
144
|
+
Warning.process do |warning|
|
145
|
+
warn = [4, warning]
|
146
|
+
end
|
147
|
+
|
148
|
+
assert_warning '' do
|
149
|
+
assert_nil(obj.instance_variable_get(:@ivar6))
|
150
|
+
end
|
151
|
+
assert_equal(4, warn.first)
|
152
|
+
assert_match(/instance variable @ivar6 not initialized/, warn.last)
|
153
|
+
ensure
|
154
|
+
Warning.clear
|
155
|
+
end
|
156
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: warning
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Evans
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
ruby-warning adds custom processing for warnings, including the
|
15
|
+
ability to ignore specific warning messages, ignore warnings
|
16
|
+
in specific files/directories, and add custom handling for all
|
17
|
+
warnings in specific files/directories.
|
18
|
+
email: code@jeremyevans.net
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files:
|
22
|
+
- README.rdoc
|
23
|
+
- CHANGELOG
|
24
|
+
- MIT-LICENSE
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- lib/warning.rb
|
31
|
+
- test/test_warning.rb
|
32
|
+
homepage: https://github.com/jeremyevans/ruby-warning
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- "--quiet"
|
39
|
+
- "--line-numbers"
|
40
|
+
- "--inline-source"
|
41
|
+
- "--title"
|
42
|
+
- 'ruby-warning: Add custom processing for warnings'
|
43
|
+
- "--main"
|
44
|
+
- README.rdoc
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '2.4'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.5.1
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Add custom processing for warnings
|
63
|
+
test_files: []
|