diff --git a/Library/Homebrew/rubocops/class_cop.rb b/Library/Homebrew/rubocops/class_cop.rb index 0976104752..3b667e75c8 100644 --- a/Library/Homebrew/rubocops/class_cop.rb +++ b/Library/Homebrew/rubocops/class_cop.rb @@ -39,6 +39,17 @@ module RuboCop 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 (send nil? ${:system :shell_output :pipe_output} $...) EOS diff --git a/Library/Homebrew/test/rubocops/class_cop_spec.rb b/Library/Homebrew/test/rubocops/class_cop_spec.rb index 469fbe8e5b..7ada0bebd3 100644 --- a/Library/Homebrew/test/rubocops/class_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/class_cop_spec.rb @@ -76,6 +76,31 @@ describe RuboCop::Cop::FormulaAudit::TestCalls do end RUBY 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 describe RuboCop::Cop::FormulaAuditStrict::Test do