From 16eded31008dcb050d36534c970f3c28994afb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Povilas=20Jur=C4=8Dys?= Date: Sun, 8 Mar 2020 06:01:35 +0200 Subject: [PATCH] Add pronto to pre_push hooks (#706) --- .gitignore | 2 + README.md | 1 + config/default.yml | 7 +++ lib/overcommit/hook/pre_commit/pronto.rb | 17 ++----- lib/overcommit/hook/pre_push/base.rb | 5 ++ lib/overcommit/hook/pre_push/pronto.rb | 12 +++++ lib/overcommit/hook/shared/pronto.rb | 21 ++++++++ spec/overcommit/hook/pre_push/pronto_spec.rb | 53 ++++++++++++++++++++ 8 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 lib/overcommit/hook/pre_push/pronto.rb create mode 100644 lib/overcommit/hook/shared/pronto.rb create mode 100644 spec/overcommit/hook/pre_push/pronto_spec.rb diff --git a/.gitignore b/.gitignore index 2bb7f841..7ea8f1f7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ coverage/ pkg/ .bundle .idea +.history/ +.vscode/ diff --git a/README.md b/README.md index f1076df6..7f996177 100644 --- a/README.md +++ b/README.md @@ -582,6 +582,7 @@ aborted. * [Brakeman](lib/overcommit/hook/pre_push/brakeman.rb) * [Minitest](lib/overcommit/hook/pre_push/minitest.rb) * [PhpUnit](lib/overcommit/hook/pre_push/php_unit.rb) +* [Pronto](lib/overcommit/hook/pre_push/pronto.rb) * [ProtectedBranches](lib/overcommit/hook/pre_push/protected_branches.rb) * [Pytest](lib/overcommit/hook/pre_push/pytest.rb) * [PythonNose](lib/overcommit/hook/pre_push/python_nose.rb) diff --git a/config/default.yml b/config/default.yml index 5a6887f4..74503e8e 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1297,6 +1297,13 @@ PrePush: flags: ['--bootstrap', 'vendor/autoload.php', 'tests'] install_command: 'composer require --dev phpunit/phpunit' + Pronto: + enabled: false + description: 'Analyzing with pronto' + required_executable: 'pronto' + install_command: 'gem install pronto' + flags: ['run', '--exit-code'] + ProtectedBranches: enabled: false description: 'Check for illegal pushes to protected branches' diff --git a/lib/overcommit/hook/pre_commit/pronto.rb b/lib/overcommit/hook/pre_commit/pronto.rb index da016e8f..664b4ef8 100644 --- a/lib/overcommit/hook/pre_commit/pronto.rb +++ b/lib/overcommit/hook/pre_commit/pronto.rb @@ -1,23 +1,12 @@ # frozen_string_literal: true +require 'overcommit/hook/shared/pronto' + module Overcommit::Hook::PreCommit # Runs `pronto` # # @see https://github.com/mmozuras/pronto class Pronto < Base - MESSAGE_TYPE_CATEGORIZER = lambda do |type| - type.include?('E') ? :error : :warning - end - - def run - result = execute(command) - return :pass if result.success? - - extract_messages( - result.stdout.split("\n"), - /^(?(?:\w:)?[^:]+):(?\d+) (?[^ ]+)/, - MESSAGE_TYPE_CATEGORIZER, - ) - end + include Overcommit::Hook::Shared::Pronto end end diff --git a/lib/overcommit/hook/pre_push/base.rb b/lib/overcommit/hook/pre_push/base.rb index 72c89d0c..35e2b3a8 100644 --- a/lib/overcommit/hook/pre_push/base.rb +++ b/lib/overcommit/hook/pre_push/base.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'forwardable' +require 'overcommit/utils/messages_utils' module Overcommit::Hook::PrePush # Functionality common to all pre-push hooks. @@ -17,6 +18,10 @@ def run? private + def extract_messages(*args) + Overcommit::Utils::MessagesUtils.extract_messages(*args) + end + def exclude_remotes @config['exclude_remotes'] || [] end diff --git a/lib/overcommit/hook/pre_push/pronto.rb b/lib/overcommit/hook/pre_push/pronto.rb new file mode 100644 index 00000000..93a0724e --- /dev/null +++ b/lib/overcommit/hook/pre_push/pronto.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'overcommit/hook/shared/pronto' + +module Overcommit::Hook::PrePush + # Runs `pronto` + # + # @see https://github.com/mmozuras/pronto + class Pronto < Base + include Overcommit::Hook::Shared::Pronto + end +end diff --git a/lib/overcommit/hook/shared/pronto.rb b/lib/overcommit/hook/shared/pronto.rb new file mode 100644 index 00000000..b1d71442 --- /dev/null +++ b/lib/overcommit/hook/shared/pronto.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Overcommit::Hook::Shared + # Shared code used by all Pronto hooks. Runs pronto linter. + module Pronto + MESSAGE_TYPE_CATEGORIZER = lambda do |type| + type.include?('E') ? :error : :warning + end + + def run + result = execute(command) + return :pass if result.success? + + extract_messages( + result.stdout.split("\n"), + /^(?(?:\w:)?[^:]+):(?\d+) (?[^ ]+)/, + MESSAGE_TYPE_CATEGORIZER, + ) + end + end +end diff --git a/spec/overcommit/hook/pre_push/pronto_spec.rb b/spec/overcommit/hook/pre_push/pronto_spec.rb new file mode 100644 index 00000000..2851a860 --- /dev/null +++ b/spec/overcommit/hook/pre_push/pronto_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Overcommit::Hook::PrePush::Pronto do + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + let(:context) { double('context') } + subject { described_class.new(config, context) } + + before do + subject.stub(:applicable_files).and_return(%w[file1.rb file2.rb]) + end + + context 'when pronto exits successfully' do + before do + result = double('result') + result.stub(:success?).and_return(true) + subject.stub(:execute).and_return(result) + end + + it { should pass } + end + + context 'when pronto exits unsucessfully' do + let(:result) { double('result') } + + before do + result.stub(:success?).and_return(false) + subject.stub(:execute).and_return(result) + end + + context 'and it reports an error' do + before do + result.stub(:stdout).and_return([ + 'file2.rb:10 E: IDENTICAL code found in :iter.', + ].join("\n")) + end + + it { should fail_hook } + end + + 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")) + end + + it { should warn } + end + end +end