as_deprecation_tracker 1.1.0 → 1.2.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
  SHA1:
3
- metadata.gz: 3c3db75579f6e32b233ab757406affac74f42210
4
- data.tar.gz: 22c98db26c0c16b71993f038804b9b6e23ad7ea4
3
+ metadata.gz: 8c9e8fd0e5c19889d05575d93539fde6cbd82f90
4
+ data.tar.gz: 6fc07fb3155fe02e64510b1e2fa7d53440a1e0b8
5
5
  SHA512:
6
- metadata.gz: 7d3a4345c389c3a37436cab4aaac86c981264f237e8e14d0366b9b3a85b53e24f6d567ecb3029daac26f6f438b4adf634ae1d8073a1d9edd3d175f6b727a8812
7
- data.tar.gz: a4609be92a9373db4acba7a048f0ee320a51f027d5295a6c2c3c3cad4d608ab8ad3872cdd5eca617313337554b932d5b810fa0f76e3bbc75230a092d481582de
6
+ metadata.gz: f4782ab953d838c2ff817d490078117d54bff31a8ecedbda70f271c3f265847333425019a3fb75a60e7309dd6a162530757e4a35906e113d33e7a2d0945a5613
7
+ data.tar.gz: 8bcff72162a895eefeb70f0bf8409562b4d9beb38248f9124c7038bb955370aef46dc6fd828e60821d98d31a2d9292f64747ed16320436a30118a46bc58cef47
data/README.md CHANGED
@@ -88,6 +88,10 @@ number may vary by up to ten lines from the recorded number by default (see
88
88
  `line_tolerance` to tune). Usually the filename and method name are sufficient
89
89
  to match the caller without needing line numbers.
90
90
 
91
+ The message is an exact string match, but if it's a multi-line message then not
92
+ all lines need to be specified. Only the first lines given will be compared
93
+ against the deprecation message.
94
+
91
95
  Additional whitelist files may be placed below the root of each Rails engine
92
96
  and will be loaded at startup in addition to the main Rails root config file.
93
97
 
@@ -21,8 +21,9 @@ module ASDeprecationTracker
21
21
 
22
22
  def write_deprecation(message, callstack)
23
23
  writer = ASDeprecationTracker::Writer.new(whitelist_file)
24
- writer.add(message, callstack)
24
+ entry = writer.add(message, callstack)
25
25
  writer.write_file
26
+ ASDeprecationTracker.whitelist.add(entry.symbolize_keys)
26
27
  end
27
28
 
28
29
  def whitelist_file
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ASDeprecationTracker
3
- VERSION = '1.1.0'.freeze
3
+ VERSION = '1.2.0'.freeze
4
4
  end
@@ -14,14 +14,14 @@ module ASDeprecationTracker
14
14
  def add_to_list(*entries)
15
15
  entries.flatten.each { |entry| @list << WhitelistEntry.new(entry.symbolize_keys) }
16
16
  end
17
- alias_method :add, :add_to_list
17
+ alias add add_to_list
18
18
 
19
19
  def clear
20
20
  @list.clear
21
21
  end
22
22
 
23
23
  def load_file(path)
24
- add_to_list(YAML.load(File.read(path)))
24
+ add_to_list(YAML.safe_load(File.read(path), [Symbol]))
25
25
  end
26
26
 
27
27
  def matches?(deprecation)
@@ -11,6 +11,7 @@ module ASDeprecationTracker
11
11
  @callstack = callstack_to_files_lines(Array.wrap(callstack))
12
12
  @engine_root = engine.present? ? engine_root(engine) : nil
13
13
  @message = message
14
+ @message_lines = message.lines.map(&:chomp) if message
14
15
  end
15
16
 
16
17
  # rubocop:disable Metrics/CyclomaticComplexity
@@ -25,9 +26,15 @@ module ASDeprecationTracker
25
26
  private
26
27
 
27
28
  def message_matches?(message)
29
+ message = clean_message(message)
30
+ return true if message == @message
31
+ return true if message.lines[0, @message_lines.length].map(&:chomp) == @message_lines
32
+ false
33
+ end
34
+
35
+ def clean_message(message)
28
36
  cleanup_match = MESSAGE_CLEANUP_RE.match(message)
29
- message = cleanup_match[1] if cleanup_match
30
- message == @message
37
+ cleanup_match ? cleanup_match[1] : message
31
38
  end
32
39
 
33
40
  def callstack_to_files_lines(callstack)
@@ -38,7 +45,8 @@ module ASDeprecationTracker
38
45
  end
39
46
 
40
47
  def callstack_matches?(callstack)
41
- callstack = Rails::BacktraceCleaner.new.clean(callstack, :silent)
48
+ # Call #to_s to replace Thread::Backtrace::Location instances
49
+ callstack = Rails::BacktraceCleaner.new.clean(callstack.map(&:to_s), :silent)
42
50
  callstack = callstack_to_files_lines(callstack)
43
51
 
