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

rubocops/text: Enforce bin/"formula" instead of "#{bin}/formula" #17826

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Library/Homebrew/rubocops/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def audit_formula(formula_nodes)
problem "Use `\#{pkgshare}` instead of `\#{share}/#{@formula_name}`"
end

interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node|
offending_node(bin_node)
cmd = bin_node.source.match(%r{\#{bin}/(\S+)})[1]&.delete_suffix('"') || @formula_name
problem "Use `bin/\"#{cmd}\"` instead of `\"\#{bin}/#{cmd}\"`"
end

return if formula_tap != "homebrew-core"

find_method_with_args(body_node, :env, :std) do
Expand All @@ -145,15 +151,26 @@ def audit_formula(formula_nodes)
end

# Check whether value starts with the formula name and then a "/", " " or EOS.
def path_starts_with?(path, starts_with)
path.match?(%r{^#{Regexp.escape(starts_with)}(/| |$)})
# If we're checking for "#{bin}", we also check for "-" since similar binaries also don't need interpolation.
def path_starts_with?(path, starts_with, bin: false)
ending = bin ? "/| |-|$" : "/| |$"
path.match?(/^#{Regexp.escape(starts_with)}(#{ending})/)
end

def path_starts_with_bin?(path, starts_with)
path_starts_with?(path, starts_with, bin: true)
end

# Find "#{share}/foo"
def_node_search :interpolated_share_path_starts_with, <<~EOS
$(dstr (begin (send nil? :share)) (str #path_starts_with?(%1)))
EOS

# Find "#{bin}/foo" and "#{bin}/foo-bar"
def_node_search :interpolated_bin_path_starts_with, <<~EOS
$(dstr (begin (send nil? :bin)) (str #path_starts_with_bin?(%1)))
EOS

# Find share/"foo"
def_node_search :share_path_starts_with, <<~EOS
$(send (send nil? :share) :/ (str #path_starts_with?(%1)))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Library/Homebrew/test/rubocops/text/strict_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,29 @@ def install
end
RUBY
end

it 'reports an offense if "\#{bin}/<formula_name>" or other dashed binaries too are present' do
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
class Foo < Formula
test do
ohai "\#{bin}/foo", "-v"
^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"`
issyl0 marked this conversation as resolved.
Show resolved Hide resolved
ohai "\#{bin}/foo-bar", "-v"
^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"`
end
end
RUBY
end

it 'reports an offense if "\#{bin}" is in a `shell_output` string' do
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
class Foo < Formula
test do
shell_output("\#{bin}/foo --version")
^^^^^^^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"`
end
end
RUBY
end
end
end
Loading