Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formulary_spec: update API tests to avoid mocking #16697

Merged

Conversation

apainintheneck
Copy link
Contributor

@apainintheneck apainintheneck commented Feb 17, 2024

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

Stop mocking the formulary loader method. We just need to set the environment variable so that it knows to load things from the API.

Fix spec that doesn't work with the CoreTap#formula_names. That method assumes that we always have a valid ruby_source_path in the API JSON even though that was optional for a while after launching the API if my memory serves me. It's probably fine to assume this should always be set though and I changed it to use Hash#fetch to give use better error messages if something goes wrong in the future.

Remove unnecessary allow(x).to receive(y).and_call_original in tap spec.

Extracted from #16460 (comment)

Stop mocking the formulary loader method. We just need to set the
environment variable so that it knows to load things from the API.

Fix spec that doesn't work with the `CoreTap.formula_names`.
That method assumes that we always have a valid `ruby_source_path`
in the API JSON even though that was optional for a while after
launching the API if my memory serves me. It's probably fine
to assume this should always be set though and I changed it to use
`Hash#fetch` to give use better error messages if something goes wrong in the future.

Remove unused `allow(x).to receive(y).and_call_original` in tap spec.
Comment on lines -1172 to +1173
new_path = File.join(tap_path, formula_hash["ruby_source_path"]) # Pathname equivalent is slow in a tight loop
# Pathname equivalent is slow in a tight loop
new_path = File.join(tap_path, formula_hash.fetch("ruby_source_path"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

File#join expects one or more strings while Array#join handles nil arguments gracefully.

irb(main):001:0> ["one", nil].join
=> "one"
irb(main):002:0> File.join("one", nil)
(irb):2:in `join': no implicit conversion of nil into String (TypeError)
        from (irb):2:in `<main>'                              
        from /Users/kevinrobell/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from /Users/kevinrobell/.asdf/installs/ruby/3.2.2/bin/irb:25:in `load'
        from /Users/kevinrobell/.asdf/installs/ruby/3.2.2/bin/irb:25:in `<main>'

Copy link
Member

Choose a reason for hiding this comment

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

As long as the API JSON is current, "ruby_source_path" will always be populated, right?

Copy link
Member

Choose a reason for hiding this comment

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

In any case this doesn't change the behaviour in case of nil, only the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As long as the API JSON is current, "ruby_source_path" will always be populated, right?

Correct.

In any case this doesn't change the behaviour in case of nil, only the error message.

Yep, and it also better shows intent.

@Bo98
Copy link
Member

Bo98 commented Feb 17, 2024

That method assumes that we always have a valid ruby_source_path in the API JSON even though that was optional for a while after launching the API if my memory serves me. It's probably fine to assume this should always be set though and I changed it to use Hash#fetch to give use better error messages if something goes wrong in the future.

Yeah was likely the cause of https://github.com/orgs/Homebrew/discussions/4701 and https://github.com/orgs/Homebrew/discussions/4750, but it's been like that for a while now

@apainintheneck
Copy link
Contributor Author

If we wanted to be really cautious, we could just skip any values that don't have this anymore but I assume the time to do that has passed.

@Bo98
Copy link
Member

Bo98 commented Feb 17, 2024

The fix would be more defaulting to something like new_formula_path but yeah this isn't going to be a concern once we drop v2 anyway in which case those old JSON files are truly not supported anymore.

@apainintheneck apainintheneck merged commit e5fefd7 into Homebrew:master Feb 19, 2024
24 checks passed
@MikeMcQuaid
Copy link
Member

Thanks @apainintheneck!

@github-actions github-actions bot added the outdated PR was locked due to age label Mar 21, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants