Add tests for autocorrect
This commit is contained in:
parent
35a8c33690
commit
0786003fe9
@ -267,7 +267,7 @@ module RuboCop
|
|||||||
shell_metacharacters = %w[> < < | ; : & * $ ? : ~ + @ !` ( ) [ ]]
|
shell_metacharacters = %w[> < < | ; : & * $ ? : ~ + @ !` ( ) [ ]]
|
||||||
|
|
||||||
find_every_method_call_by_name(body_node, :system).each do |method|
|
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 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)
|
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|
|
find_instance_method_call(body_node, "Utils", command) do |method|
|
||||||
index = parameters(method).first.hash_type? ? 1 : 0
|
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 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)
|
next unless match = regex_match_group(parameters(method)[index], shell_cmd_with_spaces_regex)
|
||||||
|
@ -715,6 +715,111 @@ describe RuboCop::Cop::FormulaAudit::ShellCommands do
|
|||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user