44
52
  @callstack.all? do |whitelist_entry|
@@ -65,7 +73,7 @@ module ASDeprecationTracker
65
73
  end
66
74
 
67
75
  def engine_root_matches?(callstack)
68
- callstack.any? { |callstack_entry| callstack_entry.start_with?(@engine_root) }
76
+ callstack.any? { |callstack_entry| callstack_entry.to_s.start_with?(@engine_root) }
69
77
  end
70
78
 
71
79
  def engine_root(engine_name)
@@ -8,7 +8,7 @@ module ASDeprecationTracker
8
8
  def initialize(filename)
9
9
  @filename = filename
10
10
  @contents = []
11
- @contents = YAML.load(File.read(filename)) || [] if File.exist?(filename)
11
+ @contents = YAML.safe_load(File.read(filename), [Symbol]) || [] if File.exist?(filename)
12
12
  end
13
13
 
14
14
  def add(message, callstack)
@@ -17,11 +17,13 @@ module ASDeprecationTracker
17
17
 
18
18
  callstack = Rails::BacktraceCleaner.new.clean(callstack, :silent)
19
19
 
20
- @contents << { 'message' => message, 'callstack' => callstack.first }
20
+ entry = { 'message' => message, 'callstack' => callstack.first }
21
+ @contents << entry
22
+ entry
21
23
  end
22
24
 
23
25
  def contents
24
- @contents.sort_by { |e| e.values_at('message', 'callstack') }.to_yaml
26
+ @contents.sort_by { |e| e.values_at('message', 'callstack').compact }.to_yaml
25
27
  end
26
28
 
27
29
  def write_file
@@ -24,15 +24,15 @@ class ReceiverTest < ASDeprecationTracker::TestCase
24
24
 
25
25
  def test_deprecation_unknown_record
26
26
  whitelist = ASDeprecationTracker::Whitelist.new
27
- ASDeprecationTracker.expects(:whitelist).returns(whitelist)
27
+ ASDeprecationTracker.expects(:whitelist).twice.returns(whitelist)
28
28
  stack = caller
29
29
  whitelist.expects(:matches?).with(message: 'deprecated call', callstack: stack).returns(false)
30
- ASDeprecationTracker::Writer.any_instance.expects(:add).with('deprecated call', stack)
30
+ whitelist.expects(:add).with(message: 'deprecated call', callstack: stack.first)
31
+ ASDeprecationTracker::Writer.any_instance.expects(:add).with('deprecated call', stack).returns(message: 'deprecated call', callstack: stack.first)
31
32
  ASDeprecationTracker::Writer.any_instance.expects(:write_file)
32
- ENV['AS_DEPRECATION_RECORD'] = 'true'
33
- ASDeprecationTracker::Receiver.new.deprecation(event(message: 'deprecated call', callstack: stack))
34
- ensure
35
- ENV.delete('AS_DEPRECATION_RECORD')
33
+ with_env(AS_DEPRECATION_RECORD: 'true') do
34
+ ASDeprecationTracker::Receiver.new.deprecation(event(message: 'deprecated call', callstack: stack))
35
+ end
36
36
  end
37
37
 
38
38
  def test_subscription
@@ -9,5 +9,17 @@ Combustion.path = 'test/internal'
9
9
  Combustion.initialize!
10
10
 
11
11
  module ASDeprecationTracker
12
- class TestCase < ::Minitest::Test; end
12
+ class TestCase < ::Minitest::Test
13
+ def with_env(new_env)
14
+ new_env.stringify_keys!
15
+ backup = ENV.to_h.slice(new_env.keys)
16
+ begin
17
+ ENV.update(new_env)
18
+ yield
19
+ ensure
20
+ new_env.keys.each { |k| ENV.delete(k) }
21
+ ENV.update(backup)
22
+ end
23
+ end
24
+ end
13
25
  end
@@ -18,6 +18,10 @@ class WhitelistEntryTest < ASDeprecationTracker::TestCase
18
18
  assert entry(callstack: nil).matches?(deprecation(callstack: caller))
19
19
  end
20
20
 
21
+ def test_matches_partial_message
22
+ assert entry(callstack: nil, message: "a\nb").matches?(deprecation(message: "a\nb\nc"))
23
+ end
24
+
21
25
  def test_matches_callstack_only
22
26
  assert entry(message: nil).matches?(deprecation)
23
27
  end
@@ -86,15 +90,19 @@ class WhitelistEntryTest < ASDeprecationTracker::TestCase
86
90
  end
87
91
 
88
92
  def test_matches_only_engine
89
- assert entry(engine: 'example').matches?(deprecation(called_in_engine: 'example'))
93
+ assert entry(message: nil, callstack: nil, engine: 'example').matches?(deprecation(called_in_engine: 'example'))
94
+ end
95
+
96
+ def test_matches_only_engine_backtrace_location
97
+ assert entry(message: nil, callstack: nil, engine: 'example').matches?(deprecation(called_in_engine: 'example', callstack: caller_locations))
90
98
  end
