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

utils/pypi: ignore test resources when counting matches #16790

Merged
merged 1 commit into from Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/utils/pypi.rb
Expand Up @@ -358,7 +358,7 @@ def self.update_python_resources!(formula, version: nil, package_name: nil, extr

ohai "Updating resource blocks" unless silent
Utils::Inreplace.inreplace formula.path do |s|
if s.inreplace_string.scan(inreplace_regex).length > 1
if T.must(s.inreplace_string.split(/^ test do\b/, 2).first).scan(inreplace_regex).length > 1
Copy link
Member

Choose a reason for hiding this comment

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

Is it genuinely impossible that the T.must won't be truthy here?

Copy link
Member Author

@cho-m cho-m Mar 4, 2024

Choose a reason for hiding this comment

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

The only way I can think of is if the Formula file is entirely blank. In this case, s.inreplace_string == "" and that will lead to nil:

brew ruby -e 'p "".split(/^  test do\b/, 2).first'
nil

If the file doesn't exist, brew fails earlier due to File.binread. If the file has at least a single character then it will return a String:

brew ruby -e 'p " ".split(/^  test do\b/, 2).first'
" "

I didn't think that would happen, but if there is an odd situation which allows for it then I can change check to something safer, e.g.

Suggested change
if T.must(s.inreplace_string.split(/^ test do\b/, 2).first).scan(inreplace_regex).length > 1
formula_contents_without_test = s.inreplace_string.split(/^ test do\b/, 2).first
if formula_contents_without_test.nil? || formula_contents_without_test.scan(inreplace_regex).length > 1
Suggested change
if T.must(s.inreplace_string.split(/^ test do\b/, 2).first).scan(inreplace_regex).length > 1
if s.inreplace_string.split(/^ test do\b/, 2).first&.scan(inreplace_regex)&.length.to_i > 1

odie "Unable to update resource blocks for \"#{formula.name}\" automatically. Please update them manually."
end
s.sub! inreplace_regex, new_resource_blocks
Expand Down