Skip to content

Commit bba3822

Browse files
jawshooahsds
authored andcommitted
Add pre-commit hook for puppet-lint
Add spec for PuppetLint pre-commit hook
1 parent 7234a80 commit bba3822

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

config/default.yml

+11
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ PreCommit:
325325
install_command: 'pip install pep8'
326326
include: '**/*.py'
327327

328+
PuppetLint:
329+
enabled: false
330+
description: 'Analyzing with puppet-lint'
331+
required_executable: 'puppet-lint'
332+
install_command: 'gem install puppet-lint'
333+
flags:
334+
- '--log-format="%{fullpath}:%{line}:%{column}:%{KIND}: %{message} (%{check})"'
335+
- '--fail-on-warnings'
336+
- '--error-level=all'
337+
include: '**/*.pp'
338+
328339
Pyflakes:
329340
enabled: false
330341
description: 'Analyzing with pyflakes'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Overcommit::Hook::PreCommit
2+
# Runs 'puppet-lint' against any modified Puppet files.
3+
#
4+
# @see http://puppet-lint.com/
5+
class PuppetLint < Base
6+
MESSAGE_REGEX = /(?<file>.+):(?<line>\d+):\d+:(?<type>\w+)/
7+
8+
MESSAGE_TYPE_CATEGORIZER = lambda do |type|
9+
type == 'ERROR' ? :error : :warning
10+
end
11+
12+
def run
13+
result = execute(command + applicable_files)
14+
output = result.stdout.chomp.gsub(/^"|"$/, '')
15+
return :pass if result.success? && output.empty?
16+
17+
extract_messages(
18+
output.split("\n"),
19+
MESSAGE_REGEX,
20+
MESSAGE_TYPE_CATEGORIZER
21+
)
22+
end
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::PuppetLint do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:applicable_files).and_return(%w[file1.pp file2.pp])
10+
end
11+
12+
context 'when puppet-lint exits successfully' do
13+
let(:result) { double('result') }
14+
15+
before do
16+
result.stub(:success?).and_return(true)
17+
subject.stub(:execute).and_return(result)
18+
end
19+
20+
context 'with no output' do
21+
before do
22+
result.stub(:stdout).and_return('')
23+
end
24+
25+
it { should pass }
26+
end
27+
28+
context 'and it reports a warning' do
29+
before do
30+
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
31+
file1.pp:2:10:WARNING: selector inside resource block (selector_inside_resource)'
32+
OUT
33+
end
34+
35+
it { should warn }
36+
end
37+
end
38+
39+
context 'when puppet-lint exits unsuccessfully' do
40+
let(:result) { double('result') }
41+
42+
before do
43+
result.stub(:success?).and_return(false)
44+
subject.stub(:execute).and_return(result)
45+
end
46+
47+
context 'and it reports a warning' do
48+
before do
49+
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
50+
file1.pp:2:10:WARNING: selector inside resource block (selector_inside_resource)'
51+
OUT
52+
end
53+
54+
it { should warn }
55+
end
56+
57+
context 'and it reports an error' do
58+
before do
59+
result.stub(:stdout).and_return(normalize_indent(<<-OUT))
60+
file1.pp:2:10:ERROR: trailing whitespace (trailing_whitespace)'
61+
OUT
62+
end
63+
64+
it { should fail_hook }
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)