Class: DeprecationTracker
- Inherits:
-
Object
- Object
- DeprecationTracker
- Includes:
- KernelWarnTracker
- Defined in:
- lib/deprecation_tracker.rb
Overview
A shitlist for deprecation warnings during test runs. It has two modes: “save” and “compare”
DEPRECATION_TRACKER=save Record deprecation warnings, grouped by spec file. After the test run, save to a file.
DEPRECATION_TRACKER=compare Tracks deprecation warnings, grouped by spec file. After the test run, compare against shitlist of expected deprecation warnings. If anything is added or removed, raise an error with a diff of the changes.
Defined Under Namespace
Modules: KernelWarnTracker, MinitestExtension
Constant Summary collapse
- UnexpectedDeprecations =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#deprecation_messages ⇒ Object
readonly
Returns the value of attribute deprecation_messages.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#shitlist_path ⇒ Object
readonly
Returns the value of attribute shitlist_path.
-
#transform_message ⇒ Object
readonly
Returns the value of attribute transform_message.
Class Method Summary collapse
- .init_tracker(opts = {}) ⇒ Object
- .track_minitest(opts = {}) ⇒ Object
- .track_rspec(rspec_config, opts = {}) ⇒ Object
Instance Method Summary collapse
- #add(message) ⇒ Object
- #after_run ⇒ Object
- #compare ⇒ Object
- #create_if_shitlist_path_does_not_exist ⇒ Object
- #create_temp_shitlist ⇒ Object
- #diff ⇒ Object
-
#initialize(shitlist_path, transform_message = nil, mode = :save) ⇒ DeprecationTracker
constructor
A new instance of DeprecationTracker.
-
#normalized_deprecation_messages ⇒ Object
Normalize deprecation messages to reduce noise from file output and test files to be tracked with separate test runs.
- #read_shitlist ⇒ Object
- #save ⇒ Object
Methods included from KernelWarnTracker
Constructor Details
#initialize(shitlist_path, transform_message = nil, mode = :save) ⇒ DeprecationTracker
Returns a new instance of DeprecationTracker.
126 127 128 129 130 131 |
# File 'lib/deprecation_tracker.rb', line 126 def initialize(shitlist_path, = nil, mode = :save) @shitlist_path = shitlist_path = || -> () { } = {} @mode = mode.to_sym end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
124 125 126 |
# File 'lib/deprecation_tracker.rb', line 124 def bucket @bucket end |
#deprecation_messages ⇒ Object (readonly)
Returns the value of attribute deprecation_messages.
124 125 126 |
# File 'lib/deprecation_tracker.rb', line 124 def end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
124 125 126 |
# File 'lib/deprecation_tracker.rb', line 124 def mode @mode end |
#shitlist_path ⇒ Object (readonly)
Returns the value of attribute shitlist_path.
124 125 126 |
# File 'lib/deprecation_tracker.rb', line 124 def shitlist_path @shitlist_path end |
#transform_message ⇒ Object (readonly)
Returns the value of attribute transform_message.
124 125 126 |
# File 'lib/deprecation_tracker.rb', line 124 def end |
Class Method Details
.init_tracker(opts = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/deprecation_tracker.rb', line 71 def self.init_tracker(opts = {}) shitlist_path = opts[:shitlist_path] mode = opts[:mode] = opts[:transform_message] deprecation_tracker = DeprecationTracker.new(shitlist_path, , mode) # Since Rails 7.1 the preferred way to track deprecations is to use the deprecation trackers via # `Rails.application.deprecators`. # We fallback to tracking deprecations via the ActiveSupport singleton object if Rails.application.deprecators is # not defined for older Rails versions. if defined?(Rails) && defined?(Rails.application) && defined?(Rails.application.deprecators) Rails.application.deprecators.each do |deprecator| deprecator.behavior << -> (, _callstack = nil, _deprecation_horizon = nil, _gem_name = nil) { deprecation_tracker.add() } end elsif defined?(ActiveSupport) ActiveSupport::Deprecation.behavior << -> (, _callstack = nil, _deprecation_horizon = nil, _gem_name = nil) { deprecation_tracker.add() } end KernelWarnTracker.callbacks << -> () { deprecation_tracker.add() } deprecation_tracker end |
.track_minitest(opts = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/deprecation_tracker.rb', line 114 def self.track_minitest(opts = {}) tracker = init_tracker(opts) Minitest.after_run do tracker.after_run end ActiveSupport::TestCase.include(MinitestExtension.new(tracker)) end |
.track_rspec(rspec_config, opts = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/deprecation_tracker.rb', line 96 def self.track_rspec(rspec_config, opts = {}) deprecation_tracker = init_tracker(opts) rspec_config.around do |example| deprecation_tracker.bucket = example..fetch(:rerun_file_path) begin example.run ensure deprecation_tracker.bucket = nil end end rspec_config.after(:suite) do deprecation_tracker.after_run end end |
Instance Method Details
#add(message) ⇒ Object
133 134 135 136 137 |
# File 'lib/deprecation_tracker.rb', line 133 def add() return if bucket.nil? [bucket] << .() end |
#after_run ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/deprecation_tracker.rb', line 144 def after_run if mode == :save save elsif mode == :compare compare end end |
#compare ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/deprecation_tracker.rb', line 152 def compare shitlist = read_shitlist changed_buckets = [] .each do |bucket, | if shitlist[bucket] != changed_buckets << bucket end end if changed_buckets.length > 0 = " \u26A0\uFE0F Deprecation warnings have changed!\n\n Code called by the following spec files is now generating different deprecation warnings:\n\n \#{changed_buckets.join(\"\\n\")}\n\n To check your failures locally, you can run:\n\n DEPRECATION_TRACKER=compare bundle exec rspec \#{changed_buckets.join(\" \")}\n\n Here is a diff between what is expected and what was generated by this process:\n\n \#{diff}\n\n See \\e[4;37mdev-docs/testing/deprecation_tracker.md\\e[0;31m for more information.\n MESSAGE\n\n raise UnexpectedDeprecations, Rainbow(message).red\n end\nend\n" |
#create_if_shitlist_path_does_not_exist ⇒ Object
200 201 202 203 204 205 |
# File 'lib/deprecation_tracker.rb', line 200 def create_if_shitlist_path_does_not_exist dirname = File.dirname(shitlist_path) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) end end |
#create_temp_shitlist ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/deprecation_tracker.rb', line 207 def create_temp_shitlist temp_file = Tempfile.new("temp-deprecation-tracker-shitlist") temp_file.write(JSON.pretty_generate()) temp_file.flush temp_file end |
#diff ⇒ Object
185 186 187 188 189 190 |
# File 'lib/deprecation_tracker.rb', line 185 def diff new_shitlist = create_temp_shitlist `git diff --no-index #{shitlist_path} #{new_shitlist.path}` ensure new_shitlist.delete end |
#normalized_deprecation_messages ⇒ Object
Normalize deprecation messages to reduce noise from file output and test files to be tracked with separate test runs
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/deprecation_tracker.rb', line 216 def normalized = read_shitlist.merge().each_with_object({}) do |(bucket, ), hash| hash[bucket] = .sort end # not using `to_h` here to support older ruby versions {}.tap do |h| normalized.reject {|_key, value| value.empty? }.sort_by {|key, _value| key }.each do |k ,v| h[k] = v end end end |
#read_shitlist ⇒ Object
229 230 231 232 233 234 |
# File 'lib/deprecation_tracker.rb', line 229 def read_shitlist return {} unless File.exist?(shitlist_path) JSON.parse(File.read(shitlist_path)) rescue JSON::ParserError => e raise "#{shitlist_path} is not valid JSON: #{e.message}" end |
#save ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/deprecation_tracker.rb', line 192 def save new_shitlist = create_temp_shitlist create_if_shitlist_path_does_not_exist FileUtils.cp(new_shitlist.path, shitlist_path) ensure new_shitlist.delete if new_shitlist end |