From 574fac4c6a4368ee42e1fcfb0281ad7d8d9eb4bd Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Thu, 16 Jul 2015 00:07:02 -0400 Subject: [PATCH] Add pre-commit hook for puppet-lint Add spec for PuppetLint pre-commit hook --- config/default.yml | 11 +++ lib/overcommit/hook/pre_commit/puppet_lint.rb | 24 +++++++ .../hook/pre_commit/puppet_lint_spec.rb | 67 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 lib/overcommit/hook/pre_commit/puppet_lint.rb create mode 100644 spec/overcommit/hook/pre_commit/puppet_lint_spec.rb diff --git a/config/default.yml b/config/default.yml index 94a749a5..5e2cc8e3 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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' diff --git a/lib/overcommit/hook/pre_commit/puppet_lint.rb b/lib/overcommit/hook/pre_commit/puppet_lint.rb new file mode 100644 index 00000000..6dddd704 --- /dev/null +++ b/lib/overcommit/hook/pre_commit/puppet_lint.rb @@ -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 = /(?.+):(?\d+):\d+:(?\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 diff --git a/spec/overcommit/hook/pre_commit/puppet_lint_spec.rb b/spec/overcommit/hook/pre_commit/puppet_lint_spec.rb new file mode 100644 index 00000000..94a0fa7e --- /dev/null +++ b/spec/overcommit/hook/pre_commit/puppet_lint_spec.rb @@ -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