Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ PreCommit:
install_command: 'npm install -g coffeelint'
include: '**/*.coffee'

CookStyle:
enabled: false
description: 'Analyze with CookStyle'
required_executable: 'cookstyle'
flags: ['--format=emacs', '--force-exclusion', '--display-cop-names']
install_command: 'gem install cookstyle'
include:
- '**/*.rb'
- '**/*.erb'

Credo:
enabled: false
description: 'Analyze with credo'
Expand Down
35 changes: 35 additions & 0 deletions lib/overcommit/hook/pre_commit/cook_style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Overcommit::Hook::PreCommit
# Runs `cookstyle` against any modified Chef Ruby files.
#
# @see https://docs.chef.io/cookstyle.html
class CookStyle < Base
GENERIC_MESSAGE_TYPE_CATEGORIZER = lambda do |type|
type =~ /^warn/ ? :warning : :error
end

COP_MESSAGE_TYPE_CATEGORIZER = lambda do |type|
type.include?('W') ? :warning : :error
end

def run
result = execute(command, args: applicable_files)
return :pass if result.success?

generic_messages = extract_messages(
result.stderr.split("\n"),
/^(?<type>[a-z]+)/i,
GENERIC_MESSAGE_TYPE_CATEGORIZER,
)

cop_messages = extract_messages(
result.stdout.split("\n"),
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+):[^ ]+ (?<type>[^ ]+)/,
COP_MESSAGE_TYPE_CATEGORIZER,
)

generic_messages + cop_messages
end
end
end
102 changes: 102 additions & 0 deletions spec/overcommit/hook/pre_commit/cook_style_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

require 'spec_helper'

describe Overcommit::Hook::PreCommit::CookStyle 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 cookstyle exits successfully' do
let(:result) { double('result') }

before do
result.stub(success?: true, stderr: '', stdout: '')
subject.stub(:execute).and_return(result)
end

it { should pass }

context 'and it printed warnings to stderr' do
before do
result.stub(:stderr).and_return(normalize_indent(<<-MSG))
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.8-compliant syntax, but you are running 2.1.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
MSG
end

it { should pass }
end
end

context 'when cookstyle 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 a warning' do
before do
result.stub(:stdout).and_return([
'file1.rb:1:1: W: Useless assignment to variable - my_var.',
].join("\n"))
result.stub(:stderr).and_return('')
end

it { should warn }

context 'and it printed warnings to stderr' do
before do
result.stub(:stderr).and_return(normalize_indent(<<-MSG))
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.8-compliant syntax, but you are running 2.1.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
MSG
end

it { should warn }
end
end

context 'and it reports an error' do
before do
result.stub(:stdout).and_return([
'file1.rb:1:1: C: Missing top-level class documentation',
].join("\n"))
result.stub(:stderr).and_return('')
end

it { should fail_hook }

context 'and it printed warnings to stderr' do
before do
result.stub(:stderr).and_return(normalize_indent(<<-MSG))
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.8-compliant syntax, but you are running 2.1.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
MSG
end

it { should fail_hook }
end
end

context 'when a generic error message is written to stderr' do
before do
result.stub(:stdout).and_return('')
result.stub(:stderr).and_return([
'Could not find cookstyle in any of the sources'
].join("\n"))
end

it { should fail_hook }
end
end
end