rubocops/text: Allow bin interpolation inside word arrays

- 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)
```
This commit is contained in:
Issy Long 2024-08-04 23:38:11 +01:00
parent 9279693a34
commit cb8769c2a0
No known key found for this signature in database
2 changed files with 39 additions and 21 deletions

View File

@ -140,6 +140,8 @@ module RuboCop
end end
interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node| interpolated_bin_path_starts_with(body_node, "/#{@formula_name}") do |bin_node|
next if bin_node.ancestors.any?(&:array_type?)
offending_node(bin_node) offending_node(bin_node)
cmd = bin_node.source.match(%r{\#{bin}/(\S+)})[1]&.delete_suffix('"') || @formula_name cmd = bin_node.source.match(%r{\#{bin}/(\S+)})[1]&.delete_suffix('"') || @formula_name
problem "Use `bin/\"#{cmd}\"` instead of `\"\#{bin}/#{cmd}\"`" do |corrector| problem "Use `bin/\"#{cmd}\"` instead of `\"\#{bin}/#{cmd}\"`" do |corrector|

View File

@ -132,35 +132,51 @@ RSpec.describe RuboCop::Cop::FormulaAuditStrict::Text do
RUBY RUBY
end end
it 'reports an offense & autocorrects if "\#{bin}/<formula_name>" or other dashed binaries too are present' do context "for interpolated bin paths" do
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") it 'reports an offense & autocorrects if "\#{bin}/<formula_name>" or other dashed binaries too are present' do
class Foo < Formula expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
test do class Foo < Formula
system "\#{bin}/foo", "-v" test do
^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"` system "\#{bin}/foo", "-v"
system "\#{bin}/foo-bar", "-v" ^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo"` instead of `"\#{bin}/foo"`
^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"` system "\#{bin}/foo-bar", "-v"
^^^^^^^^^^^^^^^^ FormulaAuditStrict/Text: Use `bin/"foo-bar"` instead of `"\#{bin}/foo-bar"`
end
end end
end RUBY
RUBY
expect_correction(<<~RUBY) expect_correction(<<~RUBY)
class Foo < Formula class Foo < Formula
test do test do
system bin/"foo", "-v" system bin/"foo", "-v"
system bin/"foo-bar", "-v" system bin/"foo-bar", "-v"
end
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 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") expect_no_offenses(<<~RUBY, "/homebrew-core/Formula/foo.rb")
class Foo < Formula class Foo < Formula
test do test do
shell_output("\#{bin}/foo --version") cmd = %W[
assert_match "help", shell_output("\#{bin}/foo-something --help 2>&1") \#{bin}/foo
assert_match "OK", shell_output("\#{bin}/foo-something_else --check 2>&1") version
]
assert_match version.to_s, shell_output(cmd)
end end
end end
RUBY RUBY