Skip to content
Closed
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
11 changes: 11 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ PreCommit:
install_command: 'pip install pep8'
include: '**/*.py'

PuppetLint:
enabled: false
description: 'Analyzing with puppet-lint'
required_executable: 'puppet-lint'
install_command: 'gem install puppet-lint'
flags:
- '--log-format="%{fullpath}:%{line}:%{column}:%{KIND}: %{message} (%{check})"'
- '--fail-on-warnings'
- '--error-level=all'
include: '**/*.pp'

Pyflakes:
enabled: false
description: 'Analyzing with pyflakes'
Expand Down
24 changes: 24 additions & 0 deletions lib/overcommit/hook/pre_commit/puppet_lint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Overcommit::Hook::PreCommit
# Runs 'puppet-lint' against any modified Puppet files.
#
# @see http://puppet-lint.com/
class PuppetLint < Base
MESSAGE_REGEX = /(?<file>.+):(?<line>\d+):\d+:(?<type>\w+)/

MESSAGE_TYPE_CATEGORIZER = lambda do |type|
type == 'ERROR' ? :error : :warning
end

def run
result = execute(command + applicable_files)
output = result.stdout.chomp.gsub(/^"|"$/, '')
return :pass if result.success? && output.empty?

extract_messages(
output.split("\n"),
MESSAGE_REGEX,
MESSAGE_TYPE_CATEGORIZER
)
end
end
end
67 changes: 67 additions & 0 deletions spec/overcommit/hook/pre_commit/puppet_lint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::PuppetLint 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.pp file2.pp])
end

context 'when puppet-lint exits successfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
end

context 'with no output' do
before do
result.stub(:stdout).and_return('')
end

it { should pass }
end

context 'and it reports a warning' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
file1.pp:2:10:WARNING: selector inside resource block (selector_inside_resource)'
OUT
end

it { should warn }
end
end

context 'when puppet-lint exits unsuccessfully' 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(normalize_indent(<<-OUT))
file1.pp:2:10:WARNING: selector inside resource block (selector_inside_resource)'
OUT
end

it { should warn }
end

context 'and it reports an error' do
before do
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
file1.pp:2:10:ERROR: trailing whitespace (trailing_whitespace)'
OUT
end

it { should fail_hook }
end
end
end