warning_signs 0.4.0 → 0.5.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: ffe169d3fa9e10de71c8ca43c6ac4cf8dd3c036f716d9a9f608fc37ce01a1879
4
- data.tar.gz: a7511cdd8e836b8e1906b8c44a9ea3ec3309918fd033935a05480f7179bc4722
3
+ metadata.gz: 4d9af94718f30c122c45153b7c2837ec2544ab06d3c3f5356b6062408e766d64
4
+ data.tar.gz: c038f7baf62f0f15c63041e47c54281e9c9c26c8dfc489f96c98780e7490b523
5
5
  SHA512:
6
- metadata.gz: babffad1137746742209cf4fe4adc14f7b2a905684b31f8e68569ccdcc9806945b88e690b75262bf62b9c4d09be77eb87cc5c7aeac58bc947d3ff3165eda9854
7
- data.tar.gz: 23750e8b4849c3a1bd2d349fb26696e36eb34afcd71e0e3cb04619ed32234549f880cdc5f28e2d1da5fc57b1b1b519fc705ea75fe00de582c82e8612129473e8
6
+ metadata.gz: 5b47dd7876dec5213c03fb2774ee2add5a5da6f35d40dac9d33f8a27dd5767ac80bbc507b009d01887695184e2c24af5bd286028b41b647e56b63e1e3fc0f74b
7
+ data.tar.gz: 14618990cbda30ebb76dcf6d362b67c4eacdbb700005d3f1aef6c15dc98348a43e5c67323ed3b286ee1b37ad5d0a88b7a4d721ea78c20948baa8a97560d0fdb6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.5.0
2
+
3
+ * Allow handlers to print out backtraces for log, stdout, and stderr behaviors
4
+
1
5
  ## 0.4.0
2
6
 
3
7
  * Allow handlers to take into account Ruby warning categories
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- warning_signs (0.4.0)
4
+ warning_signs (0.5.0)
5
5
  awesome_print
6
6
  railties
7
7
 
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 Ignoring Your Ruby and Rails Deprecations](https://blog.testdouble.com/posts/2023-04-24-stop-ignoring-your-ruby-and-rails-deprecations/).
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 upgrade tool.
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 gem looks for a `.warning_sign.yml` file when the Rails
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 sample logs all deprecation warnings:
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
@@ -155,7 +169,7 @@ Ruby warnings can have an optional category, there are two predefined
155
169
  categories, `deprecated` and `experimental`. You can specify a handler to
156
170
  match those categories based on an "only" or "except" matcher. If you want
157
171
  to specially handle warnings that do not have a defined category, you can
158
- refer to them as `blank`,
172
+ refer to them as `blank`.
159
173
 
160
174
  This handler only handles Ruby warnings that are deprecated, other warnings
161
175
  are ignored.
@@ -182,5 +196,4 @@ handlers:
182
196
 
183
197
  ## To Do:
184
198
 
185
- * write to standard out as a behavior
186
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,11 +1,12 @@
1
1
  module WarningSigns
2
2
  class Deprecation
3
- attr_accessor :message, :source, :category
3
+ attr_accessor :message, :source, :category, :backtrace
4
4
 
5
- def initialize(message, source:, category: nil)
5
+ def initialize(message, source:, category: nil, backtrace: nil)
6
6
  @message = message
7
7
  @source = source.to_s.downcase.inquiry
8
8
  @category = category
9
+ @backtrace = backtrace
9
10
  end
10
11
 
11
12
  def handler
@@ -19,6 +20,12 @@ module WarningSigns
19
20
  (result - ["raise"]) << "raise"
20
21
  end
21
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
+
22
29
  def invoke
23
30
  behaviors.each do |behavior|
24
31
  case behavior
@@ -26,8 +33,13 @@ module WarningSigns
26
33
  raise UnhandledDeprecationError, message
27
34
  when "log"
28
35
  Rails.logger.warn(message)
36
+ backtrace_lines.split("\n").each { Rails.logger.warn(_1) }
29
37
  when "stderr"
30
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
31
43
  end
32
44
  end
33
45
  end
@@ -1,6 +1,7 @@
1
1
  module WarningSigns
2
2
  class Handler
3
- attr_accessor :environments, :except, :only, :source, :category_matcher
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)
@@ -14,7 +15,8 @@ module WarningSigns
14
15
  only: [],
15
16
  source: "any",
16
17
  environments: [],
17
- ruby_warnings: {}
18
+ ruby_warnings: {},
19
+ backtrace_lines: 0
18
20
  )
19
21
  @except = except.map { Pattern.for(_1) }
20
22
  @only = only.map { Pattern.for(_1) }
@@ -24,6 +26,7 @@ module WarningSigns
24
26
  end
25
27
  @source = source.to_s.downcase.inquiry
26
28
  @category_matcher = RubyCategoryMatcher.new(**ruby_warnings.symbolize_keys)
29
+ @backtrace_lines = backtrace_lines
27
30
  raise InvalidHandlerError unless valid?
28
31
  end
29
32
 
@@ -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(event.payload[:message], source: "rails").invoke
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
@@ -1,25 +1,18 @@
1
1
  module WarningSigns
2
2
  module RubyDeprecationCatcher
3
+ include CallerLocationHelper
3
4
  def warn(message, category: nil)
4
5
  Deprecation.new(
5
6
  augmented_message(message, category),
6
7
  source: "ruby",
7
- category: category
8
+ category: category,
9
+ backtrace: caller_locations[caller_location_start..]
8
10
  ).invoke
9
11
  end
10
12
 
11
13
  def augmented_message(message, category)
12
14
  category_part = category.present? ? " #{category.upcase}: " : ": "
13
- "RUBY WARNING#{category_part}#{message} called from #{caller_location}"
14
- end
15
-
16
- def caller_location
17
- caller_locations.find do |location|
18
- !location.to_s.include?("internal:warning") &&
19
- !location.to_s.include?("warning_signs") &&
20
- !location.to_s.include?("rubygems") &&
21
- !location.to_s.include?("/gems")
22
- end
15
+ "RUBY WARNING#{category_part}#{message} called from #{caller_locations[caller_location_start]}"
23
16
  end
24
17
  end
25
18
  end
@@ -1,3 +1,3 @@
1
1
  module WarningSigns
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  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.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-09 00:00:00.000000000 Z
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