diff --git a/exe/deprecations b/exe/deprecations index db824d1..aff4176 100755 --- a/exe/deprecations +++ b/exe/deprecations @@ -3,10 +3,14 @@ require "json" require "rainbow" require "optparse" require "set" +require_relative "../lib/deprecation_tracker/valid_modes" require_relative "../lib/deprecation_tracker/shard_merger" def run_tests(deprecation_warnings, opts = {}) tracker_mode = opts[:tracker_mode] + if tracker_mode && !DeprecationTracker::VALID_MODES.include?(tracker_mode.to_sym) + abort "Invalid --tracker-mode #{tracker_mode.inspect}. Must be one of: #{DeprecationTracker::VALID_MODES_DISPLAY}." + end next_mode = opts[:next_mode] rspec_command = if next_mode "bin/next rspec" @@ -69,7 +73,7 @@ option_parser = OptionParser.new do |opts| options[:next] = next_mode end - opts.on("--tracker-mode MODE", "Set DEPRECATION_TRACKER in test mode. Options: save or compare") do |tracker_mode| + opts.on("--tracker-mode MODE", "Set DEPRECATION_TRACKER in test mode. Options: #{DeprecationTracker::VALID_MODES_DISPLAY}") do |tracker_mode| options[:tracker_mode] = tracker_mode end diff --git a/lib/deprecation_tracker.rb b/lib/deprecation_tracker.rb index 2c357be..f83ad27 100644 --- a/lib/deprecation_tracker.rb +++ b/lib/deprecation_tracker.rb @@ -10,6 +10,8 @@ # 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. # +require_relative "deprecation_tracker/valid_modes" + class DeprecationTracker UnexpectedDeprecations = Class.new(StandardError) @@ -141,6 +143,9 @@ def initialize(shitlist_path, transform_message = nil, mode = :save, node_index: @transform_message = transform_message || -> (message) { message } @deprecation_messages = {} @mode = mode ? mode.to_sym : :save + unless VALID_MODES.include?(@mode) + raise ArgumentError, "mode must be one of #{VALID_MODES_DISPLAY}, got: #{mode.inspect}" + end if @mode == :compare && node_index raise ArgumentError, "node_index cannot be used with compare mode" end diff --git a/lib/deprecation_tracker/valid_modes.rb b/lib/deprecation_tracker/valid_modes.rb new file mode 100644 index 0000000..7afe503 --- /dev/null +++ b/lib/deprecation_tracker/valid_modes.rb @@ -0,0 +1,4 @@ +class DeprecationTracker + VALID_MODES = %i[save compare].freeze + VALID_MODES_DISPLAY = VALID_MODES.map(&:to_s).join(", ").freeze +end diff --git a/spec/deprecation_tracker_spec.rb b/spec/deprecation_tracker_spec.rb index 67bb311..9e0064c 100644 --- a/spec/deprecation_tracker_spec.rb +++ b/spec/deprecation_tracker_spec.rb @@ -264,11 +264,10 @@ expect(tracker.mode).to eq(:save) end - it "does not save nor compare if mode is invalid" do - tracker = DeprecationTracker.new(shitlist_path, nil, "random_stuff") - expect(tracker).not_to receive(:save) - expect(tracker).not_to receive(:compare) - tracker.after_run + it "raises ArgumentError for invalid mode" do + expect { + DeprecationTracker.new(shitlist_path, nil, "random_stuff") + }.to raise_error(ArgumentError) end end @@ -426,6 +425,27 @@ def self.behavior tracker = DeprecationTracker.init_tracker({}) expect(tracker.mode).to eq(:compare) end + + it "raises ArgumentError when ENV['DEPRECATION_TRACKER'] is invalid" do + stub_const("ENV", ENV.to_h.merge("DEPRECATION_TRACKER" => "bogus")) + expect { + DeprecationTracker.init_tracker({}) + }.to raise_error(ArgumentError) + end + end + + describe "VALID_MODES" do + it "contains save and compare" do + expect(DeprecationTracker::VALID_MODES).to include(:save, :compare) + end + + it "accepts all valid modes without error" do + DeprecationTracker::VALID_MODES.each do |mode| + expect { + DeprecationTracker.new(shitlist_path, nil, mode) + }.not_to raise_error + end + end end describe DeprecationTracker::KernelWarnTracker do