Skip to content

Add VALID_MODES constant and validate tracker mode at initialization#186

Open
JuanVqz wants to merge 2 commits into
mainfrom
feature/valid-modes-constant
Open

Add VALID_MODES constant and validate tracker mode at initialization#186
JuanVqz wants to merge 2 commits into
mainfrom
feature/valid-modes-constant

Conversation

@JuanVqz
Copy link
Copy Markdown
Member

@JuanVqz JuanVqz commented May 18, 2026

Description

Introduces DeprecationTracker::VALID_MODES as a single source of truth for accepted tracker modes (save, compare). Constants live in lib/deprecation_tracker/valid_modes.rb so the exe/deprecations CLI can load them without pulling in the full library.

Motivation and Context

Valid mode values were duplicated as inline literals across lib/deprecation_tracker.rb and exe/deprecations. Adding or removing a mode required updating every literal. With VALID_MODES, one change propagates to all validation, error messages, and CLI help text automatically.

Additionally, passing an invalid mode previously silently no-oped (neither save nor compare ran after the test suite). It now raises ArgumentError early, covering both the library and CLI entry points.

How Has This Been Tested?

  • Updated existing test that expected silent no-op on invalid mode to now expect ArgumentError
  • Added test for invalid ENV['DEPRECATION_TRACKER'] value via init_tracker
  • Added VALID_MODES describe block: constant membership and all valid modes construct without error
  • Full spec suite passes (bundle exec rspec spec/deprecation_tracker_spec.rb)

Screenshots:

N/A

I will abide by the code of conduct

Centralizes valid tracker modes so adding/removing an option requires
a single change. Raises ArgumentError early instead of silently no-oping
on invalid input, covering both library and CLI entry points.
@JuanVqz JuanVqz self-assigned this May 18, 2026
@JuanVqz JuanVqz marked this pull request as ready for review May 18, 2026 20:39
@JuanVqz JuanVqz requested review from arielj and etagwerker May 18, 2026 20:39
Extract constants to valid_modes.rb so exe/deprecations only loads
the full lib when needed. Use VALID_MODES_DISPLAY to avoid repeated
map/join. Loosen spec assertions to not couple on mode count or message wording.
Copy link
Copy Markdown
Member

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuanVqz I think this might introduced an unwanted side effect (?)

bin/deprecations run without --tracker-mode is probably broken. The CLI builds:

command = "DEPRECATION_TRACKER=#{tracker_mode} #{rspec_command} ..."

When tracker_mode is nil, the spawned child sees ENV["DEPRECATION_TRACKER"] = ""

Empty string is truthy in Ruby, so:

mode = opts[:mode] || ENV["DEPRECATION_TRACKER"] || :save  # => ""
@mode = mode ? mode.to_sym : :save                          # => :""
# new check raises ArgumentError

Before this PR that silently no-op'd in after_run. After this PR it crashes at construction.

it "raises ArgumentError for invalid mode" do
expect {
DeprecationTracker.new(shitlist_path, nil, "random_stuff")
}.to raise_error(ArgumentError)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change intentional? because it's not testing the same thing as before

now it's testing that "at some point" it raises an error, but it's not really testing that it's not calling save or compare, the code could change to call save and fail with an ArgumentError caused by some code after that and this test would still pass, even if the failure is not caused by the invalid mode

now it's testing an implementation detail when before it was testing the actual expected behavior of not creating a sideeffect calling those methods

I think the previous test was better

stub_const("ENV", ENV.to_h.merge("DEPRECATION_TRACKER" => "bogus"))
expect {
DeprecationTracker.init_tracker({})
}.to raise_error(ArgumentError)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably include the error message or at least part of the exception message in the expectation

because now ANY ArgumentError thrown would satisfy this test, even if unrelated to a wrong tracker mode, it's not exactly testing that the reason it failed was the invalid value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants