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
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.
-
#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
Instance Method Summary collapse
- #add(message) ⇒ Object
- #compare ⇒ Object
- #create_temp_shitlist ⇒ Object
- #diff ⇒ Object
-
#initialize(shitlist_path, transform_message = nil) ⇒ 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) ⇒ DeprecationTracker
Returns a new instance of DeprecationTracker.
60 61 62 63 64 |
# File 'lib/deprecation_tracker.rb', line 60 def initialize(shitlist_path, = nil) @shitlist_path = shitlist_path @transform_message = || -> () { } @deprecation_messages = {} end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
58 59 60 |
# File 'lib/deprecation_tracker.rb', line 58 def bucket @bucket end |
#deprecation_messages ⇒ Object (readonly)
Returns the value of attribute deprecation_messages.
57 58 59 |
# File 'lib/deprecation_tracker.rb', line 57 def @deprecation_messages end |
#shitlist_path ⇒ Object (readonly)
Returns the value of attribute shitlist_path.
57 58 59 |
# File 'lib/deprecation_tracker.rb', line 57 def shitlist_path @shitlist_path end |
#transform_message ⇒ Object (readonly)
Returns the value of attribute transform_message.
57 58 59 |
# File 'lib/deprecation_tracker.rb', line 57 def @transform_message end |
Class Method Details
.track_rspec(rspec_config, shitlist_path:, mode:, transform_message: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/deprecation_tracker.rb', line 33 def self.track_rspec(rspec_config, shitlist_path:, mode:, transform_message: nil) deprecation_tracker = DeprecationTracker.new(shitlist_path, ) ActiveSupport::Deprecation.behavior << -> (, _callstack) { deprecation_tracker.add() } KernelWarnTracker.callbacks << -> () { deprecation_tracker.add() } 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 if mode == "save" deprecation_tracker.save elsif mode == "compare" deprecation_tracker.compare end end end |
Instance Method Details
#add(message) ⇒ Object
66 67 68 69 70 |
# File 'lib/deprecation_tracker.rb', line 66 def add() return if bucket.nil? @deprecation_messages[bucket] << .() end |
#compare ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/deprecation_tracker.rb', line 77 def compare shitlist = read_shitlist changed_buckets = [] .each do |bucket, | if shitlist[bucket] != changed_buckets << bucket end end if changed_buckets.length > 0 = <<~MESSAGE.red ⚠️ Deprecation warnings have changed! Code called by the following spec files is now generating different deprecation warnings: #{changed_buckets.join("\n")} To check your failures locally, you can run: DEPRECATION_TRACKER=compare bundle exec rspec #{changed_buckets.join(" ")} Here is a diff between what is expected and what was generated by this process: #{diff} See \e[4;37mdev-docs/testing/deprecation_tracker.md\e[0;31m for more information. MESSAGE raise UnexpectedDeprecations, end end |
#create_temp_shitlist ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/deprecation_tracker.rb', line 124 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
110 111 112 113 114 115 |
# File 'lib/deprecation_tracker.rb', line 110 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
133 134 135 136 137 138 139 |
# File 'lib/deprecation_tracker.rb', line 133 def normalized = read_shitlist.merge().each_with_object({}) do |(bucket, ), hash| hash[bucket] = .sort end normalized.reject {|_key, value| value.empty? }.sort_by {|key, _value| key }.to_h end |
#read_shitlist ⇒ Object
141 142 143 144 145 146 |
# File 'lib/deprecation_tracker.rb', line 141 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.}" end |
#save ⇒ Object
117 118 119 120 121 122 |
# File 'lib/deprecation_tracker.rb', line 117 def save new_shitlist = create_temp_shitlist FileUtils.cp(new_shitlist.path, shitlist_path) ensure new_shitlist&.delete end |