Add tests for autocorrect

This commit is contained in:
Rylan Polster 2020-07-05 12:12:36 -04:00
parent 35a8c33690
commit 0786003fe9
2 changed files with 107 additions and 2 deletions

View File

@ -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)

View File

@ -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