diff --git a/Library/Homebrew/rubocops/lines.rb b/Library/Homebrew/rubocops/lines.rb index ca62165b9d..0626fc346b 100644 --- a/Library/Homebrew/rubocops/lines.rb +++ b/Library/Homebrew/rubocops/lines.rb @@ -267,7 +267,7 @@ module RuboCop shell_metacharacters = %w[> < < | ; : & * $ ? : ~ + @ !` ( ) [ ]] find_every_method_call_by_name(body_node, :system).each do |method| - # Continue if a shell metacharacter is present + # Only separate when no shell metacharacters are present next if shell_metacharacters.any? { |meta| string_content(parameters(method).first).include?(meta) } next unless match = regex_match_group(parameters(method).first, shell_cmd_with_spaces_regex) @@ -281,7 +281,7 @@ module RuboCop find_instance_method_call(body_node, "Utils", command) do |method| index = parameters(method).first.hash_type? ? 1 : 0 - # Continue if a shell metacharacter is present + # Only separate when no shell metacharacters are present next if shell_metacharacters.any? { |meta| string_content(parameters(method)[index]).include?(meta) } next unless match = regex_match_group(parameters(method)[index], shell_cmd_with_spaces_regex) diff --git a/Library/Homebrew/test/rubocops/lines_spec.rb b/Library/Homebrew/test/rubocops/lines_spec.rb index 395c3de540..370bea30a9 100644 --- a/Library/Homebrew/test/rubocops/lines_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_spec.rb @@ -715,6 +715,111 @@ describe RuboCop::Cop::FormulaAudit::ShellCommands do end RUBY end + + it "separates shell commands in system" do + source = <<~RUBY + class Foo < Formula + def install + system "foo bar" + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + def install + system "foo", "bar" + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end + + it "separates shell commands with string interpolation in system" do + source = <<~RUBY + class Foo < Formula + def install + system "\#{foo}/bar baz" + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + def install + system "\#{foo}/bar", "baz" + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end + + it "separates shell commands in Utils.popen_read" do + source = <<~RUBY + class Foo < Formula + def install + Utils.popen_read("foo bar") + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + def install + Utils.popen_read("foo", "bar") + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end + + it "separates shell commands with string interpolation in Utils.popen_read" do + source = <<~RUBY + class Foo < Formula + def install + Utils.popen_read("\#{foo}/bar baz") + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + def install + Utils.popen_read("\#{foo}/bar", "baz") + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end + + it "separates shell commands following a shell variable in Utils.popen_read" do + source = <<~RUBY + class Foo < Formula + def install + Utils.popen_read({ "SHELL" => "bash" }, "foo bar") + end + end + RUBY + + corrected_source = <<~RUBY + class Foo < Formula + def install + Utils.popen_read({ "SHELL" => "bash" }, "foo", "bar") + end + end + RUBY + + new_source = autocorrect_source(source) + expect(new_source).to eq(corrected_source) + end end end