audit: add autocorrect and tests for test checks

This commit is contained in:
Jonathan Chang 2018-08-16 12:42:45 -04:00
parent 5f981a8722
commit ca59377a90
2 changed files with 36 additions and 0 deletions

View File

@ -39,6 +39,17 @@ module RuboCop
end end
end end
def autocorrect(node)
lambda do |corrector|
case node.type
when :str, :dstr
corrector.replace(node.source_range, node.source.to_s.sub(%r{(/usr/local/(s?bin))}, '#{\2}'))
when :int
corrector.remove(range_with_surrounding_comma(range_with_surrounding_space(range: node.source_range, side: :left)))
end
end
end
def_node_search :test_calls, <<~EOS def_node_search :test_calls, <<~EOS
(send nil? ${:system :shell_output :pipe_output} $...) (send nil? ${:system :shell_output :pipe_output} $...)
EOS EOS

View File

@ -76,6 +76,31 @@ describe RuboCop::Cop::FormulaAudit::TestCalls do
end end
RUBY RUBY
end end
it "supports auto-correcting test calls" do
source = <<~RUBY
class Foo < Formula
url 'https://example.com/foo-1.0.tgz'
test do
shell_output("/usr/local/sbin/test", 0)
end
end
RUBY
corrected_source = <<~RUBY
class Foo < Formula
url 'https://example.com/foo-1.0.tgz'
test do
shell_output("\#{sbin}/test")
end
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
end end
describe RuboCop::Cop::FormulaAuditStrict::Test do describe RuboCop::Cop::FormulaAuditStrict::Test do