From 73976683a1e70e2fe45b2968fedd5e22a5280f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20Moz=C5=ABras?= Date: Sun, 23 Apr 2017 21:00:21 +0300 Subject: [PATCH] Add Pronto pre-commit hook --- CHANGELOG.md | 1 + config/default.yml | 7 +++ lib/overcommit/hook/pre_commit/pronto.rb | 21 ++++++++ .../overcommit/hook/pre_commit/pronto_spec.rb | 51 +++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 lib/overcommit/hook/pre_commit/pronto.rb create mode 100644 spec/overcommit/hook/pre_commit/pronto_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e299fa2..d80163b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## master +* Add [`Pronto`](https://github.com/mmozuras/pronto) pre-commit hook * Add [`hadolint`](https://github.com/lukasmartinelli/hadolint) pre-commit hook * Use the `core.hooksPath` Git configuration option when installing hooks diff --git a/config/default.yml b/config/default.yml index c010ee81..177e6172 100644 --- a/config/default.yml +++ b/config/default.yml @@ -430,6 +430,13 @@ PreCommit: install_command: 'pip install pep8' include: '**/*.py' + Pronto: + enabled: false + description: 'Analyzing with pronto' + required_executable: 'pronto' + install_command: 'gem install pronto' + flags: ['run', '--staged --exit-code'] + PuppetLint: enabled: false description: 'Analyze with puppet-lint' diff --git a/lib/overcommit/hook/pre_commit/pronto.rb b/lib/overcommit/hook/pre_commit/pronto.rb new file mode 100644 index 00000000..29b588cd --- /dev/null +++ b/lib/overcommit/hook/pre_commit/pronto.rb @@ -0,0 +1,21 @@ +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 + end +end diff --git a/spec/overcommit/hook/pre_commit/pronto_spec.rb b/spec/overcommit/hook/pre_commit/pronto_spec.rb new file mode 100644 index 00000000..635435bc --- /dev/null +++ b/spec/overcommit/hook/pre_commit/pronto_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe Overcommit::Hook::PreCommit::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