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

cmd/update-report: improve formula file validation #15489

Conversation

apainintheneck
Copy link
Contributor

  • 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?

Currently, ruby files that are not in the Cask directory are considered to be formulae which doesn't make sense. We know that these should only be in a few directories so we can check for that explicitly.

Beyond that the Tap#cask_file? and Tap.formula_file? methods were only used inside update-report so it doesn't make sense to turn them into pathnames and expand things when we know that each string will be a relative path from a tap that we can just check with a regex.

This change will stop other tap changes like new commands or changes to other directories from showing up as new formulae.

Library/Homebrew/cmd/update-report.rb Outdated Show resolved Hide resolved
Comment on lines 569 to 593
# returns true if given path would present a {Formula} file in this {Tap}.
# accepts both absolute path and relative path (relative to this {Tap}'s path)
# @private
sig { params(file: T.any(String, Pathname)).returns(T::Boolean) }
def formula_file?(file)
file = Pathname.new(file) unless file.is_a? Pathname
file = file.expand_path(path)
return false unless ruby_file?(file)
return false if cask_file?(file)

file.to_s.start_with?("#{formula_dir}/")
end

# returns true if given path would present a {Cask} file in this {Tap}.
# accepts both absolute path and relative path (relative to this {Tap}'s path)
# @private
sig { params(file: T.any(String, Pathname)).returns(T::Boolean) }
def cask_file?(file)
file = Pathname.new(file) unless file.is_a? Pathname
file = file.expand_path(path)
return false unless ruby_file?(file)

file.to_s.start_with?("#{cask_dir}/")
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These were only used in the update report and Tap#formula_files above which has been updated.

Copy link
Member

@reitermarkus reitermarkus May 28, 2023

Choose a reason for hiding this comment

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

Tap#cask_file? is used for cask CI currently. There it is also used in combination with git diff, so may be best to move the git diff logic into Tap, so it can be used to get all changed files between two commits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you point out where specifically it's used in cask CI? I couldn't find it with a quick grep of the cask repo.

Both of these were marked @private so I didn't check other repos but now that you mentioned it I looked and one of them is used in the test bot repo.

/u/l/H/L/T/h/homebrew-test-bot (master|✔) $ git grep -F '.cask_file?'
/u/l/H/L/T/h/homebrew-test-bot (master|✔) [1]$ git grep -F '.formula_file?'
lib/tests/formulae_detect.rb:          next unless tap.formula_file?(file)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just rebased this to add back in the .cask_file? and .formula_file? methods because they are used in other repos. I made both of them more restrictive though. The use of .formula_file? in test bot would need to be changed first though but the current method already takes a string so that shouldn't be too hard.

The idea of combining the git diff logic is an interesting idea. I'd have to look to see how much logic is shared there between different use cases.

Currently, ruby files that are not in the Cask directory are
considered to be formulae if a Formula or HomebrewFormula directory
doesn't exist  which doesn't make sense. We know that these should
only be in a few directories so we can check for that explicitly.

Beyond that the `Tap#cask_file?` and `Tap.formula_file?` methods
were only used inside update-report so it doesn't make sense to
turn them into pathnames and expand things when we know that
each string will be a relative path from a tap that we can just
check with a regex.

This change will stop other tap changes like new commands or
changes to other directories fke lib/ rom showing up as new formulae.
@apainintheneck apainintheneck force-pushed the fix-cask-formula-file-validation branch from eaaf0ed to 6f7181b Compare May 28, 2023 04:34
Copy link
Contributor Author

@apainintheneck apainintheneck left a comment

Choose a reason for hiding this comment

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

Note to self: It'd probably be a good idea to add a test for casks in the update report.

Comment on lines +590 to +594
if official?
%r{^Formula(/[^/]+)+\.rb$}
else
%r{^Formula/[^/]+\.rb$}
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this difference is actually meaningly in most cases.

Copy link
Member

Choose a reason for hiding this comment

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

Does that mean if e.g. Formula/ exists, any Ruby files in the root are not considered formula files?

@MikeMcQuaid
Copy link
Member

Beyond that the Tap#cask_file? and Tap.formula_file? methods were only used inside update-report so it doesn't make sense to turn them into pathnames and expand things when we know that each string will be a relative path from a tap that we can just check with a regex.

They are also used in https://github.com/homebrew/homebrew-test-bot/

else
%r{^Formula/[^/]+\.rb$}
end
when "HomebrewFormula"
Copy link
Member

Choose a reason for hiding this comment

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

IMO we should consider deprecating both HomebrewFormula and having formula files in the root of a tap.

@@ -173,7 +173,6 @@ def setup_completion(link:)
expect(homebrew_foo_tap.tap_migrations).to eq("removed-formula" => "homebrew/foo")
expect(homebrew_foo_tap.command_files).to eq([cmd_file])
expect(homebrew_foo_tap.to_hash).to be_a(Hash)
expect(homebrew_foo_tap).to have_formula_file(formula_file)
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

@github-actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions bot added the stale No recent activity label Jun 20, 2023
@github-actions github-actions bot closed this Jun 27, 2023
@github-actions github-actions bot added the outdated PR was locked due to age label Jul 28, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2023
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 stale No recent activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants