Skip to content

Commit

Permalink
Add autocorrection for the interpolated bin audit
Browse files Browse the repository at this point in the history
- I got bored doing them manually.
- Also now more people can help with letters of the alphabet using `brew style --only=FormulaAuditStrict/Text --fix homebrew/core`.
  • Loading branch information
issyl0 committed Aug 1, 2024
1 parent 53e3632 commit 7bf7030
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Library/Homebrew/rubocops/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def audit_formula(formula_nodes)
module FormulaAuditStrict
# This cop contains stricter checks for various problems in a formula's source code.
class Text < FormulaCop
extend AutoCorrector

sig { override.params(formula_nodes: FormulaNodes).void }
def audit_formula(formula_nodes)
return if (body_node = formula_nodes.body_node).nil?
Expand All @@ -140,7 +142,9 @@ def audit_formula(formula_nodes)
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}\"`"
problem "Use `bin/\"#{cmd}\"` instead of `\"\#{bin}/#{cmd}\"`" do |corrector|

This comment has been minimized.

Copy link
@ReenigneArcher

ReenigneArcher Aug 3, 2024

Following this suggestion makes it unable to execute.

I get the following error: Failed to execute: bin/sunshine

Am I misunderstand what change to make? LizardByte/Sunshine#2953

This comment has been minimized.

Copy link
@carlocab

carlocab Aug 3, 2024

Member

Try

bin/"sunshine"

This comment has been minimized.

Copy link
@ReenigneArcher

ReenigneArcher Aug 3, 2024

Thank you, that worked... and now that I re-read the message, it's perfectly clear.

corrector.replace(bin_node.loc.expression, "bin/\"#{cmd}\"")
end
end

return if formula_tap != "homebrew-core"
Expand Down
19 changes: 14 additions & 5 deletions Library/Homebrew/test/rubocops/text/strict_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,23 @@ def install
RUBY
end

it 'reports an offense if "\#{bin}/<formula_name>" or other dashed binaries too are present' do
it 'reports an offense & autocorrects 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"`
ohai "\#{bin}/foo-bar", "-v"
^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"`
system "\#{bin}/foo", "-v"
^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"`
system "\#{bin}/foo-bar", "-v"
^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"`
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
test do
system bin/"foo", "-v"
system bin/"foo-bar", "-v"
end
end
RUBY
Expand Down

0 comments on commit 7bf7030

Please sign in to comment.