91
99
 
92
100
  def test_matches_different_engine
93
- refute entry(engine: 'another').matches?(deprecation(called_in_engine: 'example'))
101
+ refute entry(message: nil, callstack: nil, engine: 'another').matches?(deprecation(called_in_engine: 'example'))
94
102
  end
95
103
 
96
104
  def test_matches_outside_engine
97
- refute entry(engine: 'another').matches?(deprecation(called_in_engine: 'example'))
105
+ refute entry(message: nil, callstack: nil, engine: 'another').matches?(deprecation(called_in_engine: 'example'))
98
106
  end
99
107
 
100
108
  private
@@ -111,11 +119,12 @@ class WhitelistEntryTest < ASDeprecationTracker::TestCase
111
119
  end
112
120
 
113
121
  def deprecation(overrides = {})
114
- deprecation = default_deprecation
122
+ deprecation = default_deprecation.merge(overrides)
115
123
  if (engine = overrides.delete(:called_in_engine))
124
+ deprecation[:callstack] ||= []
116
125
  deprecation[:callstack] << "/home/user/engines/#{engine}/app/middleware/foo.rb:12:in `call'"
117
126
  end
118
- deprecation.merge(overrides).compact
127
+ deprecation.compact
119
128
  end
120
129
 
121
130
  def entry(overrides = {})
@@ -4,25 +4,26 @@ require 'test_helper'
4
4
  class WriterTest < ASDeprecationTracker::TestCase
5
5
  def test_add
6
6
  writer = new_writer
7
- writer.add('deprecated call', ['app/models/a.rb:23', 'app/models/b.rb:42'])
8
- assert_equal [{ 'message' => 'deprecated call', 'callstack' => 'app/models/a.rb:23' }], YAML.load(writer.contents)
7
+ ret = writer.add('deprecated call', ['app/models/a.rb:23', 'app/models/b.rb:42'])
8
+ assert_equal({ 'message' => 'deprecated call', 'callstack' => 'app/models/a.rb:23' }, ret)
9
+ assert_equal [ret], YAML.safe_load(writer.contents)
9
10
  end
10
11
 
11
12
  def test_add_strips_surrounding
12
13
  writer = new_writer
13
14
  writer.add('DEPRECATION WARNING: deprecated call (called from app/models/a.rb:23)', ['app/models/a.rb:23', 'app/models/b.rb:42'])
14
- assert_equal [{ 'message' => 'deprecated call', 'callstack' => 'app/models/a.rb:23' }], YAML.load(writer.contents)
15
+ assert_equal [{ 'message' => 'deprecated call', 'callstack' => 'app/models/a.rb:23' }], YAML.safe_load(writer.contents)
15
16
  end
16
17
 
17
18
  def test_add_cleans_callstack
18
19
  writer = new_writer
19
20
  Gem.expects(:path).returns(['/home/user/.rvm/gems/ruby-2.3.0'])
20
21
  writer.add('deprecated call', ['/home/user/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/finder_methods.rb:280:in `exists?\'', 'app/models/a.rb:23', 'app/models/b.rb:42'])
21
- assert_equal [{ 'message' => 'deprecated call', 'callstack' => 'app/models/a.rb:23' }], YAML.load(writer.contents)
22
+ assert_equal [{ 'message' => 'deprecated call', 'callstack' => 'app/models/a.rb:23' }], YAML.safe_load(writer.contents)
22
23
  end
23
24
 
24
25
  def test_contents_new_file_is_array
25
- assert_equal [], YAML.load(new_writer('').contents)
26
+ assert_equal [], YAML.safe_load(new_writer('').contents)
26
27
  end
27
28
 
28
29
  def test_contents_sorting
@@ -34,7 +35,17 @@ class WriterTest < ASDeprecationTracker::TestCase
34
35
  { 'message' => 'deprecated call 1', 'callstack' => 'app/models/a.rb:23' },
35
36
  { 'message' => 'deprecated call 1', 'callstack' => 'app/models/a.rb:42' },
36
37
  { 'message' => 'deprecated call 2', 'callstack' => 'app/models/a.rb:23' }
37
- ], YAML.load(writer.contents)
38
+ ], YAML.safe_load(writer.contents)
39
+ end
40
+
41
+ def test_contents_sorting_existing_message_without_callstack
42
+ existing = [{ 'message' => 'deprecated call 1' }]
43
+ writer = new_writer(existing.to_yaml)
44
+ writer.add('deprecated call 1', ['app/models/a.rb:23', 'app/models/b.rb:42'])
45
+ assert_equal [
46
+ { 'message' => 'deprecated call 1' },
47
+ { 'message' => 'deprecated call 1', 'callstack' => 'app/models/a.rb:23' }
48
+ ], YAML.safe_load(writer.contents)
38
49
  end
39
50
 
40
51
  def test_write_file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: as_deprecation_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominic Cleal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport