warning_signs 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +51 -4
- data/lib/warning_signs/caller_location_helper.rb +17 -0
- data/lib/warning_signs/deprecation.rb +16 -3
- data/lib/warning_signs/handler.rb +13 -3
- data/lib/warning_signs/rails_deprecation_catcher.rb +6 -1
- data/lib/warning_signs/ruby_category_matcher.rb +29 -0
- data/lib/warning_signs/ruby_deprecation_catcher.rb +11 -13
- data/lib/warning_signs/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d9af94718f30c122c45153b7c2837ec2544ab06d3c3f5356b6062408e766d64
|
4
|
+
data.tar.gz: c038f7baf62f0f15c63041e47c54281e9c9c26c8dfc489f96c98780e7490b523
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b47dd7876dec5213c03fb2774ee2add5a5da6f35d40dac9d33f8a27dd5767ac80bbc507b009d01887695184e2c24af5bd286028b41b647e56b63e1e3fc0f74b
|
7
|
+
data.tar.gz: 14618990cbda30ebb76dcf6d362b67c4eacdbb700005d3f1aef6c15dc98348a43e5c67323ed3b286ee1b37ad5d0a88b7a4d721ea78c20948baa8a97560d0fdb6
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## 0.5.0
|
2
|
+
|
3
|
+
* Allow handlers to print out backtraces for log, stdout, and stderr behaviors
|
4
|
+
|
5
|
+
## 0.4.0
|
6
|
+
|
7
|
+
* Allow handlers to take into account Ruby warning categories
|
8
|
+
* Ruby warning message better handles Ruby warning categories
|
9
|
+
|
1
10
|
## 0.3.0
|
2
11
|
|
3
12
|
* Allow multiple behaviors in a single environment
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
The Warning Signs gem builds upon the ideas of the
|
4
4
|
[Ruby Warning Gem](https://github.com/jeremyevans/ruby-warning)
|
5
|
-
and [Stop
|
5
|
+
and [Stop ignoring your Rails (and Ruby) deprecations!](https://blog.testdouble.com/posts/2023-04-24-stop-ignoring-your-ruby-and-rails-deprecations/).
|
6
6
|
|
7
7
|
The idea is to provide an easily-configurable way to manage deprecation
|
8
|
-
warnings from Ruby and Rails to support your
|
8
|
+
warnings from Ruby and Rails to support your upgrades.
|
9
9
|
|
10
10
|
## Installing Warning Signs
|
11
11
|
|
@@ -18,7 +18,7 @@ under some use cases, you may not need it in production.
|
|
18
18
|
|
19
19
|
## Using Warning Signs
|
20
20
|
|
21
|
-
The Warning Signs
|
21
|
+
The Warning Signs gem looks for a `.warning_signs.yml` file when the Rails
|
22
22
|
application starts. If it does not find the file, it will not load and the
|
23
23
|
processing of deprecations will be unaffected.
|
24
24
|
|
@@ -50,7 +50,7 @@ Handlers are matched in order. If no handler applies to a deprecation
|
|
50
50
|
warning, Ruby warnings is ignored, Rails warnings are passed through
|
51
51
|
the ActiveSupport notification mechanism.
|
52
52
|
|
53
|
-
The following
|
53
|
+
The following example logs all deprecation warnings:
|
54
54
|
|
55
55
|
```yaml
|
56
56
|
handlers:
|
@@ -98,6 +98,20 @@ No matter what order you have the behaviors in, a `raise` behavior will be
|
|
98
98
|
executed last so that the other behaviors happen before the exception is
|
99
99
|
invoked.
|
100
100
|
|
101
|
+
The attribute `backtrace_lines` can be used to print extra lines of the
|
102
|
+
backtrace to the output for the `log`, `stderr`, and `stdout` behaviors.
|
103
|
+
Currently, it's one attribute that applies to all behaviors. This can be
|
104
|
+
useful, especially if the direct cause of the issue is in a gem.
|
105
|
+
|
106
|
+
```yaml
|
107
|
+
handlers:
|
108
|
+
- environment: all
|
109
|
+
backtrace_lines: 5
|
110
|
+
behaviors:
|
111
|
+
- raise
|
112
|
+
- log
|
113
|
+
```
|
114
|
+
|
101
115
|
A common pattern is to focus only on specific deprecations and ignore others.
|
102
116
|
For example, this setting file would raise on Ruby keyword argument
|
103
117
|
deprecations and ignore other ruby deprecations
|
@@ -150,3 +164,36 @@ handlers:
|
|
150
164
|
- environment: other
|
151
165
|
behavior: log
|
152
166
|
```
|
167
|
+
|
168
|
+
Ruby warnings can have an optional category, there are two predefined
|
169
|
+
categories, `deprecated` and `experimental`. You can specify a handler to
|
170
|
+
match those categories based on an "only" or "except" matcher. If you want
|
171
|
+
to specially handle warnings that do not have a defined category, you can
|
172
|
+
refer to them as `blank`.
|
173
|
+
|
174
|
+
This handler only handles Ruby warnings that are deprecated, other warnings
|
175
|
+
are ignored.
|
176
|
+
|
177
|
+
```yaml
|
178
|
+
handlers:
|
179
|
+
- environment: all
|
180
|
+
ruby_warnings:
|
181
|
+
only:
|
182
|
+
- deprecated
|
183
|
+
behavior: log
|
184
|
+
```
|
185
|
+
|
186
|
+
This handler handles any Ruby warning with a category
|
187
|
+
|
188
|
+
```yaml
|
189
|
+
handlers:
|
190
|
+
- environment: all
|
191
|
+
ruby_warnings:
|
192
|
+
except:
|
193
|
+
- blank
|
194
|
+
behavior: log
|
195
|
+
```
|
196
|
+
|
197
|
+
## To Do:
|
198
|
+
|
199
|
+
* Ability to customize output message format
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module WarningSigns
|
2
|
+
module CallerLocationHelper
|
3
|
+
def caller_location_start
|
4
|
+
caller_locations.find_index do |location|
|
5
|
+
!ignore_line(location.to_s)
|
6
|
+
end || 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def ignore_line(line)
|
10
|
+
line.include?("<internal:") ||
|
11
|
+
line.include?("warning_signs/lib") ||
|
12
|
+
line.include?("warning_signs/spec") ||
|
13
|
+
line.include?("rubygems") ||
|
14
|
+
line.include?("/gems")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module WarningSigns
|
2
2
|
class Deprecation
|
3
|
-
attr_accessor :message, :source
|
3
|
+
attr_accessor :message, :source, :category, :backtrace
|
4
4
|
|
5
|
-
def initialize(message, source:)
|
5
|
+
def initialize(message, source:, category: nil, backtrace: nil)
|
6
6
|
@message = message
|
7
7
|
@source = source.to_s.downcase.inquiry
|
8
|
+
@category = category
|
9
|
+
@backtrace = backtrace
|
8
10
|
end
|
9
11
|
|
10
12
|
def handler
|
@@ -14,10 +16,16 @@ module WarningSigns
|
|
14
16
|
# force raise to be the last element if it is present
|
15
17
|
def behaviors
|
16
18
|
result = (handler&.environment&.behaviors || []).inquiry
|
17
|
-
return result
|
19
|
+
return result unless result.raise?
|
18
20
|
(result - ["raise"]) << "raise"
|
19
21
|
end
|
20
22
|
|
23
|
+
def backtrace_lines
|
24
|
+
lines = handler&.backtrace_lines || 0
|
25
|
+
return "" if lines.zero?
|
26
|
+
backtrace[1..lines].join("\n")
|
27
|
+
end
|
28
|
+
|
21
29
|
def invoke
|
22
30
|
behaviors.each do |behavior|
|
23
31
|
case behavior
|
@@ -25,8 +33,13 @@ module WarningSigns
|
|
25
33
|
raise UnhandledDeprecationError, message
|
26
34
|
when "log"
|
27
35
|
Rails.logger.warn(message)
|
36
|
+
backtrace_lines.split("\n").each { Rails.logger.warn(_1) }
|
28
37
|
when "stderr"
|
29
38
|
$stderr.puts(message) # standard:disable Style/StderrPuts
|
39
|
+
$stderr.puts(backtrace_lines) # standard:disable Style/StderrPuts
|
40
|
+
when "stdout"
|
41
|
+
$stdout.puts(message) # standard:disable Style/StdoutPuts
|
42
|
+
$stdout.puts(backtrace_lines) # standard:disable Style/StdoutPuts
|
30
43
|
end
|
31
44
|
end
|
32
45
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module WarningSigns
|
2
2
|
class Handler
|
3
|
-
attr_accessor :environments, :except, :only, :source
|
3
|
+
attr_accessor :environments, :except, :only, :source,
|
4
|
+
:category_matcher, :backtrace_lines
|
4
5
|
|
5
6
|
def self.from_hash(hash)
|
6
7
|
new(**hash.symbolize_keys)
|
@@ -13,7 +14,9 @@ module WarningSigns
|
|
13
14
|
except: [],
|
14
15
|
only: [],
|
15
16
|
source: "any",
|
16
|
-
environments: []
|
17
|
+
environments: [],
|
18
|
+
ruby_warnings: {},
|
19
|
+
backtrace_lines: 0
|
17
20
|
)
|
18
21
|
@except = except.map { Pattern.for(_1) }
|
19
22
|
@only = only.map { Pattern.for(_1) }
|
@@ -22,6 +25,8 @@ module WarningSigns
|
|
22
25
|
@environments << Environment.new(environment: environment, behaviors: behaviors, behavior: behavior)
|
23
26
|
end
|
24
27
|
@source = source.to_s.downcase.inquiry
|
28
|
+
@category_matcher = RubyCategoryMatcher.new(**ruby_warnings.symbolize_keys)
|
29
|
+
@backtrace_lines = backtrace_lines
|
25
30
|
raise InvalidHandlerError unless valid?
|
26
31
|
end
|
27
32
|
|
@@ -35,7 +40,12 @@ module WarningSigns
|
|
35
40
|
|
36
41
|
def match?(deprecation)
|
37
42
|
source_match?(deprecation.source) &&
|
38
|
-
pattern_match?(deprecation.message)
|
43
|
+
pattern_match?(deprecation.message) &&
|
44
|
+
category_match?(deprecation.category)
|
45
|
+
end
|
46
|
+
|
47
|
+
def category_match?(category)
|
48
|
+
category_matcher.match?(category)
|
39
49
|
end
|
40
50
|
|
41
51
|
def pattern_match?(message)
|
@@ -1,9 +1,14 @@
|
|
1
1
|
module WarningSigns
|
2
2
|
class RailsDeprecationCatcher < ActiveSupport::Subscriber
|
3
|
+
include CallerLocationHelper
|
3
4
|
attach_to :rails
|
4
5
|
|
5
6
|
def deprecation(event)
|
6
|
-
Deprecation.new(
|
7
|
+
Deprecation.new(
|
8
|
+
event.payload[:message],
|
9
|
+
source: "rails",
|
10
|
+
backtrace: caller_locations[caller_location_start..]
|
11
|
+
).invoke
|
7
12
|
end
|
8
13
|
end
|
9
14
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module WarningSigns
|
2
|
+
class RubyCategoryMatcher
|
3
|
+
attr_accessor :only, :except
|
4
|
+
|
5
|
+
def initialize(only: [], except: [])
|
6
|
+
@only = only.compact.map { _1.to_sym }
|
7
|
+
@except = except.compact.map { _1.to_sym }
|
8
|
+
end
|
9
|
+
|
10
|
+
def match?(category)
|
11
|
+
category = :blank if category.blank?
|
12
|
+
only_match?(category) && except_match?(category)
|
13
|
+
end
|
14
|
+
|
15
|
+
def only_match?(category)
|
16
|
+
return true if only.empty?
|
17
|
+
only.any? do |only_pattern|
|
18
|
+
only_pattern === category&.to_sym
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def except_match?(category)
|
23
|
+
return true if except.empty?
|
24
|
+
except.none? do |except_pattern|
|
25
|
+
except_pattern === category&.to_sym
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,20 +1,18 @@
|
|
1
1
|
module WarningSigns
|
2
2
|
module RubyDeprecationCatcher
|
3
|
-
|
4
|
-
|
3
|
+
include CallerLocationHelper
|
4
|
+
def warn(message, category: nil)
|
5
|
+
Deprecation.new(
|
6
|
+
augmented_message(message, category),
|
7
|
+
source: "ruby",
|
8
|
+
category: category,
|
9
|
+
backtrace: caller_locations[caller_location_start..]
|
10
|
+
).invoke
|
5
11
|
end
|
6
12
|
|
7
|
-
def augmented_message(message)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def caller_location
|
12
|
-
caller_locations.find do |location|
|
13
|
-
!location.to_s.include?("internal:warning") &&
|
14
|
-
!location.to_s.include?("warning_signs") &&
|
15
|
-
!location.to_s.include?("rubygems") &&
|
16
|
-
!location.to_s.include?("/gems")
|
17
|
-
end
|
13
|
+
def augmented_message(message, category)
|
14
|
+
category_part = category.present? ? " #{category.upcase}: " : ": "
|
15
|
+
"RUBY WARNING#{category_part}#{message} called from #{caller_locations[caller_location_start]}"
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: warning_signs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Noel Rappin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-byebug
|
@@ -170,6 +170,7 @@ files:
|
|
170
170
|
- bin/console
|
171
171
|
- bin/setup
|
172
172
|
- lib/warning_signs.rb
|
173
|
+
- lib/warning_signs/caller_location_helper.rb
|
173
174
|
- lib/warning_signs/deprecation.rb
|
174
175
|
- lib/warning_signs/environment.rb
|
175
176
|
- lib/warning_signs/handler.rb
|
@@ -177,6 +178,7 @@ files:
|
|
177
178
|
- lib/warning_signs/rails_deprecation_catcher.rb
|
178
179
|
- lib/warning_signs/railtie.rb
|
179
180
|
- lib/warning_signs/regex_pattern.rb
|
181
|
+
- lib/warning_signs/ruby_category_matcher.rb
|
180
182
|
- lib/warning_signs/ruby_deprecation_catcher.rb
|
181
183
|
- lib/warning_signs/string_pattern.rb
|
182
184
|
- lib/warning_signs/version.rb
|