Skip to content

Commit

Permalink
rubocops/text: Allow bin interpolation inside word arrays
Browse files Browse the repository at this point in the history
- We discovered that the following syntax in the formula `sqlsmith`
  should actually be OK because the `\n` is like whitespace.

```ruby
cmd = %W[
  #{bin}/sqlsmith
  --threads=4
  --timeout=10
]
shell_output(cmd)
```
  • Loading branch information
issyl0 authored and ctaintor committed Sep 4, 2024
1 parent 8c3eed6 commit 49e7391
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Library/Homebrew/rubocops/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def audit_formula(formula_nodes)
end

interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node|
next if bin_node.ancestors.any?(&:array_type?)

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}\"`" do |corrector|
Expand Down
60 changes: 38 additions & 22 deletions Library/Homebrew/test/rubocops/text/strict_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,51 @@ def install
RUBY
end

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
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"`
context "for interpolated bin paths" 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
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
end
RUBY

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

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

it 'does not report an offense if \#{bin}/foo and then a space and more text' do
expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb")
class Foo < Formula
test do
shell_output("\#{bin}/foo --version")
assert_match "help", shell_output("\#{bin}/foo-something --help 2>&1")
assert_match "OK", shell_output("\#{bin}/foo-something_else --check 2>&1")
end
end
RUBY
end
end

it 'does not report an offense if \#{bin}/foo and then a space and more text' do
it 'does not report an offense if "\#{bin}/foo" is in a word array' do
expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb")
class Foo < Formula
test do
shell_output("\#{bin}/foo --version")
assert_match "help", shell_output("\#{bin}/foo-something --help 2>&1")
assert_match "OK", shell_output("\#{bin}/foo-something_else --check 2>&1")
cmd = %W[
\#{bin}/foo
version
]
assert_match version.to_s, shell_output(cmd)
end
end
RUBY
Expand Down

0 comments on commit 49e7391

Please sign in to comment.