diff --git a/CHANGELOG.md b/CHANGELOG.md index 79c15e8e..1b6b96e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## master (unreleased) +* Add [`metadata-json-lint`](https://voxpupuli.org/blog/2014/11/06/linting-metadata-json/) pre-commit hook * Fix `LineEndings` pre-commit hook handling of file paths with spaces ## 0.41.0 diff --git a/README.md b/README.md index 0a62737f..b311322a 100644 --- a/README.md +++ b/README.md @@ -523,6 +523,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details. * [PhpCs](lib/overcommit/hook/pre_commit/php_cs.rb) * [PhpLint](lib/overcommit/hook/pre_commit/php_lint.rb) * [PuppetLint](lib/overcommit/hook/pre_commit/puppet_lint.rb) +* [PuppetMetadataJsonLint](lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb) * [Pycodestyle](lib/overcommit/hook/pre_commit/pycodestyle.rb) * [Pydocstyle](lib/overcommit/hook/pre_commit/pydocstyle.rb) * [Pyflakes](lib/overcommit/hook/pre_commit/pyflakes.rb) diff --git a/config/default.yml b/config/default.yml index 2f73647d..bac9a8a5 100644 --- a/config/default.yml +++ b/config/default.yml @@ -427,6 +427,14 @@ PreCommit: required_executable: 'grep' flags: ['-IHn', "^<<<<<<<[ \t]"] + PuppetMetadataJsonLint: + enabled: false + description: 'Checking module metadata' + flags: ['--strict-license', '--strict-dependencies', '--fail-on-warning'] + include: 'metadata.json' + required_executable: 'metadata-json-lint' + install_command: 'gem install metadata-json-lint' + NginxTest: enabled: false description: 'Test nginx configs' diff --git a/lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb b/lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb new file mode 100644 index 00000000..60fb3d52 --- /dev/null +++ b/lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb @@ -0,0 +1,27 @@ +module Overcommit::Hook::PreCommit + # + # Run's the Puppet metadata linter. It has support for adding options + # in the .overcommit.yaml + # + # @see https://voxpupuli.org/blog/2014/11/06/linting-metadata-json/ + # + class PuppetMetadataJsonLint < Base + MESSAGE_REGEX = /\((?.*)\).*/ + + MESSAGE_TYPE_CATEGORIZER = lambda do |type| + type == 'WARN' ? :warning : :error + end + + def run + result = execute(command, args: 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_metadata_json_lint_spec.rb b/spec/overcommit/hook/pre_commit/puppet_metadata_json_lint_spec.rb new file mode 100644 index 00000000..43da8eb7 --- /dev/null +++ b/spec/overcommit/hook/pre_commit/puppet_metadata_json_lint_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe Overcommit::Hook::PreCommit::PuppetMetadataJsonLint 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 metadata.json]) + end + + context 'when metadata-json-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)) + (WARN) requirements: The 'pe' requirement is no longer supported by the Forge. + OUT + end + + it { should warn } + end + end + + context 'when metadata-json-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)) + (WARN) requirements: The 'pe' requirement is no longer supported by the Forge. + OUT + end + + it { should warn } + end + + context 'and it reports an error' do + before do + result.stub(:stdout).and_return(normalize_indent(<<-OUT)) + (ERR) requirements: The 'pe' requirement is no longer supported by the Forge. + OUT + end + + it { should fail_hook } + end + end +end