Skip to content

Commit

Permalink
Add pronto to pre_push hooks (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasjurcys authored Mar 8, 2020
1 parent dc50788 commit 16eded3
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ coverage/
pkg/
.bundle
.idea
.history/
.vscode/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
17 changes: 3 additions & 14 deletions lib/overcommit/hook/pre_commit/pronto.rb
Original file line number Diff line number Diff line change
@@ -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"),
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+) (?<type>[^ ]+)/,
MESSAGE_TYPE_CATEGORIZER,
)
end
include Overcommit::Hook::Shared::Pronto
end
end
5 changes: 5 additions & 0 deletions lib/overcommit/hook/pre_push/base.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/overcommit/hook/pre_push/pronto.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions lib/overcommit/hook/shared/pronto.rb
Original file line number Diff line number Diff line change
@@ -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"),
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+) (?<type>[^ ]+)/,
MESSAGE_TYPE_CATEGORIZER,
)
end
end
end
53 changes: 53 additions & 0 deletions spec/overcommit/hook/pre_push/pronto_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 16eded3

Please sign in to comment.