diff --git a/lib/overcommit/hook/shared/pronto.rb b/lib/overcommit/hook/shared/pronto.rb index b1d71442..9975fbc6 100644 --- a/lib/overcommit/hook/shared/pronto.rb +++ b/lib/overcommit/hook/shared/pronto.rb @@ -1,19 +1,23 @@ # frozen_string_literal: true module Overcommit::Hook::Shared - # Shared code used by all Pronto hooks. Runs pronto linter. + # Shared code used by all Pronto hooks. Runs pronto linters. + + # @see https://github.com/prontolabs/pronto module Pronto MESSAGE_TYPE_CATEGORIZER = lambda do |type| type.include?('E') ? :error : :warning end + MESSAGE_REGEX = /^(?(?:\w:)?[^:]+):(?\d+) (?[^ ]+)/.freeze + def run result = execute(command) return :pass if result.success? extract_messages( - result.stdout.split("\n"), - /^(?(?:\w:)?[^:]+):(?\d+) (?[^ ]+)/, + result.stdout.split("\n").select { |line| line.match?(MESSAGE_REGEX) }, + MESSAGE_REGEX, MESSAGE_TYPE_CATEGORIZER, ) end diff --git a/spec/overcommit/hook/pre_commit/pronto_spec.rb b/spec/overcommit/hook/pre_commit/pronto_spec.rb index ed099ace..7a050d10 100644 --- a/spec/overcommit/hook/pre_commit/pronto_spec.rb +++ b/spec/overcommit/hook/pre_commit/pronto_spec.rb @@ -41,10 +41,15 @@ context 'and it reports a warning' do before do - result.stub(:stdout).and_return([ - 'file1.rb:12 W: Line is too long. [107/80]', - 'file2.rb:14 I: Prefer single-quoted strings' - ].join("\n")) + result.stub(:stdout).and_return <<~MESSAGE + Running Pronto::Rubocop + file1.rb:12 W: Line is too long. [107/80] + file2.rb:14 I: Prefer single-quoted strings + + ```suggestion + x = 'x' + ``` + MESSAGE end it { should warn } diff --git a/spec/overcommit/hook/pre_push/pronto_spec.rb b/spec/overcommit/hook/pre_push/pronto_spec.rb index 2851a860..3ca660e5 100644 --- a/spec/overcommit/hook/pre_push/pronto_spec.rb +++ b/spec/overcommit/hook/pre_push/pronto_spec.rb @@ -41,10 +41,15 @@ context 'and it reports a warning' do before do - result.stub(:stdout).and_return([ - 'file1.rb:12 W: Line is too long. [107/80]', - 'file2.rb:14 I: Prefer single-quoted strings' - ].join("\n")) + result.stub(:stdout).and_return <<~MESSAGE + Running Pronto::Rubocop + file1.rb:12 W: Line is too long. [107/80] + file2.rb:14 I: Prefer single-quoted strings + + ```suggestion + x = 'x' + ``` + MESSAGE end it { should warn }